SECTION 6 补充文件相关命令

1.linux补充文件相关命令

命令描述
du显示目录或文件的大小
wc统计文件行、单词、字符、字节
ln创建硬链接和软连接
which查找可执行文件位置
whereis查找二进制文件、原代码文件和帮助文档
locate在数据库中快速查找文件
type判断指定命令的类型
1.1.du命令

解释

命令用于显示目录或文件的大小,会显示指定的目录或文件所占用的磁盘空间

选项

-a或-all:显示目录中个别文件和目录的大小
-b或-bytes:显示目录或文件大小时,以byte为单位
-B <1K>或 --block-size=<1K>:指定块大小 (默认为1K)
-c或–total:除了显示个别目录或文件的大小外,同时也显示所有目录或文件的总和
-d <目录层数>或–max-depth=<目录层数>:超过指定层数的目录后,予以忽略
-D或–dereference-args:显示指定符号连接的源文件大小
-h或–human-readable:以K,M,G为单位,提高信息的可读性
-H或–si:与-h参数相同,但是K,M,G是以1000为换算单位
-k或–kilobytes:以1024
-l或–count-links:重复计算硬件连接的文件
-L<符号连接>或–dereference<符号连接>:显示选项中所指定符号连接的源文件大小
-m或–megabytes:以1MB为单位
-s或–summarize:仅显示指定目录或文件的总大小,而不显示其子目录的大小
-S或–separate-dirs:显示个别目录的大小时,并不含其子目录的大小
-x或–one-file-xystem:以一开始处理时的文件系统为准,若遇上其它不同的文件系统目录则略过
-X<文件>或–exclude-from=<文件>:在<文件>指定目录或文件
–exclude=<目录或文件>:略过指定的目录或文件,若是同名也都忽略
–help:显示帮助
–version:显示版本信息

用法

du -ah      #显示目录中个别文件和目录大小,以人类可读格式显示       
du -h dir/  #显示指定目录大小,以人类可读格式显示   
du -sh      #显示当前路径总目录大小,,以人类可读格式显示    
du -ch      #显示目录中个别文件和目录大小和总大小,以人类可读格式显示   
du -ah -d 1 ../test/ #显示指定目录层数为1层,并统计该层目录和文件大小,以人类可读格式显示    
du -ah --exclude=dir #显示忽略指定目录dir,并剩下目录和文件大小,以人类可读格式显示    

实例

[root@node1 test]# du -ah
4.0K    ./6
4.0K    ./5
4.0K    ./2
4.0K    ./3
12K     ./4
4.0K    ./dir/1
4.0K    ./dir/2
4.0K    ./dir/3
8.0K    ./dir/cp
4.0K    ./dir/test.log
28K     ./dir
60K     .
[root@node1 test]# du -h dir/
28K     dir/
[root@node1 test]# du -sh
60K     .
[root@node1 test]# du -sh ./
60K     ./
[root@node1 test]# du -ch
28K     ./dir
60K     .
60K     total
[root@node1 test]# du -ah -d 1 ../test/
4.0K    ../test/6
4.0K    ../test/5
4.0K    ../test/2
4.0K    ../test/3
12K     ../test/4
28K     ../test/dir
60K     ../test/
[root@node1 test]# du -ah --exclude=dir
4.0K    ./6
4.0K    ./5
4.0K    ./2
4.0K    ./3
12K     ./4
32K     .
1.2.wc命令

解释

命令用于计算字数,可以计算文件的Byte数、字数、或是列数,若不指定文件名称、或是所给予的文件名为"-",则wc指令会从标准输入设备读取数据

选项

-c或–bytes或–chars:只显示Bytes数
-l或–lines:显示行数
-m或–chars:显示字符数
-w或–words:只显示字数
-L或–max-line-length:显示最长行的长度
–help:在线帮助
–version:显示版本信息

用法

cat test.log|wc    #查看指定文件,参数1是行数,参数2是单词数,参数3是字节数
cat test.log|wc -l #查看指定文件,显示行数
cat test.log|wc -w #查看指定文件,显示单词数  
cat test.log|wc -c #查看指定文件,显示字节数
cat test.log|wc -m #查看指定文件,显示字符数
cat test.log|wc -L #查看指定文件,显示最长行的长度

实例

[root@node1 dir]# cat test.log
1 ZhangSan 18 man
2 LiSi 17 woman
3 WangWu 17 man
4 ZhaoLiu 19 woman
5 SunQi 20 man
[root@node1 dir]# cat test.log|wc
      5      20      84
