Chapter - 7.5 : Navigating Linux File Systems with Precision

Chapter - 7.5 : Navigating Linux File Systems with Precision

The Find Program :

Find is an extremly used utility program in the daily life of a Linux System administrator. It recurses down the file system tree from any particular directory or set of directories and locate files that matches specifies conditions. The default path name is always the present working directory, for example administrators sometimes scan for potentially large files which contain diagonstic information after a program fails they are more than several weeks old in order to remove them. It's also common to remove files that are not essential or that are outdated in the /tmp directory. Many linux ditributions use shell scripts that run periodically through cron to perform such house cleaning.

Using FInd :

When no arguments are given, find lists all files in the current directory and all of its subdirectories, commonly used options to sort in the list include :

-name

Which only list files with a certain pattern in their name.

-iname

It also ignores the case of the files names.

-type

It will restrict the results to files of a certain soecified type such as d for directory, l for symbolic links, and f for regular file.

Here's how you can search for files and directories named gcc:-

sudo find /usr -name gcc

Here's how you can search for directories named gcc:-

sudo find /usr -type f -name gcc

For regular files:-

sudo find /usr -type f -name gcc

Using Advanced Find Options :

Another good use of find is being able to run commands on the files that matches your search criteria.

  • -exec : This option is used for this purpose.

To find and remove all files that ends with .swp you can use this :

find -name "*.swp " -exec rm {} ';'

Here, the squiggly brackets are a placeholder that will be filled with all the file names that result form the find expression and preceding aommand will be run on each one individually.

NOTE : You have to end the command with either a semicolon including the single quote or a "\;" (slash semicolon).

One can use the dash ok (-ok) option which behaves the same as -exec, except that find will porpmt you for permission before executing the command, this makes it a good way to test your result before blindly executing any potentially dangerous commands.

find -name "*.swp" -exec rm{} ';'

This command will find and removes files that ends with .swp.

Finding Files Based On Time and Size :

It is sometimes the case that you wish to find files according to the attributes such as when they were created, last used or based on their size. It's easy to perform such searches. To find files based on time you can use this :

find /-ctime 3

Here -ctime is when the time inode metadata or the file and ownership permissions last changed. You can also search files based on when they were accessed or last read or based on when they were modified or last written. The number is the number of days and can be expressed in either a number in, that means exactly that value plus n which means greator than the number or minus n which means less than that number. There are similar options for times in minutes as in :

-cmin, -amin, and -mmin

To find files based on size you can do this :

find /-size 0

Note that the size here is in 512 byte blocks by default, you can also specify bytes, kilobytes, megabytes, and gigabytes etc.