Linux find command examples step by step

Linux find command examples step by step
Linux find command examples step by step

Linux find command examples step by step introduction

The tutorial Linux find command examples covers how to use find command tool to search files and directories in Linux and UNIX system.
Find command is useful for both simple Linux users and Sysadmins. Find command is used in UNIX and Linux like OS and many other operating system as an utility to find files and directories. It has many features, you can use various flags with find. The tutorial will guide you through how to use and learn the find command in depth.
There are a number of filters that are used with find, using these filters will make your search more fast and easy. Some of the most used filters are.


f = file
d = directory
l = symbolic link
s = socket

It's general syntax is 

# find -type  <filter name> 

You search criteria with the command can be based on file name, file type, file size, group, user, permission. We will cover the whole topic step by step.

As Linux find command is used to search any file or directory based on keyword, you must also check out Sed command in UNIX tutorial which will teach you how to search and replace any string within a file.


Find files or folders in the current directory

Use find command in Linux to search for files and directories present in the current directory.

$ find file1
file1


Find multiple files with same names

To find files having similar names at the starting, use the below command.

$ find file*
file1
file2
file3


Linux find command to Search directory and view sub-directory

Below is the command which will look for the directory '/dev' inside the root. It will also give the view of the files and sub-directories present inside the directory.

$ find /dev/
/dev/
/dev/rfkill
/dev/vcsa6
/dev/vcs6
/dev/vcsa5
/dev/vcs5


Find files in your current directory with 'name' option

With the option 'name' you search criteria will be based on name of files and directory. For example.

# find . -name file2
./TEST/file2
./file2

However, you can remove the option dot (.) current directory symbol, if you are already in the currenty directory. The output will be the same.

# find -name file2
./TEST/file2
./file

The below Linux find command examples will find the folder inside /etc directory.

# find /etc -name fuse.conf
/etc/fuse.conf

Ignore the case sensitive with 'iname' option with find command.

# find /etc -iname FUSE.CONF
/etc/fuse.conf


Find files and directory based on type

Find only files using 'type f'

# find -type f -name libuser.conf
./libuser.conf


Find only directoris with 'type d'

To find only directories not files, use the option 'type d'. Please see the below Linux find command examples.

# find -type d -name var
./run/initramfs/state/var
./var
./var/var
./var/named/chroot/var


Find files having similar extension

Find files having different names but similar extensions. Given below are the two Linux find command examples.

# find -type f -name '*.ini'
./gtk-3.0/settings.ini
./odbcinst.ini
./php.ini
./php.d/pdo_sqlite.ini
./php.d/sqlite3.ini

Another example of having extension '.txt'

# find -type f -name '*.txt'
./url.txt
./directory1/file2.txt
./directory1/file3.txt


Using  Not and OR operator with find

You can use 'Not' and 'OR' operation with find to satisfy specific search criteria. The below find command Linux example will illustrate the same.

To find files which begins with 'bzm' but not having '.txt' extension, use the '!' (Not) option with find. See the below given example.

# find / -name 'bzm*' !  -name  '*.txt'

Similarly, with below find command you can find all files having '.txt' or '.py' extensions. The option 'o'  (OR) operator is used here.

# find / -name '*.txt' -o -name '*.py'


Find files based on size

Find files based on size. The below Linux find command will find files having size more than 600M inside root directory

# find / -size +600M
/var/spool/abrt/ccpp-2016-12-05-12:09:18-3249/coredump

Find files having size less than 200k. Look at the below example.

# find /etc -size -200k
/etc/pbm2ppa.conf
/etc/vconsole.conf
/etc/exports.d

Find files having size between 70M to 90M. For example.

# find / -size +70M -size -90M

Also you can find files with its size. See the below example.

%s = Print size
%f = Print filename
%h = Print directory name
# find / -size +600M -printf "%h/%f,%s\n"
/var/spool/abrt/ccpp-2016-12-05-12:09:18-3249/coredump,818896896


Find top big and small files

With this command we can search for the top small and big files in Linux. Lets see the below Linux find command examples.

To find top 3 big files in your current directory.

# find . -type f -printf "%s:%p\n" | sort -r -n | head -3

or

# find . -type f -exec ls -s {} \; | sort -k1rn | head -3

To find top 2 small files, you can use.

# find . -type f -printf "%s:%p\n" | sort -n | head -3


Find files which were accessed 10 days before

Now to find files which were accessed 10 days before.

# find / -atime 10

Similarly, to find files in the root which were accessed 30 days before.

# find / -atime 30


Find files which were accessed last 3 hours

To find files in the current directory which were accessed last 3 hours, we will use the given Linux find command in the following way.

# find . -cmin -180 

Find files which were accessed last 30 mins

# find . -cmin -30


Find files which were modified

Find files in the current directory,which were modified 10 days before. use it as given below. If your search includes inside root, You may replace current directory with root directory.

# find . -mtime 10

Find files which were modified 30 mins before

# find . -cmin -30


Find files which were accessed or modified between given days

Find files accessed last between 10 to 20 days

# find / -atime +10 -atime -20

Find files modified last between 20 to 30 days

# find / -mtime +20 -mtime -30


