How to only get file name with Linux 'find'? How to only get file name with Linux 'find'? shell shell

How to only get file name with Linux 'find'?


In GNU find you can use -printf parameter for that, e.g.:

find /dir1 -type f -printf "%f\n"


If your find doesn't have a -printf option you can also use basename:

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


If you are using GNU find

find . -type f -printf "%f\n"

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 globstarfor file in **; do echo ${file##*/}; done