Previously I have asked a very similar question but it was only for one subdirectory writing files in a subdirectory to a csv file and save it to parent directory in linux command line. I have a directory called 'dir', and there are 5 subdirectories in it, sub1, sub2,..sub5. Each sub directory has many files in it. I would like write to list of files from all subdirectories to a CSV file and save it to in 'dir' directory with a command line on linux. My code does that except I only want the filenames (name1,name2 etc) but it writes the full filenames with path (./sub1/name1,./sub1/name2, etc ). The command line I am using inside the dir folder is;

dir$ find . -type f  > names.csv
  • See also the update to this answer. It is not exactly what you want, but it is related: It gathers the file names of each sub-directory in a separate file.
    – Jürgen
    Jan 17, 2019 at 2:52

2 Answers 2


Since you've tagged your question 'Linux', I assume you have GNU find - in which case you can use

find . -type f -printf '%f\n' > names.csv 

From man find:

          %f     File's  name  with  any leading directories removed (only
                 the last element).
  • 1
    +1 this is much more elegant. I was not previously familiar with -printf in find.
    – Sparhawk
    Jan 17, 2019 at 2:07
  • thank you steeldriver for the great solution, since I already accepted the previous answer, I voted for you.
    – kutlus
    Jan 17, 2019 at 4:12
dir$ find . -type f -execdir basename {} \; > names.csv


  • -execdir performs the following command for each result.
  • basename {} \; remove the part before the final /. The {} substitutes for each directory found. See man find for more info.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .