Jon*_*ith 2 command-line files
bash 脚本中的以下内容:
find /Volumes/SpeedyG -type d >> file.txt
Run Code Online (Sandbox Code Playgroud)
...可以很好地在文本文件中列出该路径中的文件夹,
/Volumes/SpeedyG/folder1
/Volumes/SpeedyG/folder2
/Volumes/SpeedyG/folder2
Run Code Online (Sandbox Code Playgroud)
但结果是文件夹的完整路径。
如果我只想要文件夹名称而不需要完整路径怎么办?
folder1
folder2
folder3
Run Code Online (Sandbox Code Playgroud)
对于 的 GNU 实现find,您可以使用格式化输出操作来执行此操作printf:
%P File's name with the name of the starting-point under
which it was found removed.
Run Code Online (Sandbox Code Playgroud)
所以
find /Volumes/SpeedyG -type d -printf '%P\n' >> file.txt
Run Code Online (Sandbox Code Playgroud)
如果要删除所有前导目录组件,可以%f使用%P. 如果你有zsh,你可以使用递归 shell 通配符和:t(tail) 限定符来执行相同的操作:
print -rC1 /Volumes/SpeedyG/**/*(ND/:t)
Run Code Online (Sandbox Code Playgroud)
find在这种情况下你根本不需要。对于任何 POSIX sh,你总是可以这样做
find dir -type d -exec sh -c '
for f do printf "%s\n" "${f##*/}"; done
' find-sh {} +
Run Code Online (Sandbox Code Playgroud)