Locating files on your Linux VPS system using whereis, which, whatis, readlink, find

Introduction

Linux system is file based, when you work long enough with your system, you may find yourself in a situation, when you no longer know exactly, where a certain file resides. It happens because sometimes you do not install that files manually, you forgot the path and for a whole bunch of reasons. Knowledge of quickly and efficiently finding files, you are interested in, which greatly increase your perfomance and save a lot of time. I will describe a basic usage of a few commands that will make locating files easier. For reference i will be using centos 7.4 system, however command syntaxis is same on most Linux systems.

Prerequisites:

1) Centos 7.4 Linux system 2) Basic knowledge how to execute commands on a Linux system

whereis, which, whatis

which: Which returns a direct path to the binary, shell command or executable. For example you run some command from command line, but want to know the exact location of the binary, you actually execute run

which <command>

for example

which yum

and you will get the output, containing full path to the binary cs1

whereis: whereis is a similar command, but it returns the location of binary, sources and man page. It also sometimes show the path to the config file. run whereis <command> for example whereis yum and get the following output: cs2whatis: whatis returns information about tool, extracted from it's man page run whatis <command> for example whatis yum and get the following output cs3

readlink

If you have a symlink on your filesystem and want to understand where the file, which is linked to is located simply run

readlink <symlink>

For example run

readlink /etc/alternatives/ld

and get the following output cs4

find

find is the most useful command to locate a specific file, it has ton of options, and it will be your most used file search tool. It has ton of options, and i am not going to cover all of them, since it will take a lot of time, but will describe most useful ways of using it.

Basic usage

find / |grep <filename>

It is a no-brain simpliest command that will scan the all the dirs and return strings, containing the word after grep. or for a specific folder

find /folder |grep <filename>

It is VERY slow, will scan all the dirs inside the dir, you are running find against, but it's a no brainer and easy to remember. Find has a usage scenario of find <path> <pattern> If no path is specified find is executed in current directory and looks through all subdirectories. I will show a set of different find patterns that are most useful:

find a file in a current directory

find . -name <filename>

cs5

find a file in a certain dir

find <dir> -name <filename>

cs6

find a file ignoring upper and lower cases of characters

find . -iname <filename>

cs7

find a regular file with a specific extension

find -type f -name <filename>.<extension>

cs8 Notice that testfile1.conf, which is a symlink, also formally has a conf extension, but is not displayed, because it is a symlink, but not a regular file. Please notice, that -type allows following options:

      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)

find all files with a specific extension in a directory

find . -type f -name "*.extension" for example find . -type f -name "*.conf" cs9 Note that symlink is once again not displayed, for same reason

find a file with a specific access rights inside the dir.

find <dir> -type f -perm <4 digit permission> -print

for example

find /root -type f -perm 0600 -print

cs10

find all executable files

find <dir> -perm /a=x

for example

find /sbin/ -perm /a=x

cs11

find a file with a certain owner

find

-user cs12

find a file with a certain group ownership

find <dir> -group <groupname> <filename>

cs13

Find time options:

you can find files, using specific time patterns:

-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 looks like

find <dir> <timeoption> 

for example find all files in current dir accessed 10 days back:

find . -atime 10

find all files modified 10 days back:

find . -mtime 10

find all file changed in last 33 minutes:

find . -cmin 33

find all files accessed in last 33 minutes:

find . -amin 33

find all files modified in last 33 minutes:

find . -mmin 33

Size-related find patterns:

find <dir> -size <size>

for example find all files that are 10GB:

find . -size 10G

cs14 find all files between 10 and 20GB:

find . -size +10G -size -20G

cs15

Advanced find search patterns

You can also combine searches, for example find a file with a .conf in a name name, that was accessed 10 days back, and is 10GB in size

find . -atime 10 -size 10G -name "*.conf"

you can also execute commands against found files, for example

find . -user vpsuser -name "testfile*" - exec <command> {} \;

like find . -user vpsuser -name "testfile*" -exec ls -l {} \; cs16 You can also reverse search patterns, so the result will be everything, that do not match the patterns. For example for such dir: cs17

find . -user vpsuser -name "testfile*" returns cs18 find . -not -user vpsuser -name "testfile*" returns cs19 So adding -not reverses the find pattern

Conclusion

Now you have enough knowledge to start effectively using different commands to navigate through filesystem on your system faster. Look into find manual for additional and advanced search patterns, combine different patters, and remember - if you suppose, there is a way to find a tricky file in 1 command, probabl