Linux Directory Structure

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Linux Platform – I Chapter: 3

Chapter 3:
Linux Directory Structure

- 0 -
Linux Platform – I Chapter: 3

Title Page
1. Definitions 4

2. Linux Directory Navigation 5

3. Creating, Moving, Copying and Deleting Files/Directories 13

4. Inodes 21

5. Hard Links vs. Soft Links 23

- 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:

[raddad@RaddadHost ~]$ ls - ld "filename with spaces"


-rw-rw-r--. 1 raddad raddad 0 Jul 17 01:58 ‘filename with spaces’
[raddad@RaddadHost ~]$

If you don’t write the quotes, you would be asking the system to list three different
files.

[raddad@RaddadHost ~]$ ls –ld ‘filename with spaces’


ls: cannot access ‘filename’: No such file or directory
ls: cannot access ‘with’: No such file or directory
ls: cannot access ‘spaces’: No such file or directory
[raddad@RaddadHost ~]$ _

- 4 -
Linux Platform – I Chapter: 3

2. Linux Directory Navigation


A location is associated with each shell in the Linux file system; this location is called
the working directory (or current directory). As mentioned before, each user has a
directory that is identified as the user’s home directory, represented by tilde (~)
character, and it contains the user personal files (configuration, data, applications,
etc.). When you first log in to Linux, you begin with your home directory as working
directory .
A file’s name is stored within its parent directory, not with the file itself. Two file names
are particularly special, a filename that consists of a single dot (.) refers to the current
directory, whereas a filename that consists of a double dot (..) refers to the parent
directory; for instance, if your current directory is /home/Jon, then (.) refers to that
directory and (..) refers to /home.
Linux uses a unified directory tree that begins with a forward slash (/) representing
the root of the tree (called “root directory”); it also represents the top level of the tree.

