≡ Menu

Linux find command tutorial with examples

The Linux find command is one of the most useful commands in the arsenal of a system administrator. The find command is very versatile and it takes an innumerable amount of arguments that makes this Linux command very powerful.

The find command is available on any flavor of Unix based systems like Linux, CentOS, Debian, Ubuntu and even other operating systems like Mac OS X and FreeBSD.

In this tutorial, I illustrate several important uses of the find command with examples and explanations of the various arguments put together into one command.

Here are the top 10 find command examples:

1) To list all files of a directory recursively

cd /path/to/directory
find . -ls -type f

The above simple command will list all files in the directory and all its sub-directories.

2) To find files with a specific name

find . -name *.sql -type f

The above command will find all files with a sql extension. It will ignore directories because of the -type f argument.

3) Find strings in the contents of files

find . -type f -exec grep -il "php.ini" {} \;

This command needs explanation. In this command, the -type f argument will search only for files and not directories. The -exec argument will execute the grep command on each file found. The -i argument to grep instructs it to search in a case-insensitive manner. The -l command will print the file name instead of a snippet from the file. The {} argument is the file name that find finds. Finally, the command is terminated by an escaped semi-colon.

4) Find all files created by a user

find . -user ewhathow

The above command will find all files and directories created by the user ewhathow

5) Delete all files created by a user

find . -user ewhathow -delete

This command will delete all files created by the user ewhathow. Note that find is recursive, so it will delete all files in the current directory and all its sub-directories.

6) Follow symbolic links in the directories

find . -L -name *.sql

The above command will find all files with the sql extension and the -L argument instructs find to follow symbolic links to files and directories. The default find behavior ignores symbolic links.

7) Find files using regular expressions

find . -regex s*.sql

The above command will find all files with the first character s and the extension sql.

8) Find all directories in the current directory

find . -type d

This command will list all sub-directories of the current directory recursively.

The -type argument takes the following types:

b	     block (buffered) special
c	     character (unbuffered) special
d	     directory
p	     named pipe (FIFO)
f	     regular file
l	     symbolic link; this is never true if the -L option or the
s	     socket
D	     door (Solaris)

9) Find file with a specific permissions

find . -perm 644
OR
find . -perm -a=x

The first command will find all files with the 644 permissions (readable and writable by owner and only readable by anyone else). The second command will find all files that are executable by all. Note that in the first command format, find looks for an exact permission set, while in the second command, find finds all files which are executable by all ignoring the read and write permissions.

10) find very large files on Linux

find . -size +1024m

The above command will find all files that are greater than 1024 mega bytes (1 GB) on the file. The size can be specified in bytes (b), kilobytes (k), megabytes (M) and giga bytes (G).

For more options that can be passed to find, refer to its man page:

man find

There you go! This list of the top 10 find command examples are the most used commands by system administrators. I hope this was of use to you!

Comments on this entry are closed.