【查看文件夹大小】

# /lib 目录大小
du -sh /lib

# /lib 子目录大小
du -sh /lib/*
 
# 查看 /lib 目录下普通文件大小
find /lib -type f -print0 | xargs -0 ls -la | \
awk -F ' ' 'BEGIN{sum=0} {sum+=$5} END{printf "%d bytes\n", sum}'


【统计文件数量】

# 查看 /lib 目录的总文件数(包含7种文件类型、包含 /lib 目录自身)
find /lib | wc -l

# 查看/lib 目录中普通文件的数量
find /lib -type f | wc -l

# 用 find、xargs、ls、cut、sort、uniq 等命令组合统计 /usr 目录每种类型的文件数量
# find 的 print0 参数与 xargs -0 参数是为了避免文件名中的特殊字符
# ls 命令带 d 参数是为了不列出目录内容,避免重复统计
find /usr -print0 | xargs -0 ls -lad | cut -c1 | sort | uniq -c

# 用 find、xargs、ls、cut、awk 等命令组合统计 /usr 目录每种类型的文件数量
# find 的 print0 参数与 xargs -0 参数是为了避免文件名中的特殊字符
# ls 命令带 d 参数是为了不列出目录内容,避免重复统计
find /usr -print0 | xargs -0 ls -lad | cut -c1 | \
awk '{++array[$0]} END{for(key in array){print key, array[key]}}'

# 用 rsync 统计 /lib 目录每种类型的文件数量
# 包含 /lib 目录自身
# --dry-run 空转
# ~/fake_dir 一个不存在的假目录
rsync -a --stats --dry-run /lib ~/fake_dir | grep "^Number of files:"


【Linux 的7种文件类型】

-普通文件(Regular file)/etc/passwd/etc/passwd
d目录(Directory files)/etc/etc
c字符设备文件(Character device file)/dev/tty/dev/tty
b块设备文件(Block file,硬盘、CDROM)/dev/sr0/dev/sr0
s套接字文件(Socket file)/dev/log/run/nscd/socket
p

管道文件

(Named pipe file or just a pipe file)

/dev/initctl/run/systemd/initctl/fifo
l符号链接文件(Symbolic link file)/dev/cdrom/dev/cdrom

注:第三列为 CentOS 5.9 下的示例文件,第四列为 Ubuntu 16.04 下的示例文件。

find 关于文件类型 type 的说明:

$ lsb_release -ds
Ubuntu 16.04.2 LTS
$ LESS="+/^\s+-type c" man find
       -type c
              File is of type c:
              b      block (buffered) special
              c      character (unbuffered) special
              d      directory
              p      named pipe (FIFO)
              f      regular file
              l      symbolic link; this is never true if the -L option or the -follow option 
                     is in effect, unless the symbolic link is broken.  
                     If you want to search for symbolic links when -L is in effect, use -xtype.
              s      socket
              D      door (Solaris)


【相关阅读】

1、海量文件拷贝(Windows/Linux)

2、查看Linux磁盘块大小


*** walker ***