Home » Linux

Find Files in a Directory

9 March 2015 No Comment
VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)

foldersSuppose you have several directories with several sub-directories with files in them. How do you go about finding the files you need? You may want to find the latest modified files. You may want to find files belonging to a certain group. You may want to find files under the dotfiles directories. The list goes on and on.

You can go about writing a program to parse through the directories. Before you start on this endeavor, consider the find command. It is a very powerful command and should be part of your tool belt.

There are so many different cases where you may want to use the find command. Below I have listed some cases where the find command may come in handy and help you solve your problem in no time at all.

Find the 10 latest modified files in a directory

find . -type f -exec stat –format “%Y :%y %n” {} \; | sort -nr | cut -d: -f2- | head

When you are in the directory you want to search, run the command above to find the 10 latest modified files in a directory. To search for more than 10 files, update the head command. For example, to change it to 20 files, change “head” to “head -20” in the command above.

If you have a lot of files in the directory, it might take some time to search through them because you are running the find command and then running the stat command on them. The command below should give you better performance.

find . -type f -printf “%T+ %p\n” | sort -nr | head

With this command, you are using the built-in printf option that is part of the find command. As such, it does not need to call another command to print out the date and file.

Find all the writable files owned by group guest in your home directory

find /home/$USER -group guest -perm -g=w

The command above gives you all the files in your home directory that are writable by group guest.

Find allĀ files under the dotfiles directories

find . -type f | grep “^\./\.”

When you are in the directory you want to search, run the command above to find all files under the dotfiles directories. You may have files in your .config, .git or .thumbnails.

Find large files that have not been modified in the past 30 days

find /mydir -type f -mtime +30 -size +10M -exec ls -l {} \; > /tmp/largefiles.txt

Run the command above to find all files under the mydir folder that have not been modified in the past 30 days. These files are also more than 10MB in size. The output will be saved to a file called largefiles.txt in the /tmp folder.

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.