Linux: Finding Files | Linux.org

Table of Contents

Linux: Finding Files | Linux.org

Linux: Finding Files

Jarret B

Active Member
Staff member
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.

I work at home and I have four children at home: Eilly, Alyse, Morgan and Grant. Three kids old enough to live on their own: Devyn, Logan and Caleb.

I had a book come out called EPUB: From the Ground Up.

You can go to Amazon or Barnes and Noble to order a copy.

You can contact me through Linux.org or at [email protected].

Tags:
June 20, 2022 at 01:00AM
Open in Evernote