Locating Files on Your Linux VPS: whereis, which, whatis, readlink, find
Overview
Efficient file navigation on a Linux system is crucial for locating files quickly. This need arises from various scenarios, such as automated installations or forgetting file paths over time. In this guide, I’ll walk you through essential commands that streamline the process of finding files, applicable across Linux distributions.
On a CentOS 7.4 system, mastering commands like find
and locate
proves invaluable for efficient file searches. These commands are particularly useful for dealing with complex directory structures or when files are distributed across various locations. The next sections will provide practical insights into using these commands, highlighting their versatility and applicability in diverse Linux environments.
Requirements
- You are currently using a CentOS 7.4 Linux system.
- You possess fundamental skills in executing commands on a Linux environment.
Whereis, Which, Whatis
which: The command provides the absolute path to the binary, shell command, or executable. For instance, if you run a command from the command line and wish to determine the precise location of the executed binary, you can use the which
command.
which
For instance
which yum
When executed, it outputs the full path to the binary, allowing you to pinpoint its exact location.
whereis: The command is similar to which
but provides more comprehensive information. It returns the location of the binary, sources, man page, and occasionally the path to the configuration file. For instance, running whereis yum
will output the following information:
whatis: The command returns information about a tool, extracted from its man page. For example, running whatis yum
will provide the following output, summarizing key details about the yum
command.
Readlink
If you have a symlink on your filesystem and want to understand where the file, which is linked to, is located, simply run the following command:
readlink
For instance, run
readlink /etc/alternatives/ld
and retrieve the following output
Find
The ‘find‘ command proves to be an invaluable tool for locating specific files, boasting a plethora of options that make it your go-to file search utility. While its extensive array of options can’t all be covered in detail due to time constraints, I’ll highlight some of the most useful ways to leverage its power.
Basic Application
find / |grep
It’s a straightforward, no-brainer command that effortlessly scans through all directories and returns lines containing the specified word after applying ‘grep,’ either throughout the entire system or within a designated folder.
find /folder |grep
While it may be on the slower side, the ‘find’ command scans through all directories within the specified directory, making it a simple and memorable option. When no path is specified, ‘find’ operates in the current directory, searching through all subdirectories. Below, I’ll demonstrate a variety of useful ‘find’ patterns to enhance its practicality:
Searching for a File in the Current Directory Using 'find'
find . -name <filename>
Searching for a File in a Specific Directory Using 'find'
find <dir> -name <filename>
Searching for a File Ignoring Upper and Lower Cases of Characters Using 'find'
find . -iname <filename>
Searching for a Regular File with a Specific Extension Using 'find'
find -type f -name <filename>.<extension>
Take note that ‘testfile1.conf,’ despite having a ‘conf’ extension and being a symlink, is not displayed. This is because it is not a regular file but a symlink. It’s important to observe that the ‘-type’ option provides various choices, including:
File is of type c:
b block (buffered) special
c character (unbuffered) special
d directory
p named pipe (FIFO)
f regular file
l symbolic link; this is never true if the -L option or the -follow option is in effect, unless the symbolic link is broken. If you want to search for symbolic links when -L is in effect, use -xtype.
s socket
D door (Solaris)
Searching for All Files with a Specific Extension in a Directory Using 'find'
find . -type f -name “*.extension” for example find . -type f -name "*.conf"
Once again, it’s worth noting that the symlink is not displayed, for the same reason as before.
Searching for a File with a Specific Access Rights Inside the Directory Using 'find'
find <dir> -type f -perm <4 digit permission> -print
For instance
find /root -type f -perm 0600 -print
Searching for all Executable Files Using 'find'
find <dir> -perm /a=x
For instance
find /sbin/ -perm /a=x
Searching for a File with a Certain Owner Using 'find'
find /path/to/search -user username
Searching for a File with a Certain Group Ownership Using 'find'
find <dir> -group <groupname> <filename>
Search time options:
You can locate files using specific time patterns with the following examples:
-atime : last access time in days
-mtime : last modify time in days
-cmin : last change time in minutes
-amin : last access time in minutes
-mmin : last modify time in minutes
Command appears to be
find <dir> <timeoption>
You can use the following command to find all files in the current directory that were accessed 10 days ago:
find . -atime 10
To find all files modified 10 days ago in the current directory, you can use the following command:
find . -mtime 10
To find all files changed in the last 33 minutes in the current directory, you can use the following command:
find . -cmin 33
To find all files accessed in the last 33 minutes in the current directory, you can use the following command:
find . -amin 33
To find all files modified in the last 33 minutes in the current directory, you can use the following command:
find . -mmin 33
Finding Files Based on Size Using `find` Patterns
find <dir> -size <size>
To find all files that are exactly 10 GB in size in the current directory, you can use the following command:
find . -size 10G
To find all files with sizes between 10 GB and 20 GB in the current directory, you can use the following command:
find . -size +10G -size -20G
Advanced File Search Patterns with `find`
You can refine your search by looking for a file with a “.conf” in its name, accessed precisely 10 days ago, and having a size of 10GB.
find . -atime 10 -size 10G -name "*.conf"
You have the capability to execute commands on located files, such as performing actions or extracting information.
find . -user vpsuser -name "testfile*" - exec {} \;
like find . -user vpsuser -name "testfile*" -exec ls -l {} \;
You can also perform a reverse search on patterns, yielding results that do not match specified criteria. For instance, within a given directory, this approach would retrieve items that fall outside the specified patterns.
find . -user vpsuser -name "testfile*"
returns
find . -not -user vpsuser -name “testfile*” returns
So adding -not
reverses the find pattern
In Summary
Now equipped with the basics, you can delve into the find manual for more advanced search patterns. Experiment with combining different patterns to enhance your file navigation skills. Keep in mind that if you can envision a more efficient way to locate a particular file, there’s likely a command or combination of commands to achieve it. Happy exploring!