3 Ways to Find Files on Linux

Table of contents:

3 Ways to Find Files on Linux
3 Ways to Find Files on Linux

Video: 3 Ways to Find Files on Linux

Video: 3 Ways to Find Files on Linux
Video: 2 Ways How to Unsubscribe Emails in Gmail in Seconds | Gmail Unsubscribe 2024, November
Anonim

Finding files on a Linux system can be difficult if you don't know how. The best way to find files on Linux is to use a few terminal commands. Mastering some of these commands can give you complete control over files, and they are more useful than the simple search functions of other operating systems.

Step

Method 1 of 3: Using "find"

690519 1
690519 1

Step 1. Search for files by name

This type of search is the most basic, and you can do this with the find command. The command below will perform a search based on the words entered in the directory you are in as well as the subdirectories within it.

find -iname "filename"

If you use -iname instead of -name, the difference between upper and lower case keywords will have no effect. The -name command considers uppercase and lowercase letters to be different characters

690519 2
690519 2

Step 2. Set the search to start in the root directory

If you want to do a system-wide search, you can add the / modifier to the keyword. The modifier tells the find command to search thoroughly, starting from the root directory.

find / -iname "filename"

  • You can start a search in a specific directory by replacing the / modifier with the directory path, for example /home/pat.
  • You can use. other than / so that searches are only performed within the directory you are in and also the subdirectories within it.
690519 3
690519 3

Step 3. Use wildcard characters

* to search for anything that partially matches your keywords.

The wildcard * character can be used to find files whose full names you don't know, or if you want to find all files with a specific extension.

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

  • The above command will return all the.conf files in the user directory named Pat (as well as the subdirectories therein).
  • You can also use it to find anything whose name partially matches that keyword. For example, if you have a lot of documents related to wikiHow, you can find them by typing "*wiki*".
690519 4
690519 4

Step 4. Make search results easier to manage

If the search results are too many, it will be difficult for you to find the file you want. Use the character | and send the search results back to the "less" filter program. That way, you can scroll through and filter your search results more easily.

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

690519 5
690519 5

Step 5. Find search results with a specific type

You can use modifiers to return only certain types of results. You can search for regular files (f), directories (d), symbolic links (l), character devices (c), and block devices (b) using appropriate modifiers.

find / -type f -iname "filename"

690519 6
690519 6

Step 6. Filter the search results by size

If you have lots of files with similar names, but know the size of the file you want, you can filter the search by size.

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

  • The above command will return search results of 50 megabytes or more. You can use + or - to search for files with a larger or smaller size. Omitting the + or - sign makes the search return only results with the truly "correct" size.
  • You can filter the search by bytes (c), kilobytes (k), megabytes (M), gigabytes (G), or 512 bytes (b) blocks. Be aware that the markings distinguish between uppercase and lowercase letters.
690519 7
690519 7

Step 7. Use boolean operators to combine search filters

You can use the -and, -or, and -not operators to combine different searches.

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

The above command will look for files in the "travelphotos" directory that are over 200 kilobytes in size, but don't have the word "2015" in their name

690519 8
690519 8

Step 8. Perform a file search by owner name or permission

If you are trying to find a specific file that belongs to a specific user, or are looking for a file with certain permissions, you can narrow the search.

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

The examples above will perform a search for a specific user, group, or permission within a keyword. You can also omit the filename to return all files that match that type. For example, find / -perm 777 will return all files with 777 permissions (no restrictions)

690519 9
690519 9

Step 9. Concatenate the commands to perform the action when the file has been found

You can combine the find command with other commands so that you can process files returned by keyword with that command. Separate the find command and the second command with the -exec flag, then end the line with {};

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

The above command will search the directory you are in (and all subdirectories in it) for files with 777 permissions. Then, the chmod command will change the permissions to 755

Method 2 of 3: Using "locate"

690519 10
690519 10

Step 1. Install

locate functionality. The locate command is generally faster than the find command, due to the database utilization of your file system. Not all types of Linux are equipped with a locate function, so enter the following command to install it:

  • Type sudo apt-get update, then press Enter.
  • You can install it on Debian and ubuntu like this: Type sudo apt-get install mlocate, then press Enter. If the locate function was installed earlier, you will see the message mlocate is already the newest version.
  • On Arch Linux, use pacman package manager: pacman -Syu mlocate
  • For Gentoo, use emerge: emerge mlocate
690519 11
690519 11

Step 2. Update

locate database You. The locate command will not be able to find anything until it is created and updated. This process is usually done automatically every day, but you can also update it manually. You need to do this if you want to use the locate function as soon as possible.

Type in sudo updatedb, then press Enter

690519 12
690519 12

Step 3. Use the

locate to perform a simple search.

The locate function works quickly, but doesn't have as many options as the find command. You can perform basic file searches in a similar way to the find command.

locate -i "*.jpg"

  • The above command will search the entire system for files with the-j.webp" />
  • Like the find command, the -i modifier also makes uppercase and lowercase letters in keywords considered the same.
690519 13
690519 13

Step 4. Limit search results

If your search returns so many results that it is difficult for you to use them, you can reduce those results with the -n modifier, followed by the number of results you want to return.

locate -n 20 -i "*.jpg"

  • Only 20 search results will be displayed.
  • You can also use | to send search results to the less filter program for easier scrolling of results.

Method 3 of 3: Searching for Text in Files

690519 14
690519 14

Step 1. Use the command

grep to find the text string in the file.

If you're looking for a file that contains a specific phrase or character string, you can use the grep command. The basic grep command has the following format:

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

  • The -r modifier makes the search "recursive", so a search will be performed within the directory and all subdirectories within it to find files containing text with the search keyword.
  • The -i modifier indicates that the search keyword does not differentiate between uppercase and lowercase letters. If you want to force the search to differentiate between uppercase and lowercase, ignore the -i modifier.
690519 15
690519 15

Step 2. Remove excess text

When doing a grep search as above, you'll see the filename with the matching letters highlighted. You can hide the matching text and show only the file directory name and path by adding:

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

690519 16
690519 16

Step 3. Hide the error message

The grep command will return an error when attempting to access a directory without the appropriate permissions or when entering an empty directory. You can send error messages to /dev/null so they don't appear.

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

Recommended: