104.7. Find system files and place files in the correct location

Weight: 2

Description: Candidates should be thoroughly familiar with the Filesystem Hierarchy Standard (FHS), including typical file locations and directory classifications.

Key Knowledge Areas:

  • Understand the correct locations of files under the FHS

  • Find files and commands on a Linux system

  • Know the location and purpose of important file and directories as defined in the FHS

Terms and Utilities:

  • find

  • locate

  • updatedb

  • whereis

  • which

  • type

  • /etc/updatedb.conf

There are over 200 Linux distributions available , all having a lot of things in common, there should be a standard way to place files in the system, and that's where FHS comes to play.


The Filesystem Hierarchy Standard (FHS) is a document that specifies a common layout of directories on a Linux/ UNIX system.




Essential command binaries


Static files of the boot loader


Device files


Host-specific system configuration


Essential shared libraries and kernel modules


Mount point for removable media


Mount point for mounting a filesystem temporarily


Add-on application software packages


Essential system binaries


Data for services provided by this system


Temporary files


Secondary hierarchy


Variable data


User home directories (optional)


Alternate format essential shared libraries (optional)


Home directory for the root user (optional)

The /usr and /var hierarchies are complex enough to have complete sections of the FHS devoted to them.

The/usrContains binaries, libraries, documentation, and source-code for second level programs.

  • /usr/bin contains binary files for user programs.

  • /usr/sbin contains binary files for system administrators.

  • /usr/lib contains libraries for /usr/bin and /usr/sbin

  • /usr/local contains users programs that you install from source.

  • /usr/src holds the Linux kernel sources, header-files and documentation.

The /var filesystem contains variable data files, including spool directories and files, administrative and logging data, and transient and temporary files. Some portions of /var are not shareable between different systems, but others, such as /var/mail, /var/cache/man, /var/cache/fonts, and /var/spool/news, may be shared.


When you run a program at the command line, the bash (or other) shell searches through a list of directories to find the program you requested. The list of directories is specified in your PATH environment variable.

[email protected]:~# echo $PATH
[email protected]:~$ echo $PATH

As you can see, the PATH variable is just a list of directory names, separated by colons. Note the differences between the user path and root path

As we said in previous sections we can change our path with export PATH=$PATH:/new/path/dir or by adding thant inside .bash_profile or .bashrc .

which, type, and whereis


The which command to search your path and find out which command will be executed (if any) when you type a command.

[email protected]:~# which ping

The which command shows you the first occurrence of a command in your path. If you want to know if there are multiple occurrences, then add the -a option


There are some commands that the which command will not find, such as shell builtins.

[email protected]:~# which for
[email protected]:~# type for
for is a shell keyword
[email protected]:~# type type
type is a shell builtin

The type command is a builtin that understand bash keywords and can tell us how a given command string will be evaluated for execution.


If you want more information than just the location of a program, you can use the whereis command.

[email protected]:~# whereis ping
ping: /bin/ping /usr/share/man/man8/ping.8.gz
[email protected]:~# whereis mkfs
mkfs: /sbin/mkfs.ext4dev /sbin/mkfs /sbin/mkfs.minix /sbin/mkfs.ext2 /sbin/mkfs.ext4 /sbin/mkfs.ext3 /sbin/mkfs.fat /sbin/mkfs.bfs /sbin/mkfs.cramfs /sbin/mkfs.ntfs /sbin/mkfs.vfat /sbin/mkfs.msdos /usr/share/man/man8/mkfs.8.gz

The whereis command can also search for man pages and source codes of programs alongside their binary location .


In an earlier tutorial in this series "104-7", you learned how to find files based on name (including wildcards), path, size, or timestamp. In another earlier tutorial in this series, “104-6” you learned how to find the links to a particular file or inode.

The find command is the Swiss Army knife of file-searching tools on Linux systems. Two other capabilities that you may find useful are its ability to find files based on user or group name and its ability to find files based on permissions.

Finding by Owner and Permissions

We can also search for files by the file owner or group owner (discussed in "104-5"). We do this by using the -user and -group parameters respectively.

### finding bu user
[email protected]:~# find /tmp -user user1 | head
### finding by group
[email protected]:~/test-space# find /etc -group shadow

use -nouser or -nogroup to search for a file with no user or with no group id.

We can also search for files with specific permissions. If we want to match an exact set of permission find / -perm 644

If we want to specify anything with at least those permissions:find / -perm -644 .

filtering by depth

We can specify the maximum depth of the search under the top-level search directory:

find -maxdepth num -name query
find -mindepth num -name query

Also it is possible to combine the min and max depth parameters to focus in on a narrow range find -mindepth num -maxdepth num -name file:

[email protected]:~# find /tmp -mindepth 1 -maxdepth 2 -user user1 | head

Like other tests, you can add a ! just before any phrase to negate it. So this will find files not belonging to user1 : find . ! -user user1

locate & updatedb

The find command searches all the directories you specify, every time you run it. To speed things up, you can use another command, locate, which uses a database of stored path information rather than searching the filesystem every time.


The locate command searches for matching files in a database that is usually updated daily (by cron job).

[email protected]:~# locate bin/ls

The locate command matches against any part of a path name, not just the file name.


The default database used by locate is stored in the /var filesystem, in a location such as /var/lib/locatedb. This may be different on systems that use slocate or mlocate packages to provide additional security or speed. You can find statistics on your locate database using locate-S :

[email protected]:~# locate -S
Database /var/lib/mlocate/mlocate.db:
25,916 directories
251,926 files
13,669,511 bytes in file names
6,067,728 bytes used to store database

The database is created or updated using the updatedb command. (This is usually run daily as a cron job).

[email protected]:~# updatedb

use -v for verbose mode to see what is going on after updatedb command!

The file /etc/updatedb.conf, or sometimes /etc/sysconfig/locate, is the configuration file for updatedb:

# PRUNENAMES=".git .bzr .hg .svn"
PRUNEPATHS="/tmp /var/spool /media /home/.ecryptfs /var/lib/schroot"
PRUNEFS="NFS nfs nfs4 rpc_pipefs afs binfmt_misc proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs mfs shfs sysfs cifs lustre tmpfs usbfs udf fuse.glusterfs fuse.sshfs curlftpfs ecryptfs fusesmb devtmpfs"

There are some PRUNING on the configuration file which cause locate never search for those kinds of files or directories like /tmp or /var/spool . You can let locate to search for them too if you like by manipulating this file.

that's all.

Congratulation we have done lpic1-101 !!!