用来显示目录中的信息,可以根据不同的选项显示不同的效果
常用选项:
-
-a: 显示全部文件,包括隐藏的信息
-
-l: 以详细信息形式打开,能看到权限、所有者等信息
-
-d: 只显示给定参数的信息,而不是某个某个目录中的子文件信息
-
-h: 以大家习惯的容量单位显示文件大小,如K、M、Q等
-
-i: 显示inode号
测试:
# 默认显示的是当前所在目录中的文件
[root@localhost ~]# ls
anaconda-ks.cfg install.log install.log.syslog
[root@localhost ~]#
# 显示当前目录中的所有文件
[root@localhost ~]# ls -a
. anaconda-ks.cfg .bash_logout .bashrc install.log .tcshrc
.. .bash_history .bash_profile .cshrc install.log.syslog .Xauthority
[root@localhost ~]#
#以列表形式显示当前目录的文件
[root@localhost ~]# ls -l
总用量 48
-rw-------. 1 root root 1289 9月 30 22:56 anaconda-ks.cfg
-rw-r--r--. 1 root root 28425 9月 30 22:56 install.log
-rw-r--r--. 1 root root 9043 9月 30 22:55 install.log.syslog
[root@localhost ~]#
#可以组合若干选项,如-al,用来详细信息显示全部文件
[root@localhost ~]# ls -al
总用量 84
dr-xr-x---. 2 root root 4096 10月 7 22:04 .
dr-xr-xr-x. 25 root root 4096 10月 7 22:03 ..
-rw-------. 1 root root 1289 9月 30 22:56 anaconda-ks.cfg
-rw-------. 1 root root 482 10月 1 01:55 .bash_history
-rw-r--r--. 1 root root 18 5月 20 2009 .bash_logout
-rw-r--r--. 1 root root 176 5月 20 2009 .bash_profile
...
[root@localhost ~]#
# 查询 /etc/ 目录的信息
[root@localhost ~]# ls -ld /etc/
drwxr-xr-x. 107 root root 12288 10月 7 22:03 /etc/
[root@localhost ~]#
#以习惯的容量单位显示文件的大小
[root@localhost ~]# ls -lh
总用量 48K
-rw-------. 1 root root 1.3K 9月 30 22:56 anaconda-ks.cfg
-rw-r--r--. 1 root root 28K 9月 30 22:56 install.log
-rw-r--r--. 1 root root 8.9K 9月 30 22:55 install.log.syslog
[root@localhost ~]#
其他:
ll:等价于 ls -l
2) cd用来切换目录
-
cd /:切换到根目录
-
cd ~ 或者 cd:切换到当前用户的家目录
-
cd - :切换到上一次访问目录
-
cd ..:切换到上一级目录
其他:
pwd:用来显示当前所在的目录
3) mkdir(目录)作用 :用来创建文件夹目录
选项:
-p : 递归创建目录
测试: 创建多个目录
# 可以一次创建若干层目录
[root@localhost ~]# mkdir -p aa/bb/cc
[root@localhost ~]#
作用: 更新已存在的时间,如果文件不存在,则创建空的文件
测试:
#更新已存在文件的时间
[root@localhost ~]# touch install.log
[root@localhost ~]#
# 查看文件状态
[root@localhost ~]# stat install.log
File: "install.log"
Size: 28425 Blocks: 64 IO Block: 4096 普通文件
Device: 805h/2053d Inode: 393219 links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2021-10-08 00:28:41.043378911 +0800
Modify: 2021-10-08 00:28:41.043378911 +0800
Change: 2021-10-08 00:28:41.043378911 +0800
[root@localhost ~]#
# 创建一个新的文件
[root@localhost ~]# touch test
[root@localhost ~]#
# 文件已创建,大小为0
[root@localhost ~]# ll
总用量 48
-rw-------. 1 root root 1289 9月 30 22:56 anaconda-ks.cfg
-rw-r--r--. 1 root root 28425 10月 8 00:28 install.log
-rw-r--r--. 1 root root 9043 9月 30 22:55 install.log.syslog
-rw-r--r--. 1 root root 0 10月 8 00:29 test
[root@localhost ~]#
cat : 一般用来查询比较短的文件
more : 分屏,分行显示文件
less : 分行显示文件
head : 显示文件开头的若干行(可以使用 -n 参数指定行数 )
tail : 显示文件结尾的若干行(可以使用 -n 参数指定行数)
tail -f: 用来监控文件发送内容
测试:
[root@localhost ~]# ls
anaconda-ks.cfg install.log install.log.syslog test
[root@localhost ~]#
# 显示文件开头的行数
[root@localhost ~]# head install.log
安装 copy-jdk-configs-3.3-9.el6.noarch
warning: copy-jdk-configs-3.3-9.el6.noarch: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
安装 java-1.7.0-openjdk-1.7.0.181-2.6.14.10.el6.x86_64
安装 libgcc-4.4.7-23.el6.x86_64
安装 setup-2.8.14-23.el6.noarch
安装 tzdata-2018e-3.el6.noarch
安装 filesystem-2.4.30-3.el6.x86_64
安装 mesa-dri-filesystem-11.0.7-4.el6.x86_64
安装 foomatic-db-filesystem-4.0-8.20091126.el6.noarch
安装 xml-common-0.6.3-33.el6.noarch
[root@localhost ~]#
# 显示文件开头的若干行(可以使用 -n 参数指定行数 )
[root@localhost ~]# head -n 3 install.log
安装 copy-jdk-configs-3.3-9.el6.noarch
warning: copy-jdk-configs-3.3-9.el6.noarch: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
安装 java-1.7.0-openjdk-1.7.0.181-2.6.14.10.el6.x86_64
[root@localhost ~]#
# 显示文件结尾的若干行(可以使用 -n 参数指定行数)
[root@localhost ~]# tail -n 3 install.log
安装 words-3.0-17.el6.noarch
安装 centos-indexhtml-6-2.el6.centos.noarch
*** FINISHED INSTALLING PACKAGES ***[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
tail -f :用来监控文件内容的变化,如r日志文件
测试:
[root@localhost ~]#
# 创建一个 hsj 文件
[root@localhost ~]# touch hsj
[root@localhost ~]#
# 默认显示的是当前所在目录中的文件
[root@localhost ~]# ls
aaa anaconda-ks.cfg install.log install.log.syslog test
# 接收文件
[root@localhost ~]# tail -f test
hi
hollo
^C
[root@localhost ~]#
# 传递方
[root@localhost ~]# echo "hi" >> test
[root@localhost ~]# echo "hollo" >>test
[root@localhost ~]# ^C
[root@localhost ~]#
上面详细的显示 下面的看不清可以看上面的
6) ln作用: 可以用来创建连接,链接分类:软连接,硬链接
选项:
-
-s : 用来创建软连接(建议使用绝对路径来创建)
测试:
[root@localhost ~]# ls
aaa anaconda-ks.cfg install.log install.log.syslog test
[root@localhost ~]#
# 创建软连接
[root@localhost ~]# ln -s /root/test /tmp/mytest
[root@localhost ~]#
# 查看
[root@localhost ~]# cd /tmp/
[root@localhost tmp]#
[root@localhost tmp]# ll
总用量 0
lrwxrwxrwx. 1 root root 10 10月 8 01:27 mytest -> /root/test
-rw-------. 1 root root 0 9月 30 22:52 yum.log
[root@localhost tmp]#
使用 r m-rf 强制,递归删除文件或目录
测试:
[root@localhost ~]# ls
aaa anaconda-ks.cfg install.log install.log.syslog test
[root@localhost ~]#
# 用rm -rf 删除 aaa 开头的文件
[root@localhost ~]# rm -rf aaa*
[root@localhost ~]#
[root@localhost ~]# ls
anaconda-ks.cfg install.log install.log.syslog test
[root@localhost ~]#
# 删除 rm -rf test 的文件
[root@localhost ~]# rm -rf test
[root@localhost ~]#
[root@localhost ~]# ls
anaconda-ks.cfg install.log install.log.syslog
[root@localhost ~]#
[root@localhost ~]# cd /
[root@localhost /]# ls
bin cgroup etc lib lost+found misc net proc sbin srv tmp var
boot dev home lib64 media mnt opt root selinux sys usr
[root@localhost /]# cd tmp/
[root@localhost tmp]# ls
mytest yum.log
[root@localhost tmp]# rm -rf mytest
[root@localhost tmp]#
[root@localhost tmp]# ls
yum.log
[root@localhost tmp]#
[root@localhost tmp]# rm -rf yum.log
[root@localhost tmp]#
[root@localhost tmp]# ls
[root@localhost tmp]#
作用 : 用来拷贝文件或目录
选项:
-
-a : 可以从来递归拷贝文件或目录,同时保待与原文件一致的状态
注意: 也用来备份文件,比如配置文件
测试:
[root@localhost ~]#
[root@localhost ~]# ls
anaconda-ks.cfg install.log install.log.syslog
[root@localhost ~]#
#
[root@localhost ~]# touch aaa
[root@localhost ~]#
[root@localhost ~]# ls
aaa anaconda-ks.cfg install.log install.log.syslog
[root@localhost ~]#
[root@localhost ~]# cp aaa aaa_bat
[root@localhost ~]#
[root@localhost ~]# ls
aaa aaa_bat anaconda-ks.cfg install.log install.log.syslog
[root@localhost ~]#
#
[root@localhost ~]# cp -a install.log /tmp/inst.log
[root@localhost ~]#
# 跳转到 tmp 文件里面
[root@localhost ~]# cd /tmp/
[root@localhost tmp]#
[root@localhost tmp]# ls
inst.log
[root@localhost tmp]#
作用:用来移动文件或目
注意:可以用来文件重命名
测试:
#创建 一个cca 的文件
[root@localhost ~]# touch cca
[root@localhost ~]#
[root@localhost ~]# ls
aaa anaconda-ks.cfg cca install.log install.log.syslog my test
[root@localhost ~]#
# 使用一个mv 修改之前的cca 的名字改成bbc
[root@localhost ~]# mv cca bbc
[root@localhost ~]#
[root@localhost ~]# ls
aaa anaconda-ks.cfg bbc install.log install.log.syslog my test
aaa anaconda-ks.cfg bbc install.log install.log.syslog my test
# 使用mv 把root 里面的aaa 移动到文件夹里面tmp文件里面 移动到 某个位置
[root@localhost ~]# cd /tmp
[root@localhost tmp]# ll
总用量 32
-rw-r--r--. 1 root root 28425 10月 8 00:28 inst.log
drwxr-xr-x. 2 root root 4096 10月 10 01:09 test
[root@localhost tmp]#
[root@localhost tmp]# cd
[root@localhost ~]#
[root@localhost ~]# mv aaa /tmp
[root@localhost ~]#
[root@localhost ~]# cd /tmp
[root@localhost tmp]#
[root@localhost tmp]# ll
总用量 36
-rw-r--r--. 1 root root 18 10月 12 18:56 aaa
-rw-r--r--. 1 root root 28425 10月 8 00:28 inst.log
drwxr-xr-x. 2 root root 4096 10月 10 01:09 test
[root@localhost tmp]#
目录和文件
测试
[root@localhost ~]#
[root@localhost ~]# ll
总用量 60
# -rw 开头的是文件
-rw-------. 1 root root 1289 9月 30 22:56 anaconda-ks.cfg
-rw-r--r--. 1 root root 3 10月 12 19:05 bbb
-rw-r--r--. 1 root root 0 10月 12 17:43 bbc
-rw-r--r--. 1 root root 28425 10月 8 00:28 install.log
-rw-r--r--. 1 root root 9043 9月 30 22:55 install.log.syslog
# drwxr 开头的是目录
drwxr-xr-x. 2 root root 4096 10月 10 19:40 my
drwxr-xr-x. 2 root root 4096 10月 10 01:12 test
[root@localhost ~]# rm -rf aaa bbb bbc
[root@localhost ~]#
作用:用来查询命令所在的位置
区别:whereis 除了查询目录所中位置,还会查询其帮助文档的位置
测试:
[root@localhost ~]#
[root@localhost ~]# whereis ls
ls: /bin/ls /usr/share/man/man1p/ls.1p.gz /usr/share/man/man1/ls.1.gz
[root@localhost ~]#
[root@localhost ~]# which ls
alias ls='ls --color=auto'
/bin/ls
[root@localhost ~]#
[root@localhost ~]# alias
alias cp='cp -i'
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@localhost ~]#
作用:用来查询某个文件的所在位置
注意:需要先使用updatedb命令,更新数据库,才能查询到文件位置
测试
[root@localhost ~]#
[root@localhost ~]# touch abcd
[root@localhost ~]#
[root@localhost ~]# locate abcd
[root@localhost ~]#
[root@localhost ~]# updatedb
[root@localhost ~]#
[root@localhost ~]# locate abcd
/root/abcd
[root@localhost ~]#
作用: 根据各种指定的选项来查找文件
语法:find 查找路径 选项 要查找的文件名
选项:
1>根据文件名字查找
-
name: 根据文件名查找
-
-iname : 根据文件名查找,不区分大小写
-
-inode: 根据inode 查找
测试
[root@localhost ~]# ls
abcd anaconda-ks.cfg bbb install.log install.log.syslog my test
[root@localhost ~]#
# 在根据目录中查找文件名为 abcd 的文件
[root@localhost ~]# find / -name abcd
/root/abcd
[root@localhost ~]#
# 忽略大小写查找 abcd 文件
[root@localhost ~]# find / -iname abcd
/root/abcd
[root@localhost ~]#
2> 根据文件大小查找
-
-size 指定文件大小 ,可以用 + - 来查找大小或小于的文件
关于文件大小的计数单位:
-size n[cwbkMG] File uses n units of space. The following suffixes can be used: ‘b’ for 512-byte blocks (this is the default if no suffix is used) ‘c’ for bytes ‘w’ for two-byte words ‘k’ for Kilobytes (units of 1024 bytes) ‘M’ for Megabytes (units of 1048576 bytes) ‘G’ for Gigabytes (units of 1073741824 bytes) The size does not count indirect blocks, but it does count blocks in sparse files that are not actually allocated. Bear in mind that the ‘%k’ and ‘%b’ format specifiers of -printf handle sparse files differently. The ‘b’ suffix always denotes 512-byte blocks and never 1 Kilobyte blocks, which is different to the behaviour of -ls.
[root@localhost ~]# [root@localhost ~]# ls -lh 总用量 56K -rw-r--r--. 1 root root 0 10月 12 23:04 abcd -rw-------. 1 root root 1.3K 9月 30 22:56 anaconda-ks.cfg -rw-r--r--. 1 root root 0 10月 12 22:25 bbb -rw-r--r--. 1 root root 28K 10月 8 00:28 install.log -rw-r--r--. 1 root root 8.9K 9月 30 22:55 install.log.syslog drwxr-xr-x. 2 root root 4.0K 10月 10 19:40 my drwxr-xr-x. 2 root root 4.0K 10月 10 01:12 test [root@localhost ~]# # 在当前目录查找文件大小 大于25k 的文件 [root@localhost ~]# find . -size +25k ./install.log [root@localhost ~]# # 在当前目录查找文件大小 小于1字节的文件 的文件 [root@localhost ~]# find . -size -1c ./abcd ./bbb [root@localhost ~]# # 在当前目录查找文件大小等于28k 的文件 [root@localhost ~]# find . -size 28k ./install.log [root@localhost ~]#
3> 根据时间是他图
-
-atime : 按照访问时间查找
-
-mtime: 按照内容修改时间查找
-
-ctime: 按照文件状态修改时间查找
-
可以用+ - 来指定若干天以前或以内的时间范围查找
[root@localhost ~]# [root@localhost ~]# stat abcd [root@localhost ~]# # 查找文件的内容修改时间在5天以内的文件 [root@localhost ~]# find . -mtime -5 [root@localhost ~]# #查找文件的内容修改时间在前5天前到6天之间的文件 [root@localhost ~]# find . -mtime 5 [root@localhost ~]# # 查找文件的内容修改时间在11 天以前的文件 [root@localhost ~]# find . -mtime +10 [root@localhost ~]#
4> 根据文件权限查找
-
-perm : 来指定权限
-
可以使用 +- 来指定每个位置是否具备某个权限,还是每个位置都严格匹配某个权限
[root@localhost ~]# [root@localhost ~]# ll 总用量 56 -rw-r--r--. 1 root root 0 10月 12 23:04 abcd -rw-------. 1 root root 1289 9月 30 22:56 anaconda-ks.cfg -rw-r--r--. 1 root root 0 10月 12 22:25 bbb -rw-r--r--. 1 root root 28425 10月 8 00:28 install.log -rw-r--r--. 1 root root 9043 9月 30 22:55 install.log.syslog drwxr-xr-x. 2 root root 4096 10月 10 19:40 my drwxr-xr-x. 2 root root 4096 10月 10 01:12 test [root@localhost ~]# [root@localhost ~]# # 在当前目录查找权限为600的文件 [root@localhost ~]# find . -perm 600 ./.bash_history ./anaconda-ks.cfg ./.lesshst ./.Xauthority [root@localhost ~]# # 在当前目录查找权限为644的文件 [root@localhost ~]# find . -perm 644 ./.tcshrc ./install.log ./abcd [root@localhost ~]# # 在当前目录查找权限为4的文件(注意+号代表只要有一个位置为4即可) [root@localhost ~]# find . -perm +444 ./.bash_history ./anaconda-ks.cfg ./.tcshrc ./test ./my [root@localhost ~]# # 在当前目录查找权限为4的文件(注意-号代表所有者,所属组,其他人位置都是4) [root@localhost ~]# find . -perm -444 ./.tcshrc ./test ./my [root@localhost ~]#
5>根据所有者所有属组查找
-
-uid: 根据用户id 查找
-
-gid: 根据用户组id查找
-
-user: 根据用户名查找
-
-group: 根据组名查找
-
-nouser: 查找没有用户的文件
[root@localhost ~]# # 查找所有者为 tom 的所有文件 [root@localhost ~]# find / -user tom [root@localhost ~]# #查找没有用户的文件(这些文件一般都是外来文件) [root@localhost ~]# find / -nouser
6>根据文件类型查找
-
-type f :普通文件 ( file)
-
-type d: 目录(direcory)
-
type l: 连接文件(link)
[root@localhost tmp]# ll 总用量 16 -rwxr-xr-x. 1 root root 12 10月 9 09:31 aaa drwxr-xr-x. 2 tom tom 4096 10月 9 11:37 bbb drwxr-xr-x. 2 root root 4096 10月 11 13:37 my drwxr-xrwx. 2 root root 4096 10月 9 10:03 test #在当前目录下查找连接文件 [root@localhost tmp]# find . -type 1 find: -type 的参数未知: 1 #在/root 目录下查找目录 [root@localhost tmp]# find . /root -type d . ./test ./.ICE-unix /root /root/test /root/my [root@localhost tmp]# # 在当前目录中查找普通文件 [root@localhost tmp]# find . -type f ./aaa ./inst.log [root@localhost tmp]#
7> 并列条件查找
-
-a : 并且( and )
-
-o : 或者( or )
-
-not : 取反( not )
[root@localhost ~]# [root@localhost ~]# ls -lh 总用量 56K -rw-r--r--. 1 root root 0 10月 12 23:04 abcd -rw-------. 1 root root 1.3K 9月 30 22:56 anaconda-ks.cfg -rw-r--r--. 1 root root 0 10月 12 22:25 bbb -rw-r--r--. 1 root root 28K 10月 8 00:28 install.log -rw-r--r--. 1 root root 8.9K 9月 30 22:55 install.log.syslog drwxr-xr-x. 2 root root 4.0K 10月 10 19:40 my drwxr-xr-x. 2 root root 4.0K 10月 10 01:12 test [root@localhost ~]# # 在当前目录中查找前5k 的文件 [root@localhost ~]# find . -size +5k ./install.log ./install.log.syslog [root@localhost ~]# # 在当前目录中查找5-10k的文件,并且所有者为 root [root@localhost ~]# find . -size +5k -a -size -10k -a -user root ./install.log.syslog [root@localhost ~]#
8> 查找的后续操作
-
-exec: 查找结果后执行某个命令
-
-ok : 查找结果后带确认提示的执行某个命令
[root@localhost ~]#
# 查找3-10k 的普通文件 ,并且显示详细信息
[root@localhost ~]# find . -size +3k -a -size -10k -a -type f -exec ls -lh {} ;
-rw-r--r--. 1 root root 8.9K 9月 30 22:55 ./install.log.syslog
[root@localhost ~]#
[root@localhost tmp]# ll
总用量 36
-rw-r--r--. 1 root root 0 10月 13 01:16 a
-rw-r--r--. 1 root root 18 10月 12 18:56 aaa
-rw-r--r--. 1 root root 0 10月 13 01:16 b
-rw-r--r--. 1 root root 0 10月 13 01:17 c
-rw-r--r--. 1 root root 28425 10月 8 00:28 inst.log
drwxr-xr-x. 2 root root 4096 10月 10 01:09 test
[root@localhost tmp]#
# 查找0 字节的文件, 并且确认提示的进行删除(按y或n确定或取消)
[root@localhost tmp]# find . -size 0 -ok rm -rf {} ;
< rm ... ./c > ? y
< rm ... ./a > ? y
< rm ... ./b > ? n
[root@localhost tmp]# ls
aaa b inst.log test
[root@localhost tmp]#
9> 通配符
-
*:匹配若干个字符
-
?:匹配一个字符
-
[] : 从中括号范围内匹配一个字符
-
[^] :与中括号范围内的字符不匹配的字符
[root@localhost ~]# # 从根目录中查找以 abc 开头的文件 [root@localhost ~]# find / -name "abc*" /usr/lib64/python2.6/abc.py /usr/lib64/python2.6/abc.pyc /usr/lib64/python2.6/abc.pyo /usr/share/vim/vim74/syntax/abc.vim [root@localhost ~]# # 找以 abc开头,并且后面有3个字符的文件名 [root@localhost ~]# find / -name "abc???" /usr/lib64/python2.6/abc.py [root@localhost ~]# # 以 abc、bbc开头的文件名 [root@localhost ~]# find / -name "[ab]bc*" /var/lib/yum/yumdb/f/bbcf362a6735114bc8db89df8eb5ba8a658888af-file-libs-5.04-30.el6-x86_64 /usr/lib64/python2.6/abc.py /usr/lib64/python2.6/abc.pyc /usr/lib64/python2.6/abc.pyo /usr/share/vim/vim74/syntax/abc.vim [root@localhost ~]# # 找以 abc、bbc、cbc开头的文件 [root@localhost ~]# find / -name "[a-c]bc*" /var/lib/yum/yumdb/f/bbcf362a6735114bc8db89df8eb5ba8a658888af-file-libs-5.04-30.el6-x86_64 /lib/modules/2.6.32-754.el6.x86_64/kernel/crypto/cbc.ko /usr/lib64/python2.6/abc.py /usr/lib64/python2.6/abc.pyc /usr/lib64/python2.6/abc.pyo /usr/share/man/man3/cbc_crypt.3.gz /usr/share/man/overrides/ru/man3/cbc_crypt.3.gz /usr/share/vim/vim74/syntax/abc.vim [root@localhost ~]# # 查找不是以 a-f 为首字母的文件,该文件后面还必须有bc和其他任意字符 [root@localhost ~]# find / -name "[^a-f]bc*" /lib/modules/2.6.32-754.el6.x86_64/kernel/fs/mbcache.ko /lib/modules/2.6.32-754.el6.x86_64/kernel/drivers/watchdog/sbc_fitpc2_wdt.ko /sys/module/mbcache /usr/lib/cups/monitor/tbcp /usr/lib64/python2.6/encodings/mbcs.pyc /usr/lib64/python2.6/encodings/mbcs.pyo /usr/lib64/python2.6/encodings/mbcs.py /usr/bin/mbchk /usr/share/man/man1/mbchk.1.gz [root@localhost ~]#



