How to Find Files in Linux
Use the "find" command
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.
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.
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*".
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
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"
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.
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".
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).
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
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
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.
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.
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
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.
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
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.
You should read it
- Search for files and directories in Linux using the command line interface
- Instructions for using find command in Linux
- How to compare two text files on Linux Terminal
- How to access Linux files on Windows 10
- How to Find a File in Linux
- File Management in Unix / Linux
- Is it possible to run .exe files on Linux?
- How to use pandoc to convert files on Linux
- How to create new files in Linux
- How to Create ISO Files on Linux
- How to find the MAC address using the command line in Linux
- How to copy and rename files in Linux
Maybe you are interested
How to use the file search command on Windows, find saved files
Why is tldr command better than man command in Linux?
Should I use PowerShell or Command Prompt?
What to do when open command window here does not appear?
How to switch users on the Linux command line
How to fix Mac Homebrew error 'zsh: command not found: brew'