If my terminal is currently in home/stuff/stuff2 and there are more folders and files under stuff2, how can I launch a command to get the file names of all the files and folders from this directory and onwards (Including all sub directories).

The operation needs to do the following:

  1. The file path must start at the location of the terminal command, so I don't want full files paths. Example, instead of the read out being home/stuff/stuff2/stuff3/cat.png I would prefer the read out to be stuff3/cat.png
  2. It must capture every file and folder and files within those folders.
  3. It needs to be piped (I think this is the term) to a text file.

Any ideas?

  • Do you want the names of the directories by themselves, or just the files in the directories? ie, do you want, for example, "Music", "Music/file1.mp3", ..., "Pictures", "pic1.jpg", ... – Marty Fried Nov 15 '14 at 3:55
  • @joseph Why not just running find * (?) if you are in home/stuff/stuff2 directory? – αғsнιη Nov 15 '14 at 7:38
  • By the way, if you're talking about running a command with output to a file, the more correct term is redirecting. Strictly speaking, piping refers to running a command with its output going directly to the input of another command. – G-Man Says 'Reinstate Monica' Nov 17 '14 at 22:33

The command

find -printf "%P\n" > file

will list all files and directories below the current directory and redirect the list to file. From the printf section of man find

  %P     File's name with the name of the command  line  argument
         under which it was found removed.
  • find -printf "%P\n" > file will also print the directory names by themselves. I suggest you use the switch I specified, -type f, to only list filenames. – Marty Fried Nov 15 '14 at 3:46
  • 2
    @MartyFried the OP specifically asks for "a command to get the file names of all the files and folders" – steeldriver Nov 15 '14 at 3:48
  • 1
    Granted. I assumed he meant the contents of the folders, but that could be incorrect. Hopefully, he will comment or edit his question to clear it up. – Marty Fried Nov 15 '14 at 3:53

In addition to find, it is also possible to do this with just the bash shell:

shopt -s globstar
printf "%s\n" ** >file

All output file names are printed relative to the current directory.

shopt -s globstar enables globstar which gives bash some of the power of find. With globstar, two stars in a row will, as man bash states, "match all files and zero or more directories and subdirectories." Here, printf is used to format the file names one to a line. The output is sent to a text file called file.

File names with special characters

This question asks for output in a text file. Text files generally have lines separated by newline characters. If any file or directory name itself contains a newline character, this will lead to confusion. Unless it is known that no such file exists, it is better to use a null-separated file, not a newline-separated text file.

In this case, one can use:

printf "%s\000" ** >file


find -print0 >file

Since the nul character (ascii 000) is never allowed in a filename, this is the one format that can be used to store file names in a file without danger of losing information.


If you can handle file in the form "./dir/filename", then a simple form of the find command would work:
find . -type f > filename will pipe all files below the current directory into "filename".

After seeing @steeldriver's answer, a combination of his solution and mine would eliminate the "./" prefix, like so:

If you do not want blank directory names in the list (ie, just the file names in each directory,
find -type f -printf "%P\n" > file

If you actually do want the directory names by themselves along with the directory/filenames, then omit the "-type f" flag from all the examples.

  • 1
    OP want both directory names and files. so remove the -type f – αғsнιη Nov 15 '14 at 7:37
  • @KasiyA: We don't really know that. I have found that for a very large percentage of questions by novices, they are not worded in such a way that you can take them literally, word for word. Your interpretation is different than mine, but until I hear differently, I will not assume one or the other is what he actually wanted. – Marty Fried Nov 15 '14 at 16:07
  • 1
    yes that's right. He said "file names of all the files and folders from this directory". this sentence of him is unclear. but I think he wants all files and folders name according to his accepted answer. – αғsнιη Nov 15 '14 at 16:41

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.