The Linux find Directory Command: Explained

You may need to find a directory in your Linux based operating system, and you are suffering because you do not know the actual command to find the location of different files present on your Linux based operating system. The Linux find directory command is used for this purpose.

The Find command is used to search for the location of files both in Unix or Linux based systems. The find command is included in the “findutils” package, and it is present in all Linux distributions. You can look for directories and files while applying numerous conditions. The action will be taken according to your applied conditions which will affect the result.

Introduction to the Linux find directory command

The simplest form of the find command is given by:

find [paths] [expression] [actions]

The other forms of the find command are given as when find command is executed, it looks for directories and files in every path. Once it finds a directory in the provided path, then it starts to look for other files and directories in that same path. This process doesn’t stop until you have looked for all the files in your stated path.

find /where/to/look/up criteria action
find /dir/path/look/up criteria action
find /dir/path/look/up -name "name-of-dir"
find /dir/path/look/up -name "pattern"
find /dir/path/look/up -name "name-of-dir" -print
find /dir/path/look/up -name "name-of-dir"
find / -name "name-of-dir"
find / -type d -name "name-of-dir"
find / -type d -name "name-of-dir" 2>/dev/null

Searching all directories and files

If you want the list of all the files and directories from your specified path, then you can do it by specifying the path. For example, if you want a list of all the files from usr/share path, then you can use the following command:

find /usr/share

linux find command all directories

If you want to list files from more then one directories at once then you can use the following command: Once you run this command, a list of all directories and files would get listed on the console from your specified path. The list can be very lengthy based upon your stored content.

find /bin /usr/li

linux find command many directories

Suppose you want a list of content present in your current working directly. It can be quickly done by putting a period (.) after the find command, just like this:

find .

linux find command working directory

If somehow you forget to put the period after the find command, then still data from the current working directory will be shown by running the find command only.

find

linux find command all directory

Filtering items with their names

Many times, there is a need to filter different items with the help of their names. Linux find directory command can also help you to do so.

Suppose you want to search for a file named as NEW.txt which is located inside the /Desktop directory then you can use “-name” switch as shown below:

find Desktop -name NEW.txt

linux find command by name

Utilizing the “-name” switch is more of case sensitive. To use a case insensitive command you can use “-iname” in place of “-name”

find Desktop -iname new.txt

linux find command by name insensitive

Both of these switches support “wildcards” which are used as placeholders for unknown characters. The “* “wildcard is used to show any number of unknown characters whereas “?” is used to a single unknown character.

To use these wildcards, you must put the parameter of “name / iname” command inside double or single quotation marks.

Suppose if you want to search for all the files in /Desktop directory that include .txt at their last. Then it can be done by the following Linux find directory command:

find Desktop -name '*.txt'

linux find command by extension

Now if you want to search for the files that include three characters in their name, then you can use “?” wildcard enclosed in quotation marks.

find Desktop -name '???'

linux find command by char count

Specifying the search for either files or directories

When using Linux to find directory command, you can also specify the type of your search. You can use “-type” switch for specifying that either you are searching for just files or directories. There are three parameters of “-type” switch in total:

  • f – files
  • d – directories
  • l – symbolic links

If you want to find all the directories present in the /usr path then you can use:

find /usr -type d

linux find command by directory

There is another way to use switches by combining them. For example, to search for “.txt” with -name switch and at the same time specifying the type of file by “-type” switch:

find /usr -type f -name '*.txt'

linux find command by file

Same as the above example, you can also find directories by using the “d” parameter in the /usr path. For finding “cherrytree” named directories:

find /usr -type d -name 'cherrytree'

linux find command by name

Empty switch for finding empty files

You can use the “-empty” switch within the Linux within the Linux find directory command. This switch is used to find any empty files or any empty directories present in your specified path. For finding any empty files in /usr directory, you can apply the following command with “-type” switch:

find /usr -type d -empty

linux find command empty directory

Negating matches in find commands

If you want to exclude something from the search, you can use find command to negate the condition.

Suppose you want want to search for the files without “. Py” extension in the /usr directory. You can include an exclamation mark (!) with the “-name” switch, as shown below:

find /usr -type f ! -name '*.py'

linux find command with name and extension

The exclamation mark can also be utilized to oppose any other switch. For example, in the case of an “-empty” switch, you can use “!” to search for non-empty directories in place of empty directories.

find /usr -type f ! -empty

linux find command with type

Finding files based on their ownership

There is an option for finding files based on the ownership of the user. You can use the “-user” switch for searching the files related to a particular user. If you want to search for the files that are related to a user named “root” then you can apply the following command to get a list of all their files.

find / -type f -user root

linux find command by user

Instead of username, you can also use user-ID to check for all the files belonging to that user. If you do not know your user ID, then you can find it by using the following command.

id -u <username>

linux find command by user id

You can also search group-wise with the help of group name or group-ID. For finding your group ID, you can use the following command.

id -g <username>

linux find command by group id

Searching files based on their sizes

Linux find directory command also allows you to search files based on their size. For this purpose “-size” switch is used, and it applies only on files, not on directories.

You can specify the file size by putting a number that is followed by an alphabet. These letters are used with the “-size” switch,

  • c- bytes
  • k – kilobytes
  • m – megabytes
  • g – gigabytes

You can also apply a less than or greater than a condition with the help of “+” and “-” signs.

Here is an example to find all system files with 20KB size:

find / -size 20k

linux find command by kb

If you want to search for files that are greater than 1 GB, you can use:

find / -size +1G

linux find command by gb

In case you want files with sizes smaller than 20MB, then you can use the following statement:

find / -size -20M

linux find command by mb

Taking actions with find directory command

The commands we used until now are only to show the results on the terminal, but there are some actions we can perform with the help of find command. We can also perform necessary actions like copying or deleting a file.

For deleting files

You can easily delete any file or directory by simple using “-delete” with your find command. Suppose you want to delete all the empty files present in /Desktop directory then you can use the following command:

find Desktop -type f -empty -delete

linux find command delete if empty

Commands execution

The “-exec” switch is used to execute a custom command. You can do a lot with the help of the “-exec” switch.

For copying all files at once, you can use:

find ~ -type f -name '*.mp3' -exec cp {} ';'

linux find command copy and delete

In the command above, we use “;” to show the ending of the command which is enclosed in quotation marks. The “{}” (brackets) you see are used to define the location.

Now when there is a matching file detected according to the specified, cp command is activated to move the files from the location.

Conclusion

You can do a lot more with the help of the find command. It is handy for those who are used to Linux CLI (command-line-interface). The find command comes packed with many possibilities that will help you to perform different actions and do a lot more. There are also tutorials on the Linux move and copy commands, have a look at them.

Leave a Comment

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

Share via
Copy link
Powered by Social Snap