Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List only file names in directories and subdirectories in bash

Tags:

bash

I need to create a list of files in a directory and all of its subdirectories. I've used

find . -type f

to get the list of files, but I only need the file name, not the path that leads to it.

So instead of returning this:

./temp2/temp3/file3.txt
./temp2/file2.txt
./file.txt

I need to return

file3.txt
file2.txt
file.txt
like image 851
TheRunner565 Avatar asked Jan 01 '26 03:01

TheRunner565


2 Answers

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

or better yet:

find . -type f -printf "%f\n"
like image 93
Eduardo Avatar answered Jan 04 '26 14:01

Eduardo


You can use printf option in gnu find:

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

For non-gnu find use -execdir and printf:

find . -type f -execdir printf "%s\n" {} +
like image 30
anubhava Avatar answered Jan 04 '26 13:01

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!