Linux find more than one file

How to Search and Find Files Recursively in Linux

This brief tutorial explains how to search and find the files recursively in the Linux operating systems.

After reading this article, you will be able to find any file recursively using the different techniques including a single file search, multiple files search, find files by permissions, and more. This document is optimized for both new and experienced Linux users. All methods are valid for every Linux distribution.

All examples in this tutorial contain screenshots to make it easy for any Linux user to understand and reproduce them.

Finding Files Recursively in Linux

The find command does not need flags to search the files recursively in the current directory. You only need to define the main directory and the file name using the –name option. This command will search the file within the main directory and all subdirectories.

The syntax is simple, as shown in the following:

If you want to find the 27.jpg file within your home directory and subdirectories, run the following command as shown in the following screenshot:

As you can see, the file was found in the /home/linuxhint/Downloads/recur subdirectory.

An alternative to the previous command is the tree command shown in the following example where you search the same file named 27.jpg within the current directory:

As you can see in the previous figure, the format is pretty different. It seems to be more user friendly or nicer as long as you don’t need to copy the full path to paste it.

The following screenshot shows how to use the find command to recursively search more than a file.

The syntax to search multiple files recursively is the following:

Note that there is a –o flag before the second file name. You can add more than one file by adding more –oname flags. For example, to find 4 files with the same command, use the following syntax:

Читайте также:  Python2 pip arch linux

In the practical example described in the following image, use this command to find a file named 27.jpg and a file whose name begins with “DIAGRAM” but without specifying its extension. Instead, use a wildcard (*) to find any file named DIAGRAM independently of its type.

As you can see in the previous image, both files were found recursively.

The next example describes how to find the files by extension using the find command. In the following figure, you can see how to recursively find all the .jpg files using the wildcard again. The syntax is pretty simple:

Thus, to find all the .jpg files recursively, run the following command:

As shown in the previous image, all the jpg files including their path are listed successfully. You can replace the .jpg extension for any extension that you want to search like .png, .txt, .c and more.

Now, let’s assume that you don’t want to find a file but a directory recursively. All you need to do is to use the same command that was shown in the first example of this tutorial then add the -type d option. The syntax as follows:

In the following practical example, use the previous syntax to find the recur directory.

As you see in the previous figure, the directory named “recur” was found successfully.

You also can find the files by size using the following syntax where is the main directory containing the subdirectories and the is the size of the files that you can list with their full path.

The following example describes how to find the 10 MB size files. You can replace the M defining units in MB with c for bytes, w for two two byte words, k for kibytes and G for gibibytes (note units are case sensitive).

To find the 10 mebibytes files, execute the following command:

Читайте также:  Python windows linux directory

All 10M files were properly listed with their paths.

The syntax to find the files based on their permissions is shown in the following:

Let’s assume that you want to identify and list the files with read, write, and executing permissions (777). The command to run is the following:

The last example of this tutorial shows how to find and list the files and directories by size.

As shown, the files are listed by size with proper units. The 0 size directories and files are empty.

Conclusion

Linux versatility and flexibility allows to find the files (and other functions) recursively in many ways. They can easily be executed by all the Linux users independently of his knowledge level, from the new users to the system administrators. All techniques previously described are valid for all the Linux distributions and even to some Unix systems. According to their man pages, some flags may vary in some distributions, but most of them are universal. In case your Linux distribution does not match any of the previously explained commands, you can read the man page. It is highly recommended to the readers to practice the examples to incorporate this knowledge.

Thank you very much for reading this Linux tutorial. Keep following us for more Linux professional tips.

About the author

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.

Источник

How to Use ‘find’ Command to Search for Multiple Filenames (Extensions) in Linux

Many times, we are locked in a situation where we have to search for multiple files with different extensions, this has probably happened to several Linux users especially from within the terminal.

There are several Linux utilities that we can use to locate or find files on the file system, but finding multiple filenames or files with different extensions can sometimes prove tricky and requires specific commands.

Find Multiple File Names in Linux

One of the many utilities for locating files on a Linux file system is the find utility and in this how-to guide, we shall walk through a few examples of using find to help us locate multiple filenames at once.

Читайте также:  Linux centos debian ubuntu opensuse

Before we dive into the actual commands, let us look at a brief introduction to the Linux find utility.

The simplest and general syntax of the find utility is as follows:

# find directory options [ expression ]

Let us proceed to look at some examples of find command in Linux.

1. Assuming that you want to find all files in the current directory with .sh and .txt file extensions, you can do this by running the command below:

# find . -type f \( -name "*.sh" -o -name "*.txt" \)

Find .sh and .txt Extension Files in Linux

Interpretation of the command above:

  1. . means the current directory
  2. -type option is used to specify file type and here, we are searching for regular files as represented by f
  3. -name option is used to specify a search pattern in this case, the file extensions
  4. -o means “OR”

It is recommended that you enclose the file extensions in a bracket, and also use the \ ( back slash) escape character as in the command.

2. To find three filenames with .sh , .txt and .c extensions, issues the command below:

# find . -type f \( -name "*.sh" -o -name "*.txt" -o -name "*.c" \)

Find Multiple File Extensions in Linux

3. Here is another example where we search for files with .png , .jpg , .deb and .pdf extensions:

# find /home/aaronkilik/Documents/ -type f \( -name "*.png" -o -name "*.jpg" -o -name "*.deb" -o -name ".pdf" \)

Find More than 3 File Extensions in Linux

When you critically observe all the commands above, the little trick is using the -o option in the find command, it enables you to add more filenames to the search array, and also knowing the filenames or file extensions you are searching for.

Conclusion

In this guide, we covered a simple yet helpful find utility trick to enable us find multiple filenames by issuing a single command. To understand and use find for many other vital command line operations, you can read our article below.

Источник

Оцените статью
Adblock
detector