Linux Blog

Linux find command

Filed under: Shell Script Sundays — TheLinuxBlog.com at 6:30 am on Sunday, November 7, 2010

linux find command

For those a little scared of the terminal using the Linux find command may seem a little daunting. To be honest though the find command really isn’t that hard to get the hang of. By effectively learning and using the Linux find command you’ll open up a whole new can of searching capabilities. You’ll increase your capabilities, boost productivity, and be more likely to find what your looking for. Alright, enough of the pep talk already and lets get to the core that is the powerful Linux find command.

Basic Searching

So, the first thing you need to know about the Linux find command, is how to find stuff. The simplest syntax for find is as follows:

find . -name "example_filename.txt"

What this does is find the file named (-name) example_filename.txt (“example_filename.txt”) from the present working directory “.” This may seem like a lot to find this file, but if you have a thousand directories on your desktop and you don’t know where you put that blasted example_filename.txt then it will find it. It will also do it quickly.

If you don’t remember the case of the example_filename.txt (perhaps you named it ExAmPlE_FiLeNaMe.TXT) you can use the -iname switch. You use it just like the above example.

What if you’re too lazy to cd into the directory you know the file is in. Or better put: search for files in another directory.  Well, instead of the . you can use the path. So if you want to search in /usr/share/icons you’d do

find /usr/share/icons -iname "*mail*"

Woah, what are those stars? Those are wildcards and they’re covered in the next section.

Wild Cards

So, you’ve mastered how to find a file if you know the file name, lets move on to wildcards. With the Linux Find Command, wildcards are a way of searching without knowing the everything about what you are trying to find.

The most commonly used wildcard for find is the asterisk. * will help you find what you need with only knowing part of what you wish to find.

Lets say you have a picture named example_filename.png only you don’t remember what type of picture it is, it could be .jpg, .jpeg, .gif or even the .exe that your gross aunt fanny’s infected Windows box sent you. You’d do something like this:

find . -iname "example_filename.*"

If you need to find a file where you only know the middle of a filename you can also use the asterisk at the beginning or anywhere inbetween as follows:

find . -iname "*foo*bar*"

This would match foobar.*, foodbar.* tofoobar*  tofootbarf* and so on.

Pretty simple huh? But wait there’s more! You can also use “?” as a wildcard. The question mark is a great way of finding things that you know have one certain digit. Take the following example:

You have a number of pdf files, they are sorted by a letter, followed by a number so A1.pdf through whatever, and B1.pdf through whatever, and C1.pdf through however many you want. Well, you only want the .pdf’s that have the number 2 in them, but you want all letters. You’d do something like this:

find . -iname "?2.pdf"

This is just as useful to know as the * command which I find myself using more often.

Date / Time

Handily the Linux find command also knows how to find files by date. You can search for files if you know the last accessed time, last changed time or last modified date. There are a huge number of options available with this one but the easiest is mmin (-mmin n). Make sure you represent n as a negative number.

find . -mmin -60

You can also use -mmin with a pattern

find . -mmin -60 -iname "*.doc"

Size

Finding files by size is pretty straight forward. The most common are search by kilobyte, megabyte and gigabyte these are represented respectively below.

find . -size 512k
find . -size 256M
find . -size 2G

Type

Sometimes it’s nice to be able to search just for directories, or only search for files. It can be useful for getting counts of directories or counts of files when used with wc. If you want to use the Linux find command to find all directories you can use the following:

find . -type d -iname "*test*"

The same goes for the file type, except f is used. (The following is piped to wc to get a file count)

find . -type f -iname "*test*" | wc -l

There are other types that can be used, but the ones that are useful to the average user are f (file) d (directory) and l (symlink)

Permissions

If you need to you can use the Linux find command to search for files by permission type. The easiest method is -perm and the octal representation. For more information on the octal format try: Introduction to CHMOD – Octal Format.

To simply put it you’d do something like this:

find . -perm -777

This article took me forever to write, because its hard to explain how to use the Linux Find Command in great detail. With such a powerful tool, it really is useful to read the find man page to get an understanding of the advanced options. Play with it, a lot is trial and error but with time you’ll be a master, I’m sure of it.

Pointing out Mistakes or giving Tips & Tricks are encouraged and welcomed. Please post them in the comments!

Man Pages for commands in this post »

find

5 Comments »

Comment by kaleb

November 7, 2010 @ 8:24 am

pretty good and its deffintly one of the more commonly ignored commands and it is deffintly a great tool to have in your box. but what im seeing here in your article is that you forgot to mention grep and pipe. while not directly related to the find command they are related to narrowing down resaults. for example when i want to find something i typicly use

find / | grep blaa.txt

find / will find every file on the filesystem which in linux is a fairly instant resualt but is huge and it doesnt show me anything i want to know. but i can easiy add | grep blaa.txt which will find that file anywhere on the filesystem almost instantly.

just my two cents on the subject

Comment by TheLinuxBlog.com

November 8, 2010 @ 11:01 am

Hey Kaleb, you don’t need to use grep to find a file, if your filename is blaa.txt you can do find / -name “blaa.txt” or find / -iname “blaa.txt”

Comment by anarchy1

November 17, 2010 @ 12:37 am

true grep isnt required but for anyone like me ..
I just love grep if you dont know the exact name of the file lets say the file is called
foobar1.txt

and all i can remember is it starts with
foob
then find wont necessarily find the resault you want
while
find \ | grep -i foob
will probably give you the closest match with no effort, and if you have multiple foobar.txt for example
foobar1.txt
2foobar.txt
and
101foobar.txt
then grep will show you these easy.
and color the exact mach to
what more could i ask for..?

Pingback by The Linux Blog » Linux History Command

November 17, 2010 @ 5:02 am

[…] are doomed to repeat typing. A lot. Seriously, the history command can help you remember the exact Linux find command with the intricate search options you typed a while ago. It could help you open up your x2x or […]

Comment by abhilash

December 21, 2010 @ 2:04 pm

how to find all files contating a particular text
for eg
i want to list all files in folder /home which contains “linux”

RSS feed for comments on this post. TrackBack URI

Leave a comment

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