How to only get file name with linux `find`?

I'm using find to all files in directory, so I get a list of paths. However, I need only file names. ie I get ./dir1/dir2/file.txt and I want to get file.txt


在GNU find你可以使用-printf参数,例如:

find /dir1 -type f -printf "%fn"

如果你的find没有-printf选项,你也可以使用basename:

find ./dir1 -type f -exec basename {} ;

If you are using GNU find

find . -type f -printf "%fn"

Or you can use a programming language such as Ruby(1.9+)

$ ruby -e 'Dir["**/*"].each{|x| puts File.basename(x)}'

If you fancy a bash (at least 4) solution

shopt -s globstar
for file in **; do echo ${file##*/}; done
链接地址: http://www.djcxy.com/p/47256.html

上一篇: Python:在列表中查找

下一篇: 如何只用linux`find`来获取文件名?