A Little About Linux: How Do I Find Where Things Are?

So, you've logged on to a Linux console for the first time and you want to be able to actually navigate around and find files and applications.  If you've come from a Windows background, that doesn't seem too much of a task and to be fair, even in Linux, it's much better now than it used to be.  If I were writing something about the layout of the Linux file system back in the 1990s, I would be using phrases like "this partition holds application binaries, but this partition also holds application binaries as does this", or "configuration files can exist here, or anywhere the developer wants".  In other words, lots of files were in different places depending on the Linux distribution you were using and depending on the decision of the developer for each application installed.

Back then distributions did tend to put files and folders where they wanted to a certain extent and it wasn't a great time for the newbie.  Then it started to change when the Filesystem Hierarchy Standard (FHS) was adopted and by the time the first couple of years of the 21st Century had arrived, you could skip from one distribution to another and have a fairly good idea that you were going to find the same files, in the same place, at the same point in the hierarchy (you can look at the original FHS website to find out about the decisions they made.)  From the point when FHS was created in 1997, all distributions and developers started to adopt this standard and by the mid-21st century it's probably fair to say that most Linux distributions were adhering to this layout.  Now we're on version 2.3 of FHS and if you perform a list operation on the root partition of any modern Linux distribution, you will see this;

The root directory is the top of the hierarchy.  All other directories are below this.  This is not to be confused with /root.
This contains core executables that manage the Linux environment and are available to all users. e.g. cp, ls, cat, mount.
This contains files related to the bootloader e.g. kernels, GRUB and efi.

As Linux generally treats devices as files, any of the hardware interfaces will appear in this directory.   

This directory holds static configuration files only.  It should not contain binaries.

This directory is where user data (personal files and user specific settings) sit.

Libraries that are required for the executables in /bin and /sbin. 

As with /lib but containing 64 bit libraries.

This contains any mount points for removable media such as CD-ROMs/DVDs. Normally anything that is auto-mounted is here.

Any devices that are temporarily mounted are located under here.

Any pre-compiled and/or packaged applications that don't come with the distribution go here.

This directory is a virtual filesystem which is created and managed by Linux to access kernel and process information as files.

This is the home directory of the root user.  Not to be confused with / the root directory.

Contains programs run by the System Administrator.
Contains sub-directories for system specific services.  Used so that users can find the location of data files for a particular service.

Another virtual filesystem which contains kernel exported data.

A place for applications to create temporary files.  Scheduled routines in the distribution usually tidy this up automatically.

Any other applications used to make up the Linux distribution tend to go here.  They can be mounted as read-only and used by multiple users and systems.  /usr normally has a mirror of some top level directories underneath e.g. /usr/local (Any System Administrator complied and installed applications), /usr/bin (Non-essential executables for all users), /usr/sbin (Non-essential system related binaries), /usr/lib (Libraries for /usr/bin and /usr/sbin), /usr/src (Kernel source code). 

Transient or variable files which will change regularly during the operation of the system e.g. logs, spooler files, temporary email content.

Tools to Find Things
There are a few command line tools you can use to find things on the file system.  They are very useful and I really don't think you can live on the Linux command line without knowing at least some of them.

find - This is the most powerful and useful tool.  You give it a starting point in the file system (if you have no idea where the file is / is a good option), then you may wish also to supply additional parameters and it simply moves down the file system hierarchy to find files that match.  Useful options are;

Find a specific file name, starting the search from the root level
find / -name sausage.txt

Find a specific file name, starting the search from the current location
find / -name sausage.txt

Find all files that end in .sh regardless of case
find / -iname *.sh

The -perm parameter allows you search for permission related cases e.g. finding all files set as 777
find / -type f -perm 0777 -print 

There are also ways of finding files based on time.  These usually consist of specifying a letter which corresponds to what you are looking for; Last Accessed (a), Last Modified (m), Last Changed (c) and then the time period; 'min' for minutes and 'time' for searching over 24 hour periods.

Find files changed in the last 10 minutes starting from /
find / -cmin 10 -print

Find files modified in the last 7 days starting from current directory
find . -mtime -7 -print

Find files modified longer than 7 days ago
find / -mtime +7 -print

Finally for this article (although there more options within the command) there is the size parameter when you're looking for files of a specific... size (yes, it's that logical)

Find all files greater than 100MB in size
find / -size +100M

locate - Essentially this is a simpler version of find, with some specific differences such as the fact that you don't have as many search parameters and that it's much faster as it uses a database to keep track of where the files are. Some examples of search parameters for locate are;

Find all files ending in .sh regardless of case, and print their locations
locate -i *.sh

Find all files ending in .sh but only print the number of files returned
locate -ic *.sh

The database for the locate command is controlled by using updatedb, so you can run updatedb on its own to update the locate database.  /etc/updatedb.conf allows you to configure what type of files and directories are excluded in the search database.

whereis - This command searches standard binary and library locations to find the path to a file;

To find out where the cp (copy) command is located;
whereis cp
cp: /bin/cp /usr/share/man/man1p/cp.1p.gz /usr/share/man/man1/cp.1.gz

which - This command is fairly limited in use, it only returns the path and alias of the command if configured, but only searches along your existing path.  It could however be useful if you need to find a path within a shell script;

which cp
alias cp='cp -i'


type - This command returns whether a command is built-in, external or an alias in terms of how it is interpreted when run;

type type
type is a shell builtin

type less
less is /usr/bin/less

type cp
cp is aliased to `cp -i'

No comments: