Command Introduction
mkdir Create directory commands make directory
Touch Create File "touch", modify timestamp
PWD Displays the current path
-ls-l (long) d (directory) display directory or file full list
Ls-l/root/
Tree view directory Structure
CD switch directory hierarchy change directory
echo Print output (printf complex output)
SEQ print sequence of numbers, hint: seq 10 is equivalent to echo{0..10}
Cat view file contents, write to File
Vi/vim File Editor
CP copy file, copy directory
MV moving files, moving directories
RM Delete files, directories
-F Force Delete file-R recursively delete deleted files or directories-fr delete files, directories
RmDir Delete Empty directory, basically has been eliminated
grep 核心命令之一(三剑客的老三)过滤 -v排除sed 核心命令之一(三剑客的老二) -n取消默认输出 p打印 d删除awk 核心命令之一(三剑客的老大)head 头部 显示文件头部-n -行数 head -n 5 oldboy.txt (默认行数:10)tail 尾部 显示文件尾部-n -行数 tail -n 2 oldboy.txt (默认行数:10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
Introduction to Symbols
; Delimiter for multiple commands
/root or path delimiter
重定向(数据流朝着箭头的方向流动,会覆盖原来的文件)
> Chase Emphasis (Append content at the end of the original file)
.. Return to the previous directory CD:,
. Current directory
{} with echo print sequence echo{1..40}
1
2
3
4
5
6
7
8
"Case Demo"
1. Create a directory (file) data
mkdir Create directory commands make directory
-ls-l (long) d (directory) display directory or file full list
CD switch directory hierarchy change directory
Answer:
Mkdir/data
CD/; Mkdir/data
Ls-l/root/See what files are in the/root/directory.
2. Create the Oldboy.txt file under/data
Answer:
Touch/data/oldboy.txt
Cd/data; Touch Oldboy.txt
Time Process:
Touch/data/oldboy.txt
Ls-l/data/oldboy.txt <--View the created oldboy.txt, note that there is no above d parameter, for what?
If there is a file with the same name in/data, it will not be prompted or overwritten, and the timestamp of the file will be updated.
"Summary" touch [filename]---"Touch" the file, if the file does not exist, create the file, if the file exists, change the file access time Atime time stamp information.
Directory separators for Windows: \
Directory separators for Linux:/
3. Vi/vim Notepad Editor
I, a
Command mode "= =" Insert mode
Esc
Command mode: No insert keyword
Insert mode: has insert keyword
: Wq Write and exit
: Q Exit directly
4. Add "Oldboy" to the Oldboy.txt file under/data
Answer:
Method One: General editing Method--With VI editor
Execute VI oldboy.txt into the VI editor (default command mode), click A or I into edit mode, type the content Oldboy, then press ESC on the keyboard to exit edit mode (default command mode), and finally tap: Wq (Write quit) to save and exit.
Method Two: Single-line Insert text, put to the end of the file
echo "ABCD" > Oldboy.txt
Cat Oldboy.txt
Method Four: Multiple lines of text
Cat>>/data/oldboy.txt
Method Five: Multiple lines of text: Write scripts commonly used, append multiple lines, the content is placed in the tail of the file
Cat>>/data/oldboy.txt<<eof
Aaa
Bbb
Ccc
Ddd
Eof
"Note" EOF to shelf, EOF must appear in pairs, or can be substituted with other paired identifiers
Function: Append the above four lines to the/data/oldboy.txt file
5. CP copy file, copy directory (-R)
(1) Copy the Oldboy.txt file to the/tmp/
Format: DST file path for files under CP src file path
Answer:
cp/data/oldboy.txt/tmp/
ls/tmp/
(2) Copying files or directories-a-p-R
Summarize:
CP [-AIFPRU] [source file] [target file]
-A: equivalent to-PDR
-P: Together with the properties of the file, copy the past
-R: Recursive for copying directories
6, MV Mobile files, mobile directory (without-R)
mv/data/root/
"Tip" If you want to take a copy of the directory, try not to mv/data//root/, move the directory, the source directory end not an extra slash.
Although this is possible, but we have to develop good habits and norms.
Summary
MV [-ifu] [source file/source directory] [destination file/target directory]
-I: If the destination file already exists, ask whether to overwrite
-F: Enforced and does not ask
-U: Moves if the destination file already exists and the destination file is newer than the source file
Tip: If the source file is multiple, then the destination file is at the end, and the directory
7, RM (remove) Delete files, delete directories (-R)
(1) Delete file: RM file name
Go to the data directory in the root directory and delete the Oldboy.txt
Answer:
cd/root/data/
RM oldboy.txt
Y: Yes
Description
RM-F file name//forced delete, not prompt, deprecated
(2) Delete directory: Rm-r directory name
rmdir datarm -fr 目录 //强制删除目录
8. grep (in behavioral units) for filtering operations
Topic 1: Suppose that there are three lines in Oldboy.txt AAA,BBBB,CCC,ABCD so want to just print out AAA,BBBB,CCC how should I print?
Answer:
Law One:
grep-v ABCD Oldboy.txt//Remove the ABCD from the Oldboy.txt and print it out again
Law II:
Head-3 oldboy.txt//Print out the first three lines in the Oldboy.txt file
Fahsarm
SED/^abcd/d oldboy.txt//Remove after the beginning of the ABCD to print: where ^ is indicated with the beginning, if not ^, indicates the inclusion .
Extension
sed-n/a/p oldboy.txt//-n means printing without the default, including a
sed-n/^a/p oldboy.txt//-n means printing without default, beginning with a
Title 2: Print the line containing a in oldboy.txt?
Answer:
grep a oldboy.txt
sed-n/a/p oldboy.txt
1
2
3
4
5
6
7
8
9
Ten
One
,
,
,
,
,
,
,
,
,
,
,
,
30
~
+
(
)
48
PNs
All
up
+
up
+
$
* * * * *
+/-
-
-
-66
+
+
"
"
"* * * * * *
" * * * * * * * * * "
*
[+] +
* * * * * * * * * * * * * *
* 94
* * * * *
102
103
104
117
106
107
108
109
+
111
113
118
119
121
122
======================================================================= ==========================
Shortcut key tab: command, directory completion key
Man + command: Find the definition of a command
Command Description:
Cancel Default
Alias aliases
Alias|grep CP
Unalias Canceling aliases
/bin/cp
\cp
which 查看命令的路径whereis,locate,find / -type f -name "cp"mkdir -p /data/oldboy 其中:必须加-p,才可以递归创建多个目录
[Find command]
Find Lookup Path
Find/-type F-mtime by modified time-name "Oldboy.txt" in the root directory looking for a path of type "file" named "Oldboy.txt"
Find and Xargs are used more frequently (connected by pipes |)
sed -i s###gakwxargs 用于把find/ls的输出交给后面处理man 查看命令以及配置文件等帮助
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
Description of the symbol:
| Pipeline: Pass the output of the previous command to the last command to continue processing
s## #g
1
2
3
4
1. Use a command to complete the creation of the directory/oldboy/test, that is to create the/oldboy directory and/oldboy/test directory
Answer:
Mkdir-p/oldboy/test//-p Recursive create directory, general first level directory does not exist when using-p, otherwise error
tree/oldboy///View directory tree results
"Problem" How to install tree?
2, the known/tmp directory already exists text.txt file, how to execute the command to the/tmp/test.txt copy to/mnt under the/mnt/text.txt, and let the Linux system does not prompt whether to overwrite (root authority).
Answer:
Law One:
\cp/tmp/text.txt/mnt/
Law II:
/bin/cp/tmp/text.txt/mnt/where which CP gets/bin
Law III: Alias
Alias View aliases
By default, the alias of the CP is cp-i with the alias of view CP: Alias|grep CP
So you can first remove the CP alias with Unalias
Unalias CP//Not that CP uses aliases
cp/tmp/text.txt/mnt/
Tip: The command-line processing alias is only valid at that time, and it fails after rebooting the system
"Note" The system defaults the role of aliases:
Prevent human error by adding some protection parameters to the dangerous command
To simplify a lot of complex strings
2, man + command
Role: The role of the Find command
3. Which command
Role: Find the path to a command
4. Find + directory +-type type +-name name lookup path
Find + directory +-type type +-name name +-exec + Command parameter type + {} \;
Case:
C
Find/-type F-name "Oldboy.txt"-exec rm-f {} \;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Write a picture description here
deleting all "oldboy.txt" files can also be used in the following ways
Find/-type f-name "Oldboy.txt" | Xargs rm-f
"Explanation" where | The meaning is "pipeline", the following is the explanation of the pipeline:
Write a picture description here
Find/-type f-name-mtime +15 "Oldboy.txt" | Xargs Rm-f by Time
Find/-type f-name-mtime +5 "Oldboy" | Xargs RM-RF
Write a picture description here
of which:-mtime
Write a picture description here
5. Title: View only the contents of the Oldboy.txt file (100 lines) 第20-30 line
Answer:
Method one;
Head-n-Oldboy.txt | Tail-n-10
Method Two: sed is good at outputting "line"
Sed-n 20,30p Lodboy.txt
Method Two: Awk is good at outputting "columns", where rows can also be handled, just a little more complicated.
awk ' {if (nr>19&&nr<31) print $} ' oldboy.txt//$0 means processing "line"
6, Title:/data directory and its subdirectories all files with the name of the extension. SH end are replaced with AAAA in all strings.
Answer:
Find/data-type f name "sh" | xargs sed-i s#aaaa#aaaa#g
Where: s## #g: s# replaced string # Replaced by string #g
-I indicates that the file is written after substitution
Bulk View: Xargs is a file for the previous find, cat processing by row
Find/data-type f name "sh" | Xargs Cat
Some commands for Linux