[root@node1 dir]# cat test.log|wc -l
5
[root@node1 dir]# cat test.log|wc -w
20
[root@node1 dir]# cat test.log|wc -c
84
[root@node1 dir]# cat test.log|wc -m
84
[root@node1 dir]# cat test.log|wc -L
18
1.3.ln命令

解释

命令是一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同步的链接,可以分为软链接和硬链接

软链接

1.软链接,以路径的形式存在,类似于Windows操作系统中的快捷方式
2.软链接可以 跨文件系统 ,硬链接不可以
3.软链接可以对一个不存在的文件名进行链接
4.软链接可以对目录进行链接

硬链接

1.硬链接,以文件副本的形式存在,但不占用实际空间
2.不允许给目录创建硬链接
3.硬链接只有在同一个文件系统中才能创建

选项

–backup[=CONTROL]:备份已存在的目标文件
-b:类似–backup,但不接受参数
-d:允许超级用户制作目录的硬链接
-f:强制执行
-i:交互模式,文件存在则提示用户是否覆盖
-n:把符号链接视为一般目录
-s:软链接(符号链接)
-v:显示详细的处理过程
-S:“-S<字尾备份字符串>
-V:”-V<备份方式>“或”–version-control=<备份方式>"
–help:显示帮助信息
–version:显示版本信息

用法

ln test.log test-2.log      #创建文件硬链接,修改内容数据同步,删除源文件,硬链接数据不受影响 
ln -s test.log test-2.log   #创建文件软件链接,修改内容数据同步,删除源文件,软链接数据已经不存在并报错  
unlink test-2.log或rm -rf test-2.log #正确删除链接方法
ln -f test-2.log test-1.log #创建文件硬链接,强制覆盖另一文件内容 

实例

[root@node1 dir]# ln test.log test-2.log
[root@node1 dir]# ll
total 12
drwxr-xr-x 2 root root 4096 Dec 14 14:36 test
-rw-r--r-- 2 root root   21 Dec 14 14:36 test-2.log
-rw-r--r-- 2 root root   21 Dec 14 14:36 test.log
[root@node1 dir]# head -v test.log test-2.log
==> test.log <==
This is a test file.

==> test-2.log <==
This is a test file.

[root@node1 dir]# echo "test This is a test file." >> test.log
[root@node1 dir]# echo "test-2 This is a test file." >> test-2.log
[root@node1 dir]# head -v test.log test-2.log
==> test.log <==
This is a test file.
test This is a test file.
test-2 This is a test file.

==> test-2.log <==
This is a test file.
test This is a test file.
test-2 This is a test file.

[root@node1 dir]# rm -rf test.log
[root@node1 dir]# ll
total 8
drwxr-xr-x 2 root root 4096 Dec 14 14:36 test
-rw-r--r-- 1 root root   75 Dec 14 14:47 test-2.log
[root@node1 dir]# head -v test-2.log
==> test-2.log <==
This is a test file.
test This is a test file.
test-2 This is a test file.

[root@node1 dir]# ln -s test.log test-2.log
[root@node1 dir]# ll
total 8
drwxr-xr-x 2 root root 4096 Dec 14 14:36 test
lrwxrwxrwx 1 root root    8 Dec 14 14:55 test-2.log -> test.log
-rw-r--r-- 1 root root   21 Dec 14 14:55 test.log
[root@node1 dir]# head -v test.log test-2.log
==> test.log <==
This is a test file.

==> test-2.log <==
This is a test file.

[root@node1 dir]# echo "test This is a test file." >> test.log
[root@node1 dir]# echo "test-2 This is a test file." >> test-2.log
[root@node1 dir]# head -v test.log test-2.log
==> test.log <==
This is a test file.
test This is a test file.
test-2 This is a test file.

==> test-2.log <==
This is a test file.
test This is a test file.
test-2 This is a test file.

[root@node1 dir]# rm -rf test.log
[root@node1 dir]# ll
total 4
drwxr-xr-x 2 root root 4096 Dec 14 14:36 test
lrwxrwxrwx 1 root root    8 Dec 14 14:55 test-2.log -> test.log
[root@node1 dir]# cat test-2.log
cat: test-2.log: No such file or directory
[root@node1 dir]# ll
total 8
drwxr-xr-x 2 root root 4096 Dec 14 14:36 test
lrwxrwxrwx 1 root root    8 Dec 14 14:55 test-2.log -> test.log
-rw-r--r-- 1 root root   21 Dec 14 15:00 test.log
[root@node1 dir]# rm -rf test-2.log
[root@node1 dir]# ll
total 8
drwxr-xr-x 2 root root 4096 Dec 14 14:36 test
-rw-r--r-- 1 root root   21 Dec 14 15:00 test.log

