Finding Commands and Documentation
Recall that the
whatis
command (or man -f
) will tell you which section a man page is stored in. If you use this command often enough, you will likely come across an unusual output, such as the following:sysadmin@localhost:~$ whatis ls ls (1) - list directory contents ls (lp) - list directory contents sysadmin@localhost:~$
Based on this output, there are two commands that list directory contents. The simple answer to why there are two
ls
commands is that UNIX had two main variants, which resulted in some commands being developed "in parallel". This resulted in some commands behaving differently on different variants of UNIX. Many modern distributions of Linux include commands from both UNIX variants.
This does, however, pose a bit of a problem: when you run the
ls
command, which command is executed? The focus of the next few sections will be to answer this question as well as to provide you with the tools to find where these files reside on the system.1 Where Are These Commands Located?
To search for the location of a command or the man pages for a command, use the
whereis
command. This command searches for commands, source files and man pages in specific locations where these files are typically stored:sysadmin@localhost:~$ whereis ls ls: /bin/ls /usr/share/man/man1p/ls.1.gz /usr/share/man/man1/ls.1.gz sysadmin@localhost:~$
Man pages are normally easily distinguished between commands as they are normally compressed with a command called
gzip
, resulting in a filename that ends in .gz
.
The interesting note is that you see there are two man pages listed, but only one command (
/bin/ls
). This is because the ls
command can be used with the options/features that are described by either man page. So, when you are learning what you can do with the ls
command, you can explore both man pages. Fortunately, this is more of an exception as most commands only have one man page.2 Find Any File or Directory
The
whereis
command is designed to specifically find commands and man pages. While this is useful, there are times where you want to find a file or directory, not just files that are commands or man pages.
To find any file or directory, you can use the
locate
command. This command will search a database of all files and directories that were on the system when the database was created. Typically, the command to generate this database is run nightly.sysadmin@localhost:~$ locate gshadow /etc/gshadow /etc/gshadow- /usr/include/gshadow.h /usr/share/man/cs/man5/gshadow.5.gz /usr/share/man/da/man5/gshadow.5.gz /usr/share/man/de/man5/gshadow.5.gz /usr/share/man/fr/man5/gshadow.5.gz /usr/share/man/it/man5/gshadow.5.gz /usr/share/man/man5/gshadow.5.gz /usr/share/man/ru/man5/gshadow.5.gz /usr/share/man/sv/man5/gshadow.5.gz /usr/share/man/zh_CN/man5/gshadow.5.gz sysadmin@localhost:~$
Any files that you created today will not normally be searchable with the
locate
command. If you have access to the system as the root
user (the system administrator account), you can manually update the locate
database by running the updatedb
command. Regular users cannot update the database file.
Also note that when you use the
locate
command as a regular user, your output may be limited due to file permissions. Essentially, if you don't have access to a file or directory on the filesystem due to permissions, the locate
command won't return those names. This is a security feature designed to keep users from "exploring" the filesystem by using the locate
database. The root
user can search for any file in the locate
database.3 Count the Number of Files
The output of the
locate
command can be quite large. When you search for a filename, such as passwd
, the locate
command will produce every file that contains the string passwd
, not just files named passwd
.
In many cases, you may want to start by listing how many files will match. You can do this by using the
-c
option to the locate
command:sysadmin@localhost:~$ locate -c passwd 97 sysadmin@localhost:~$
4 Limiting the Output
You can limit the output produced by the
locate
command by using the -b
option. This option will only include listings that have the search term in the basename of the filename. The basename is the portion of the filename not including the directory names.sysadmin@localhost:~$ locate -c -b passwd 83 sysadmin@localhost:~$
As you can see from the previous output, there will still be many results when you use the
-b
option. To limit the output even further, you place a \
character in front of the search term. This character limits the output to filenames that exactly match the term:sysadmin@localhost:~$ locate -b "\passwd" /etc/passwd /etc/cron.daily/passwd /etc/pam.d/passwd /usr/bin/passwd /usr/share/doc/passwd /usr/share/lintian/overrides/passwd sysadmin@localhost:~$
Finish.....
Thank for comming my website....
0 comments