How to Find Files in Linux

If you don't know how, finding files in Linux can be quite difficult. Here, the best way is to use a few different terminal commands. They are much more powerful than the simple search features on other operating systems, and when you know how to use them well, you will have complete control over your files.

Use the "find" command

How to Find Files in Linux Picture 1How to Find Files in Linux Picture 1

Find files by name. This is the most basic search method you can do using the find. The command below will find files in the current directory and any of its subdirectories.

find -iname "filename"

Use -inameinstead -nameto ignore capitalization and lowercase elements in your query. The order -nametakes this factor into account.

How to Find Files in Linux Picture 2How to Find Files in Linux Picture 2

Set the search to start from the root directory. To search across your entire computer, you can add modifiers /to your query. Thanks to that, the command findwill recognize and search all directories from the root directory.

find / -iname "filename"

You can start your search in a specific directory by replacing the mark /with a path, such as /home/pat.

You can use .instead /to only perform a search on the current folder and its subfolders.

How to Find Files in Linux Picture 3How to Find Files in Linux Picture 3

Use wildcards. * to find any files containing parts of the query. This character *can be useful for finding items whose full name you don't know, or when you want to find all files with a certain extension.

find /home/pat -iname "*.conf"

The above command will return every .conf file in Pat's user directory (and subdirectories).

You can also use it to find any files that contain a partial filename. For example, if there are a lot of documents related to wikiHow, you can find them all by typing "*wiki*".

How to Find Files in Linux Picture 4How to Find Files in Linux Picture 4

Simplify returned results. It can be difficult to sift through when there are so many results returned. At this point, use characters |and send the search results to the "less" screening program. You can then browse and filter the results much more easily.

find /home/pat -iname "*.conf" | less. less

How to Find Files in Linux Picture 5How to Find Files in Linux Picture 5

Find specific types of search results. You can use modifiers to get certain types of search results. You can find regular files ( f), directories ( d), symbolic links ( l), control devices ( c), and block devices ( b) with matching modifier characters.

find / -type f -iname "file-name"

How to Find Files in Linux Picture 6How to Find Files in Linux Picture 6

Filter search results by size. When you have multiple files with the same name and know the size of the file you're looking for, you can filter your search results by this criteria.

find / -size +50M -iname "filename"

The above command will return files of 50 MB or larger. +You can use or characters -to find files that are larger or smaller in size. When these characters are not used, the search command will return a file of the exact size requested

You can filter by bytes ( c), kilobytes ( k), megabytes ( M), gigabytes ( G), or 512-byte blocks ( b). Note that this section is case sensitive.

How to Find Files in Linux Picture 7How to Find Files in Linux Picture 7

Use logical operators to combine search and filter types. -andYou can use the (and), -or(or) and (not) operators -notto combine search types together.

find /travel_image -type f -size +200k -not -iname "*2015*"

The above command will find files in the "travel_images" folder that are larger than 200 kilobytes and whose names do not contain "2015".

How to Find Files in Linux Picture 8How to Find Files in Linux Picture 8

Find files by owner or access permissions. If you're looking for someone else's files or files with certain permissions, you can narrow the search scope.

find / -user pat -iname "filename" find / -group users -iname "filename" find / -perm 777 -iname "filename"

The above examples query for certain users, groups, and access rights, respectively. You can also omit the filename to return all files of the specified type. Such as find / -perm 777will return every file with access permission 777 (unlimited).

How to Find Files in Linux Picture 9How to Find Files in Linux Picture 9

Combine commands to perform processing after finding the file. You can combine the command findwith other commands to execute these commands on the returned files. Separate the command findand the second command with an equal -execand end the command line with a comma{} ;

find . -type f -perm 777 -exec chmod 755 {} ;

The above command combination will find all files with 777 permissions in the current directory (and subfolders) and then use the command chmodto change that access permission to 755.

Use the "locate" command

How to Find Files in Linux Picture 10How to Find Files in Linux Picture 10

Feature settings. locate . In general, the command locateruns faster than commands findby working on top of your file structure database. Not all Linux distributions have this feature available. Therefore, you need the following commands to try installing them:

Type sudo apt-get updateand press ↵ Enter.

You can install on Debian and Ubuntu by: Type sudo apt-get install mlocateand press ↵ Enter. If locatealready installed, the following message will appear:mlocate is already the newest version.

In Linux Arch, use the pacman package manager:pacman -Syu mlocate

With Gentoo, use emerge:emerge mlocate

How to Find Files in Linux Picture 11How to Find Files in Linux Picture 11

Update database. locate your. The command locatewon't be able to find anything until its database is built and updated. Even though this task is run automatically every day, you can still do it yourself and you will have to do so to use it locateright away.

Type sudo updatedband press ↵ Enter.

How to Find Files in Linux Picture 12How to Find Files in Linux Picture 12

Use . locate to execute simple queries. Even though it's fast, the command locatedoesn't have as many options as the find. The execution of the basic search command in this command is very similar to the basic search command in the find.

locate -i "*.jpg"

The above command finds files with the extension .jpgon the entire system. Wildcards *play the same role as in the find.

Like the command find, -iit does not consider capitalization or lowercase letters in your query.

How to Find Files in Linux Picture 13How to Find Files in Linux Picture 13

Limit search results. If your search returns too many results, you can narrow them down using the option -nfollowed by the number of results you want to display.

locate -n 20 -i "*.jpg"

Only the first 20 search results that match the query will be displayed.

You can also use the |send results tag lessto make browsing easier.

Find files containing certain text

How to Find Files in Linux Picture 14How to Find Files in Linux Picture 14

Use command . grep to find files containing certain text content. To find files containing certain phrases or strings of characters, you can use the command grep. grepThe basic command has the following format:

grep -r -i "search query" /path/to/directory/

-rset the search to "recursive", meaning that every file containing the keyword in the current directory and all its subdirectories will be searched.

-iindicates that the above query does not distinguish between upper and lower case. If you want to be case-sensitive, omit the -i.

How to Find Files in Linux Picture 15How to Find Files in Linux Picture 15

Remove text content. When executing a search command grepwith a similar structure as above, you will receive returned results including the file name and highlighted text that matches the query content. You can hide this matching text, showing only the file name and path, by adding the following to the command:

grep -r -i "search query" /path/to/directory/ | cut -d: -f1

How to Find Files in Linux Picture 16How to Find Files in Linux Picture 16

Hide error messages. The command grepwill return an error when trying to access a directory without the appropriate permissions or searching for an empty directory. You can send error messages to /dev/null to hide in the output.

4 ★ | 1 Vote