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
May be interested
- How to create new files in Linuxthere are a number of different linux applications and commands that will create new files for you, even without launching the application. the method you use will depend on your purpose for the file. let's take a look at the options so you can see which one is most useful for you!
- How to Create ISO Files on Linuxtoday's tipsmake will show you how to turn multiple files into one iso file on a linux computer. you need to use the linux command line to do it.
- How is Arch Linux different from other Linux versions?arch linux is arguably the most misunderstood linux distribution. many people find arch difficult to install and maintain.
- How to find the MAC address using the command line in Linuxtoday, tipsmake.com will talk about an important command in linux, ip. this command works on all linux distributions, including ubuntu, arch linux, debian, fedora, etc. '
- How to copy and rename files in Linuxthere are more ways to copy and rename files on linux than just cp and mv. try some commands and other ways suggested below.
- What is UID in Linux? How to find and change the UIDif you've ever interacted with linux systems, chances are you've come across these two words, uid and gid. if you are not familiar with them, let's dig deeper through the following article.
- How to find large files on Windows 10is your computer hard drive or solid state drive on windows 10 full? want to find out all the files that are taking up the most space on the drive? read the following article to learn how to search without using a third-party application.
- 14 interesting Linux commands in Terminalterminal is a very powerful tool, but it can become 'interesting' through a few bash commands that quantrimang will introduce to you later. let's follow up and find out because some commands are quite useful.
- How to synchronize files and folders on Linuxlinux administrators must keep folders synced on a regular basis. there are two simple use scenarios for synchronizing these files and folders.
- The Easiest Ways to Find Large Files on Windowsa quick and effective solution is to identify and delete large, space-consuming files that are hiding on your computer.