2-1. Input for becoming a WEB engineer (Linux basics)

Introduction

This is a continuation of this article. 1-1. Input (preparation) to become a WEB engineer An article on inputs and their outputs to lay the foundation for Linux. Also, since there is a lot of content, the articles will be posted separately.

As mentioned in the previous article, the teaching materials and environment are as follows. ・ Use Udemy for business as teaching materials. [Can be done in 5 days] Introduction to Linux for the first time (LPIC Level 1 compatible) (Lecturer: Hiroki Inoue) -The Linux environment (Ubuntu) utilizes the EC2 instance of AWS. -Region: Asia Pacific (Tokyo) ap-northeast-1  -AMI:Ubuntu Server 20.04 LTS (HVM), SSD Volume Type -Instance type: t2.micro

Linux commands (UNIX)

Write commands for operating Linux. However, I will omit the parts that I can already handle.

Relative and absolute paths

Absolute path

Enter all the paths from the root directory.

/usr/local/bin/***

Relative path

Specify the relative positional relationship from the current directory.

Current position


./

Also, if you want to specify/usr/local/from/usr/local/bin, use the following.

One level up


../

Shell operation

Shell type

The standard is bash (bourne again shell).

Command history

Use the history command.

Command history


history

In addition, past commands are numbered and can be reused using the numbers. This can prevent typos. For example, if there is a history of [117 pwd], the reuse command is as follows.

Command reuse


!117

Command line beginning and end

When the characters entered on the command line are long, it is troublesome to move sideways with the arrow keys! There is a way to move in one shot.

Move to the beginning


Ctr + a

Move to the end


Ctr + e

Shell variables

Create a variable on the command line and check

Creating variables


INFILE="infile.txt"

You can check it by displaying [infile.txt] with the following command.

Confirmation of variables (1)


echo $INFILE
Or
echo "$INFILE"

In the case of the following command, [$ INFILE] is displayed.

Confirmation of variables (2)


echo '$INFILE'

You can reset the variables with the following command. It can be confirmed that the variable has been reset by checking the variable (1). (Blank is displayed)

Variable reset


unset INFILE

Environment variable

You can check the environment variable settings with the env command.

Checking environment variables


env | more
* You can check the contents by scrolling with more

If you want to add an environment variable, use the following command

Add environment variables


export INFILE

vi editor

Describe how to use the text editor. For example, to open result.txt with a text editor, use the following command. If this file does not exist, a new file will be created at the same time.

Text editor


vi result.txt

Since the operation method is complicated, it is summarized in the table below.

category command Contents
End key :q Exit without saving
:q! Exit without saving the changed line
:w Save but do not finish
:wq Save and exit
Key to enter edit mode i Insert from the current cursor position
R Replace from current cursor position
A Add to the end of the current line
O Insert line before the current line
o Insert line after current line
Cursor movement key h(←) left
j(↓) Up
k(↑) under
l(→) right
0 To the beginning of the line
$ To the end of the line
[Enter] To the beginning of the next line
w To the next word
b To the previous word
ctrl + f To the next screen
ctrl + b To the previous screen
1G To the beginning of the sentence
G To the end of the sentence
nnG To line nn
Change key x Delete one character
dd Delete one line (cut)
cw Change one word
c$ Change from cursor position to end of line
dw Delete one word
d$ Delete from the cursor position to the end of the line
Search /Regular expression 前方Search
?Regular expression Backward search
n Next candidate
N Previous candidate
Replace :1,$s/Regular expression/Replace文字列/g 文書内の全ての「Regular expression」を「Replace文字列」に置換
Shortcut yy 1 line copy
dd 1 line cut
p Paste

File manipulation

ls options

Display including hidden files


ls -a

File details


ls -l

File details (combinations), including hidden files


ls -la

Also display inode number


ls -i

Check file attributes

Check attributes


file text.txt

Copy file (dd command)

[if =] is the copy source file name, [of =] is the copy destination file name, [bs] is the block size (bytes) to copy, and [count] is the number of blocks to copy.

Copy file(dd)


dd if=/dev/zero of=zero.dat bs=512 count=1024

Copy file (cp command)

If the copy source is hello.txt and the copy destination is hello1.txt, the following The copy source hello.txt remains.

Copy file(cp)


cp hello.txt hello1.txt

Delete file

Delete file


rm hello2.txt

Move files

The file to be moved is hello2.txt, and the destination is the source code. The original hello2.txt will be moved and will disappear from the original directory.

Move files


mv hello2.txt sourcecode/

Delete directory

If the directory is stored in it as it is, it cannot be deleted, so add an option and forcibly delete it.

Forced deletion of directories


rm -fR sourcecode

File search

You can search for files in the directory. For example, the command to display a list of file names with [networks] in the current directory is as follows. Also, by adding sudo, you can avoid search errors for unauthorized files.

File search by name


sudo find . -name networks

Compression and archiving

There are several types of compression / decompression methods

tar command

Learn this because the tar command is the mainstream

option

option Contents
f file name
c Create
x Deployment
r Add to the end
v Show details
j Handle bzip2 files
z Handle gzip files

compression

The command to compress the directory called work2 using tar and create a file called work2.tar.gz is as follows. Also, with the z option, gzip is also used for compression. The original files and directories compressed with the tar command remain. When decompressing with the tar command alone, the compressed file remains.

compression


tar czf work2.tar.gz work2

Deployment

The command to decompress the file created by compression is as follows.

Deployment


tar xvzf work2.tar.gz

For gzip alone

You can use gzip by itself without combining it with the tar command. Compressing text.txt with gzip produces a text.txt.gz file.

Compress with gzip alone


gzip text.txt

Extract with gzip alone


gzip -vd text.txt.gz
* V shows details

The compressed file disappears when the gzip command is used alone.

For bzip2 command

The usage is the same as the gzip command alone. The compressed and generated file will be test3.txt.bz2.

For xz command

The usage is the same as the gzip command alone. The compressed and generated file will be test3.txt.xz.

For the cpio command

The CPIOl command has three modes.

When filelist.txt is used to store the file name list, use the following command to create an archive file.

Compression with the cpio command


cpio -o < filelist.txt > files.cpio
Or
ls | cpio -o > files.cpio

Deployment with the cpio command


cpio -i < files.cpio

You can also move files in copy path mode. For example, the command to move to the directory called copyto is as follows.

Copy path mode for cpio commands


cpio -pd copyto < filelist.txt

Link

How to access a file with a different name

A file system is a "file management mechanism" (VFS), and each device is associated with an inode.

Hard link

Use the ln command to create a hard link. Comparing the two, it can be confirmed that each has the same inode. Even if you delete one, you can refer to the other.

Creating a hard link


ln test.txt sample.txt

Checking the inode


ls -li

Symbolic link

Use the ln command to create a hard link. Comparing these two, it can be confirmed that they have different inodes and sample.txt is linked to test.txt. If you delete the original file, the other cannot be referenced.

Creating a symbolic link


ln -s test.txt sample.txt

Checking the inode


ls -li

Stream & Pipe & Redirect

Standard input / output

Input / output role
Standard input (stdin[0]) Input channel
Standard output(stdout[1]) Output channel
Standard error(stderr[2]) Channel to output the error

redirect

redirect motion
cmd > file Redirect output
cmd >> file Append output to file (add)
cmd < file Input redirection
cmd << xxx Specifying the input termination string (EOF, etc.)
cmd 2> file Error output to file
cmd file 2>&1 Standard output and error output to a file

pipe

redirect motion
cmd1 | cmd2 Pass cmd1 standard output to cmd2 standard input
cmd1 2>&1 | cmd2 Pass cmd1 standard output and error output to cmd2 standard input
cmd1 | tee file | cmd2 Write cmd1 standard output to file and pass it to cmd2 standard input

Text streams and filters

Use the cut command

cut command


cut [option]file name
option function
-d: Specify the delimiter. In the case of the left, ":」
-f*,* Specifying the number to be processed

For example, the command to retrieve the user name and home directory from [root: x: 0: 0: root:/root:/bin/bash] is as follows.

cut command


cut -d: -f1,6 passwd

Regular expressions and search

A regular expression is to pattern a set of strings

Meta character

Expression function
^word Match beginning of line
$word Match end of line
. Any single letter
* Repeat of the previous character
? Matches 0 or 1 of the previous character
+ One or more repetitions of the previous character
[] []Match any one of them
¥< word ¥> String that matches the word
¥{n,m¥} Repeat n times or more and m times or less
| Specify multiple conditions
¥ Make the next character literal instead of metacharacter

Character class

Expression function
[:alnum:] Alphabet and numbers
[:alpha:] Alphabet
[:digit:] Numbers
[:lower:] Lowercase
[:upper:] uppercase letter
[:space:] space

literal

The character itself. Used in combination with metacharacters. For example, when xyz [1-4], one of xyz1, xyz2, xyz3, xyz4

Use regular expressions

Regular expression


grep [option] 'pattern(Regular expression) 'file name
Or
egrep [option] 'pattern(Regular expression) 'file name
Or
fgrep [option]String file name
option function
-c Show only matching lines
-i Case insensitive
-n Display matching lines with a line number at the beginning
-v Show lines that do not match the pattern
-E Same as egrep
-F Same as fgrep

Replacement with sed

Replacement


sed 's/[pattern]/Substitute character'file name

Process operation and control

The job will be [ls -l | more] and the process will be [ls -l] [more]. The process number (PID) is automatically assigned and can be confirmed with the ps command. You can also check resource consumption with the top command.

at the end

I learned the basics of commands as the basis of linux. Next time, we will learn management work and file system management

Recommended Posts

2-2. Input for becoming a WEB engineer (Linux basics)
2-1. Input for becoming a WEB engineer (Linux basics)
[Must-see for beginners] Basics of Linux
Linux Basic Education for Front-end Engineer
Linux basics
Linux basics
An introduction to self-made Python web applications for a sluggish third-year web engineer
I searched for the skills needed to become a web engineer in Python
How to create a shortcut command for LINUX
Build a mruby development environment for ESP32 (Linux)
pyenv for linux
Linux command basics
Released a web service for scoring handwriting using DeepLearning
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 7-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 1-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 2-
Create a QR code for the URL on Linux
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 0-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 5-
[Part 2] Let's build a web server on EC2 Linux
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 6-
A brief summary of Linux antivirus software for individuals
How to create a local repository for Linux OS
[Linux] Basics of authority setting by chmod for beginners
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 4-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 3-