[root@node1 dir]# ln -s test.log test-2.log
[root@node1 dir]# ll
total 8
drwxr-xr-x 2 root root 4096 Dec 14 14:36 test
lrwxrwxrwx 1 root root    8 Dec 14 15:01 test-2.log -> test.log
-rw-r--r-- 1 root root   21 Dec 14 15:00 test.log
[root@node1 dir]# unlink test-2.log
[root@node1 dir]# ll
total 8
drwxr-xr-x 2 root root 4096 Dec 14 14:36 test
-rw-r--r-- 1 root root   21 Dec 14 15:00 test.log
[root@node1 dir]# ll
total 12
drwxr-xr-x 2 root root 4096 Dec 14 14:36 test
-rw-r--r-- 1 root root   23 Dec 14 15:07 test-1.log
-rw-r--r-- 1 root root   23 Dec 14 15:07 test-2.log
[root@node1 dir]# head -v test-1.log test-2.log
==> test-1.log <==
This is a test file-1

==> test-2.log <==
This is a test file-2
[root@node1 dir]# ln -f test-2.log test-1.log
[root@node1 dir]# head -v test-1.log test-2.log
==> test-1.log <==
This is a test file-2

==> test-2.log <==
This is a test file-2
[root@node1 dir]# ll
total 12
drwxr-xr-x 2 root root 4096 Dec 14 14:36 test
-rw-r--r-- 2 root root   23 Dec 14 15:07 test-1.log
-rw-r--r-- 2 root root   23 Dec 14 15:07 test-2.log
1.4.which命令

解释

命令用于查找可执行文件位置

选项

-a:显示所有匹配的命令路径,而不仅仅是第一个匹配的路径
-V:显示which命令的版本信息
–skip-alias:忽略命令的别名

用法

which ls        #查看命令路径,只匹配一个,显示别名信息
which -a python #查看命令路径,匹配所有的
which --skip-alias ls #查看命令路径,不是显示别名信息

实例

[root@node1 ~]# which ls
alias ls='ls --color=auto'
        /usr/bin/ls
[root@node1 ~]# which -a python
/usr/bin/python
/usr/local/bin/python
[root@node1 ~]# which --skip-alias ls
/usr/bin/ls
1.5.whereis命令

解释

命令用于查找文件,只能用于查找二进制文件、原代码文件和man帮助文档

选项

-b:只查找二进制文件
-B<目录>:只在设置的目录下查找二进制文件
-m:只查找帮助文档
-M<目录>:只在设置的目录下查找帮助文档
-s:只查找原始代码文件
-S<目录>:只在设置的目录下查找原始代码文件
-f:不显示文件名前的路径名称
-u:查找不包含指定类型的文件
-l:输出所有效查找路径

用法

whereis ls     #查找二进制文件路径、帮助文档路径 
whereis -b ls  #查找二进制文件路径
whereis -m ls  #查找帮助文档路径
whereis -s ls  #查找原始代码文件
whereis -l     #输出所有效查找路径

实例

