Finding Files On The Command Line

Want to support HowtoForge? Become a subscriber!
 
Submitted by ardchoille42 (Contact Author) (Forums) on Tue, 2009-09-01 13:17. :: Linux

Finding Files On The Command Line

One of the things I like about Linux is the command line. I have used nautilus, gnome-commander, konqueror, kommander, dolphin and thunar to manage files in Linux and these file managers are great for what they do. But there are times when one simply wants to find a file when working on the command line without having to open a GUI application.

From the find man page:

GNU find searches the directory tree rooted at each given file name by evaluating the given expression from left to right, according to the rules of precedence until the outcome is known at which point find moves on to the next file name.

Find empty directories:

find /path -depth -type d -empty

Find empty files:

find /path -depth -type f -empty

Find a file with a specific name:

find /path -name name_of_file

Find a files with specific extensions:

find /path -name "*.given_extension"

Find files with specific permissions which have a ".txt. file extension:

find /path -name '*.txt' -perm 644

Find files with some given permissions:

find /path -perm -permision_bits

Find files with a given name and any extension:

find /path -name 'given_name.*'

Find files modified in the latest blocks of 24 hours:

find /path -mtime n

Where n is:

  • 0 for the last 24 hours
  • 1 for the last 48 hours
  • 2 for the last 72 hours

Find files that were accessed in the latest blocks of 24 hours:

find -atime n

Where n is:

  • 0 for the last 24 hours
  • 1 for the last 48 hours
  • 2 for the last 72 hours

Find files according to owner:

find /path -user root

One can also pipe find commands to the xargs command to execute commands on files.

Find and delete files:

find /path -name mytestfile | xargs rm

See man find and man xargs for more information about these powerful commands.

Many new Linux users are intimidated by the command line and this feeling should be overcome from the onset because the command line can be faster and more powerful than most GUI applications.


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Anonymous (not registered) on Sat, 2014-02-15 19:35.
Immensely frustrating when I want to find a type of file, edited only a few days ago and has a particular fragment of text. I just can't see anything that will do it (and get somebody elses project going again)
Submitted by Agkelos (not registered) on Fri, 2009-09-04 21:46.

Useful article!

I find the "find" command very useful when some databases get corrupted. I use:

 

find /var/db/mysql -name "*.MYI" -exec myisamchk -r '{}' \;

 

It works like a charm ;)

Submitted by Anonymous (not registered) on Thu, 2013-06-27 19:15.

Nice info, Here

 Here is a way to convert the command to vbs file to search files.

http://www.funbutlearn.com/2012/08/the-easy-way-to-search-files-search.html

Submitted by Alinn (not registered) on Wed, 2009-09-02 20:06.
Find and Copy files?
Submitted by Anonymous (not registered) on Wed, 2009-09-02 14:19.
To significantly speed up deletion of large numbers of files, finish the find command with a +, not a \;, thus:

find /path -iname "*somefile*" -exec rm "{}" +
Submitted by Anonymous (not registered) on Wed, 2009-09-02 13:03.

You could use -amin, -cmin and -mmin to improve granularity in searches.

In -amin, -cmin and -mmin n is number of minutes, so you can search files accessed/created/modified in the last 5 minutes for example.

Submitted by imneo (not registered) on Wed, 2009-09-02 09:56.

Here is how you can find large files (or files larger then %)

find / -type f -size +10000k -exec ls -lh {} \; | awk '{ print $8 ": " $5 }'
Submitted by Anonymous (not registered) on Wed, 2009-09-02 08:22.

 An alternative way to find and delete files using only find.

find /path -name mytestfile -exec rm '{}' \;

Everything between -exec and \; get executed per file, and '{}' is replaced with the name of the file found.

This can be used to do just about anything with files. So for example, find and delete all CVS folders in a project:

find /path -name CVS -type d -exec rm -r '{}' \;

 Create and md5 hash of all files in a folder

find /home/williamb/ -type f -exec md5sum '{}' \;

 Possibilities are endless...

Submitted by Anonymous (not registered) on Tue, 2009-09-08 23:39.
Command line?  You mean like the old days of DOS?  Does that come complete with a mono-chrome green-screen monitor? Wow, talk about antiquated and obsolete. 
Submitted by Anonymous (not registered) on Thu, 2010-03-25 12:21.

Command line is not for you Anonymous. Don't use it! If you can't click on it it does not exist for you.

Submitted by Amza Marian (not registered) on Thu, 2009-09-10 03:21.

Re: Find and delete using only find Submitted by Anonymous (not registered) on Tue, 2009-09-08 23:39.

Command line? You mean like the old days of DOS? Does that come complete with a mono-chrome green-screen monitor? Wow, talk about antiquated and obsolete.

-------------------------

This is not for DOS. He talking about CLI ( command line interface or / and command line interpreter ) for linux. This is a SHEL. You can read more here -> http://en.wikipedia.org/wiki/Command-line_interpreter

Great tutorial.