第2章命令工具
图2-2
2.1基本命令
2.1.1目录及文件的基本操作
- pwd
描述:pwd命令的作用是显示当前工作目录的名称。
用法:pwd [选项]…
选项:-P显示链接的真实路径。
[root@centos6 Desktop]# pwd
/root/Desktop
[root@centos6 test]# pwd
/tmp/test
[root@centos6 test]# pwd -P
/tmp/pass
#返回当前工作目录/root/Desktop
#返回当前工作目录/tmp/test
#返回链接的真实路径/tmp/pass1 - cd
描述:cd命令的作用是切换当前工作目录。
/tmp/test 是/tmp/pass 的链接文件o
• 31 •Linux运维之道
[root@centos6
[root@centos6
[root@centos6
[root@centos6
〜]# cd /usr/src/
src]# cd •.
usr]# cd -
〜]# cd
#切换工作目录至/usr/src/
#切换工作目录至当前目录的上一级目录
#返回至/usr/src目录
#切换工作目录至当前用户的家目录 - Is
描述:命令的作用是显示目录与文件信息。
用法:Is [选项]…[文件/目录]…
选项:-a显示所有的信息,包括隐藏文件与目录。
-d显示目录本身的信息,而非目录下的资料信息。
-h人性化显示容量信息。
-1长格式显示详细信息。
-c显示文件或目录属性最后修改的时间。
-u显示文件或目录最后被访问的时间。
-t以修改时间排序,默认按文件名称排序。
[root@centos6
[root@centos6
[root@centos6
[root@centos6
[root@centos6
[root@centos6
[root@centos6
[root@centos6
[root@centos6
test]# Is
test]# Is /etc
test]# Is -a
test]# Is -1
test]# Is -Id /root
test]# Is -lh
test]# Is -lc /etc/passwd
test]# Is -lu /etc/passwd
test]# Is -It
#显示当前目录下的子文件与目录名称
#显示/etc目录下的子文件与目录名称
#查看以.开始的隐藏文件与目录
#查看文件与目录的详细信息2
#查看当前root目录自身的详细信息
#人性化显示容量信息
#查看/etc/passwd文件的属性变更时间
#查看/etc/passwd最后访问时间
#查看档案信息并以修改时间排序 - touch
描述:创建或修改文件时间。
[root@centos6 test]# touch hello.txt
如果hdlo.txt不存在,则创建,如果已存在,则更新文件所有的时间为当前系统时间。 - mkdir
描述:创建目录。
默认显示的时间为文件被修改的时间,容量单位为字节。
• 32 •第2章命令工具
用法:mkdir [选项]…[目录]…
选项:-p创建多级目录。
[root@centos6 test]# mkdir leo
[root@centos6 test]# mkdir -p /tn^>/test/jerry/book/computer - cp
描述:复制文件与目录。
用法:cp [选项]源目标
选项:-r递归,复制子文件与子目录,一般复制目录时使用。
[root@centos6 test] # cp /etc/hosts /tmp/ #复制文件/etc/hosts 至/tmp 目录下
[root@centos6 test] # cp /etc/hosts /tmp/host #复制文件/etc/hosts 至/目录下并
改名为host
[root@centos6 test] # cp -r /var/log/ /trap/ #复制目录/var/log 至/tmp/目录下 - rm
描述:删除文件或目录。
用法:mv [选项]…文件…
选项:-f不提示,强制删除。
-i删除前,提示是否删除。
-r删除目录以及目录下的所有内容。
[root@centos6 test] # rm readme. txt #删除文件 readme • txt
[root@centos6 test]# rm -rf Jacob # 删除目录且不提示 - mv
描述:移动(重命名)文件或目录。
[root@centos6 test] # mv hello. txt hello . doc #将 hello • txt 改名为 hello • doc
[root@centos6 test] # mv hello.doc /root/ #将 hello• doc 移至/root 目录下 - find
描述:搜索文件或目录。
用法:find [命令选项][路径][表达式选项]
选项:-empty查找空白文件或目录。
-group按组查找。
• 33 •Linux运维之道
-name按名称查找。
-iname按名称查找,不区分大小写。
-mtime按修改时间查找。 ♦
-size 按容量大小查找。
-type按档案类型查找,文件(f)、目录(d)、设备(b, c)、链接(1)等。
-user按用户查找。
-exec对找到的档案执行命令。
-a 并且。
-o 或者。
[root@centos6 test]# find 一name hello.doc
[root@centos6 test] # find /root-name ’’*.log
[root@centos6 test] # find 一iname 11 Jacob11
[root@centos6 test]# find / -empty
[root@centos6 test]# find / -group tom
[root@centos6 test]# find / -mtime -3
[root@centos6 test]# find / -mtime +4
[root@centos6 test]# find / -mtime 2
[root@centos6 test]# find ./ -size+lOM
[root@centos6 test]# find •/ -type f
[root@centos6 test]# find / -user tom
[root@centos6 test]# find./ -size +1M -exec Is -1 {} ;
[root@centos6 test]# find / -size +1M -a -type f
#查找当前目录下名为hello,
doc的档案3
#查找/root目录下所有名称以
.log结尾的档案
#不区分大小写查找档案jacob
#查找计算机中所有的空文档
#查找计算机中所属组为tom的档案
#查找计算机中所有3天内被修改
过的档案
#查找计算机中所有4天前被修改
过的档案
#查找计算机中2天前的当天被修
改过的档案
#查找当前目录下大于10MB的档案
#查找当前目录下的所有普通文件
#查找计算中tom所拥有的所有档案
#查找大于1MB的文件后列出文件
的详细信息
#查找计算机中所有大于1MB的文件 - du
描述:计算文件或目录的容量。
用法:du [选项]…[文件或目录]…
选项:-h人性化显示容量信息。
-s仅显示总容量。
[root@centos6 test]# du /root #查看/root目录及子目录的容量信息
这里的档案指的是文件或目录。
• 34 •