[root@node1 ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
[root@node1 ~]# whereis -b ls
ls: /usr/bin/ls
[root@node1 ~]# whereis -m ls
ls: /usr/share/man/man1/ls.1.gz
[root@node1 ~]# whereis -s ls
ls:
[root@node1 ~]# whereis -l
bin: /usr/bin
bin: /usr/sbin
bin: /usr/lib
bin: /usr/lib64
bin: /etc
bin: /usr/etc
bin: /usr/games
bin: /usr/local/bin
bin: /usr/local/sbin
bin: /usr/local/etc
bin: /usr/local/lib
bin: /usr/local/games
bin: /usr/include
bin: /usr/local
bin: /usr/libexec
bin: /usr/share
bin: /opt/containerd/bin
bin: /opt/cni/bin
man: /usr/share/man/zh_TW
man: /usr/share/man/pt_BR
man: /usr/share/man/man3p
man: /usr/share/man/man3
man: /usr/share/man/es
man: /usr/share/man/id
man: /usr/share/man/man9
man: /usr/share/man/man9x
man: /usr/share/man/man1x
man: /usr/share/man/man7
man: /usr/share/man/zh_CN
man: /usr/share/man/sv
man: /usr/share/man/man4x
man: /usr/share/man/ja
man: /usr/share/man/fr
man: /usr/share/man/nl
man: /usr/share/man/man5
man: /usr/share/man/ru
man: /usr/share/man/man1p
man: /usr/share/man/pt
man: /usr/share/man/man0p
man: /usr/share/man/da
man: /usr/share/man/hu
man: /usr/share/man/man6x
man: /usr/share/man/man3x
man: /usr/share/man/de
man: /usr/share/man/man7x
man: /usr/share/man/man1
man: /usr/share/man/man6
man: /usr/share/man/sk
man: /usr/share/man/man2
man: /usr/share/man/tr
man: /usr/share/man/man2x
man: /usr/share/man/pl
man: /usr/share/man/ko
man: /usr/share/man/mann
man: /usr/share/man/man5x
man: /usr/share/man/man8x
man: /usr/share/man/man4
man: /usr/share/man/man8
man: /usr/share/man/it
man: /usr/share/man/cs
src: /usr/src/debug
src: /usr/src/kernels
1.6.locate命令

解释

命令用于快速查找文件,它通过搜索数据库而不是实时搜索文件系统来定位文件,因此速度非常快

选项

-A或–all:仅打印与所有模式匹配的条目
-b或–basename:仅匹配路径名的基本名称
-c或–count:只输出找到的数量
-d或–database DBPATH:使用DBPATH指定的数据库,而不是默认数据库 /var/lib/mlocate/mlocate.db
-e或–existing:仅打印当前现有文件的条目
-0或–null:在输出上带有NUL的单独条目
-S或–statistics:不搜索条目,打印有关每个数据库的统计信息
-q:安静模式,不会显示任何错误讯息
-P或–nofollow或-H:检查文件存在时不要遵循尾随的符号链接
-l或–limit或-n LIMIT:将输出(或计数)限制为LIMIT个条目
-n:至多显示n个输出
-m或–mmap:被忽略,为了向后兼容
-r或–regexp REGEXP:使用基本正则表达式
–regex:使用扩展正则表达式
-q或–quiet:安静模式,不会显示任何错误讯息
-s或–stdio:被忽略,为了向后兼容
-o:指定资料库存的名称
-h或–help:显示帮助
-i或–ignore-case:忽略大小写
-V或–version:显示版本信息

用法

updatedb       #更新系统上数据库文件,如果刚创建的需要使用更新一下
locate test-log- #查找文件路径
locate -c test-log- #查找文件路径数量
locate -i test-log- #查找文件路径忽略大小写
locate -r "^/root.*.log$" #查找文件路径,使用正则匹配
locate -S #列出所使用的数据库文件的总目录数、总文件数、文件中的总字节数、用于存储数据库的总字节数 
locate -l 1 test-log-  #查找文件路径,只匹配一行  

实例

[root@node1 dir]# locate test-log-
/root/test/dir/test-log-1.log
/root/test/dir/test-log-2.log
[root@node1 dir]# locate -c test-log-
2
[root@node1 dir]# locate -i test-log-
/root/test/dir/Test-Log-1.log
/root/test/dir/test-log-1.log
/root/test/dir/test-log-2.log
[root@node1 dir]# locate -r "^/root.*.log$"
/root/test/test.log
/root/test/dir/Test-Log-1.log
/root/test/dir/test-log-1.log
/root/test/dir/test-log-2.log
[root@node1 dir]# locate -S
Database /var/lib/mlocate/mlocate.db:
        17,882 directories
        149,266 files
        13,405,537 bytes in file names
        3,851,149 bytes used to store database
[root@node1 dir]# locate -l 1 test-log-
/root/test/dir/test-log-1.log
1.7.type命令

解释

命令用于判断指定命令的类型是外部指令、命令别名或内部指令

选项

-t:输出“file”、“alias”、“builtin”、“keyword”或者“function”,分别表示给定的指令为“外部指令”、“命令别名”、“内部指令”、”关键字“或者“shell函数和系统函数”
-p:如果给出的指令为外部指令,只显示命令绝对路径
-a:如果给出的指令为外部指令,显示命令绝对路径和命令类型、别名信息
-f:排除对shell函数的查找

用法

declare -F     #列出所有shell函数
declare -f _cd #列出shell函数详细信息 
type -t chmod #判断是外部指令
type -t ls    #判断是别名指令
type -t cd    #判断是内部指令
type -t for   #判断是系统关键字
type -t _cd   #判断是shell函数
type -a cd    #显示命令类型和二进制文件路径
type -a -p cd #显示命令绝对路径

实例

[root@node1 dir]# type -t chmod
file
[root@node1 dir]# type -t ls
alias
[root@node1 dir]# type -t cd
builtin
[root@node1 dir]# type -t for
keyword
[root@node1 dir]# type -t _cd
function
[root@node1 dir]# type -a cd
cd is a shell builtin
cd is /usr/bin/cd
[root@node1 dir]# type -a -p cd
/usr/bin/cd

  • 31
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维日常学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值