Find command in unix with examples

Linux

 

The find command lets you search for files according to various criteria. Like the tools we have already discussed, find has a large number of options that you can read about in its man page. Here is the general format of find:

[[email protected] ~]$ find start_dir [options]

where start_dir is the directory from which the search should start.

To find all files in the current directory (i.e., the “.” directory) that have not been accessed in at least seven days, use the following command:

[[email protected] ~]$ find . -atime 7

Type this command to find all files in your present working directory whose names are core and then delete them (i.e., automatically run the rm command):

[[email protected] ~]$ find . -name core -exec rm {} \;

The syntax for the -exec option with the find command as used here can be hard to remember sometimes, and so you can also use the xargs method instead of the exec option used in this example. Using xargs, the command would then be written

[[email protected] ~]$ find . -name 'core' | xargs rm


To find all files in your PWD whose names end in .txt (i.e., files that have the .txt extension) and are also less than 100 kilobytes (K) in size, issue this command:

[[email protected] ~]$ find . -name '*.txt' -size -100k

To find all files in your PWD whose names end in .txt (i.e., files that have the .txt extension) and are also greater than 100K in size, issue this command:

[[email protected] ~]$ find . -name '*.txt' -size 100k

 

In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.

Leave a Reply