Commands.page Logo

List All Files in Directory Tree to Text File Ubuntu

This guide explains how to generate a complete list of files within a directory tree and save it to a text file on Ubuntu. You will learn the most efficient command-line methods using standard tools to capture file paths and names for documentation or backup purposes.

Using the Find Command

The most reliable method for this task is using the find utility. It recursively searches through the specified directory and all subdirectories. Open your terminal and run the following command, replacing the path with your target directory:

find /path/to/directory -type f > filelist.txt

This command searches for items of type file (-type f) and redirects the standard output into filelist.txt. The resulting text file will contain the absolute or relative path of every file found within the tree.

Using the LS Command

You can also use the ls command with the recursive flag. This approach is quicker to type but includes directory headers in the output, which may require cleaning later. Execute this command to save the recursive list:

ls -R /path/to/directory > filelist.txt

The -R flag tells the system to list subdirectories recursively. Like the find method, the output is saved directly to the specified text file in the current working directory.

Understanding Output Redirection

The greater-than symbol > is used to redirect command output into a file. This operator overwrites any existing file with the same name. If you wish to append the new list to an existing file without deleting previous content, use two greater-than symbols >> instead. Ensure you have write permissions in the destination folder to create or modify the text file successfully.