25 useful find command examples for Linux beginners

Linux-find-command-with-examples

Find command is one of the most useful & important command used in Linux. Its available & installed by default on almost all the versions of Linux. Everything on Linux is in the form of files, & we should be able to locate a file when required.

With the use of find command, we can look for the files that are needed based on a number of search criteria, we can use single or combine a number of criteria & then we can perform actions on the result obtained. In this tutorial, we are going to discuss find command with the help of some examples,

1) Lists all the files in current directory & its sub-directories

To list all the files in current directory & the sub-directories, we can use

$ find

Alternatively, we can also use ‘find . ’ which will also provide the same result as above.

2) Find all the files or directories of your present working directory

To look only for directories, we can use

$ find . -type d

To search all the files only & not directories, use

$ find . -type f

3) Lists all the files of a specific directory

To find all the files in a particular directory, we can use

$ find /root

This command will look for all the files in /root directory.

4) Find a file with name in a directory

To look for a file by its name in a particular directory, command is

$ find /root -name "linuxtechi.txt"

This will look for linuxtechi.txt file in /root folder. We can also look for all the files with .txt extension,

$ find /root -name "*.txt"

5) Find a file in multiple directories

To find a file by its name in multiple directories, we can use

$ find /root /etc -name "linuxtechi.txt"

With this command, we can look for linuxtechi.txt file in /root & /etc directories.

6) Find a file with name ignoring case

To look for file with its name irrespective of the case i.e. whether its upper case or lower case, we can use ‘-iname‘ option in find command

$ find /root -iname "Linuxtechi.txt"

The result of the command will provide all the files that are named linuxtechi.txt, whether its in lower case or upper case or in mixed cases.

7) Find all file types other than the mentioned type

Let’s suppose we want to find all the files that are not the mentioned type, to achieve this we can use,

$ find /root -not -name "*.txt"

8) Find files with multiple conditions

We can also combine more than one condition to search the files , Let’s suppose we want to search files of ‘.txt’ and ‘.html’ extensions

$ find . -regex ".*\.\(txt\|html\)$"

9) Find files with using OR condition

We can also combine multiple search criteria & then look for the files based on the fulfillment of any of the one condition using OR operator,

$ find -name "*.txt" -o -name "linuxtechi*"

10) Find all the files based on their permissions

To look for files based on the permissions, use -perm option in find command

$ find /root -type f -perm 0777

11) Find all the hidden files

To search for all the hidden files in a directory, command is

$ find  ~ -type f name ".*"

12) Find all the files with SGID

To locate all the files with SGID bits, we can use

$ find . -perm /g=s

13) Find all the files with SUID

To locate all the files with SUID bits, we will use

$ find . -perm /u=s

14) Find all executable files

To only look for the files that are executable, command is

$ find . -perm /a=x

15) Find all the read-only files

We can also look for only read-only files using find command,

$ find /root -perm /u=r

16) Find all the files owned by a user

To locate all the file that are owned by a particular user, for example linuxtechi, we will use the following  command,

$ find . -user linuxtechi

17) Find all the files owned by a group

To locate all the files that are owned by a particular group, use

$ find . -group apache

18) Find files of particular size

If we want want to search a file for which we know the exact size, then we can use ‘-size‘ option with find command to locate the file

$ find / -size -2M

19) Find all the files of size range

If we are looking for a file for which we don’t know the actual size but know a range of size or just want to locate all the files within a size range, then we can also locate the file using that criteria

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

We can also use find command to locate all the files whose size is greater than 50 MB

$ find / -size +50M

20) Find files that are modified N days ago

For example, we want to locate all the files that have been modified 8 days ago. We can accomplish that using ‘-mtime‘ option in find command

$ find / -mtime 8

21) Find files that have been accessed N days ago

Similarly like above example, we can also locate files that have been accessed 8 days ago using ‘-atime’,

$ find / -atime 8

22) Find all the empty files or directories

To locate all the empty files on the system, we will use beneath command

$ find / -type f -empty

Similarly, to locate all the empty directories

$ find ~/ -type d -empty

23) Find largest and smallest files

To list largest or smallest file, we will combine ‘sort‘ command with find command & if we further want to list top three of those largest files, we will combine ‘head‘ command.

To list top three files in the current directory, command is

$ find . -type f -exec ls -s {} \; | sort -n -r | head -3

We can similarly find the smallest files in the current directory,

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

24) Find all the files with specific permissions & change them to 644 (or other permissions)

With find command, we can also achieve some advanced functionalities. For example, we can list all the files that have permission 644 and then change those permissions to 777. To do this, run

$ find / -type f -perm 644 -print -exec chmod 777 {} \;

25) Find all the files matching a criteria & delete them

We might be required to locate & delete files matching a criteria. To do this with find command, run

$ find / -type f -name 'linuxtechi.*' -exec rm -f {} \;

These were some simple examples demonstrating the functionality of find command & it can be used to perform tedious, repetitive search/locate task more easy.

4 Responses

  1. samspade says:

    Item 8
    We can also combine more than one condition to search the files we need,
    $ find ./root -name “*.txt” | -name “linuxtechi*”
    That pipe looks like a mistake.

  2. Suraj Padmasali says:

    Thank you for making an awesome list of commands. It’s very useful but I don’t find anything about grep command? It’s amazingly powerful and helpful command in Linux to access file contents.

  3. Sir, how to install Nero application in my laptop. I am using Linux ubuntu 16.4 OS at dell inspiron 15 5000 core i5 7th gen. I am confused. So clear and send practice video and pdf

Leave a Reply

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

Pin It on Pinterest