Powered By GitBook
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.

FHS

The Filesystem Hierarchy Standard (FHS) is a document that specifies a common layout of directories on a Linux/ UNIX system.
Directory
Purpose
bin
Essential command binaries
boot
Static files of the boot loader
dev
Device files
etc
Host-specific system configuration
lib
Essential shared libraries and kernel modules
media
Mount point for removable media
mnt
Mount point for mounting a filesystem temporarily
opt
Add-on application software packages
sbin
Essential system binaries
srv
Data for services provided by this system
tmp
Temporary files
usr
Secondary hierarchy
var
Variable data
home
User home directories (optional)
lib
Alternate format essential shared libraries (optional)
root
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.

PATH

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.
1
[email protected]:~# echo $PATH
2
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
Copied!
1
[email protected]:~$ echo $PATH
2
/home/user1/bin:/home/user1/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Copied!
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

which

The which command to search your path and find out which command will be executed (if any) when you type a command.
1
[email protected]:~# which ping
2
/bin/ping
Copied!
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

type

There are some commands that the which command will not find, such as shell builtins.
1
[email protected]:~# which for
2
[email protected]:~# type for
3
for is a shell keyword
4
[email protected]:~# type type
5
type is a shell builtin
Copied!
The type command is a builtin that understand bash keywords and can tell us how a given command string will be evaluated for execution.

whereis

If you want more information than just the location of a program, you can use the whereis command.
1
[email protected]:~# whereis ping
2
ping: /bin/ping /usr/share/man/man8/ping.8.gz
3
4
[email protected]:~# whereis mkfs
5
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
Copied!
The whereis command can also search for man pages and source codes of programs alongside their binary location .

find

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.
1
### finding bu user
2
[email protected]:~# find /tmp -user user1 | head
3
/tmp/gnome-software-ZPJTE0
4
/tmp/gnome-software-ZPJTE0/debconf.socket
5
/tmp/gnome-software-YDX3E0
6
/tmp/gnome-software-YDX3E0/debconf.socket
7
/tmp/gnome-software-GD8NE0
8
9
### finding by group
10
[email protected]:~/test-space# find /etc -group shadow
11
/etc/shadow
12
/etc/gshadow
Copied!
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:
1
find -maxdepth num -name query
Copied!
1
find -mindepth num -name query
Copied!
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:
1
2
[email protected]:~# find /tmp -mindepth 1 -maxdepth 2 -user user1 | head
3
/tmp/gnome-software-ZPJTE0
4
/tmp/gnome-software-ZPJTE0/debconf.socket
5
/tmp/gnome-software-YDX3E0
6
/tmp/gnome-software-YDX3E0/debconf.socket
7
/tmp/gnome-software-GD8NE0
8
/tmp/gnome-software-GD8NE0/debconf.socket
9
/tmp/gnome-software-U5COF0
10
/tmp/gnome-software-U5COF0/debconf.socket
Copied!
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.

locate

The locate command searches for matching files in a database that is usually updated daily (by cron job).
1
[email protected]:~# locate bin/ls
2
/bin/ls
3
/bin/lsblk
4
/bin/lsmod
5
/sbin/lsmod
6
/sbin/lspcmcia
7
/usr/bin/lsattr
8
/usr/bin/lsb_release
9
/usr/bin/lscpu
10
/usr/bin/lsdiff
11
/usr/bin/lshw
12
/usr/bin/lsinitramfs
13
/usr/bin/lsipc
14
/usr/bin/lslocks
15
/usr/bin/lslogins
16
/usr/bin/lsof
17
/usr/bin/lspci
18
/usr/bin/lspgpot
19
/usr/bin/lsusb
20
/usr/lib/klibc/bin/ls
21
/usr/lib/plainbox-provider-checkbox/bin/lsmod_info
22
/usr/lib/plainbox-provider-resource-generic/bin/lsb_resource
Copied!
The locate command matches against any part of a path name, not just the file name.

updatedb

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 :
1
[email protected]:~# locate -S
2
Database /var/lib/mlocate/mlocate.db:
3
25,916 directories
4
251,926 files
5
13,669,511 bytes in file names
6
6,067,728 bytes used to store database
Copied!
The database is created or updated using the updatedb command. (This is usually run daily as a cron job).
1
[email protected]:~# updatedb
Copied!
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:
1
PRUNE_BIND_MOUNTS="yes"
2
# PRUNENAMES=".git .bzr .hg .svn"
3
PRUNEPATHS="/tmp /var/spool /media /home/.ecryptfs /var/lib/schroot"
4
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"
Copied!
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 !!!

.
.
.
.
Last modified 1yr ago