目录
1. ls 指令
语法: ls [ 选项 ] [ 文件 ]
对于目录,该命令列出该目录下的所有子目录与文件。对于文件,将列出⽂件名以及其他信息。
常用选项:
-a 列出⽬录下的所有文件,包括以.开头的隐含⽂件。
-d 将⽬录像⽂件⼀样显示,而不是显示其下的⽂件。如:ls ‒d 指定目录
-i 输出⽂件的i节点的索引信息。如 ls ‒ ai 指定文件
-k 以k字节的形式表示文件的大小。如 ls ‒ alk 指定文件
-l 列出文件的详细信息
-r 对目录反向排序
-t 以时间排序
举例:
[iu@iZ2ze5ncfh4b12o80084czZ study]$ ls lesson2
dir pd1 test.c test.zip
[iu@iZ2ze5ncfh4b12o80084czZ lesson2]$ ls -a
. .. dir pd1 test.c test.zip
[iu@iZ2ze5ncfh4b12o80084czZ lesson2]$ ls -a -l
total 24
drwxr-xr-x 4 root root 4096 Feb 23 18:13 .
drwxr-xrwx 8 root root 4096 Feb 26 22:03 ..
drwxr-xr-x 2 root root 4096 Feb 23 18:13 dir
drwxr-xr-x 3 root root 4096 Feb 23 18:13 pd1
-rw-r--r-- 1 root root 72 Feb 23 18:13 test.c
-rw-r--r-- 1 root root 193 Feb 23 18:13 test.zip
[iu@iZ2ze5ncfh4b12o80084czZ lesson2]$ ls -a -l -f
.. dir . test.zip test.c pd1
[iu@iZ2ze5ncfh4b12o80084czZ lesson2]$ ls -a -l -F
total 24
drwxr-xr-x 4 root root 4096 Feb 23 18:13 ./
drwxr-xrwx 8 root root 4096 Feb 26 22:03 ../
drwxr-xr-x 2 root root 4096 Feb 23 18:13 dir/
drwxr-xr-x 3 root root 4096 Feb 23 18:13 pd1/
-rw-r--r-- 1 root root 72 Feb 23 18:13 test.c
-rw-r--r-- 1 root root 193 Feb 23 18:13 test.zip
[iu@iZ2ze5ncfh4b12o80084czZ lesson2]$ ls -a -l -F -n
total 24
drwxr-xr-x 4 0 0 4096 Feb 23 18:13 ./
drwxr-xrwx 8 0 0 4096 Feb 26 22:03 ../
drwxr-xr-x 2 0 0 4096 Feb 23 18:13 dir/
drwxr-xr-x 3 0 0 4096 Feb 23 18:13 pd1/
-rw-r--r-- 1 0 0 72 Feb 23 18:13 test.c
-rw-r--r-- 1 0 0 193 Feb 23 18:13 test.zip
2. pwd命令
语法:pwd
显示⽤户当前所在的目录。
[iu@iZ2ze5ncfh4b12o80084czZ study]$ pwd
/home/iu/study
3. cd 指令
Linux系统中,磁盘上的文件和目录被组成⼀棵目录树,每个节点都是目录或文件,其中普通⽂件⼀定是目录树的叶子节点。
理解路径存在的意义:树状组织方式,都是为了保证快速定位查找到指定的文件,而定位⽂件就需要具有唯⼀性的方案来进行定位⽂件。其中任何⼀个节点,都只有⼀个父节点,所以,从根目录开始,定位指定文件,路径具有唯⼀性。
路径:
绝对路径:⼀般从/开始,不依赖其他目录的定位⽂件的方式
相对路径:相对于当前用户所处目录,定位⽂件的路径方式
绝对路径⼀般不会随着用户的路径变化而丧失唯⼀性,⼀般在特定服务的配置文件中经常被使用;相对路径因为它的便捷性,⼀般在命令行中使用较多
[iu@iZ2ze5ncfh4b12o80084czZ study]$ cd /home/iu/study
[iu@iZ2ze5ncfh4b12o80084czZ study]$ pwd
/home/iu/study
// .. 可以返回上级目录
[iu@iZ2ze5ncfh4b12o80084czZ study]$ cd ..
[iu@iZ2ze5ncfh4b12o80084czZ ~]$ pwd
/home/iu
[iu@iZ2ze5ncfh4b12o80084czZ ~]$ cd /home/iu/study/lesson1
[iu@iZ2ze5ncfh4b12o80084czZ lesson1]$ pwd
/home/iu/study/lesson1
[iu@iZ2ze5ncfh4b12o80084czZ lesson1]$ cd ../lesson2
[iu@iZ2ze5ncfh4b12o80084czZ lesson2]$ pwd
/home/iu/study/lesson2
// - 直接回退到最近一次所处的目录
[iu@iZ2ze5ncfh4b12o80084czZ lesson2]$ cd -
/home/iu/study/lesson1
// ~ 返回到家目录
[iu@iZ2ze5ncfh4b12o80084czZ lesson1]$ cd ~
[iu@iZ2ze5ncfh4b12o80084czZ ~]$ pwd
/home/iu
4. touch 指令
语法: touch [选项]... [文件] ...
touch命令参数可更改文档或目录的日期时间,包括存取时间和更改时间,或者新建⼀个不存在的文件。
常用选项:
-a: change only the access time
-c: change only the modification time
举例:
//创建普通⽂件
[iu@iZ2ze5ncfh4b12o80084czZ test]$ ls
dir file.txt
[iu@iZ2ze5ncfh4b12o80084czZ test]$ touch newFile.txt
[iu@iZ2ze5ncfh4b12o80084czZ test]$ ls
dir file.txt newFile.txt
//修改⽂件access时间
[iu@iZ2ze5ncfh4b12o80084czZ test]$ touch -a newFile.txt
//修改⽂件Modify时间
[iu@iZ2ze5ncfh4b12o80084czZ test]$ touch -m newFile.txt
5. mkdir指令
语法: mkdir [选项] [文件]...
在当前目录下创建⼀个名为“dirname”的目录
常用选项:
-p: 可以是⼀个路径名称。此时若路径中的某些目录尚不存在,加上此选项后,系统将自动建立好那些尚不存在的目录,即⼀次可以建立多个目录。
//创建普通空⽬录
[iu@iZ2ze5ncfh4b12o80084czZ test]$ ls -l
total 4
drwxrwxr-x 2 iu iu 4096 Jan 11 14:22 dir
-rw-rw-r-- 1 iu iu 0 Jan 11 14:22 file.txt
-rw-rw-r-- 1 iu iu 0 Jan 11 15:09 newFile.txt
[iu@iZ2ze5ncfh4b12o80084czZ test]$ mkdir mydir
[iu@iZ2ze5ncfh4b12o80084czZ test]$ ls -l
total 8
drwxrwxr-x 2 iu iu 4096 Jan 11 14:22 dir
-rw-rw-r-- 1 iu iu 0 Jan 11 14:22 file.txt
drwxrwxr-x 2 iu iu 4096 Jan 11 15:15 mydir
-rw-rw-r-- 1 iu iu 0 Jan 11 15:09 newFile.txt
//递归建⽴多个⽬录,创建指定路径
[iu@iZ2ze5ncfh4b12o80084czZ test]$ mkdir -p path1/path2/path3/path4
[iu@iZ2ze5ncfh4b12o80084czZ test]$ ls -l
total 12
drwxrwxr-x 2 iu iu 4096 Jan 11 14:22 dir
-rw-rw-r-- 1 iu iu 0 Jan 11 14:22 file.txt
drwxrwxr-x 2 iu iu 4096 Jan 11 15:15 mydir
-rw-rw-r-- 1 iu iu 0 Jan 11 15:09 newFile.txt
drwxrwxr-x 3 iu iu 4096 Jan 11 15:16 path1
[whb@bite-alicloud test]$ tree path1
path1
└── path2
└── path3
└── path4
6. rmdir指令
rmdir是⼀个与mkdir相对应的命令,mkdir是建立目录,而rmdir是删除命令
语法:rmdir [参数] [文件]
删除空目录
适用对象:具有当前目录操作权限的所有使用者
常用选项:
-p 当子目录被删除后如果父目录也变成空目录的话,就连带父目录⼀起删除。
举例:
//删除空⽬录
[iu@iZ2ze5ncfh4b12o80084czZ test]$ ll
total 12
drwxrwxr-x 2 iu iu 4096 Jan 11 14:22 dir
-rw-rw-r-- 1 iu iu 0 Jan 11 14:22 file.txt
drwxrwxr-x 2 iu iu 4096 Jan 11 15:15 mydir
-rw-rw-r-- 1 iu iu 0 Jan 11 15:09 newFile.txt
drwxrwxr-x 3 iu iu 4096 Jan 11 15:16 path1
[iu@iZ2ze5ncfh4b12o80084czZ test]$ tree mydir/
mydir/
0 directories, 0 files
[wiu@iZ2ze5ncfh4b12o80084czZ test]$ rmdir mydir
[iu@iZ2ze5ncfh4b12o80084czZ test]$ ll
total 8
drwxrwxr-x 2 iu iu 4096 Jan 11 14:22 dir
-rw-rw-r-- 1 iu iu 0 Jan 11 14:22 file.txt
-rw-rw-r-- 1 iu iu 0 Jan 11 15:09 newFile.txt
drwxrwxr-x 3 iu iu 4096 Jan 11 15:16 path1
//删除路径
[iu@iZ2ze5ncfh4b12o80084czZ test]$ tree path1
path1
└── path2
└── path3
└── path4
3 directories, 0 files
[iu@iZ2ze5ncfh4b12o80084czZ test]$ rmdir -p path1/path2/path3/path4
[iu@iZ2ze5ncfh4b12o80084czZ test]$ ll
total 4
drwxrwxr-x 2 iu iu 4096 Jan 11 14:22 dir
-rw-rw-r-- 1 iu iu 0 Jan 11 14:22 file.txt
-rw-rw-r-- 1 iu iu 0 Jan 11 15:09 newFile.txt
//指定路径中有不为空的路径,便⽆法删除
[iu@iZ2ze5ncfh4b12o80084czZ test]$ rmdir -p path1/path2/path3
rmdir: failed to remove ‘path1/path2/path3’: Directory not empty
[iu@iZ2ze5ncfh4b12o80084czZ test]$ touch path1/myfile.txt
[iu@iZ2ze5ncfh4b12o80084czZ test]$ touch path1/path2/myfile.txt
[iu@iZ2ze5ncfh4b12o80084czZ test]$ tree path1/
path1/
├── myfile.txt
└── path2
├── myfile.txt
└── path3
└── path4
3 directories, 2 files
[iu@iZ2ze5ncfh4b12o80084czZ test]$ rmdir -p path1/path2/path3/path4
rmdir: failed to remove directory ‘path1/path2’: Directory not empt
7. rm命令
语法: rm [参数] [文件]
删除文件或目录
适⽤对象:所有使用者
常用选项:
-f 即使文件属性为只读,直接删除
-i 删除前逐⼀询问确认
-r 删除⽬录及其下所有文件
举例:
//删除普通⽂件
[iu@iZ2ze5ncfh4b12o80084czZ test]$ ll
total 8
drwxrwxr-x 2 iu iu 4096 Jan 11 14:22 dir
-rw-rw-r-- 1 iu iu 0 Jan 11 14:22 file.txt
-rw-rw-r-- 1 iu iu 0 Jan 11 15:09 newFile.txt
drwxrwxr-x 3 iu iu 4096 Jan 11 15:26 path1
[iu@iZ2ze5ncfh4b12o80084czZ test]$ rm file.txt
[iu@iZ2ze5ncfh4b12o80084czZ test]$ ll
total 8
drwxrwxr-x 2 iu iu 4096 Jan 11 14:22 dir
-rw-rw-r-- 1 iu iu 0 Jan 11 15:09 newFile.txt
drwxrwxr-x 3 iu iu 4096 Jan 11 15:26 path1
//删除⽬录⽂件
[iu@iZ2ze5ncfh4b12o80084czZ test]$ ll
total 8
drwxrwxr-x 2 iu iu 4096 Jan 11 14:22 dir
-rw-rw-r-- 1 iu iu 0 Jan 11 15:09 newFile.txt
drwxrwxr-x 3 iu iu 4096 Jan 11 15:26 path1
[iu@iZ2ze5ncfh4b12o80084czZ test]$ rm dir
rm: cannot remove ‘dir’: Is a directory
[iu@iZ2ze5ncfh4b12o80084czZ test]$ rm -r dir
[iu@iZ2ze5ncfh4b12o80084czZ test]$ ll
total 4
-rw-rw-r-- 1 iu iu 0 Jan 11 15:09 newFile.txt
drwxrwxr-x 3 iu iu 4096 Jan 11 15:26 path1
8. man指令
语法:man [ 选项 ] 命令
查看联机手册获取帮助
常用选项:
-k 根据关键字搜索联机帮助
num 只在第num章节查找
-a 将所有章节的都显⽰出来,⽐如man printf它缺省从第⼀章开始搜索,知道就停止,用a选 项,当按下q退出,他会继续往后⾯搜索,直到所有章节都搜索完毕
举例:
[iu@iZ2ze5ncfh4b12o80084czZ test]$ man printf
[iu@iZ2ze5ncfh4b12o80084czZ test]$ man fork
[iu@iZ2ze5ncfh4b12o80084czZ test]$ man 2 frok
[iu@iZ2ze5ncfh4b12o80084czZ test]$ man 3 printf
[iu@iZ2ze5ncfh4b12o80084czZ test]$ man 7 signal
[iu@iZ2ze5ncfh4b12o80084czZ test]$ man man
9. cp指令
语法: cp [ 选项 ] 源文件或目录 目标文件或目录
复制文件或目录(cp指令用于复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是⼀个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中)
常用选项:
-f或--force 强⾏复制文件或目录,不论目的⽂件或目录是否已经存在
-i或--interactive 覆盖文件先询问用户
-r 递归处理,将指定目录下的文件与子目录⼀并处理。
举例:
//cp普通⽂件
[iu@iZ2ze5ncfh4b12o80084czZ test]$ echo "hello ">myfile.txt
[iu@iZ2ze5ncfh4b12o80084czZ test]$ cat myfile.txt
hello
[iu@iZ2ze5ncfh4b12o80084czZ test]$ ll
total 4-rw-rw-r-- 1 iu iu 22 Jan 11 15:47 myfile.txt
[iu@iZ2ze5ncfh4b12o80084czZ test]$ cp myfile.txt myfile-backup.txt
[iu@iZ2ze5ncfh4b12o80084czZ test]$ ll
total 8
-rw-rw-r-- 1 iu iu 22 Jan 11 15:47 myfile-backup.txt
-rw-rw-r-- 1 iu iu 22 Jan 11 15:47 myfile.txt
[iu@iZ2ze5ncfh4b12o80084czZ test]$ cat myfile-backup.txt
hello
//cp如果⽬标⽂件存在,就覆盖
[iu@iZ2ze5ncfh4b12o80084czZ test]$ echo "hello iu" > myfile.txt
[iu@iZ2ze5ncfh4b12o80084czZ test]$ cat myfile.txt
hello iu
[iu@iZ2ze5ncfh4b12o80084czZ test]$ cp myfile.txt myfile-backup.txt
[iu@iZ2ze5ncfh4b12o80084czZ test]$ cat myfile-backup.txt
hello iu
10. mv指令
语法: mv [选项] 源文件或目录 目标文件或目录
mv命令是move的缩写,可以用来移动文件或者将文件改名,经常用备份文件或者目录
a)mv命令中第⼆个参数类型的不同(是文件还是目录),mv命令将文件重命名或将其移至⼀个新的目录中。
b)当第⼆个参数类型是文件时,mv命令完成文件重命名,此时,源文件只能有⼀个,它将所给的源文件或目录重命名为给定的目标⽂件名。
c)当第⼆个参数是已存在的目录名称时,目录参数可以有多个,mv命令将各参数指定的源文件都移至目标目录中。
常用选项:
-f:force强制的意思,如果目标文件已经存在,不会询问而直接覆盖
-i:若目标文件已经存在时,就会询问是否覆盖。
举例:
//更改名称
[iu@iZ2ze5ncfh4b12o80084czZ test]$ touch myfile.txt
[iu@iZ2ze5ncfh4b12o80084czZ test]$ ll
total 0-rw-rw-r-- 1 iu iu 0 Jan 11 15:56 myfile.txt
[iu@iZ2ze5ncfh4b12o80084czZ test]$ mv myfile.txt yourfile.txt
[iu@iZ2ze5ncfh4b12o80084czZ test]$ ll
total 0
-rw-rw-r-- 1 iu iu 0 Jan 11 15:56 yourfile.txt
//如果当前路径存在同名⽂件,改名即覆盖
[iu@iZ2ze5ncfh4b12o80084czZ test]$ touch myfile.txt
[iu@iZ2ze5ncfh4b12o80084czZ test]$ ll
total 0
-rw-rw-r-- 1 iu iu 0 Jan 11 15:58 myfile.txt
-rw-rw-r-- 1 iu iu 0 Jan 11 15:57 yourfile.txt
[iu@iZ2ze5ncfh4b12o80084czZ test]$ mv yourfile.txt myfile.txt
[iu@iZ2ze5ncfh4b12o80084czZ test]$ ll
total 0
-rw-rw-r-- 1 iu iu 0 Jan 11 15:57 myfile.txt
11. cat 指令
语法: cat [ 选项 ] [ 文件 ]
查看目标文件的内容
常用选项:
-b 对非空输出行编号
-n 对输出的所有行编号
-s 不输出多行空行
举例:
[whb@bite-alicloud test]$ cnt=0; while [ $cnt -le 20 ]; do echo "hello iu";
let cnt++; done > temp.txt
[whb@bite-alicloud test]$ cat -b temp.txt
1 hello iu
2 hello iu
3 hello iu
4 hello iu
5 hello iu
6 hello iu
7 hello iu
8 hello iu
9 hello iu
10 hello iu
...
12. more指令
语法: more [选项]
more命令,功能类似cat,但是可以通过下键,向下查找,但不能向上查找
常用选项:
-n 指定输出行数
q 退出more
举例:
[iu@iZ2ze5ncfh4b12o80084czZ test]$ cnt=0; while [ $cnt -le 2000 ]; do echo "hello
iu"; let cnt++; done > temp.txt
//-n指定输出⾏数
[iu@iZ2ze5ncfh4b12o80084czZ test]$ more -10 temp.txt
hello iu
hello iu
hello iu
hello iu
hello iu
hello iu
hello iu
hello iu
hello iu
hello iu
--More--(0%)
13. less 指令
语法: less [ 参数 ] 文件
less与more类似,但使用less可以随意浏览文件,而more仅能向前移动,却不能向后移动,而且less在查看之前不会加载全部文件。
常用选项:
-i 忽略搜索时的大小写
-N 显示每行的行号
/字符串:向下搜索“字符串”的功能
?字符串:向上搜索“字符串”的功能
q 退出
举例:
//命令⾏输出多⾏⽂本
[iu@iZ2ze5ncfh4b12o80084czZ test]$ cnt=0; while [ $cnt -le 2000 ]; do echo "hello
$cnt"; let cnt++; done > temp.txt
// 测试搜索和-N等功能
[iu@iZ2ze5ncfh4b12o80084czZ test]$ less -N temp.txt
1 hello 0
2 hello 1
3 hello 2
4 hello 3
5 hello 4
6 hello 5
7 hello 6
8 hello 7
9 hello 8
10 hello 9
11 hello 10
12 hello 11
13 hello 12
14 hello 13
...
14. head指令
语法: head[参数] [文件]
head用来显示文件的开头至标准输出中(默认head命令打印其文件的开头10行)。
常用选项:
-n <行数> 显示的行数
举例:
[iu@iZ2ze5ncfh4b12o80084czZ test]$ head temp.txt
hello 0
hello 1
hello 2
hello 3
hello 4
hello 5
hello 6
hello 7
hello 8
hello 9
[iu@iZ2ze5ncfh4b12o80084czZ test]$ head -5 temp.txt
hello 0
hello 1
hello 2
hello 3
hello 4
15. tail 指令
语法: tail [参数] [文件]
用于显示指定文件末尾内容,作为输入信息进行处理
常用选项:
-f 循环读取
-n 显示行数
举例:
[iu@iZ2ze5ncfh4b12o80084czZ test]$ tail temp.txt
hello 1991
hello 1992
hello 1993
hello 1994
hello 1995
hello 1996
hello 1997
hello 1998
hello 1999
hello 2000
[iu@iZ2ze5ncfh4b12o80084czZ test]$ tail -3 temp.txt
hello 1998
hello 1999
hello 2000
16. find 指令
语法: find [选项] [文件]
⽤于在文件树中查找文件,并作出相应的处理
常用选项:
-name 按照文件名查找文件
举例:
//在指定路径下搜索执⾏名称的⽂件
[iu@iZ2ze5ncfh4b12o80084czZ test]$ find ~ -name test.c
/home/iu/test_code/code/test.c
/home/iu/test_code/test.c
17. which指令
语法: which [系统命令文件]
搜索系统指定的命令
举例:
[iu@iZ2ze5ncfh4b12o80084czZ test]$ which ls
alias ls='ls --color=auto'
/usr/bin/ls
[iu@iZ2ze5ncfh4b12o80084czZ test]$ which pwd
/usr/bin/pwd
18. whereis 指令
语法: whereis [文件]
用于找到程序的源、⼆进制文件或手册
举例:
[iu@iZ2ze5ncfh4b12o80084czZ test]$ whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
[iu@iZ2ze5ncfh4b12o80084czZ test]$ whereis libc.so
libc: /usr/lib64/libc.so /usr/lib64/libc.a /usr/share/man/man7/libc.7.gz
19. alias 指令
语法:alias [文件]=“name”
设置命令的别名
举例:
[iu@iZ2ze5ncfh4b12o80084czZ test]$ alias iu='ls -a -l -n'
[iu@iZ2ze5ncfh4b12o80084czZ test]$ which iu
alias iu='ls -a -l -n'
/usr/bin/ls
[iu@iZ2ze5ncfh4b12o80084czZ test]$ iu
total 36
drwxrwxr-x 2 1000 1003 4096 Jan 11 17:59 .
drwx------ 22 1003 1003 4096 Jan 11 17:57 ..
-rw-rw-r-- 1 1003 1003 28667 Jan 11 18:29 temp.txt
20. grep 指令
语法: grep [选项] 搜寻字符串 文件
在⽂件中搜索字符串,将找到的行并打印出来
常用选项:
-i: 忽略大小写,大小写视为相同
-n:输出行号
-v:反向选择,亦即显示出没有'搜寻字符串'内容的那一行
举例:
[iu@iZ2ze5ncfh4b12o80084czZ test]$ cat temp.txt
abcd
ABCD
hello
iu
1234
//基本查找
[iu@iZ2ze5ncfh4b12o80084czZ test]$ grep "abcd" temp.txt
abcd
//忽略⼤⼩写的不同,所以⼤⼩写视为相同
[iu@iZ2ze5ncfh4b12o80084czZ test]$ grep -i "abcd" temp.txt
abcd
ABCD
//输出⾏号
[iu@iZ2ze5ncfh4b12o80084czZ test]$ grep -n "abcd" temp.txt
1:abcd
[iu@iZ2ze5ncfh4b12o80084czZ test]$ grep -ni "abcd" temp.txt
1:abcd
2:ABCD
//反向选择,亦即显⽰出没有搜寻字符串内容的那⼀⾏
[iu@iZ2ze5ncfh4b12o80084czZ test]$ grep -v "abcd" temp.txt
ABCD
hello
iu
1234
[iu@iZ2ze5ncfh4b12o80084czZ test]$ grep -vn "abcd" temp.txt
2:ABCD
3:hello
4:iu
5:1234
[iu@iZ2ze5ncfh4b12o80084czZ test]$ grep -vni "abcd" temp.txt
3:hello
4:iu
5:1234