0

I'm trying to list all files, except hidden ones, in only the subdirectories of a folder in bash by doing:

$ find ./public -mindepth 3 -type f -not -path '*/\.*'

That returns:

./public/mobile/images/image1.jpg
./public/mobile/images/image2.png
./public/mobile/images/image3.jpg
./public/mobile/javascripts/java1.js
./public/mobile/javascripts/java2.js
./public/mobile/javascripts/java3.js
./public/mobile/stylesheets/main.css
./public/mobile/views/doc1.html
./public/mobile/views/doc2.html
./public/mobile/views/doc3.html

How can I ignore the file path and show only the file name with the extension?

Thank you :)

mariec
  • 213
  • 3
  • 9
  • This should help http://stackoverflow.com/questions/15627446/find-and-basename-not-playing-nicely You could use search here or google :) – stryku Oct 23 '14 at 20:07
  • Thanks! Solved :) I've been like half hour searching something like that but didn't succeed...Next time I'll be more insistent! – mariec Oct 23 '14 at 20:19
  • possible duplicate of [How to only get file name with linux \`find\`?](http://stackoverflow.com/questions/5456120/how-to-only-get-file-name-with-linux-find) – Reinstate Monica Please Oct 23 '14 at 20:29

2 Answers2

1

Use -printf additionally to the find command, instead of -print.

find ./public -mindepth 3 -type f -not -path '*/\.*' -printf %f\\n

Note the usage of \\n - you need \n to add a new line after file name, but add another \ as escape or add some quotes to prevent interpreting \n by shell

davegson
  • 8,205
  • 4
  • 51
  • 71
dr_agon
  • 323
  • 2
  • 8
0

If you are using bash 4 or later, you can skip using find and using a file pattern instead.

shopt -s globstar    # For **
printf "%s\n" public/*/*/**/*.*

If you expect some files to have no extension, you'll need to use a loop and filter out non-file matches manually.

for f in */*/*/**/*; do
    [[ -f $f ]] || continue
    printf "%s\n" "${f##*/}"
done
chepner
  • 497,756
  • 71
  • 530
  • 681