How to Output a List of Files to a File and Sort Them in Linux

How to Output a List of Files to a File and Sort Them in Linux

4 Comments on How to Output a List of Files to a File and Sort Them in Linux

Have you ever wondered how to dump thousands of files to a text file and sort them alphabetically? This tutorial will show you how to do it and create a bash script.

Listing Your Files Recursively
The command I will be using to list all the files is find. To list all the files recursively in a directory use:

find /your/path

List AVI Files Only
To list all files with a certain file extension recursively use the -name option in find:

find /your/path -name *.avi

List Just The Files
To output just the file names and not the directory path add -printf “%f\n” to the end:

find /your/path -name *.avi -printf "%f\n"

The directories will still be listed as find enters the directory. To have only the files listed add -type f to find:

find /your/path -name *.avi -type f -printf "%f\n"

Sorting The List Alphabetically
To sort the list alphabetically, pipe the output to sort:

find /your/path -name *.avi -type f -printf "%f\n" | sort

Output The List To A File
To output the list to a file just add > /path/to/file.txt to the end:

find /your/path -name *.avi -type f -printf "%f\n" | sort > /path/to/file.txt

Adding What We Have Learnt To A Script
The script is going to take a command line argument which is the directory path to list, list the files recursively, sort them alphabetically and output them to the second command line argument which is the file to output to:

echo "dumping a list of files from '$1' to '$2'"
echo "starting..."
find $1 -type f -printf "%f\n" > $2
echo "finished."

The output will be something like this:

./dumpfiles /Repository/Media/Media1/TV/ ~/Desktop/test.txt
dumping a list of files from '/Repository/Media/Media1/TV/' to '/home/bill/Desktop/test.txt'

About the author:

Bill Payne started working as a paid professional software developer at the young age of 12 years old developing simple games and other applications for pre-packaged computers. Bill has since developed software for many industries such as direct sales and the the stock market. Bill has now started sharing his many years of software development experience through a blog on the MPSHouse website and one on one lessons.


  1. ianr  - 9 July , 2012 - 11:20 pm
    Reply /

    Some of these examples don’t work for me:
    find /your/path -name *.avi
    I get “find: paths must precede expression”
    I have to make it:
    find /your/path -name ‘*.avi’ or find /your/path -name “*.avi”
    in order for a name starting with a wildcard to work. This seems to be true on a pretty pure ubuntu 10.04, 12.04 and centos 5.x. The 12.04 I have done nothing to, so I think I am talking about a stock bash, not something I’ve done something special to.

    It took me a long time to figure this out; HTH

  2. bill  - 11 July , 2012 - 7:06 pm
    Reply /

    I am glad you worked it out and thanks for sharing. I have not needed to use quotes and I am using Ubuntu 12.04 (Gnome Shell Remix).

  3. Mike  - 26 July , 2012 - 12:46 pm
    Reply /

    the -printf switch doesn’t work in OSX. I get an error: -printf: unknown option

    • bill  - 27 July , 2012 - 5:04 pm
      Reply /

      I have not tested this on an Apple machine. It seems that the formatted print is not available on OSX. You can check the OSX man page for find here

Leave a comment

Back to Top