linux - 在 find 命令中查找文件数和文件名,忽略目录

标签 linux unix find command

我正在寻找这样的东西

test1 : 2 
aaa.txt 
bbb.txt

test2 : 3 
ababa.txt 
cbbab.txt 
ddddd.txt

test3 : 1 
mmmm.txt

但我当前的代码也列出了该目录。如何删除输出中的目录

这是我的代码

find /tmp/test/ -maxdepth 2 -mindepth 1 -type d | while read dir; do printf "%s : " "$dir"; find "$dir" -maxdepth 1 -type f | wc -l; find "$dir" -maxdepth 1 -type f ; done;

我应该改变什么?

最佳答案

并非所有输出行都会被捕获在变量中,因此使用 ${var##*/} 从 var 中删除路径将不起作用。因此,只需使用 sed 切断完整输出的路径即可:

find /tmp/test/ -maxdepth 2 -mindepth 1 -type d | 
   while read dir; do
      printf "%s : " "$dir"
      find "$dir" -maxdepth 1 -type f | wc -l
      find "$dir" -maxdepth 1 -type f
   done | sed 's#.*/##'

当您想要更好的布局时,可以使用以下结构:

find /tmp -maxdepth 2 -mindepth 1 -type d | while read dir; do 
   # Delete path from dir with ##*/
   printf "%s : " "${dir##*/}"
   find "$dir" -maxdepth 1 -type f | wc -l
   # Replace path with some spaces
   find "$dir" -maxdepth 1 -type f | sed 's#.*/#   #'
   # Redirect the "permission denied messages" to the end of the Galaxy.
done 2>/dev/null

关于linux - 在 find 命令中查找文件数和文件名,忽略目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35349567/

相关文章:

matlab - 查找矩阵的第一个非零列(矢量化版本)

java - 从终端运行代码时会创建文本文件,但在 Java 中运行时不会

python - 如何从 (ba)sh 脚本导入变量?

linux - 使用 Ansible 在 Linux 上安装 Swift 3 + libdispatch

linux - MAC OS X 别名文件夹/文件不会在 Windows 上保留

bash - 格式化目录中的所有 XML 文件并将它们保存在子目录中

javascript - 为什么 jQuery next 找不到下一个 div?

c - Windows 程序员转向 Linux - 编码约定

shell - "inplace"shell命令的包装器

c - C 中的 UNIX 共享内存和信号量