Linux Directory Structure
Linux Directory Structure
Linux Directory Structure
Chapter 3:
Linux Directory Structure
- 0 -
Linux Platform – I Chapter: 3
Title Page
1. Definitions 4
4. Inodes 21
- 1 -
Linux Platform – I Chapter: 3
Objectives
Upon completion this chapter, the student should be able to :
1. Describe important elements for navigating Linux filesystems;
2. Create, copy, move, and remove files and directories ;
3. Look for files and directories using file globbing.
Keywords
FHS, Root Directory (/), Home Directory (~), cd, Absolute Path, Relative Path, File
Globbing, find, locate, touch, mkdir, cp, mv, rm, rmdir, inode, hard link, soft link, ln.
- 2 -
Linux Platform – I Chapter: 3
1. Definitions
The Linux Foundation maintains a Filesystem Hierarchy Standard (FHS) that defines
the Linux directory structure. For that reason, you find the same directory structure in
(almost) all the Linux distributions .
Root:
The base of the Linux file system hierarchy begins at the root. Directories branch off
the root, but everything starts at root. The (/) character is the symbol of the root
directory; it is also reserved as the separator between files and directories in a
pathname.
Directory:
it is a file containing entries; every entry is a named reference to a file. Therefore, a
directory is a mapping between the human name for the file and the filesystem’s
reference .
Files:
A file consists of a series of bytes; filesystems impose no structure on files’ contents.
Data files, executable programs, text files and shared libraries are all stored as regular
files. One difference between Linux file names and those of many other operating
systems is that Linux file names are case-sensitive. In other words, Filename.sh is
different from filename.sh or FILENAME.SH; all three files can co-exist in the same
directory. Although it is possible to create these files, it may be unwise to do so, as it
may confuse you later. There is no need to use “.” (dot) in a filename. Sometime dot
improves readability of filenames. You can use dot based filename extension to identify
file. For example, “.sh” extension to identify Shell files and “.tar.gz” to identify
compressed archives.
In most Linux distributions, file names may be up to 255 characters (255 bytes), they
could consist of alphabet upper and lowercase letters, numbers, and certain symbols
such as “.” (dot), and “_” (underscore). All other characters, except the forward slash
( / ) character, are valid .
- 3 -
Linux Platform – I Chapter: 3
It is often unwise to use certain special characters in file names such as: > < ? * "
| : & ) ( ; ,as well as spaces, tabs and other non-printable characters. Each one of
these symbols must be quoted or escaped using backward slash ( \ ) symbol. For
instance:
If you don’t write the quotes, you would be asking the system to list three different
files.
- 4 -
Linux Platform – I Chapter: 3
[student@StudentHost ~]$ ls /
In the enclosed table, you find a list of important directories and descriptions about
their contents:
Directory Description
It is accessible to all users and contains the most important commands that
/bin
ordinary users might issue such as: ls, cp
It’s a temporary location for CD-ROMs inserted in the system. However, the
/cdrom
standard location for temporary media is inside the /media directory.
/dev It contains a large number of device files that function as hardware interfaces.
- 5 -
Linux Platform – I Chapter: 3
/lib It holds the libraries needed by the binaries in /bin and /sbin directories
If the file system crashes, a file system check will be performed at next boot.
/lost+found Any corrupted files found will be placed in the lost+found directory, so you
can attempt to recover as much data as possible
It is fairly new, and gives applications a standard place to store transient files
/run they require like sockets and process IDs. These files can’t be stored in /tmp
because files in /tmp may be deleted
This is similar to the /bin directory. The only difference is that is contains the
/sbin
binaries that can only be run by root or a sudo user.
- 6 -
Linux Platform – I Chapter: 3
It contains “data for services provided by the system.” If you were using the
/srv Apache HTTP server to serve a website, you’d likely store your website’s
files in a directory inside the /srv directory.
It is usually used by applications for storing temporary files. Protecting /tmp
from the rest of the hard disk by placing it on a separate partition can ensure
/tmp
that applications are able to complete their processing, even if the rest of the
disk fills up.
It contains most of the applications and utilities available to CentOS or RHEL
users. Having /usr on a separate partition lets you mount that file system as
Read Only after the operating system has been installed. This prevents
/usr attackers from replacing or removing important system applications with their
own versions that may cause security problems. A separate /usr partition is
also useful if you have diskless workstations on your local network. Using
NFS, you can share /usr over the network with those workstations
cd command
The cd (change directory) command is used to change the current working directory
to another directory. Here are some basic uses for the cd command:
Command changes to
cd your home directory
cd ~ your home directory
cd - the last working directory
cd Desktop the subdirectory Desktop
cd ./Desktop the subdirectory Desktop
cd .. the parent directory
cd /usr/local/lib the directory lib specified by an absolute path name
cd ../home/rami the directory rami specified by a relative path name
- 7 -
Linux Platform – I Chapter: 3
File globbing
File globbing [3] is “a feature provided by the UNIX/Linux shell to represent multiple
filenames by using special characters called wildcards with a single file name. A
wildcard is essentially a symbol which may be used to substitute for one or more
characters. Therefore, we can use wildcards for generating the appropriate
combination of file names as per our requirement”. The following table contains file
globbing (or dynamic filename generation) provided by bash shell:
Character matches
* 0 or more characters
? exactly one characters
[...] exactly one of the included characters
[a-z], [3-6] any character from a to z, from 3 to 6
[^...] or exactly one of the excluded characters
[!...]
- 8 -
Linux Platform – I Chapter: 3
[student@StudentHost Test]$ ls
file1 file2 file3 file44 fileab fileac fileB filed
[student@StudentHost Test]$ ls file*
file1 file2 file3 file44 fileab fileac fileB filed
[student@StudentHost Test]$ ls fil*44
file44
[student@StudentHost Test]$ ls file?
file1 file2 file3 fileB filed
[student@StudentHost Test]$ ls file?4
file44
[student@StudentHost Test]$ ls file?b
fileab
[student@StudentHost Test]$ ls file??
file44 fileab fileac
[student@StudentHost Test]$ ls file[a4][!z]
file44 fileab fileac
[student@StudentHost Test]$ ls file[^vb][!z]
file44 fileab fileac
[student@StudentHost Test]$ ls file[^vb][^z]
file44 fileab fileac
[student@StudentHost Test]$ ls
file1 file2 file3 file44 fileab fileac fileB filed
[student@StudentHost Test]$ ls [[:alpha:]]ile2
file2
[student@StudentHost Test]$ ls file[[:digit:]]
file1 file2 file3
[student@StudentHost Test]$ ls file[^[:digit:]]?
fileab fileac
find command
This command locates a specific file or group of files, it searches the directory tree for
files meeting a specified criteria (options). The search is done recursively in the sub-
- 9 -
Linux Platform – I Chapter: 3
directories. File Globbing could be used with this command. The syntax of find
commas is:
The default directory is ".", the default option is "every file", and the default action is
"print" (the filename), so running the find command without arguments will simply
descend the current directory, printing every filename. If given a directory name as a
single argument, the same would be done for that directory. The following table
summarizes some of the more common search options:
option Description
-empty File is a directory or regular file, and is empty.
-group gname File is owned by the group gname.
-inum n File has an inode number n.
-links n File has n links.
-mmin n File was last modified n minutes ago.
-mtime n File was last modified n days ago.
-name pattern File's name matches the file glob pattern.
-newer File was modified more recently than filename.
filename
-perm mode File's permissions are exactly mode.
-perm -mode All of the permission bits mode are set for the file.
-perm /mode Any of the permission bits mode are set for the file.
-size n File has a size of n.
-type c File is of type c, where c is "f" (regular file), "d" (directory), or "l" (soft link).
- 10 -
Linux Platform – I Chapter: 3
You can also specify what you would like done to files that meet the specified criteria.
If no criteria is specified, the file name is printed to standard out (terminal window),
one file per line. Other options, that you can use, are shown in the following table:
Switch Action
Execute command on matching files. Use {} to indicate where filename
-exec command ;
should be substituted.
Here are some examples illustrating how to use the find command :
This command line finds all files in the current directory (and all subdirectories), then
it displays the results on the terminal window.
This command line finds all files in the current directory (and all subdirectories) with
the extension ‘.c’ and displays the results on the terminal window.
This command line looks inside the ‘/’ directory and every subdirectory for every file
having myfile as name and displays the results on the terminal window.
This command line looks inside the current directory and every subdirectory for every
file having been accessed in the last 10 minutes and whose name begins with ‘foo’.
Then, the results will be shown on the terminal window.
- 11 -
Linux Platform – I Chapter: 3
This command line looks for every file whose name ends with ‘.odf’ and copies them
to /backup/.
This command line finds and then removes every file whose name ends with ‘.odf’.
locate command
This command prints the names of files and directories that match a supplied pattern.
It is faster of the find command because it relies on a database (updated daily by
default) instead of searching real time. The downside of this is that the database could
be outdated and locate command will not find files created today or it will find files
that have been deleted since the last update of the database. Therefore, you have to
update the database (as root user) with the updatedb command when you cannot
find some files you expect to find them. Here is an example:
/usr/share/backgrounds/tiles/house.png
/usr/share/gnome/help/house/C/figures/house_applet.png
/usr/share/gnome/help/house/es/figures/house_applet.png...
Output is truncated…
- 12 -
Linux Platform – I Chapter: 3
Option Description
-a Change the access time
-m Change the modification time
-c Force touch to not create any new file
Set the access/modify time to a specific datetime in format
-t
[[CC]YY]MMDDhhmm[.ss]
-r Use the timestamp of another file as reference
-d Set date in general human readable formats.
- 13 -
Linux Platform – I Chapter: 3
mkdir command
The mkdir command in Linux allows the user to create directories (or folders) and
subdirectories (or subfolders). This command can create multiple directories at once
as well as set the permissions for the directories. It is important to note that the user
executing this command must have enough permissions to create a directory in the
parent directory, or he/she may receive a ‘permission denied’ error. The syntax of this
command is:
- 14 -
Linux Platform – I Chapter: 3
option Description
It enables the command to create parent directories as necessary. If the
-p
directories exist, no error is specified.
It sets the permissions for the created directories. The syntax of the mode is
-m
the same as the chmod command.
- 15 -
Linux Platform – I Chapter: 3
cp command
You can use the cp command for copying files and directories. Furthermore, more
than one file may be copied at a time if the destination is a directory. The syntax of
the command is:
You can also copy all underlying files and subdirectories using the -R option:
This command creates a directory named dir2 whose contents will be identical to
dir1. However, if dir2 already exists, nothing will be overwritten; the directory dir1
will be copied into the dir2 directory under the name dir2/dir1.
Remarks
• If the destination exists and it is a directory, the copy is placed there with the
same name;
• If the destination exists and it is a file, the copy overwrites the destination file
• If the destination does not exist, the copy is created with that name.
- 16 -
Linux Platform – I Chapter: 3
mv command
For moving files, you can use mv command which can be used also to rename files
and directories. If you want to move file file1 into directory Dir1, you could use the
following command line:
If the destination Dir1 does not exist, the file1 will be renamed and Dir1 will be its
new name.
The command:
moves file1 and file2 to Dir1. If you want to move the directory Dir1 into the
directory Dir2, you should type:
Remarks
• If the destination exists and is a directory, the source files or directory is moved
there with the same name;
• If the destination exist and is a file, the source file is moved to that filename,
overwriting the file;
• If the destination does not exist, the source file or directory is renamed with that
name.
Here are some examples:
- 17 -
Linux Platform – I Chapter: 3
Dir22:
f3 f4
Dir33:
Dir44 f7 f77 ftest
[student@StudentHost ~]$ mv Dir11 Dir22
[student@StudentHost ~]$ ls Fir11
ls: cannot access ‘Fir11’: No such file or directory
[student@StudentHost ~]$ ls Dir11
ls: cannot access ‘Dir11’: No such file or directory
rm command
The rm command deletes files and directories. You can delete files using the following
syntax:
You can use it to remove non-empty directory recursively (delete directory and its
content):
rmdir command
For deleting empty directories, you should use rmdir command:
- 18 -
Linux Platform – I Chapter: 3
- 19 -
Linux Platform – I Chapter: 3
And similar to the mkdir -p option, you can also use rmdir –p to recursively remove
empty directories.
On Linux, there is no garbage - at least not for the shell. So once removed, a file is
really gone, and there is generally no way to get it back unless you have backups, or
you have a real good recovery application. To protect the novice user from mistaken
deletion, the interactive behavior of the rm, cp and mv commands can be activated
using the -i option. In that case the system won't immediately act upon request.
Instead, it will ask for confirmation, so it takes an additional click on a key to delete
directories and/or files:
- 20 -
Linux Platform – I Chapter: 3
4. Inodes
An inode (index node) is an entry in the “inode table”, it contains information about a
file (the metadata), including:
• File type, permission, link count (count of path names pointing to this file), UID,
GID;
• The file’s size and various time stamps;
• Pointers to the file’s data blocks on disk;
• Other metadata.
Note: A file’s inode does not contain the file name.
Each filesystem supports a fixed-length inode table and each inode is associated with
one file, so the number of inodes effectively limits the number of files you can store.
Briefly, the inode number is an integer associated with the contents of a file. While the
human way to reference a file is the file name, computer’s reference for the file is the
file’s inode .
Directories are special files that are used to create and hold access paths to the files
in the filesystem. A directory is nothing more than a mapping between the human
name for the file and the computer’s inode. Consequently, the file’s name is found in
the directory that stores the file .
The first two entries for every directory are always the standard (.) and (..) entries
meaning "this directory" and "the parent directory" respectively.
- 21 -
Linux Platform – I Chapter: 3
ls -i
The ls -i command displays the inode number of a given file. When the argument
is a directory name, this command displays the inode number for each file and each
sub-directory stored in the directory.
cp and inode
The cp command allocates a free inode number placing a new entry in the inode table.
It creates a directory entry, referencing the human file name to the inode number.
Finally, it copies data into new file .
mv and inode
If the destination of the mv command is on the same file system as the source, the mv
command creates a new directory entry, with the news file name, deletes the old
directory entry with the old file name. If the destination is a different file system, the
mv command acts as a copy and remove.
Note: The mv command has no impact on the inode table (except for a time stamp)
or the location of the data on the disk (no data is moved).
rm and inode
If the link count was 1, the rm command frees the file’s inode number, places data
bocks on the free list and removes the directory entry. Data is not actually removed,
but will be overwritten when the data blocks are used by another file. The entry in the
directory is effectively deleted; the inode number is made available; the block locations
that the file was using are placed on the free list. However, if the link count was greater
than 1, the rm command decrements the link count by 1 and removes the mapping
between the name of deleted file and its previous inode number.
- 22 -
Linux Platform – I Chapter: 3
The ln command creates hard links if you use it without options; an extra entry is
added in the directory and the link count to that file is increased by 1. It means, a new
file name is mapped to an existing inode and you do not create a new inode. Here is
a command showing you how can you create a hard link to a file :
Where filename is the source and linkname is the name of the hard link which
represents the new name of the same physical file.
- 23 -
Linux Platform – I Chapter: 3
soft link:
A soft link (sometimes called symbolic link) is a file that points to another file, effectively
allowing you to have multiple file names representing a single physical file. Soft links
do not link to inodes, but create a name to name mapping.
Soft links are created with ln -s command that gives an inode of its own.
Permissions on a soft link have no meaning, since the permissions of the target apply.
Hard links are limited to their own partition (because they point to an inode), soft links
can link anywhere (other file systems, even networked). Permissions for a soft link
appear as lrwxrwxrwx, but are not interpreted as full Read/Write/Execute
permissions. If you try to open a soft link, the permissions on the file that link points
to (the original file) determine whether or not you can access the file .
- 24 -
Linux Platform – I Chapter: 3
Remove links
You can remove Links using rm command:
You can note that the hard link has count decreased by 1.
- 25 -
Linux Platform – I Chapter: 3
Questions
1. Display your current directory, and then change it to the /etc directory.
2. Change to your home directory using only three key presses .
3. Change to the parent directory of the current directory .
4. Find all files created after January 12th 2020 .
5. Find all *.odf files created in September 2009.
6. Create a file called test.txt. Can you find this file with locate? Why not? How do
you make locate find this file?
7. Create the files one.txt and two.txt
8. Create a directory ~/DirTest and enter it .
9. Create the files one.txt and two.txt in DirTest.
10. Change the date on one.txt to match yesterday's date.
11. Copy one.txt to copy.one.txt
12. Rename copy.one.txt to ramy
13. Put some text in the files one.txt and two.txt
14. Create a hard link to one.txt named hlone.txt.
15. Display the inode numbers of both files: one.txt and hlone.txt .
16. Use the find command to list the two hard_linked files
17. Everything about a file is in the inode, except two things: name them !
18. Create a soft link to two.txt called soft_two.txt .
19. Find all files with inode number 2. What does this information tell you?
20. Find all soft links in the directories /etc/rc3.d /
21. Use find to look in your home directory for regular files that do not(!) have one
hard link
22. Create a directory ~/testetcbackup and copy all *.conf files from /etc into it.
Did you include all subdirectories of /etc ?
23. Use one command to remove the directory ~/testetcbackup and all files into it .
- 26 -
Linux Platform – I Chapter: 3
References
1. Red Hat Linux Essentials RH033-RHEL5-en-2-20070306
2. Paul Cobbaut, “Linux Fundamentals”,
https://linux-training.be/funhtml/index.html. Updated on 2015-05-24
3. https://www.linuxnix.com/10-file-globbing-examples-linux-unix/
- 27 -