[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 contains unshareable files related to the initial booting of the computer


/boot such as Linux Kernel and files used by the boot loader; sometimes, the /boot
directory exist on a separate partition

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

It contains the core configuration files of the system, it is used primarily by


/etc the administrator and services, such as: the password file and networking
files

/home It contains non-root home directories

/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 contains subdirectories where removable media devices inserted into the


/media
computer are mounted.

It is where system administrators mounted temporary file systems while using


them. For example, if you’re mounting a Windows partition to perform some
/mnt
file recovery operations, you might mount it at /mnt/windows. However, you
can mount other file systems anywhere on the system.
It contains subdirectories for optional software packages. For example, a
/opt proprietary program might dump its files in /opt/application when you install
it.

It similar to the /dev directory because it doesn’t contain standard files. It


/proc
contains special files that represent system and process information.

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

/root It is the home directory of the user root

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

It contains regularly-changing system files such as logs, print spools and


/var
email spools

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

absolute and relative paths


All files and directories appear under the root directory (/), even if they are stored on
different physical devices. The path name from the root directory to a certain file or
directory is known as “absolute path” to that file, whereas “relative path” is a path
name that begins with any directory name except the root directory.)/(
The command lines below first show the current directory /home/student/. From
within this directory, you have to type cd /home instead of cd home to go to the /home
directory using absolute path or cd . using relative path because /home is the parent
directory of your home directory.

[student@StudentHost ~]$ pwd


/home/student/
[student@StudentHost ~]$ cd home
-bash: cd: home: No such file or directory
[student@StudentHost ~]$ cd /home
[student@StudentHost home]$ cd–
[student@StudentHost ~]$ cd..
[student@StudentHost home]$

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

any character belongs to the character class “keyword”. Where keyword


[[:keyword:]]
can be: alpha, upper, lower, digit, alnum, punct, space…

Here are some examples:

[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

Here are some examples of how to use character classes [[:keyword:]]:

[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:

$find <directory> [option(s)] [action]

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).

-user uname File is owned by the user uname.

- 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.

-ok command ; Like -exec, but prompt for each file

-ls Print file in ls -dils format.

Here are some examples illustrating how to use the find command :

[student@StudentHost ~]$ find . -print

This command line finds all files in the current directory (and all subdirectories), then
it displays the results on the terminal window.

[student@StudentHost ~]$ find . -name “*.c” -print

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.

[student@StudentHost ~]$ find / -name myfile -print

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.

[student@StudentHost ~]$ find / -name “foo*” -amin -10 -print

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.

[student@StudentHost ~]$ find / -name "*.odf" -exec cp {} /backup;\ /

- 11 -
Linux Platform – I Chapter: 3

This command line looks for every file whose name ends with ‘.odf’ and copies them
to /backup/.

student@StudentHost ~]$ find / -name "*.odf" -ok rm;\ }{

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:

[student@StudentHost ~]$ locate "*house*.png"

/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

3. Creating, Moving, Copying and Deleting Files/Directories


Day after day, the number of your files is continuously growing, and you have to
reorganize things. Therefore, you have to know how to move, delete, copy, and
rename files and directories. Here are some commands that help you to do that.
touch command
You can create more than one file simultaneously using one touch command as
follows:
$ touch [options] filename1 [filename2, filename3]
Linux file systems maintain three time stamps for every file: creation time, last
modification time and last access time. If you use, touch command without options,
touch sets the modification and access times to the current time. In the table, some
touch options are described:

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.

Here some examples:

- 13 -
Linux Platform – I Chapter: 3

[student@StudentHost ~]$ touch abc.txt efg.txt


[student@StudentHost ~]$ ls –l *.txt
-rw-rw--. 1 student student 0 Jul 20 19:49 abc.txt
-rw-rw--. 1 student student 71 Oct 18 2019 b.txt
-rw-rw--. 1 student student 148 Jul 19 23:58 cc.txt
-rw-rw--. 1 student student 120 Oct 18 2019 c.txt
-rw-rw--. 1 student student 0 Jul 20 19:49 efg.txt
[student@StudentHost ~]$
[student@StudentHost ~]$ touch -cma abc.txt
[student@StudentHost ~]$ ls–ld abc.txt
-rw-rw-r--. 1 student student 0 Jul 20 19:51 abc.txt
[student@StudentHost ~]$
[student@StudentHost ~]$
[student@StudentHost ~]$ touch -c -t 07201900 efg.txt
[student@StudentHost ~]$
[student@StudentHost ~]$ ls –ld efg.txt
-rw-rw-r--. 1 student student 0 Jul 20 19:00 efg.txt
[student@StudentHost ~]$
[student@StudentHost ~]$ touch -r efg.txt abc.txt
[student@StudentHost ~]$
[student@StudentHost ~]$ ls–l –r efg.txt abc.txt
-rw-rw-r--. 1 student student 0 Jul 20 19:00 efg.txt
-rw-rw-r--. 1 student student 0 Jul 20 19:00 abc.txt
[student@StudentHost ~]$ touch –d ‘19 Jul‘ abc.txt
[student@StudentHost ~]$ ls –l abc.txt
-rw-rw-r--. 1 student student 0 Jul 19 00:00 abc.txt
[student@StudentHost ~]$
[student@StudentHost ~]$

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:

$ mkdir [options] directory-name(s)

The following table shows some options and their descriptions:

- 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.

-v It displays a message for every directory created

It sets the permissions for the created directories. The syntax of the mode is
-m
the same as the chmod command.

here are some examples:

[student@StudentHost ~]$ mkdir -m a=rw -vp Dir1/Dir2/ Dir3


mkdir: created directory ‘Dir1’
mkdir: created directory ‘Dir1/Dir2’
mkdir: created directory ‘Dir1/Dir2 /Dir3’
[student@StudentHost ~]$
[student@StudentHost ~]$ ls -ld Dir1
drwxrwxr-x. 3 student student 4096 Jul 20 18:57 Dir1
[student@StudentHost ~]$
[student@StudentHost ~]$ ls – lR Dir1
Dir1:
total 4
drwxrwxr-x. 3 student student 4096 Jul 20 18:57 Dir2
Dir1/ Dir2:
total 4
drw-rw-rw-. 2 student student 4096 Jul 20 18:57 Dir3
Dir1/Dir2/Dir3:
total 0

- 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:

$ cp [options] file1 file2 dest

You can also copy all underlying files and subdirectories using the -R option:

student@StudentHost ~]$ cp -R dir1 dir2

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.

Here are two examples:

[student@StudentHost ~]$ cp abc.txt efg.txt ./Dir1


student@StudentHost ~]$
student@StudentHost ~]$ ls -l ./Dir1
total 4
-rw-rw-r--. 1 student student 0 Jul 20 20:36 abc.txt
drwxrwx-x. 3 student student 4096 Jul 20 18:57 Dir2
-rw-rw-r--. 1 student student 0 Jul 20 20:36 efg.txt
[student@StudentHost ~]$

- 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:

[student@StudentHost ~]$ mv file1 Dir1

If the destination Dir1 does not exist, the file1 will be renamed and Dir1 will be its
new name.
The command:

[student@StudentHost ~]$ mv file1 file2 Dir1

