Easy methods to Use the Discover Command to Seek for Information in Linux

There are times when you want to access a particular file but cannot find it on your system due to a lack of folder organization. Fortunately, Linux gives you some useful utilities that you can use to easily find files on your computer.

The find command is one such tool that allows you to search for a file by file name, permissions, extension, size, and so on. This guide explains the Linux Find command and provides a few examples that show how powerful this utility can be.

What is the search command?

As the name suggests, it is Find This command allows a user to search for files that exist in their local storage. In contrast to normal search functions in Linux file managers, the find command has additional functions that can be used to filter the files under certain conditions.

The find command also provides several criteria for finding files on a computer. You can even use regular expressions to match the name of a file to a specific pattern.

How to find files on Linux

The find command has many options and features that filter the files based on the specified conditions.

Find command syntax

The basic syntax of the find command is:

find (path) (options) (expression)

For example, the following command searches for text files in the file /At home Directory.

find / home -type f -name "* .txt"

Note that before you can search for files in your storage, you must have read permissions to this particular directory.

Search for files by name

The most common use of the find command is to search for a file by name. To find a file using the filename, use the -Surname Flag with the standard command.

find / home -type f -name filename.txt

The above command looks for a file named Filename.txt by doing /At home Directory. The -Type f Option tells the system that we're looking for one file.

If you want to ignore upper and lower case in the file name, replace the -Surname Option with -iname.

find / home -type f -iname filename

This command searches for a file with one of the following names: filename, filename, filename, FiLename, and so on.

As with any other Linux command, you can use it . (Period) to also indicate the relative path of the current directory.

Find . -type f -name filename.txt

Similar, /. to the /Root and ~ to the /At home can also be used.

Look for files by extension

Finding files with a specific extension can help narrow down your search results. Use the following regular expression with the to find a file by its extension -Surname and -iname Flag.

find / home -type f -name "* .pdf"

This command displays a list of all the files that have them .pdf Extension. Note that you have to escape from this asterisk ((* *) Characters with either Quotes (("") or a backward slash ((.) so that the terminal interprets it as a wildcard character.

You can also invert the above command with the command -Not Flag. The following command looks for files that do not have the .pdf Extension.

find / home -type f -not -name "* .pdf"

You can even pass the find command along with other Linux commands. For example, to change the moderation permissions for any file that matches the condition:

find / home – type f "* .pdf" -exec chmod -777 {} ;

This command searches for all PDF Files in the /At home Directory and change their permissions so anyone can read, write, and run these files.

Look for specific file types

In addition to files, the find command can search for other types of files. Directories, symbolic links, sockets, and drawing devices are some of the file types that find supports.

So far we've been using them -Type f Option in the find command. The f stands for file. To check for other file types on Linux, replace f with other reserved characters.

  • f: regular files
  • d: Directories
  • l: symbolic links
  • c: Drawing devices
  • b: Block devices
  • p: Named Pipe
  • s: Sockets

How to find subdirectories in the /At home Directory:

find / home type d

Find files by size

The -Size You can use flag to search for files that take up a certain amount of space on the hard disk. The following suffixes indicate the different file sizes:

  • b: 512-byte blocks
  • c: Bytes
  • w: Two-byte words
  • k: Kilobytes
  • M.: Megabytes
  • G: Gigabytes

To find all files that are 1GB in size:

find / home type f size 1G

To search for files less than 1 GB, add the following: minus ((– –) Characters in front of the size:

find / home type f size -1G

Similarly, use the plus ((+) Operator to find files larger than 1 GB:

find / home type f size + 1G

To search for files in a size range:

find / home -type f-size + 1M-size -10M

Find files using timestamps

You may already know that Linux assigns specific timestamps to every file in your storage. These time stamps contain the modification time, the modification time and the access time.

To find files with a specific modification time:

find / home -type f -name "* .txt" -mtime 5

The above command will print all files that have changed in the last five days. Likewise, you can also use -a time and -ctime to filter the files according to the access time and the modification time.

You can use that too plus and minus Characters to find files larger or smaller than a specific timestamp.

find / home -type f -name "* .txt" -mtime +5

Look for files with specific permissions

The -Perm This option allows users to search for files with specific permissions.

find / home type f -perm 777

Use the slash Character (/.) to list the file when at least one category has corrected the permissions provided.

find / home-type f -perm / 777

Search files by owner

Use the -User Flag to get files owned by a specific user.

find / home -user randomuser

Find and delete files

Add that to delete all filtered files with find -Clear Flag at the end of the command.

find / home -type f -name "* .pdf" -delete

The above command will delete all of them PDF Files that are in the /At home Directory.

You cannot delete non-empty directories with find. You need to use the rm command to delete such folders on your Linux system.

Organize files on Linux

Finding files is difficult when you have hundreds of directories on your system with no names to match. The find command is useful when you want to filter out files in a directory based on a specific criterion.

To get the most out of your storage on a Linux system, organizing and managing files is a must. By grouping folders properly and removing redundant data, you can quickly access the files you want.

9 Essential Tips for Managing and Organizing Your Computer Files

There's no perfect way when it comes to managing computer files, but these tips will help you bring order out of the chaos.

Continue reading

About the author

Deepesh Sharma
(35 articles published)

Deepesh is the junior editor for Linux at MUO. He has been writing informational content on the Internet for over 3 years. In his spare time he enjoys writing, listening to music and playing the guitar.

More
From Deepesh Sharma

Subscribe to our newsletter

Sign up for our newsletter to receive tech tips, reviews, free e-books, and exclusive offers!

One more step …!

Please confirm your email address in the email we just sent you.

Leave a Reply

Your email address will not be published. Required fields are marked *