+3 votes
318 views
How to find a file in Linux in all directories?

in Linux / Unix by (551k points)
recategorized by | 318 views

3 Answers

+4 votes
Best answer

Command Description
find . -name testfile.txt Find a file called testfile.txt in current and sub-directories.
find /home -name *.jpg Find all .jpg files in the /home and sub-directories.
find . -type f -empty Find an empty file within the current directory.
find /home -user exampleuser -mtime 7 -iname ".db" Find all .db files (ignoring text case) modified in the last 7 days by a user named exampleuser.

man ls:
-t     sort by modification time
-r,    reverse order while sorting (--reverse )
-1     list one file per line

find /wherever/your/files/hide -type f -exec ls -1rt "{}" +;

If command * exits with a failure status (ie Argument list too long), then you can iterate with find. Paraphrased from: The maximum length of arguments for a new process

  • find . -print0|xargs -0 command (optimizes speed, if find doesn't implement "-exec +" but knows "-print0")
  • find . -print|xargs command (if there's no white space in the arguments)

If the major part of the arguments consists of long, absolute or relative paths, then try to move your actions into the directory: cd /directory/with/long/path; command * And another quick fix may be to match fewer arguments: command [a-e]*; command [f-m]*; ...


by (3.5m points)
selected by
+3 votes

Linux: File search
Only search for a specific file type and ignore subfolders
Search for files of a specific size
Linux: search folder

Opens in Linux a terminal with the key combination Ctrl + Alt + T .

Linux: File search

  • Terminal command: find -name {Datei}
  • Example: find -name Background.jpg

The command searches the current directory and subdirectories for the Background.jpg file. Make sure you specify the file extension, otherwise you will not get any matches, but you will be looking for folders.

  • Example with specification of the search directory: find / -name Test * .jpg

The command searches the entire file system (starting with the root directory "/") for JPG files that begin with "test" (* is wildcard). It may take a while to search the entire file system.

Note: If the file you are looking for contains spaces, you must put them in quotation marks, such as find -name "My Background.jpg" . If you also want to search for hidden files, use * as a wildcard: find -name "* My Background.jpg" .

Only search for a specific file type and ignore subfolders

  • Terminal command: find -maxdepth 1 -name *.{Dateiendung}
  • Example: find -maxdepth 1 -name *.pdf

The command looks for PDF files in the current directory, but ignores any PDF files that are in subfolders.

Search for files of a specific size

minimum size

  • Terminal command: find -size +{Mindestgröße}M
  • Example: find -size + 5M

Searches for files that are at least 5 MB in size.

maximum size

  • Terminal command: find -size -{Maximalgröße}M
  • Example: find -size -7M

Searches for files up to 7 MB in size.

At least and maximum

  • Terminal command: find -size +{Mindestgröße}M -size -{Maximalgröße}M
  • Example: find -size + 11M -size -20M

Searches for files that are at least 11 MB and a maximum of 20 MB in size.

Linux: search folder

The same command can be used to search for folders:

  • Terminal command: find -name {Ordner}
  • Example: find -name images

Searches the current directory and subdirectories for the Pictures folder.

Note: If the folder you are looking for contains spaces, you must put it in quotation marks, such as find -name "My Pictures" . If you also want to search for hidden folders, use * as a wildcard: find -name "* My pictures" .

Search only specific folders

  • Terminal command: find {Suchordner1} {Suchordner2} -name {Ordner}
  • Example: find / tmp / etc -name test

Searches only the folders / tmp and / etc, if it contains the folder "Test".


by (551k points)
+4 votes

So you can quickly find files in Linux by name
Search for large files
Find recently changed files
If locate brings an error

If you want to search for files with Linux, it works quite simply on the command line. It's up to you whether you can easily locatewith  locate files or find it extremely flexible with the help of  find . We've put together search tips for you in this post, all of which work on Mac OS.

So you can quickly find files in Linux by name

  • Enter a locate , followed by the searched name part, such as locate mysql
  • The locate command finds all files that have the specified string in the path or file name itself.
  • To avoid the case being incorrect, use the -i parameter  . The command locate -i mysql would also find a MySQL.info file .

    If you are looking for files in Linux, the find command will help.,

    The find command can ignore the case if desired.

  • Sometimes it is annoying that the pathname is also searched for the keyword. Then use the parameter -b like "basename" and locate  only gets hits where the search term appears in the file name itself.

The query by locate  is quite fast, because the search is not live in the file system, but in a database, which should be automatically updated by the system at night. If this does not happen by itself, you can create this database with the command updatedb  .

If locate brings an error

In this case, most likely just the program package that includes locate is not installed .

On a Debian system, install it with the following commands and let it fill up the database:

sudo apt-get install locate
sudo updatedb

Search for large files

In contrast to locate, the find command does not use its own database, but searches live for all currently available files in a directory tree according to specific criteria such as name, type, size or age. For example, the following command gives you all the files in the / var / www folder that are more than 100 MB in size:

find /var/www -size +100M -ls

The first parameter specifies the start folder for the search. The -size + 100M part limits the results to hits larger than 100 MB in size. The -ls causes the ad not only to contain the file name, but also detailed information such as owner, size, and creation date.

You can also use two sizes to define a range. If you want to search for all files between 100 and 120 MByte, use:

find /var/www -size +100M -size -120M-ls

Find recently changed files

Also, to find files that were last modified, find help .

Write this command line:

find . -name '*.doc' -type f -mtime -1

The command looks for the current directory (that is the period) and its subdirectories for all files ending in * .doc that are less than a day old. If you want to search over all files, just omit the part name '* .doc'  . The parameter -type f ensures that only files are output, not directories that fit into the schema.

If you do not just want the path and file names as answers, but a detailed list, add -ls to the end:

find . -name '*.doc' -type f -mtime -1 -ls

Responsible for these age questions is the parameter -mtime . The number behind it stands for days, so -mtime -1 for the past day, ie the last 24 hours.

Depending on the sign you control the days. Write for example

find . -name '*.doc' -type f -mtime +1

and come out all files and directories that are older than a day.

If the number is unsigned find finds files that are the specified days plus an old one. With -mtime 5 looking place so all files that have between five and six days under his belt.


by (551k points)

Related questions

+5 votes
1 answer
asked Oct 6, 2019 in Linux / Unix by backtothefuture (551k points) | 212 views
+5 votes
1 answer
+3 votes
1 answer
asked May 22, 2019 in Linux / Unix by backtothefuture (551k points) | 175 views
+5 votes
1 answer
asked Jun 22, 2019 in Linux / Unix by backtothefuture (551k points) | 298 views
+5 votes
1 answer
asked Nov 2, 2019 in Linux / Unix by backtothefuture (551k points) | 254 views
Sponsored articles cost $40 per post. You can contact us via Feedback

Most popular questions within the last 30 days

  1. Cell phone location by number: How easy it is to do it in Latam
10,634 questions
10,766 answers
510 comments
3 users