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
Write commands for operating Linux. However, I will omit the parts that I can already handle.
Enter all the paths from the root directory.
/usr/local/bin/***
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
../
The standard is bash (bourne again shell).
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
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
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
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
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 |
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 attributes
file text.txt
[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
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
rm hello2.txt
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/
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
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
There are several types of compression / decompression methods
Learn this because the tar command is the mainstream
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 |
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
The command to decompress the file created by compression is as follows.
Deployment
tar xvzf work2.tar.gz
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.
The usage is the same as the gzip command alone. The compressed and generated file will be test3.txt.bz2.
The usage is the same as the gzip command alone. The compressed and generated file will be test3.txt.xz.
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
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.
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
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
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 | 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 |
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 |
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
A regular expression is to pattern a set of strings
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 |
Expression | function |
---|---|
[:alnum:] | Alphabet and numbers |
[:alpha:] | Alphabet |
[:digit:] | Numbers |
[:lower:] | Lowercase |
[:upper:] | uppercase letter |
[:space:] | space |
The character itself. Used in combination with metacharacters. For example, when xyz [1-4], one of xyz1, xyz2, xyz3, xyz4
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
sed 's/[pattern]/Substitute character'file name
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.
I learned the basics of commands as the basis of linux. Next time, we will learn management work and file system management
Recommended Posts