Linux File System Hierarchy

Linux: Filesystem Hierarchy

LINUX FILESYSTEM

Linux Filesystem is an inverted tree structure which consists folders and files. At the root of the filesystem is ‘/’ (forward slash). Everything under Linux is kept inside ‘/’.

When the disk is partitioned, the partitions are attached to one (or more) of the directories in the Linux filesystem. The process of attaching a partition to the directory is called ‘Mounting’.

Linux: Filesystem Hierarchy
Linux: Filesystem Hierarchy

 

FILESYSTEM HIERARCHY STANDARDS

Detailed information about the Linux Filesystem Hierarchy Standards is available in manual page – hier.

/       Root of Linux Filesystem

/bin Binary Executable files are kept here

/boot Booting related files are kept here

/dev Device files are kept here

/etc System-wide configuration files are kept here

/home Location for the home directories of regular users

/lib64 Libraries for binary executables are kept here

/mnt Temporary mount point

/opt Optional Programs are installed here

/proc Kernel pseudo filesystem

/root Home directory of super user root

/sbin System Binary Executable files are kept here

/tmp Temporary files are kept here

/usr User Filesystem

/var Variable files are kept here

HOW TO CREATE PATHNAMES OF A FOLDER OR FILES IN LINUX?

Linux files can be accessed by either Absolute Path or Relative Path

Absolute Path

It is the complete path to any file or directory. It always starts with a ‘/’.

Relative Path

A Path which is relative to current working directory. Some special characters used at the starting of Relative Path are –

  • .       Current Directory
  • ..      Parent Directory
  • ~     Home Directory
  • –      Previous Directory

Linux Tutorials: relative-path

HOW TO NAVIGATE INTO FILESYSTEM IN LINUX?

To navigate Linux filesystem following three commands are needed.

  • pwd – To print current working directory.
  • ls – To List directory contents.
  • cd – To change Directory.
$ pwd
$ ls
$ cd /
$ cd /etc
$ cd /var/spool/mail
$ ls /var/log
$ cd ~
$ cd .
$ cd ..
$ cd ../etc/ssh
$ cd ../../var/spool/mail
$ cd ../../boot/grub2
$ cd -
$ cd

 

KNOW ABOUT FILE CREATION IN LINUX

Files can be created with ‘touch’ command.

$ touch foo.txt
$ ls
$ touch bar.txt /tmp/ace.txt ../../tmp/mono.txt
$ ls

Actually touch is not used to create a file but is used to change the timestamps of a file or a directory.

$ file foo.txt
$ stat foo.txt
$ touch foo.txt
$ stat foo.txt

The file command displays the type of a file based on certain tests including the pattern matching as per the patterns stored in /usr/share/misc/magic file.

The stat command displays the file statistics including the data from inode table.

KNOW ABOUT DIRECTORY CREATION IN LINUX

Directories can be created with ‘mkdir’ command.

# mkdir /testdir
$ mkdir -v /tmp/testdir
$ mkdir -pv testdir/nesteddir

Option v displays a message that the directory is created.
Option p will create the parent directory tree if does not exist.

WHAT IS PATTERN MATCHING OR GLOBBING IN LINUX?

BASH has pattern matching or wildcards feature making managing files easier. It is also called globbing. Globbing expands the wildcards into a list of matching path names.

*		Any string of 0 or more characters
?		Any single character
~		Current user's home directory
~username	Given user's home directory
[abc]		Any one character in enclosed class
[!abc]		Any one character not in enclosed class
[[:alpha:]]	Any alphabetic character
[[:digit:]]	Any digit, 0-9
[[:lower:]]	Any lower case alphabetic character
[[:upper:]]	Any upper case alphabetic character

The following commands will help us understand the globbing.

$ mkdir glob; cd glob
$ touch alfa bravo charlie delta echo able baker cast dog easy
$ ls
$ ls a*
$ ls *a*
$ ls [ac]*
$ ls ????
$ ls ?????
$ echo anushesh | tr [[:lower:]] [[:upper:]]

Tilde expansion

$ ls ~/glob
$ echo ~/glob