Find files having specific group

You can find all files which belong to a particular group. For example to find files which belong to group 'zimbra'.

# find -group zimbra
./var/log/zimbra.log
./var/log/zimbra-stats.log
./var/spamassassin
./var/db/sudo/zimbra

Similarly, you can find all files belonging to a specific group made by you. For example, in the below example we will find files belongs to a group 'john'.

# find -group john
./run/gdm/auth-for-john-wrmhiZ
./run/gdm/auth-for-john-wrmhiZ/database
./run/console/john
./run/console/console.lock

 


Find files having a particular user

To find files belongs to a particular user, use the option '-user'. We have created a user 'test'. Below is the Linux find command examples which will search for the files in root directory having user 'test'.

# find / -user test
/var/spool/mail/test
/home/test
/home/test/Public/file1.txt
/home/test/Pictures/3842591.png

Now to ignore the file search belongs to a group use '!'. For example to negate the files having user 'test' we will use '!'. I have used backslash here to escape the negation.

# find / \! -user test


Find files based on permission

Here we are going to see how to find files based on their permissions. 'perm' option is used for the purpose. I have created a file called
'permfile.txt' under /home directory and given the permission '444'. To find the file based on permission, use the command as given.

$ find /home -type f -perm 222
./permfile.txt

In the same way we can nagate file having permission 222. See the example.

$ find /home -type f ! -perm 222


Find files having write, executable and read only permission

# find . -perm /u=w    { Files having write permission }
# find . -perm /u=x    { Files having execute permission }
# find . -perm /u=r    { Files having read permission }


Find SGID files

SGID stands for SET GROUP ID. It sets group id permission to users on command or program executions. It is a special type of file permission.

To find SGID files having permission 740, we will use find command in the following way.

# find . -perm 2740

Here SGID=2


Find SUID files

SUID stands for SET USER ID. It is a special type of file permission. This permission is given to user temporarily for executing any program or command.

So, to find all SUID files in your current directory use the below command.

# find . -perm /u=s


Find sticky bit files and folders

Sticky bit mainly used on folders to avoid deletion from other users other than root and owner of the folder.

Sticky bit files are denoted as numerical '1'. To find sticky bit files of permission 740, we use the find command as given below.

# find . -perm 1740


Find files based on inode number

Each file has different inode number. For example two files with same name created in two other directories will have different inode numbers.

# ls -il file1*
790150 -rw-r--r-- 1 root root 0 Dec 18 23:15 file1.txt
2512 -rw-r--r-- 1 root root 0 Dec 18 23:15 file1.txt

Now to find file with inode number '2512', use the command as given.

# find -inum 2512


Find hidden files

The below find command will find all hidden files in linux. For example to find all hidden files in root directory is.

# find / -type f -name ".*"


Find empty files

You can also find empty files in Linux. For example.

To find empty files in current directory.

# find . -type f -empty

Or to find empty files in root directory.

# find / -type f -empty

To find empty files owned by a particular user, use the below find command example in the following way. The below example will find empty files in root directory owned by user 'test'

# find / -type f -user test -empty

Find empty files and delete

Use the given command to find and delete empty files.

# find / -type f -empty -delete

Find empty files owned by a user and delete

# find . -type f -user test -empty -delete


Find directories which are empty

The following Linux find command will find only directories (not files) which are empty.

# find / -type d -empty


How to avoid error output while searching

Find command search for files in all of its directory and sub-directories. But if you don't have the permission, your output will generate permission error as given below.

$ find / -name "*.py"

find:  /etc/ root: Permission denied

You will have a permission denied error.

To get an output without any error messages on the terminal, send this error messages to a null file as given below.

$ find / -name "*.py" 2>/dev/null

 

Find text within multiple files

How about finding texts within multiple files. You can do that with the combination of 'find' and 'exec'. In the below example we are searching for the string in all the '.txt' files under root directory.

# find / -type f -name "*.txt" -exec grep 'Linux is wonderful' {} \;
find: ‘/run/user/1000/gvfs’: Permission denied
Linux is wonderful
Linux is wonderful

In the above output you can see the 'permission denied' error also printed along with the output, you may choose to skip it with 'xdev' option.

# find / -xdev -type f -name "*.txt" -exec grep 'Linux is wonderful'  {} \;
Linux is wonderful
Linux is wonderful

But, with 'xdev' it will take a bit longer to process the result. In that case to be a bit faster use 'maxdepth' with the Linux find command.

# find / -maxdepth 1 -type f -name "*.txt" -exec grep 'Linux is wonderful' {} \;

To see the path of the files where where the text is found, add option 'l' with grep as giben below.

# find / -maxdepth 1 -type f -name "*.txt" -exec grep -l 'Linux is wonderful'  {} \;
/file2.txt
/file1.txt


Man find

To know more about options you can use with Linux find command, go to man page.

# man find

Or look at the below links.

http://man7.org
https://en.wikipedia.org


This is it with article Linux find command examples step by step. If you like this article, don't forget to comment and share.

Download Our Free eBook now

Linux and UNIX Shell scripting ebook

Be the first to comment

Leave a Reply

Your email address will not be published.


*


shares