Site Menu

Share

LINUX CLASSES - DATA MANIPULATION

How Do I Find Files with Linux?

The find command locates files in many different ways. Unlike the rest of the commands in this section, find does not look at the contents of a file--it only helps you find files that meet certain criteria, such as name, size, age, and type. The general form of the find command is find <starting point> <search criteria> <action>

The starting point is the name of the directory where find should start looking for files. The find command examines all files in this directory (and any subdirectories) to see if they meet the specified search criteria. If any do, find performs the specified action on each found file. Here are some of the most useful search criteria options:

-name pattern Find files with names that match the pattern.
-size [+|-] n Find files larger or smaller than a certain size.
-atime [+|-] n Find files accessed before or after a certain date.
-mtime [+|-] n Find files modified before or after a certain date.
-type filetype Find only regular files or only directories.

And here are the actions that can be applied to found files:

-print Print just the names of matching files.
-ls Print the names, dates, sizes, and so on of matching files.
-exec command Execute a command with the file name as input.
-ok command Same as -exec, but asks for confirmation first.

That all might look a bit confusing, so here are some examples to bring things down to earth. To find files (starting in the current directory) with names ending with .data and to print their names, try this:

find . -name '*.data' -print
company.data
donor.data
grades.data
sorted.data
words.data

To find files larger than 40K and print the file names and details (use a minus sign instead of a plus sign to find files smaller than a certain size), issue this command:

find . -size +40k -ls
-rw-rw-r-- hermie users 56720 Jan 16 12:42 bigfile
-rw-rw-r-- hermie users 415206 Feb 27 21:37 largefile
-rw-rw-r-- hermie users 315428 Jan 07 05:23 hugefile

To find files ending with .dat that are smaller than 100K, enter

find . -name *.txt -size -100k -ls
-rw-rw-r-- hermie users 26720 Feb 06 23:52 recipes.txt
-rw-rw-r-- hermie users 506 Feb 18 18:45 poem.txt

To find files that have not been accessed for over 30 days and delete them (by sending their names to the rm command), enter

find . -atime +30 -exec rm {} \;

To find directories (starting in the junk directory) and conditionally delete them (by sending their names to the rmdir command), enter

find junk -type d -ok rmdir {} \;

For more information on the find command, see the find manual.

Previous Lesson: Crunching Data
Next Lesson: Pipe Fitting

[ RETURN TO INDEX ]


   

Comments - most recent first
(Please feel free to answer questions posted by others!)

Doug     (28 Aug 2012, 23:50)
Absolutely just what I needed! Thanks for posting :)
chacha Fred     (13 Apr 2012, 16:25)
I need assistance a script that can check a file size x.gz in a folder and if its less than 6mb it should move it to another folder and send an email of invalid file size.

if file is greater than 6mb it should move to a different folder.

thanks
Fred
Harshit     (29 Feb 2012, 03:51)
Sebastiaan Kop
good question among all.
as from i know this is not possible to find.
but if you execute that thing than please post here how to find.
Sebastiaan Kop     (15 Feb 2012, 17:31)
Hey lowfat guy,

I want to make a find command that will do the following:

find all the folders starting in the folder /volume1/music. But i want to exclude the folders that have files of the following types in them: mp3 or flac or wav.
Then display the list of folders found on the screen.

I've been searching my ... off for this but i cant find a solution.

Can you help?

Thanks.
Senthl     (13 Feb 2012, 18:51)
cat *.mp3 > /home/username/mp3list.txt

must run this inside the music folder ie where you saved the music files
applicable for mp3 only
change extension as per needed

username actually the user login name
Vijayabaskar     (07 Feb 2012, 17:53)
simple and powerful explation for each and every commands
Annon     (07 Jan 2012, 06:45)
Typo...

To find files ending with .dat that are smaller than 100K, enter

find . -name *.txt -size -100k -ls
Rafael     (13 May 2011, 17:24)
Hey, I need to create a file list in text format for Icecast.

I have a folder with subfolders and subfolders with lots of musics.
I need to list all those musics in a .txt file.
I use Linux OS.
what to do?
sreepathiNaidu     (20 Jan 2011, 02:43)
Really its a awesome handbook to people starting newly to AWk......Great experience that i am done with this.
Pharmb46     (29 Oct 2010, 08:58)
Very nice site!
Pharmf732     (29 Oct 2010, 08:58)
Hello! keecddd interesting keecddd site!
Paul     (22 Jun 2010, 06:57)
Typo I think,
To find files ending with .dat that are smaller than 100K, enter
should be
To find files ending with .txt that are smaller than 100K, enter

I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But don't ask me to send what you already have. :-)

NO SPAM! If you post garbage, it will be deleted, and you will be banned.
*Name:
Email:
Notify me about new comments on this page
Hide my email
*Text:
 
 


Ask Bob Rankin - Free Tech Support


Copyright by - Privacy Policy
All rights reserved - Redistribution is allowed only with permission.