Brace expansion

$ echo {a,b,c,d,e}.log
$ echo file{1..5}.txt
$ echo file{a..e}.txt
$ echo file{1,2}{a,b}.txt
$ echo file{a{1,2},b{3,4},c}.txt

Command Substitution

$ echo "Today is `date +%A`"
$ echo "The time is $(date +%M) minutes past $(date +1%p)."

Sometimes we do not want the arguments to expand and use them literally. In such cases, we use backslash.

$ echo $SHELL
$ echo \$SHELL
$ echo '$SHELL'
$ echo "$SHELL"

HOW TO REMOVE FILES AND DIRECTORIES IN LINUX?

Files can be removed with ‘rm’ command.

# rm /testfile
$ rm -v /tmp/testfile
$ rm -vf testfile

Empty directories can be removed with ‘rmdir’ command. To remove non-empty directories use ‘rm’ command.

# rmdir /testdir
$ rmdir -v /tmp/testdir
$ rm -rvf testdir
Option f forcefully implies the operation.
Option r will recursively traverse the directory and remove files from inside-out.
Files/Directories can be copied with cp command.

HOW TO COPY FILES AND DIRECTORIES FROM ONE LOCATION TO ANOTHER?

Files/Directories can be copied with cp command.
$ mkdir ~/cptest
$ cd ~/cptest
$ touch file{1,2,3,4,5,6,7,8,9}
$ mkdir dir{1..9}
$ cp file1 dir1
$ cp -v file2 file3 dir2
$ cp -v file3 newfile
$ cp -v f* dir3
$ cp -v [nf]* dir4
$ cp -rv dir1 dir5
$ cp -rv dir2 dir3 dir6
$ cp -rv dir4 newdir
$ cp -rv d*
We are using regular expression for creating files and directories.
file{1,2,3,4,5,6,7,8,9} will first expand into file1 file2 file3 file4 file5 file6 file7 file8 file9 and then it will be executed by touchthereby creating nine files. Similarly, nine directories will be created.

HOW TO MOVE FILES AND DIRECTORIES IN LINUX?

Files/Directories can be moved with commandmv.

$ mkdir ~/mvtest
$ cd ~/mvtest
$ touch file{1..9}
$ mkdir dir{1..9}
$ mv file1 dir1
$ mv -v file2 file3 dir2
$ mv -v file4 newfile
$ mv -v [nf]* dir4
$ mv -v dir1 dir5
$ mv -v dir2 dir3 dir6
$ mv -v dir4 newdir
$ mv -v d*

READING FILES

To read a regular file we have many commands. cat command is used mostly to read a file.
wc is used to count the lines, words and characters in the file.
nl is used to number the lines of a file but it does not count blank lines.

 

$ cat /etc/issue
$ cat /etc/hosts
$ cat /etc/issue /etc/hosts
$ cat -n /etc/issue
$ wc /etc/issue
$ nl /etc/issue
$ tac /etc/issue
$ cat /etc/services

Option n of cat will display the file with line numbers just like nl command.

READING FILE WITH PAGERS

It is not a good idea to read a huge file with cat command as it dumps the whole file on screen and screen has limited buffer so it is not possible to read the whole file. Instead, we use pagers. There are two of them – more and lessmore does not support for navigation keys but less does.

$ more /etc/services
$ less /etc/services

Return key scrolls down 1 line, Spacebar scrolls down 1 page and ‘b’ scrolls up 1 page in both more and less.

READING LIMITED LINES OF FILE

It is also possible to read limited number of lines from beginning of the file or end of the file with head and tail respectively.

$ head /etc/services
$ head -n 4 /etc/services
$ tail /etc/services
$ tail -n 2 /etc/services

Option n with both head and tail is used to specify the number of lines to display.

SEARCHING TEXT IN FILE

 To search text in a file use grep command.
$ grep root /etc/passwd
$ grep http /etc/services
$ grep ^http /etc/services
$ grep http$ /etc/services
$ grep ^$ /etc/services

Symbol ^ indicates the start of line and symbol $ indicates the end of line.