Linux find file by name recursive

How to Search and Find Files Recursively in Linux?

When you search for a file recursively, the search begins in the current directory and then moves on to check all the subdirectories down through the directory tree until there is no sub-directory left.

The “recursive search and find” technique is helpful when you have many files and directories on your system and want to locate a specific file quickly.

This post will list and elaborate on all the methods to search and find files in Linux with the following outline:

How to Search and Find Files Recursively Using the find Command in Linux?

The ‘find’ command is one of the most straightforward command line tools to look for files recursively. Its basic syntax is explained below:

Here, the path is the directory where you want to search, whereas the -name specifies the pattern to search for.

Search for a File Recursively

Below is an example of the find command to search “file1” from the current directory (Home) and its sub-directories.

The above command, when executed, found three instances of “file1” that were being searched using the ‘find’ command.

If you want to do the above in a specified directory, use the ‘cd’ command below to navigate and search the same as above.

When the ‘cd’ command is executed on the provided folder, you will notice the same directory added next to the hostname, which is “Music,” in our case.

Search and Find Files Recursively Based on Extension

To search and find the files recursively based on their extension, use this format of the ‘find’ command.

In the output above, the paths and names of the files with the “.txt” extension are printed.

Search and Find Files Recursively Based on Extension and Size

If the files need to be found based on their size, use this format of the ‘find’ command.

$ find ~/ -name "*.txt" -and -size +10k

This will recursively look for files with the .txt extension larger than 10KB and print the names of the files you want to be searched in the current directory. The file size can be specified in Megabytes (M) or Gigabytes (G). Use this command to find only the files above 5MB free of extensions or names.

The above image shows that all files of the current directory with more than 5MB are printed with their paths.

How to Find and Search Files Using the tree Command in Linux?

Unlike the find command, the ‘tree’ command displays the path, total number of files, and directories where the searched file is in a tree form. It is not pre-installed but can be installed using either of these commands based on your distro.

$ sudo yum install tree #For CentOS/Fedora $ sudo sudo apt install tree #For Debian/Ubuntu-based

Here are examples of using the ‘tree’ command to search and find recursively in Linux by following the below syntax:

Читайте также:  What is linux mint based on

In the above command, the ‘path’ represents the directory or folder. At the same time, the ‘-P’ specifies the pattern to search for.

Use tree Command to Find a File Based on Extension

Let’s recursively use this command to find the files with the “.txt” extension.

Use tree Command to Exclude Specific Pattern

You can use the ‘-I’ option to exclude specific patterns from the search:

In the above image, the “.” is used to view the content, where the files with the “.txt” extension will be searched in the Music directory.

Use the tree Command to Copy the Output

If you want to copy the output of the tree command (we used the above example) to an output file, use this command.

To view the output, use any text editor (we’re using nano editor).

Display Full Path Using the tree Command

The tree command with the flag “-f” displays the full path of each file to be printed, which is used in this format.

The above image shows the full paths where the searched file is located, making it easier for the user to find it, and more information can be read using this help command for the tree command.

Conclusion

Finding files from the directories take time unless you know where your file is. If there are many files and directories, then the find and tree commands in Linux allow you to have a recursive search. This post has briefly explained the working/usage of the find and tree commands to search for files recursively in Linux.

Источник

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.

Читайте также:  Linux mint обновление системы через терминал

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:

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:

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.

Читайте также:  Simply linux live cd

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 Search for Files Recursively into Subdirectories

I am trying to look for all XML files in a particular directory and all sub-directories (recursively) inside it. ls -R *.xml is only listing files in the current directory. I am quite sure, the sub-folders themselves have several .xml files, but none are showing up. Is this a configuration issue?

5 Answers 5

. is the current directory. If you need to search in another directory, replace . with the directory path.

Does it search for the required file recursively in the directory rooted at current directory. In my case it just checked in the current directory only, didn’t check the subdirectory.

Actually I searched for .php files in current directory. But it returned only .php files in current directory, didn’t searched recursively in sub-directories. That’s why I’m asking whether find command searches recursively or not.

@mostafiz, the find command searches recursively. If you don’t quote the parameter, I think your shell might do an expansion on the * , so it will match the files in the current directory.

sudo find . -print | grep -i '.*[.]xml' 

-1 for mixing find and grep , when find can do filtering using both regexes and globs, and not using find ‘s -print0 and grep’s -z when you do need to mix.

ls doesn’t have options to filter the output. For that you would need to use pipe. This passes the output from ls to grep , which then filters them to show just the .xml files.

bash

Using globstar shell option, we can make use of recursive globbing ./**/*

bash-4.3$ shopt -s globstar bash-4.3$ for i in ./**/*.xml; do printf "%s\n" "$i" ; done ./adwaita-timed.xml ./bin/hw5/stuff/book/chapter42servletexample/build/web/META-INF/context.xml ./bin/hw5/stuff/book/chapter42servletexample/build/web/WEB-INF/beans.xml ./bin/hw5/stuff/book/chapter42servletexample/build/web/WEB-INF/web.xml 

Perl

Perl has a module Find , which allows for recursive directory tree traversal. Within the special find() function, we can define a wanted subroutine and the directory that we want to traverse, in this example that’s . . The one-liner in such case would be:

bash-4.3$ perl -le 'use File::Find; find(sub,".")' ./adwaita-timed.xml ./CLEAR_DESKTOP/blahblah/hw5/stuff/book/jsf2demo/build/web/WEB-INF/beans.xml ./CLEAR_DESKTOP/blahblah/hw5/stuff/book/jsf2demo/build/web/WEB-INF/web.xml ./CLEAR_DESKTOP/blahblah/hw5/stuff/book/liangweb/build.xml 

Python

While Perl has a whole module dedicated to recursive tree traversal, Python has a neat function walk() that is part of os module, and repeatedly returns tuple of topmost path, list of all subdirectories, and list of filenames. We can do the following:

bash-4.3$ python -c 'import os,sys; [ sys.stdout.write(os.path.join(r,i)+"\n") for r,s,f in os.walk(".") for i in f if i.endswith(".xml") ]' ./adwaita-timed.xml ./CLEAR_DESKTOP/blahblah/hw5/stuff/book/jsf2demo/build/web/WEB-INF/beans.xml ./CLEAR_DESKTOP/blahblah/hw5/stuff/book/jsf2demo/build/web/WEB-INF/web.xml ./CLEAR_DESKTOP/blahblah/hw5/stuff/book/liangweb/build.xml 

This might be far neater as a script:

#!/usr/bin/env python import os,sys for r,s,f in os.walk("."): for i in f: if i.endswith(".xml") print(os.path.join(r,i)) 

find

Other answers have mentioned find for recursive traversal, and that’s the go-to tool for the job. What does need mention is the fact that find has multiple command line switches, such as -printf to print output in desired format, -type f to find only regular files, -inum to search by inode number, -mtime to search by modification date, -exec <> \; to execute a particular command to process the file with passing file as argument ( where <> is standard find placeholder for current file) , and many others so please read the manpage for find .

Источник

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