1. Command basis
1.1 command operation syntax
-
Command [parameter] [file]
-
Parameter, optional
-
Documents, optional
-
2. Basic command
2.1. View the current working directory
-
pwd
-
print name of current/workin directory
-
Print name or working directory
[root@oldboyedu ~]# pwd /root [root@oldboyedu ~]# cd /opt/ [root@oldboyedu /opt]# pwd /opt [root@oldboyedu /opt]# cd /etc/ [root@oldboyedu /etc]# pwd /etc [root@oldboyedu /etc]#
-
2.2. Change directory
-
cd
-
cd directory
[root@oldboyedu /etc]# cd - return to last directory /opt [root@oldboyedu /opt]# cd.. go to the next level directory [root@oldboyedu /]# cd. Go to the current directory [root@oldboyedu /]# [root@oldboyedu /]# cd ~ to the current user's directory
-
Relative path and absolute path:
- Relative path:
- Paths that do not start from / are relative paths
- Absolute path
- Path from / to
2.3. Display in tree structure
-
tree
-
-L number of layers
[root@oldboyedu ~]# tree -L 1 / / ├── bin -> usr/bin ├── boot ├── dev ├── etc ├── home ├── lib -> usr/lib ├── lib64 -> usr/lib64 ├── media ├── mnt ├── opt ├── proc ├── root ├── run ├── sbin -> usr/sbin ├── srv ├── sys ├── tmp ├── usr └── var
-
-d display directory
[root@oldboyedu ~]# tree -L 1 -d /etc/ /etc/ ├── abrt ├── alternatives ├── audisp ├── audit ├── bash_completion.d ├── binfmt.d ├── chkconfig.d ├── cron.d
-
2.4 create directory
-
mkdir
[root@oldboyedu ~]# ls anaconda-ks.cfg [root@oldboyedu ~]# mkdir oldboy [root@oldboyedu ~]# ls anaconda-ks.cfg oldboy [root@oldboyedu ~]# ls / bin dev home lib64 mnt proc run srv tmp var boot etc lib media opt root sbin sys usr [root@oldboyedu ~]# mkdir /oldboy [root@oldboyedu ~]# ls anaconda-ks.cfg oldboy [root@oldboyedu ~]# ls / bin dev home lib64 mnt opt root sbin sys usr boot etc lib media oldboy proc run srv tmp var
-
-p recursive creation
[root@oldboyedu ~]# mkdir sun/yu mkdir: cannot create directory 'sun/yu': No such file or directory #mkdir: cannot create directory 'sun/yu': no such file or directory [root@oldboyedu ~]# mkdir -p sun/yu [root@oldboyedu ~]# ls anaconda-ks.cfg oldboy sun [root@oldboyedu ~]# ls sun/ yu
-
2.5 creating documents
-
touch
[root@oldboyedu ~]# touch 1 [root@oldboyedu ~]# ls 1 anaconda-ks.cfg oldboy sun
- {} generate sequence
#What do I do if I want to create 100 txt files? [root@oldboyedu ~]# touch {1..100}.txt [root@oldboyedu ~]# ll total 0 -rw-r--r--. 1 root root 0 Apr 5 09:04 100.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 10.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 11.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 12.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 13.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 14.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 15.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 16.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 17.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 18.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 19.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 1.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 20.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 21.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 22.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 23.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 24.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 25.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 26.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 27.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 28.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 29.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 2.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 30.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 31.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 32.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 33.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 34.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 35.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 36.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 37.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 38.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 39.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 3.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 40.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 41.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 42.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 43.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 44.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 45.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 46.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 47.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 48.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 49.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 4.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 50.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 51.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 52.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 53.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 54.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 55.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 56.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 57.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 58.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 59.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 5.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 60.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 61.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 62.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 63.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 64.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 65.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 66.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 67.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 68.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 69.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 6.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 70.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 71.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 72.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 73.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 74.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 75.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 76.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 77.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 78.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 79.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 7.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 80.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 81.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 82.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 83.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 84.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 85.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 86.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 87.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 88.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 89.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 8.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 90.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 91.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 92.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 93.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 94.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 95.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 96.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 97.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 98.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 99.txt -rw-r--r--. 1 root root 0 Apr 5 09:04 9.txt
#If I create four directories of a b c d in the current directory, and then create four files of a b c d, will it succeed [root@oldboyedu ~]# mkdir a b c d [root@oldboyedu ~]# ls a b c d [root@oldboyedu ~]# touch a b c d [root@oldboyedu ~]# ll total 0 drwxr-xr-x. 2 root root 6 Apr 5 09:06 a drwxr-xr-x. 2 root root 6 Apr 5 09:06 b drwxr-xr-x. 2 root root 6 Apr 5 09:06 c drwxr-xr-x. 2 root root 6 Apr 5 09:06 d //Why is that? Because in Linux, everything is a file, and the directory is a file
2.5 display contents under the directory
-
ls (list)
[root@oldboyedu ~]# ls a b c d
-
-l (long) the contents of the current directory are displayed in a long format
[root@oldboyedu ~]# ls -l total 0 drwxr-xr-x. 2 root root 6 Apr 5 09:06 a drwxr-xr-x. 2 root root 6 Apr 5 09:06 b drwxr-xr-x. 2 root root 6 Apr 5 09:06 c drwxr-xr-x. 2 root root 6 Apr 5 09:06 d
-
-d only display its own directory
[root@oldboyedu ~]# touch {1..5}.txt [root@oldboyedu ~]# ls 1.txt 2.txt 3.txt 4.txt 5.txt a b c d [root@oldboyedu ~]# ls -d .
-
-A shows all files, including hidden files (files starting with a)
[root@oldboyedu ~]# ls -la total 24 dr-xr-x---. 6 root root 213 Apr 5 09:10 . dr-xr-xr-x. 17 root root 224 Apr 5 09:03 .. -rw-r--r--. 1 root root 0 Apr 5 09:10 1.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 2.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 3.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 4.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 5.txt drwxr-xr-x. 2 root root 6 Apr 5 09:06 a drwxr-xr-x. 2 root root 6 Apr 5 09:06 b -rw-------. 1 root root 554 Apr 4 18:35 .bash_history -rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout -rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile -rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc drwxr-xr-x. 2 root root 6 Apr 5 09:06 c -rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc drwxr-xr-x. 2 root root 6 Apr 5 09:06 d -rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc
-
-A shows all files, excluding files beginning with
[root@oldboyedu ~]# ls -lA total 24 -rw-r--r--. 1 root root 0 Apr 5 09:10 1.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 2.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 3.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 4.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 5.txt drwxr-xr-x. 2 root root 6 Apr 5 09:06 a drwxr-xr-x. 2 root root 6 Apr 5 09:06 b -rw-------. 1 root root 554 Apr 4 18:35 .bash_history -rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout -rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile -rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc drwxr-xr-x. 2 root root 6 Apr 5 09:06 c -rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc drwxr-xr-x. 2 root root 6 Apr 5 09:06 d -rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc
-
2.6 copying files and directories
-
cp (copy)
-
cp [OPTION]... SOURCE... DIRECTORY
Options source directory-
-r recursion, copying directories
[root@oldboyedu ~]# ls 1.txt 2.txt 3.txt 4.txt 5.txt a b c d [root@oldboyedu ~]# cp a /opt/ cp: omitting directory 'a cp:omitting directory'a [root@oldboyedu ~]# cp -r a /opt/ [root@oldboyedu ~]# ll /opt/ total 0 drwxr-xr-x. 2 root root 6 Apr 5 09:14 a
-
-p keep file properties
[root@oldboyedu ~]# cp -p 2.txt /opt/ [root@oldboyedu ~]# ll 2.txt /opt/2.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 2.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 /opt/2.txt
-
-a keep file properties and links
[root@oldboyedu ~]# ll /etc/system-release lrwxrwxrwx. 1 root root 14 Apr 4 18:05 /etc/system-release -> centos-release [root@oldboyedu ~]# cp -a /etc/system-release ./ [root@oldboyedu ~]# ll /etc/system-release system-release lrwxrwxrwx. 1 root root 14 Apr 4 18:05 /etc/system-release -> centos-release lrwxrwxrwx. 1 root root 14 Apr 4 18:05 system-release -> centos-release
-
-i confirm
[root@oldboyedu ~]# ls 1.txt 2.txt 3.txt 4.txt 5.txt a b c d system-release [root@oldboyedu ~]# cp 1.txt /opt/ [root@oldboyedu ~]# cp 1.txt /opt/ cp: overwrite '/opt/1.txt'? y //It's because of alias CP ='cp-i '
-
-
2.7 view or set alias
-
alias
[root@oldboyedu ~]# alias alias cp='cp -i' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' alias ls='ls --color=auto' alias mv='mv -i' alias rm='rm -i' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
-
Setting aliases
alias cp='cp -i'
-
How not to use aliases:
- [root@oldboyedu ~]# \cp 1.txt /opt/
- [root@oldboyedu ~]# /bin/cp 1.txt /opt/
2.8 deleting files or directories
-
rm
-
-f. force deletion (only files can be deleted)
[root@oldboyedu ~]# rm -f 1.txt [root@oldboyedu ~]# ll total 0 -rw-r--r--. 1 root root 0 Apr 5 09:10 2.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 3.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 4.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 5.txt drwxr-xr-x. 2 root root 6 Apr 5 09:06 a drwxr-xr-x. 2 root root 6 Apr 5 09:06 b drwxr-xr-x. 2 root root 6 Apr 5 09:06 c drwxr-xr-x. 2 root root 6 Apr 5 09:06 d lrwxrwxrwx. 1 root root 14 Apr 4 18:05 system-release -> centos-release [root@oldboyedu ~]# rm -f a rm: cannot remove 'a': Is a directory rm:Cannot delete“ a": Is a directory
-
-r recursively delete, delete directories and files
[root@oldboyedu ~]# rm -rf a [root@oldboyedu ~]# ll total 0 -rw-r--r--. 1 root root 0 Apr 5 09:10 2.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 3.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 4.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 5.txt drwxr-xr-x. 2 root root 6 Apr 5 09:06 b drwxr-xr-x. 2 root root 6 Apr 5 09:06 c drwxr-xr-x. 2 root root 6 Apr 5 09:06 d lrwxrwxrwx. 1 root root 14 Apr 4 18:05 system-release -> centos-release
-
-i confirm
alias rm='rm -i'
-
Be careful:
- Try not to use rm, if you want to use find instead
- Set rm alias, for example: alias rm='echo, please use find '
- Backup before delete
- Move the file to be deleted to / tmp before deleting
- Go to the directory where you want to delete the file and delete the file
2.9. Cancel alias
-
unalias
[root@oldboyedu ~]# alias alias cp='cp -i' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' alias ls='ls --color=auto' alias mv='mv -i' alias rm='rm -i' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' [root@oldboyedu ~]# unalias cp [root@oldboyedu ~]# alias alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' alias ls='ls --color=auto' alias mv='mv -i' alias rm='rm -i' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@oldboyedu ~]# unalias cp [root@oldboyedu ~]# [root@oldboyedu ~]# cp 2.txt /opt/ [root@oldboyedu ~]# cp 2.txt /opt/ [root@oldboyedu ~]# cp 2.txt /opt/
2.10 moving files or directories (equivalent to cutting)
-
mv
[root@oldboyedu ~]# ll total 0 -rw-r--r--. 1 root root 0 Apr 5 09:10 2.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 3.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 4.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 5.txt drwxr-xr-x. 2 root root 6 Apr 5 09:06 b drwxr-xr-x. 2 root root 6 Apr 5 09:06 c drwxr-xr-x. 2 root root 6 Apr 5 09:06 d lrwxrwxrwx. 1 root root 14 Apr 4 18:05 system-release -> centos-release [root@oldboyedu ~]# mv a /opt/ mv: cannot stat 'a': No such file or directory [root@oldboyedu ~]# mv -r a /opt/ mv: invalid option -- 'r' Try 'mv --help' for more information. [root@oldboyedu ~]# [root@oldboyedu ~]# ll total 0 -rw-r--r--. 1 root root 0 Apr 5 09:10 2.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 3.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 4.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 5.txt drwxr-xr-x. 2 root root 6 Apr 5 09:06 b drwxr-xr-x. 2 root root 6 Apr 5 09:06 c drwxr-xr-x. 2 root root 6 Apr 5 09:06 d lrwxrwxrwx. 1 root root 14 Apr 4 18:05 system-release -> centos-release [root@oldboyedu ~]# mv b /opt/ [root@oldboyedu ~]# ll /opt/ total 0 -rw-r--r--. 1 root root 0 Apr 5 09:25 1.txt -rw-r--r--. 1 root root 0 Apr 5 09:30 2.txt drwxr-xr-x. 2 root root 6 Apr 5 09:14 a drwxr-xr-x. 2 root root 6 Apr 5 09:06 b [root@oldboyedu ~]# ll total 0 -rw-r--r--. 1 root root 0 Apr 5 09:10 2.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 3.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 4.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 5.txt drwxr-xr-x. 2 root root 6 Apr 5 09:06 c drwxr-xr-x. 2 root root 6 Apr 5 09:06 d lrwxrwxrwx. 1 root root 14 Apr 4 18:05 system-release -> centos-release [root@oldboyedu ~]# mv 2.txt /opt/ mv: overwrite '/opt/2.txt'? y #mv: overwrite "opt/2.txt"? #The reason is alias MV ='mv-i ' [root@oldboyedu ~]# ll /opt/ total 0 -rw-r--r--. 1 root root 0 Apr 5 09:25 1.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 2.txt drwxr-xr-x. 2 root root 6 Apr 5 09:14 a drwxr-xr-x. 2 root root 6 Apr 5 09:06 b [root@oldboyedu ~]# ll total 0 -rw-r--r--. 1 root root 0 Apr 5 09:10 3.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 4.txt -rw-r--r--. 1 root root 0 Apr 5 09:10 5.txt drwxr-xr-x. 2 root root 6 Apr 5 09:06 c drwxr-xr-x. 2 root root 6 Apr 5 09:06 d lrwxrwxrwx. 1 root root 14 Apr 4 18:05 system-release -> centos-release
-
-i confirm
alias mv='mv -i'
-
2.11 output and printing
-
echo
[root@oldboyedu ~]# echo "sun" sun
-
-n do not wrap output
[root@oldboyedu ~]# echo -n "sunsun" sunsun[root@oldboyedu ~]#
-
-e support escape character
-
\n wrap
[root@oldboyedu ~]# echo -e "sun\nsun" sun sun
-
\t tab
[root@oldboyedu ~]# echo -e "sun\tsun" sun sun
-
-
2.12 output
-
printf
[root@oldboyedu ~]# printf sun sun[root@oldboyedu ~]# printf sunsss sunsss[root@oldboyedu ~]#