3 ways to find a file in Linux

3 ways to find a file in Linux
3 ways to find a file in Linux
Anonim

Finding a file in a Linux machine can be difficult if you don't know how. The best way to find files is to use several different terminal commands. Mastering these commands can give you complete control over your files, and they are much more powerful than simple search features of other operating systems.

Steps

Method 1 of 3: Use "find"

690519 1

Step 1. Search for a file by its file name

This is the most basic search you can run using the find command. The command below will search for your query in the current folder and in any subfolder.

find -iname "filename"

Using -iname instead of -name ignores the case of your request. The -name command is case sensitive

690519 2

Step 2. Start the search in the “root” folder

If you want to search your entire system, you can add the / modifier to your query. This will tell find to search all directories from the "root" folder.

find / -iname "filename"

  • You can start the search in a specific folder by replacing / with the path of a directory, such as / home / pat.
  • You can use a. instead of a / to force the search to be done only in the current folder and its subfolders.
690519 3

Step 3. Use the wildcard character

* to look for something that matches that piece of query.

The wildcard character * can be useful when searching for something whose full name you don't know, or if you want to search for anything that has a particular extension.

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

  • This will return all the.conf files in Pat's user folder (and its subfolders).
  • You can also use it to find anything that matches a portion of the filename. For example, if you have a lot of documents related to wikiHow, you can find them all at once by typing "* wiki *".
690519 4

Step 4. Make your search results more manageable

If you get a lot of search results, it may be difficult for you to navigate through them. Use the character | to send the search results to "less" filtering software. It can allow you to scroll through and filter results much more easily.

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

690519 5

Step 5. Look for specific types of results

You can use modifiers to return only particular types of results. You can search for normal files (f), directories (d), symbolic links (l), character devices (c), and blockers (b) using the appropriate modifier.

find / -type f -iname "filename"

690519 6

Step 6. Filter your search results by size

If you have many files with similar names, but you know the size of what you are looking for, you can filter your results by their size.

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

  • This will return results that are 50 megabytes or more. You can use + or - to search for larger or smaller sizes. Forget the + or - will search for files that are exactly the size specified.
  • You can filter by number of bytes (c), kilobytes (k), megabytes (M), gigabytes (G), or by blocks of 512 bytes (b). Note that the size tag is case sensitive.
690519 7

Step 7. Use Boolean operators to combine search filters

You can use the -and, -or, and -not operators to combine different types of searches into one.

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

The above command will find files in the "vacation photos" directory that are greater than 200 kilobytes in size but do not contain "2015" anywhere in their filename

690519 8

Step 8. Search for files by owner or permissions

If you are trying to search for a specific file owned by a particular user, or files with certain permissions, you can narrow your search field.

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

The above examples will search for the users, groups or permissions specified in the request. You can also omit the name of the file you are looking for to see all files that match the requested type returned. For example, find / -perm 777 will return all files with 777 permissions (no restrictions)

690519 9

Step 9. Combine commands to perform actions when files are found

You can combine the find command with other commands so that you can run the latter on the files that are returned by the query. Separate the find command from the second command with the -exec tag, then end the line with {};

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

This will search the current folder (and all of its subfolders) for files that have 777 permissions. The console will then use the chmod command to change the permissions to 755

Method 2 of 3: Use "locate"

690519 10

Step 1. Install the feature

locate. The locate command generally works much faster than find, because it works using a database drawn from your file structure. Not all Linux distributions come with the locate feature installed straight away, so type the following commands to get it installed:

  • Type sudo apt-get update and press Enter.
  • Type sudo apt-get install mlocate and press Enter. If locate is already installed, you will receive the message mlocate is already the newest version.
690519 11

Step 2. Update your database

locate. The locate command will not be able to find anything until its database has been built and updated. This is done automatically every day, but you can initiate an update manually as well. You will need to do this if you want to start using locate immediately.

Type sudo updatedb and press Enter

690519 12

Step 3. Use

locate to do simple research.

The locate command is fast, but it does not have as many options as the find command. You can run a basic file search more or less the same way as with the find command.

locate -i "*.jpg"

  • This command will search for files with the extension.jpg" />
  • As with the find command, the -i ignores the case of your query.
690519 13

Step 4. Limit Your Search Results

If your queries return too many results to handle, you can remove some of them by using the -n option, followed by the number of results you want displayed.

locate -n 20 -i "*.jpg"

  • Only the first 20 results corresponding to the query will be displayed.
  • You can also use the trick of the | to send the results to less for easier browsing.

Method 3 of 3: Find text in files

690519 14

Step 1. Use the command

grep to search for strings of characters within files.

If you are looking for a file that contains a certain phrase or string of characters, you can use the grep command. A basic grep command takes the following form:

grep -r -i "request" / path / to / directory /

  • The -r option sets the search to "recursive", makes the search "recursive", so it will search for any file containing the string of characters from the query in the current folder and all of its subfolders.
  • The -i option indicates that the query is not case sensitive. If you want to force the search to be case sensitive, omit the -i operator.
690519 15

Step 2. Remove additional text

When you do a search with grep as above, you will see the name of the file which will be highlighted next to the search text. You can hide the corresponding text and simply display the filenames and their paths by including the following:

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

690519 16

Step 3. Hide error messages

The grep command will return an error when it tries to access folders without the proper permissions or when it tries to launch into empty folders. You can return error messages to / dev / null, which will hide them from your output display.

grep -r -i "request" / path / to / directory / 2> / dev / null

Popular by topic