Entries Comments



Further uses of the Find command

8 November, 2007 (12:42) | misc, UNIX | By: passion@linux

In another article, we briefly looked at the find command, which searches for specified files. We will now look at more uses of this very powerful command.

It can be used to search for files using various criteria, including name, owner and time last changed. Additionally, it can execute a command or script for each of the matching files. It could therefore be used for many purposes, for example, to remove files not accessed since a given date, to recompile all programs modified since the time of the last compile, or to backup all the files belonging to a specific user.

The syntax of the command is:

find
where is the pathname to the directory where the search is to begin and
and specify the search criteria and any actions to be taken, as detailed below:

-name Find files whose name matches the given pattern. The pattern should be in quotes.
-print Display the path of matching files
-user Find files belonging to a specific user
-exec command {} \; Execute Unix/Linux command for each matching file.
-atime (+t,-t,t) Find files accessed more that +t days ago, less than -t or precisely t days ago.
-ctime (+t,-t,t) Find files changed more that +t days ago, less than -t or precisely t days ago.

-perm Find files with given permissions.
-type Locate files of a given type:
c=character device b=blocked device d=directory
p=pipe s=socket f=normal file l=symbolic link
-size nx Find files with size larger than n where x can be b=bytes;
c=characters; k=kilobytes w=2 byte word:

Examples:

find . –name myfile
Search for myfile under the current directory

find /usr/sam/cprogs –ctime –1 –exec cc {} \;

Compile all programs that have changed less than one day ago.

Write a comment