June 6, NOTES:

REVIEW:

  • Finding files is another useful task you can perform on your server when working on the command line.
  • The “find” command can be very useful at the start of a pipe to search for files
  • Most desktop-based Linux distributions have a way to find files from the desktop, but you can also find files from the command line using the find command.
  • “find” searches through a directory tree looking for files that match the criteria supplied.
  • If you just type find from the command line, the program will list every file in the current directory and all of its sub-directories.the simplest way to filter the results is to use the -name option

Example:

find -name photo.png (uses built-in defaults)

The full command: find . -name photo.png -print

  • “.” means search from the current directory downwards, and -print tells find to display the path of the matching files.
  • These can be omitted, however. If you want to search from a different starting point or you want to specify a different action, then you will need to include them.

For example, if you want to find where your Linux distro keeps the “gzip” command, then you would use:

sudo find / -name gzip -print

The “/” tells find to start searching from the root directory and it is best to use “sudo” as this gives find the privileges needed to read in every directory.

More Examples:

Find all files in the /etc and put the list in etcfiles.txt: find /etc > etcfiles.txt

Find files that end in .conf in the current directory (and all subdirs): find . -name "*.conf"

Find files of type file(not directory) that end in .conf: find . -type f -name "*.conf"

Find files of type directory that end in .bak: find /data -type d -name "*.bak"

Find files that are newer than file42.txt: find . -newer file42.txt

Find can also execute another command on every file found.

For example,the following command will look for every *.odf file and copy them to /backup/:

find /data -name "*.odf" -exec cp {} /backup \;

Find can also execute, after your confirmation, on every file found:

-> For example: remove *.odf files if you approve of it:

find / data -name "*.odf" -ok rm {} \;

case insensitive "find"

  • It is also possible to perform case insensitive matches on the filename by using-inamerather than-name, for example:
  • Example: find -iname photo.png

Other Example:

  • "Find" isn’t limited to only searching for files based on their name. You can also search based on file permission, size, type and when the file was last accessed.
  • For example, to find all the directories with a file permission of 777 (something that is not generally recommended) use:

sudo find / -type d -perm 777

Where -type d tells find to only match directories (not files) and -perm specifies the permissions, in this case 777.

To search for files greater than a certain size, use the -size option like this:

find . -size +25M

-- That will find all the files over 25MB in the current directory and its sub-directories.

Performing actions with the "find" command:

  • So far we have only printed the paths of the files found, however it is possible to perform actions on these files.
  • For example, to delete all the .mp4 files on your system which are larger than 1GB you would use:

find . -name "*.mp4" -size +1000M -print -exec rm -f {} \;

-- The -name option tells find that we are only interested in files with the .mp4 extension.

-- The -size options means find will only match files over 1GB and

-- the -exec rm -f {} \; part tells find to run (execute) the rm command on any files find.

-- Note that the -f part of the rm command will mean you won’t be prompted before the file is deleted.

There are lots of tricks which can be performed with find; here is a short list of useful command combinations:

Find all directories with 777 permissions and change the permissions to 755:

*find/-type d -perm777-print-execchmod755{} \;

Find all the empty files in the /tmp directory and delete them:

find/tmp -type f -empty-print-execrm-f{} \;

Find all the files which have been accessed in the last 50 days:

find/-atime50

Find all the files which are larger than 50MB but smaller than 100MB:

find/-size +50M -size-100M