Basic functionality of "Find" in Linux

Linux OS Topics
Post Reply
Ageek
Sergeant
Sergeant
Posts: 22
Joined: Sat Aug 01, 2009 10:54 am

Basic functionality of "Find" in Linux

Post by Ageek » Tue Jun 21, 2011 3:30 pm

Find
‘Find’ is very power full tool that we can use in Linux terminal as a searching tool. ‘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.

SYNOPSIS
find [path...] [expression]
EXAMPLES

Name, user and location related searching

Code: Select all

	find / -name hello 
Looks for a file named "hello" starting at the root directory (searching all directories including mounted file systems). The `-name' option makes the search case sensitive. You can use the -iname option to find something regardless of case.

Code: Select all

	find / -iname hello
will give the result regardless of the case.

Code: Select all

find /home -name *ello
This will list down the files which end with ‘ello’ inside the /home directory

Code: Select all

	find /home -user udithaM 
Time related searching

This will list down the files which own by the user inside the /home directory

Code: Select all

find $HOME -mtime 0

This will list down the file which are modified within the last 24 hrs inside the /home directory.

Other options can use with time:
-amin n File was last accessed n minutes ago.

-cmin n File's status was last changed n minutes ago.

-atime n
File was last accessed n*24 hours ago. When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed at least two days ago.
-ctime n
File's status was last changed n*24 hours ago. This option can be used as above

Eg :

Find every file under the directory /etc that was modified more than 20 days ago.

Code: Select all

find /etc -mtime +20

How do i find file containing ‘Colombo’. So that i can use it to change the timezone.

Code: Select all

grep -irn ‘colombo’ /etc
Type related searching

Searching can be performed according to type such as file, directory, link.
-type f - for file seraching
d - for directory seraching
l - for link searching

Eg :
Searching file name timezone inside the /etc folder

Code: Select all

	find / -type f -name -i TIMEZONE
Exercises:
Identify the difference between these two

Code: Select all

	find /tmp -name udithaM -type f -print | xargs /bin/rm -f
Find files named pulse in or below the directory /tmp and delete them. Note that this will work incorrectly if there are any filenames containing newlines, single or double quotes, or spaces.

Code: Select all

	find /tmp -name udithaM -type f -print0 | xargs /bin/rm -f
Find files named core in or below the directory /tmp and delete them, processing filenames in such a way that file or directory names containing single or double quotes, spaces or newlines are correctly handled.
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Re: Basic functionality of "Find" in Linux

Post by Saman » Tue Jun 21, 2011 3:49 pm

Ohhh... I was waiting for this article.
Thanks a lot.
Post Reply

Return to “Linux”