Linux find all files with name recursively

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:

Читайте также:  Microsoft edge для linux

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.

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:

Читайте также:  Zabbix agent 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:

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.

Источник

Recursively find all files named «file.txt» and execute a sed command

The challenge of my task is that the file.txt can be in subfolder or in sub-sub folders. The general structure looks like this:

 folder |___subfolder |_____file.txt | |___subfolder |____subfolder |_______file.txt etc 

My previous code does not deal with the sub-sub folder and I am not sure how to add in this part (since some sub folder does not have any subsequent folders)

 for dir in ./*/ ; do if [ -d "$dir" ]; then cd $d; for subdir in ./*/; do cd $subdir && $(sed command file.txt) && cd ..; done cd .. fi done 
find . -name 'file.txt' | sed command file.txt 

3 Answers 3

find . -name 'file.txt' -exec sed command <> + 

This finds all files named file.txt that in subdirectories of . and runs sed command against those files.

Читайте также:  Как установить линукс mint

If you want sed to modify those files in place, then add the -i option.

Althought -exec . + is now required by POSIX (hat tip: jordanm), some people may be using old versions of BSD find that do not support + . If you have one of those, then use (hat tip: unxnut):

find . -name 'file.txt' -exec sed command <> \; 

More Secure Alternative

If rapid changes are made to the directory structure, -exec can be subject to a race-condition. For greater security, use -execdir (hat tip: unxnut):

find . -name 'file.txt' -execdir sed command <> + 

Note that, if you have the current directory in your PATH , then -execdir will refuse to run.

@unxnut What was the reason for replacing -exec sed command <> + with the much less efficient -exec sed command <> \; ?

I checked again and you are right. However, a better option for exec is execdir , preventing some race conditions.

I would suggest to use find -print0|xargs -0 sed ‘some sed stuff’ -i , as find -exec spawns one process per file, while xargs uses one sed for all files, which is much faster if you have a lot of files.

Unless you know what that sed command is doing you want to run one sed per input file.
You can do this with either a for loop (if your shell supports recursive globbing) e.g. zsh , ksh93 , yash , bash ( tcsh and fish as well, but the loop syntax is different there).

shopt -s globstar # bash #set -o globstar # ksh93 #set -o extended-glob # yash for f in **/file.txt; do [ -f "$f" ] && sed 'cmd' "$f"; done 

or (as pointed out by other people) with find :

find . -type f -name 'file.txt' -exec sed 'cmd' <> \; 

Now, one may wonder — why not «optimize» this and use find s -exec with + or a zsh -ism like:

Sure, these would be more efficient as they invoke sed with multiple file operands but that may well the problem:
for the sake of simplicity, imagine you’re trying to print only the 1st line of each file using the sed command 1q 1 (without editing the file in-place 2 , maybe to redirect the combined output to another file or just to inspect it) so you would run either

find . -name 'file.txt' -exec sed '1q' <> + 

however they will both fail to deliver because
if multiple file operands are specified, the named files shall be read in the order specified and the concatenation shall be edited
and as a result sed will only print the 1st line of input not the 1st line of each file 3 .
Now, gnu sed has the -s switch:

-s, --separate consider files as separate rather than as a single continuous long stream 

that might come in handy sometimes but in this particular case ( 1q ), it won’t help.

1: if you don’t like 1q try $d which should delete the last line from each file
2: you might get away with gnu sed sometimes as -i implies -s , but definitely not when one of the sed commands is q
3: you might argue that this is because q quits — but the same would happen if you used sed -n ‘1p’ — I only used q to emphasize 2:

Источник

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