• We did not send an email asking for donations - please read this post.

Linux: Finding Files

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
265
Reaction score
253
Credits
6,739
When using any Operating System (OS), it is sometimes necessary to find files. Some commands we’ll cover in this article will look for specific types of files. Others will look for any file in specific places and one will show you the explanation of a command.

These can be very useful for Linux users.

Which

This command will search through the path of the OS to find the executable file specified.

Let’s look at the path set for your system. Open a terminal and type the command ‘echo $PATH’. You should get a resulting line that lists the path set for the OS. My path is ‘/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin’. A colon :)) separates each directory. There are nine entries in my path.

When I perform a search, it starts with the folder ‘/usr/local/sbin’ and then so on to the end of the list.

If we find the file, then the program lists its path, and the program stops searching, unless you use the ‘-a’ parameter to search all paths and list all programs found. The ‘-a’ command can be useful if you make a program yourself and attempt to run it, and another program executes. You can see how many files have the same name and the order in which they will run.

To use ‘which’, just type the command ‘which <-a> <filename>’. Here, the ‘-a’ is not required, but a filename is required.

Let’s do a test. I ran the command ‘which which’. This will list the folder where the program found ‘which’. The result on my system is:

/usr/bin/which

This tells me that the program is in the folder ‘/usr/bin/’. The folder is the fourth listed in my path.

For fun, let’s try ‘which -a which’. Now we can see if there is more than one file with executable permissions named ‘which’. The result is:

/usr/bin/which
/bin/which


As stated before, the first file found is the fourth item in the path. The second file found is the sixth item in the path. It found the files in order of the path.

If you want to change the path, you perform an ‘export’ command:

export PATH=$PATH:/home/jarret/Scripts

The command will add the folder ‘/home/jarret/Scripts’ to the end of the path list. We can place is at the beginning of the path by just moving the new folder before the current path ($PATH):

export PATH=/home/jarret/Scripts:$PATH

If you want to reorder the path, then you can echo the path, copy it and then paste it to change things around:

export /usr/local/sbin:/usr/local/bin:/usr/sbin:/home/jarret/Scripts:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Just make sure you do not remove any necessary folders from the path or the system cannot locate programs and executed when needed.

To make the new path permanent, you need to add the ‘export’ line to two files by using the two commands:

gedit .profile
gedit .bashrc


Add the export line at the bottom of the files. If an export line already exists, replace it. To make it active, you need to log out and back into the OS.

Whereis

Whereis will look for a file, even if it isn’t executable. The files it looks for include binary, source code and manual pages. The folders it searches are hard-coded into the program. To see the list of hard-coded folders, use the command ‘whereis -l’. This list is long, so I will not list it here. You can run the command on your system to see the list.

The command is basically ‘whereis <filename>’. There are some options we can add, but let’s start with the basics.

When we covered the ‘which’ command, we tried to find the location of the ‘which’ command itself. We can try it now with ‘whereis’. The command ‘whereis which’ returns:

which: /usr/bin/which /bin/which /usr/share/man/man1/which.1.gz

It returned the same locations, but also found the ‘man’ pages at ‘/usr/share/man/man1’.

You may notice that the line starts with the filename ‘which:’ followed by the findings. You can list multiple filenames, separated by a space, to perform multiple searches at once. Say I wanted to find both ‘which’ and ‘whereis’. I could use the command ‘whereis which whereis’ to get the following result:

which: /usr/bin/which /bin/which /usr/share/man/man1/which.1.gz
whereis: /usr/bin/whereis /usr/share/man/man1/whereis.1.gz


If you only want to list the binaries (compiled code) use the ‘-b’ option. For only the source code, use the ‘-s’ option. To find only the ‘man’ pages, use the option ‘-m’.

So, to find the executable (compiled code that is set with execute permissions) for ‘which’, perform the command:

whereis -b which

The command on my system returned the following line:

which: /usr/bin/which /bin/which

The ‘whereis’ command searches more folders than the ‘which’ command searches and returns more file types. With the option of ‘-b’, it can perform the same, but may find a file that exists outside the path.

Type

The ‘type’ command allows you to find out information about a file. It will, by default, tell you if the command is a built-in command, binary or alias.

