Using the find command in Linux

Files and folders can be found in many different ways regardless of operating systems. In Linux, it equips the find command to do search for files. Many users use the find command with just the basic parameters. They get the results that they are looking for. Unfortunately, most of the users don’t spend much their time to learn all about the find command in a professional way. I, therefore, want to stick here a few ways to use this excellent tool that make you surprised.

Table of Contents

Basic usage

Let’s give an example. We want to find all the file which the names contain the word ‘Klamt’ in the current directory, the syntax of find command looks like:

find . -name '*Klamt*'

in which:

  • Dot (.) indicates the current directory
  • -name: given search text is the filename rather than any other attribute of a file
  • ‘*Klamt*’: search text that we have entered. Always enclose the filename in single quotes. Using wildcards to search all possibilities containing the keyword ‘Klamt’.
find ~/Downloads -iname '*poster*'

in which:

  • ~/Downloads: the directory where the files are potentially located.
  • -iname: looking for files based their file name without regarding letter case.

Advanced usages

find /Documents -name '*.pdf' -size -5000k
find / -size +10000k

The 1st command would find within a directory called /Documents, only those pdf files that have a size less than 5000 Kilobytes (< 5MB). The 2nd command would search from the / directory for any file that is larger than 10000k (> 10MB). We could use M for Megabyte instead of k short for Kilobyte.

The best part is that find supports boolean operators to make the search even stronger. For this example, I will use a command that combines my knowledge that the file is less than 5 megabytes in size, and also more than 2. The command is the following.

find / -size -5M -and -size +2M

We also tell the find command to seek the files which have been accessed or modified for minutes, hours.

$ find /home/david -amin -10 -name '*.c'
$ find /home/david -atime -2 -name '*.c'
$ find /home/david -mmin -10 -name '*.c'
$ find /home/david -mtime -2 -name '*.c'

The first command searches for those files that are present in the directory /home/david and its subdirectories which end in .c and which have been accessed in the last 10 minutes. The second command does the same but searches for those files that have been accessed in the last 10 hours. The third and the fourth commands do the same as the first and second commands but they search for modified files rather than accessed files. Only if the contents of the files have been modified, their names would be returned in the search results.

 

References

  1. http://www.codecoffee.com/tipsforlinux/articles/21.html
  2. http://hacklog.in/find-explained-through-a-screencast/
  3. http://about_linux_command_find.onlinephpfunctions.com/
  4. https://www.linode.com/docs/tools-reference/tools/find-files-in-linux-using-the-command-line/

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.