moves file1 and file2 to Dir1. If you want to move the directory Dir1 into the
directory Dir2, you should type:

[student@StudentHost ~]$ mv dir1 dir2

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

[student@StudentHost ~]$ ls Dir*


Dir11:
f1 f2

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

[student@StudentHost ~]$ ls -R Dir22


Dir22:
Dir11 f3 f4
Dir22/ Dir11:
f1 f2
[student@StudentHost ~]$ mv Dir22 Fir11
[student@StudentHost ~]$
[student@StudentHost ~]$ ls -R Dir22
ls: cannot access ‘Dir22’: No such file or directory
[student@StudentHost ~]$ ls -R Fir11
Fir11:
Dir11 f3 f4
Fir11/ Dir11:
f1 f2

rm command
The rm command deletes files and directories. You can delete files using the following
syntax:

$rm [options] file1 file2

You can use it to remove non-empty directory recursively (delete directory and its
content):

[student@StudentHost ~]$ rm -r dir1

rmdir command
For deleting empty directories, you should use rmdir command:

- 18 -
Linux Platform – I Chapter: 3

[student@StudentHost ~]$ rmdir dir1

Here are some examples:

[student@StudentHost ~]$ cd ./Dir1


[student@StudentHost Dir1]$ ls
abc.txt Dir2 efg.txt
[student@StudentHost Dir1]$ rm efg.txt abc.txt
[student@StudentHost Dir1]$ ls
Dir2
[student@StudentHost Dir1]$
[student@StudentHost Dir1]$ cd . .
[student@StudentHost ~]$ rm -ir .Dir1
rm: cannot remove ‘Dir1’: No such file or directory
[student@StudentHost ~]$
[student@StudentHost ~]$ cd
[student@StudentHost ~]$
[student@StudentHost ~]$ rm -ir ./Dir1
rm: descend into directory’./Dir1‘? y
rm: descend into directory ‘./Dir1/Dir2‘? y
rm: remove directory ‘./Dir1/Dir2/Dir3 ‘? y
rm: remove directory ‘./Dir1/Dir2‘? y
rm: remove directory ‘./Dir1‘? y
[student@StudentHost ~]$ ls -ld Dir1
ls: cannot access ‘Dir1’: No such file or directory
[student@StudentHost ~]$

- 19 -
Linux Platform – I Chapter: 3

And similar to the mkdir -p option, you can also use rmdir –p to recursively remove
empty directories.

[student@StudentHost ~]$ rmdir -p ./Dir/SubDir

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:

[student@StudentHost test]$ rm -ri dir2


rm: descend into directory ‘dir2’? y
rm: remove regular file ‘dir2/f12’?y
rm: remove regular file ‘dir2/ln’?y
rm: remove directory ‘dir2’?y
[student@StudentHost test]$

- 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.

Figure 1: inode entries stored in a directory

- 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

5. Hard Links vs. Soft Links


Links provides a connection between files. They are used in Linux to give commands
multiple names, to help make files more accessible and to enable programs that look
for the same files in different locations to access the same files using different names.
Therefore, a link is a way to give a file multiple identities. In Linux, there are two types
of links, hard link and soft link. Click on the buttons below to read more about links
and how to remove them.
Hard link
Hard link is a path name that references an inode; therefore, every individual file is
hard linked at least once and is referenced by its inode number. The name of a file is
separated from its inode and stored in a directory (not in the inode); so, it is possible
to have multiple file names pointing to the same inode number, every one represents
one hard link .

Figure 2: The structure of hard linked files

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 :

[student@StudentHost ~]$ ln filename linkname

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

[student@StudentHost ~]$ ls -li filename linkname


266123- rw-rw-r-- 2 student student 103 29 Sep 25 20:56 filename
266123 -rw-rw-r-- 2 student student 103 29 Sep 25 20:56 linkname

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.

Figure 3: The structure of symbolically linked files

Soft links are created with ln -s command that gives an inode of its own.

[student@StudentHost ~]$ ln -s filename softlinkname

[student@StudentHost ~]$ ls -li filename softlinkname


266123- rw-rw-r-- 2 student student 103 29 Sep 25 20:56 filename
266895 lrwxrwxrwx 1 student student 4 29 Sep 25 20:56 softlinkname
-> filename

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:

[student@StudentHost ~]$ rm linkname


[student@StudentHost ~]$ rm softlinkname
[student@StudentHost ~]$ ls -li filename
266123 -rw-rw-r-- 1 student student 103 29 Sep 25 20:56 filename

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 -

You might also like