Thursday, 27 September 2018

25 useful find command examples for Linux beginners

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.

Find Command, Linux Command, Linux Tutorial and Material, LPI Certification

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 "lpicentral.txt"

This will look for lpicentral.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 "lpicentral.txt"

With this command, we can look for lpicentral.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 "lpicentral.txt"

The result of the command will provide all the files that are named lpicentral.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 "lpicentral*"

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 lpicentral, we will use the following  command,

$ find . -user lpicentral

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 'lpicentral.*' -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.

Related Posts

0 comments:

Post a Comment