Basic functionality of "Find" in Linux
Posted: 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
Name, user and location related searching
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.
will give the result regardless of the case.
This will list down the files which end with ‘ello’ inside the /home directory
Time related searching
This will list down the files which own by the user inside the /home directory
This will list down the file which are modified within the last 24 hrs inside the /home directory.
Other options can use with time:
Eg :
Find every file under the directory /etc that was modified more than 20 days ago.
How do i find file containing ‘Colombo’. So that i can use it to change the timezone.
Type related searching
Searching can be performed according to type such as file, directory, link.
Eg :
Searching file name timezone inside the /etc folder
Exercises:
Identify the difference between these two
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.
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.
‘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
EXAMPLESfind [path...] [expression]
Name, user and location related searching
Code: Select all
find / -name hello
Code: Select all
find / -iname hello
Code: Select all
find /home -name *ello
Code: Select all
find /home -user udithaM
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
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
Identify the difference between these two
Code: Select all
find /tmp -name udithaM -type f -print | xargs /bin/rm -f
Code: Select all
find /tmp -name udithaM -type f -print0 | xargs /bin/rm -f