For example, try the following in a terminal:

type cd
type which
type ls
type while


The first command shows that ‘cd’ is a built-in shell command.

The second command lists ‘which’ as a hashed file, then shows its location.

The third command results in an alias. The ‘ls’ command is aliased to the command ‘ls --color=auto’.

The last command is checking for the ‘while’ command that is used in the BASH scripts. The result shows that it is a ‘shell keyword’.

The command ‘type’ will return the first instance of the file it finds. You can search for more instances by using the ‘-a’ option.

Whatis

The ‘whatis’ command does not locate files as the other commands do, but lists a description of what the command does. The information comes from the man pages and using different options, you can vary the output information.

Let’s initially give this a try. We started with the which command, so let’s try ‘whatis which’. The output is:

which (1) - locate a command

A listing can show more than one entry, such as ‘whatis arp’. You get the following:

arp (7) - Linux ARP kernel module.
arp (8) - manipulate the system ARP cache


For a description, the definition is better from section 8. We can display a specific section by using the option ‘-s’ and the number of the section or sections in double quotes. Such as the following:

whatis -s “8” arp

If you want to specify more than one section, separate them with a comma (,).

If we assume that there is a command you do not remember, but you recall it starts with ‘ch’, you can perform a wildcard search:

whatis -w “ch*”

The result is:

chacl (1) - change the access control list of a file or directory
chage (1) - change user password expiry information
chardet (1) - universal character encoding detector
chardet3 (1) - universal character encoding detector
chardetect (1) - universal character encoding detector
chardetect3 (1) - universal character encoding detector
charmap (5) - character set description file
charsets (7) - character set standards and internationalization
chat (8) - Automated conversational script with a modem
chattr (1) - change file attributes on a Linux file system
chcon (1) - change file security context
chcpu (8) - configure CPUs
check-language-support (1) - returns the list of missing packages in order to provide a complete language environment
checkXML5 (1) - An XML lint tool for KDE DocBook XML documents.
cheese (1) - tool to take pictures and videos from your webcam
chfn (1) - change real user name and information
chgpasswd (8) - update group passwords in batch mode
chgrp (1) - change group ownership
chmem (8) - configure memory
chmod (1) - change file mode bits
tc-choke (8) - choose and keep scheduler
chown (1) - change file owner and group
chpasswd (8) - update passwords in batch mode
chroot (8) - run command or interactive shell with special root directory
chrt (1) - manipulate the real-time attributes of a process
chsh (1) - change login shell
chvt (1) - change foreground virtual terminal


Now you can easily scan over the list and find your command.

Conclusion

These commands can be useful for finding files on your system.

Keep these in mind when you are searching for something, or a file isn’t running properly. For example, a command is running an older version than you want. You can determine where the command files are located. The newer version of the file may be located later in the path list, causing the older version to be executed.

Definitely be aware of these commands.
 


wizardfromoz

Administrator
Staff member
Gold Supporter
Joined
Apr 30, 2017
Messages
7,779
Reaction score
6,605
Credits
28,603
Nice article Jarret, but wondering if you are also going to cover commands such as

locate

from mlocate and plocate

... or if there is a "Linux Finding Files II" in the wings, I will zip my lip.

I use locate all the time.

Avagudweegend

Wizard
 

Dart

Member
Joined
Jan 5, 2022
Messages
65
Reaction score
60
Credits
778
I also use locate a LOT. Just trying which, it seems to be quite a bit quicker. I can see an advantage in using which, whereis & whatis. It'll end up saving me a bit of time. For example, I don't have a "stock" format for shell scripts. Each is individually written from scratch. They only contain what's necessary. Sometimes I have to put in some commands that are off the beaten path to make sure they're caught. I like to put the OS commands into variables with the full path. Which... Is perfect for this I.E. pass=`which randpass.` This is perfect for running on slightly different versions the OS where files aren't always in the same place. (Why oh why hadn't I thought of this before!) (8^0> Ya learn something new every day LOL!

Great article Jarret! Thanks for the info!
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Linux.org Hosting Donations
Consider making a donation

Members online

No members online now.

Latest posts

Top