2

I have to find some type of files in a directory and it's subdirectories and I only have to print out the filenames. So here's the main command:
find -type f -name "*.c"

Now, how could I cut the paths from each result? (it possibily could be done with awk, but I hope, that there's an easier way to do that).

5

GNU find supports the -printf predicate, which supports the %f format specifier for outputting on the filename.

find -type f -name "*.c" -printf '%f\n'
  • answered so fast. tried out. thanks again ! :D – Adorjan May 20 '13 at 14:58
4

Using awk is not so complicated:

find . -name '*.c' -type f | awk -F/ '{print $NF}'

Or sed:

find . -name '*.c' -type f | sed 's|.*/||'

(assuming file names don't contain newline characters) and that will work with any find implementation.

  • Assuming file names and path directory names don't contain newline characters. – Hauke Laging May 20 '13 at 15:40
2

You can use the basename program to get the file name:

find . -type f -name \*.c -exec basename {} +

This works on finds that don't have the GNU -printf extension.

1

GNU find knows -printf. If you search subdirectories, too, then file names can occur multiple times (in general)!

You need -printf "%p\n" or -printf "%f\n".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.