0% found this document useful (0 votes)
4 views21 pages

SA1-CHAPTER-3

The document provides a comprehensive overview of managing files from the command line in a Linux environment, detailing commands for navigating the file system, creating and managing directories and files, and understanding the Linux file system hierarchy. It explains the purpose of various directories such as /usr, /bin, and /etc, and describes the difference between absolute and relative paths. Additionally, it includes practical command examples for file management tasks like creating, copying, moving, and removing files and directories.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views21 pages

SA1-CHAPTER-3

The document provides a comprehensive overview of managing files from the command line in a Linux environment, detailing commands for navigating the file system, creating and managing directories and files, and understanding the Linux file system hierarchy. It explains the purpose of various directories such as /usr, /bin, and /etc, and describes the difference between absolute and relative paths. Additionally, it includes practical command examples for file management tasks like creating, copying, moving, and removing files and directories.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 21

##########: SA1:- CHAPTER-3 :##########

==========: MANAGING FILES FROM THE COMMAND LINE :==========

1- Describing Linux File System Hierarchy Concept ===== pwd, echo, cd,
cd.. cd., cd../..
2- Describing Linux File System Hierarchy Concepts ===== cd ~, touch, ls, la -l,
ls -lr, ls

lt, ls-ld,ls -la, ls -li, ls -lz


3 - Absolute and Relative Path and Navigating Path ===== head tail wc tab use
4- Managing File Using the Command-Line Tool =====
*Create Directory,Copying Files
*Moving Files,Removing Files & Directories ===== mkdir, -p, -v, cp, -r, -
v, -f, -p

mv, rm, -r, -v, -f, -I, rmdir


5 - Managing Link Between The Files
*Create Hard Link,Create Soft Link ===== ln, -s, df

================================================================================

##########: Linux File System Hierarchy :##########

All files on a Linux system are stored on file systems, organized into a
single inverted tree of directories, known as a file-system hierarchy.

This tree is inverted because the root of the tree is said to be at the
top of the hierarchy, and the branches of directories and subdirectories, below the
root.

The / directory is the root directory at the top of the file-system hierarchy.
The / character is also used as a directory separator in file names.
================================================================================

##########: Red Hat Enterprise Linux Directories :##########

The /usr Installed software, shared libraries, include files, and read-only program
data.

##########: The Subdirectory Include :##########

/usr/bin :- User commands. (Norma


User)
/usr/sbin :- System administration commands. (Super User)
/usr/local :- Locally customized software.

[root@JAVED Desktop]# ls
Data Test.txt

[root@JAVED Desktop]# which ls (Find


Location of ls)
alias ls='ls --color=auto'
/usr/bin/ls

[root@JAVED Desktop]# /usr/bin/ls


Data Test.txt
[root@JAVED Desktop ]# #PATH Enviromental

[root@JAVED Desktop]# echo $PATH (Print Path)


/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/root/bin

[root@JAVED Desktop ~]# which useradd (Find Location of


useradd)
/usr/sbin/useradd

[root@JAVED Desktop ~]# /usr/sbin/useradd Parwez

[root@JAVED Desktop ~]# which cat (Find Location of


cat)
/usr/bin/cat

[root@JAVED Desktop ~]# /usr/bin/cat /etc/passwd | grep Parwez


Parwez:x:1001:1001::/home/Parwez:/bin/bash

[root@JAVED Desktop ~]# ls /bin/ (Command


Location)

[root@JAVED Desktop ~]# ls /sbin/ (Command Location)

================================================================================

##########: / :##########

• This Directory is Called as The ‘root’ Directory


• It is at Top of The File System Structure.
• All Other Directories are Placed Under it.
================================================================================

##########: /root :##########

• This is The Default Home Directory of The Root.

Note:- In Linux / Unix The Administrator is Called as root.

Home directory of the Superuser, root.

[root@JAVED ~]# cd /root/


(Change Directory)
[root@JAVED ~]# pwd (Print
Working Directory)
/root
[root@JAVED ~]# ls
(List Content)
Documents Pictures Templates
Desktop Downloads Music
[root@JAVED ~]#
================================================================================

##########: /home :##########

• Home directories of Regular Users to store their personal data and


configuration files.

[root@JAVED home]# cd /home/ (Change


Directory)
[root@JAVED home]# ls (List
Directory)
MD.JAVED Parwez Tabrez
[root@JAVED home]# ls MD.JAVED/ (List
Directory)
Desktop Documents Downloads Music
Pictures Public Templates Videos
[root@JAVED home]#
================================================================================

##########: /boot :##########

• It Contains The Kernal, Which is the Core of The Operating System.


• It Also Contains the Files Related For Booting the OS Such as boot Loader

[root@JAVED ~]# cd /boot/


(Change Directory)
[root@JAVED ~]# ls
(List Directory)
config-4.18.0-193.el8.x86_64
efi
grub2
initramfs-0-rescue-7742d4063905421ea63107380513a3f7.img
initramfs-4.18.0-193.el8.x86_64.img
initramfs-4.18.0-193.el8.x86_64kdump.img
loader
System.map-4.18.0-193.el8.x86_64
vmlinuz-0-rescue-7742d4063905421ea63107380513a3f7
vmlinuz-4.18.0-193.el8.x86_64
[root@JAVED ~]#
================================================================================

##########: /bin :##########

• bin Stand for Binary


• It Contains Essential Commands Which are Used by All Users.
• Example:- ping, cat, chmod, etc.
================================================================================

##########: /sbin :##########

• Sbin Stand for System binary.


• It Contains Essential System Commands Which can Only be Used by The Super
User (root).
• Example:- fdisk, useradd, etc.
================================================================================

##########: /usr :##########

• Usr Stands for Unix System Resources


• It Contains the Programs And Applications Which are Available for Users
(Similar To Program File in Windows).
================================================================================

##########: /var :##########

• Var Stand for Variable


• Variable data specific to this system that should persist between boots.
Files that dynamically change, such as databases, cache directories, log files,
printer-spooled documents, and website content may be found under /var.

[root@ JAVED Desktop]# cd /var/ (Change


Directory)
[root@ JAVED var]# ls (List
Directory)
account cache db ftp gopher lib lock mail opt run tmp
adm crash empty games kerberos local log nis preserve spool yp
[root@ JAVED var]# cd log/
(Change Directory)
[root@ JAVED log]# pwd (Print
Working Directory)
/var/log

[root@ JAVED log]# ls (List


Directory)
anaconda dnf.rpm.log private tuned
audit firewalld qemu-ga boot.log
chrony insights-client secure

[root@ JAVED log]# tail secure (it Will


Show User Create Logs)
================================================================================

##########: /dev :##########

• dev Stand for Device


• It Contains information about all Hardware Devices.

[root@ JAVED boot]$ cd /dev/ (Change


Directory)
[root@ JAVED dev]$ ls (List
Directory)
autofs initctl ppp stdout tty26 tty45 tty7 vcsa1
cdrom loop-control raw tty10 tty3 tty49 ttyS1 vcsa5
char lp0 rtc tty11 tty30 tty5 ttyS2 vcsa6

[root@ JAVED dev]$ df -hT (Check


Disk With Mount Point)
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 xfs 50G 4.5G 46G 9% /
/dev/sda3 xfs 2.0G 56M 2.0G 3% /home
/dev/sda1 xfs 1014M 225M 790M 23% /boot
[root@ JAVED dev]$
================================================================================

##########: /etc :##########

• etc Stand for et cetera


• It Contains All the Configurations Files.

[User Database /etc/passwd]

[root@ JAVED Desktop]# /usr/sbin/useradd Tabrez (Check Tabrez User)


[root@ JAVED Desktop]# /usr/bin/cat /etc/passwd | grep Tabrez
Tabrez:x:1002:1002::/home/Tabrez:/bin/bash
SSH Configuration File /etc/ssh/sshd.conf

[root@ JAVED Desktop]# ls /etc/ssh/sshd_config (List File)


/etc/ssh/sshd_config
[root@ JAVED Desktop]#
================================================================================

##########: /opt :##########

• Opt Stand for Optional


• It generally Contains the Third Party Softwares.

Example:- Open Office, Antivairus, etc.


================================================================================

##########: /media :##########

• It is the default mount Point for Removable Storage media such as cdrom / dvd
and Pendrive, etc.
================================================================================

##########: /run :##########

• Runtime data for processes started since the last boot. This includes
process ID files and lock files, among other things. The contents of this
directory are recreated on reboot.

[root@JAVED ~]# cd /run/


(Change Directory)
[root@JAVED ~]# ls
(List Directory)
alsactl.pid dmeventd-client podman sshd.pid
systemd lvm rpcbind.sock tmpfiles.d

[root@JAVED ~]# df
(Check Disk Free)
tmpfs 184380 1168 183212 1% /run/user/42
/dev/sr0 8238560 8238560 100% /run/media/root/RHEL-8-2-0-BaseOS-x86_64

[root@JAVED ~]# ls /run/media/root/RHEL-8-2-0-BaseOS-x86_64/


AppStream BaseOS
[root@JAVED ~]#
================================================================================

##########: /tmp :##########

• A world-writable space for temporary files. Files which have not been
accessed, changed,
or modified for 10 days are deleted from this directory automatically.
• Another temporary directory exists, /var/tmp, in which files that have not
been accessed,
changed, or modified in more than 30 days are deleted automatically.

[root@JAVED ~]# cd /tmp/


(Change Directory)
[root@JAVED tmp]# ls (List
Directory)
systemd-private-3134b62909e5466ba936d93b09d8aa33-bolt.service-eY1zm5
systemd-private-3134b62909e5466ba936d93b09d8aa33-geoclue.service-MTp

[root@JAVED tmp]# ls /var/tmp/ (List


Directory)
systemd-private-3134b62909e5466ba936d93b09d8aa33-bolt.service-3vkXla
systemd-private-3134b62909e5466ba936d93b09d8aa33-geoclue.service-IbU

[root@JAVED ~]# su - MD.JAVED (Switch


User)
[MD.JAVED@ JAVED ~]$ cp /etc/hosts /tmp/ (Copy File into
Directory)
[MD.JAVED@ JAVED ~]$ ls /tmp/ (List
Directory)
hosts
systemd-private-3134b62909e5466ba936d93b09d8aa33-bolt.service-eY1zm5
systemd-private-3134b62909e5466ba936d93b09d8aa33-colord.service-KCZvX4
vmware-root_876-2689209388
[MD.JAVED@ JAVED ~]$
================================================================================

##########: Absolute And Relative Path & Navigation Path :##########

The path of a file or directory specifies its unique file system location.

##########: Absolute Path :##########

It is a fully qualified name, specifying the files exact location in


the file system hierarchy. It begins at the root (/) directory and specifies
each subdirectory that must be traversed to reach the specific file.
A path name with a forward slash (/) as the first character is an absolute path
name.
================================================================================

##########: Relative Path :##########

The working directory or current working directory refer to their current location.
A relative path identifies a unique file, specifying only the path necessary to
reach the file from the working directory. A path name with anything other than a
forward slash as the first character is a relative path name.

[root@JAVED ~]# mkdir Data


(Relative Path)
[root@JAVED ~]# ls
(List Directory)
Data Documents Pictures

[root@JAVED ~]# mkdir /Data (Absolute


Path)
[root@JAVED ~]# ls / (List
Directory)
Data etc lib media opt root sbin sys usr

[root@JAVED ~]# ls
(List Directory)
Data Desktop Downloads Videos

[root@JAVED ~]# cp /etc/hosts . (Absolute


Path)
[root@JAVED ~]# ls
(List Directory)
Data hosts Desktop Pictures

[root@JAVED ~]# cp /etc/hosts /root/Desktop/ (Relative Path)


[root@JAVED ~]# ls /root/Desktop/ (List
Directory)
Data hosts Test.txt
================================================================================

##########: Managing File Using The Command-Line Tools :##########

##########: The Use of pwd Command :##########

The pwd command displays the full path name of the current working directory for
that shell.

Directory
Root User :- /root
Regular User :- /home

[root@JAVED ~]# pwd (Print


Working Directory)
/root
[root@JAVED ~]# su - u1 (Switch
User)
[u1@ JAVED ~]$ pwd
(Print Working Directory)
/home/u1
[u1@ JAVED ~]$ cd Desktop/
(Change Directory)
[u1@ JAVED Desktop]$ pwd (Print
Working Directory)
/home/u1/Desktop
[u1@ JAVED Desktop]$

================================================================================

##########: The Use of mdkir Command :##########

The mkdir means Make Directory.


This command is used to create one or more directories or subdirectories.
Takes as arguments a list of paths to the directories you want to create
Have some options:

-p, --parents == no error if the directory exist, make parent directories as need
-v, --verbose == print a message for each created directory

Create a directory ======= mkdir directory


Copy a directory ======= cp file new-file
Copy a directory and its contents ======= cp -r directory new-directory
Move or rename a file or directory ======= mv file new-file
Remove a file ======= rm file
Remove a directory containing files ======= rm -r directory
Remove a Empty directory ======= rmdir directory

[root@JAVED ~]# mkdir (We


Need to Provide Argument)
mkdir: missing operand
Try 'mkdir --help' for more information.

[root@JAVED ~]# mkdir –help (More Help


for mkdir)
[root@JAVED ~]# mkdir D1
(Create Directory Current Location)
[root@JAVED ~]# mkdir –p
(Create Parent Directory)

[root@JAVED ~]# mkdir /root/Desktop/D1 (Create Directory


Target Location)
D1

Note:- There is Only D1 Directory if you want to Create D2 & D3 Folder in D1


Directory

[root@JAVED ~]# mkdir /root/Desktop/D1/D2/D3


mkdir: cannot create directory ‘/root/Desktop/D1/D2/D3’: No such file or directory
[root@JAVED ~]# mkdir /root/Desktop/D1/D2/D3 -pv
mkdir: created directory '/root/Desktop/D1/D2'
mkdir: created directory '/root/Desktop/D1/D2/D3'

[root@JAVED ~]# ls -R /root/Desktop/D1 (Print Directory


Recursive)
/root/Desktop/D1:
D2
/root/Desktop/D1/D2:
D3
/root/Desktop/D1/D2/D3:

[root@ JAVED Software]# pwd (Print


Working Directory)
/root/Desktop/Data/Software

[root@ JAVED Software]# mkdir ../../Parwez (To Create


Directory in /)
[root@ JAVED Software]# ls ../.. (Go Two Step
Back)
Parwez
[root@ JAVED Software]#
[root@ JAVED Desktop]# mkdir {1..5} (To Create
Multiple Directory)
[root@ JAVED Desktop]# ls (List
Directory)
1 2 3 4 5
================================================================================

##########: rm & rmdkir To Remove File & Directories :##########

The rm command removes files.


By default, rm will not remove directories that contain files, unless you add the -
r or --recursive option.
The rm options:

-r :- Recursive
-v :- Verbose
-f :- Forcefully
The RMDIR command only removes the directory that is empty.
The rm -d (which is equivalent to rmdir), or rm –r

[root@ JAVED Desktop]# rmdir 1 (To Remove


Blank Directory)
[root@ JAVED Desktop]# ls (List
Directory)
2 3 4 5
[root@ JAVED Desktop]# mkdir /root/Desktop/D1/D2/D3 -pv (Create Parent
Directory)
mkdir: created directory '/root/Desktop/D1'
mkdir: created directory '/root/Desktop/D1/D2'
mkdir: created directory '/root/Desktop/D1/D2/D3'
[root@ JAVED Desktop]# tree D1 (Show in
Tree Format)
D1
└── D2
└── D3
[root@ JAVED Desktop]# rmdir D1 (We can Only
Delete Blank Directory)
rmdir: failed to remove 'D1': Directory not empty

[root@ JAVED Desktop]# touch file{1..3} (To Create


Multiple Blank File)
[root@ JAVED Desktop]# ls
2 3 4 5 D1 file1 file2

[root@ JAVED Desktop]# rm -v file1 (Delete File with


Confirmation)
rm: remove regular empty file 'file1'? y
removed 'file1'

[root@ JAVED Desktop]# ls


2 3 4 5 D1 file2 file3

[root@ JAVED Desktop]# rm -vf file2 (To Delete


File Forcefully)
removed 'file2'

[root@ JAVED Desktop]# rm -vr 2


rm: remove directory '2'? y
removed directory '2'
(Verbose recursively)

[root@ JAVED Desktop]# ls


3 4 5 D1 file3
[root@ JAVED Desktop]# rm -r 3 (Delete File
Recursively)
rm: remove directory '3'? y

[root@ JAVED Desktop]# ls


4 5 D1 file3
[root@ JAVED Desktop]# rm -rvf 4 (Delete Directory Forcefully in Verbose Mode)
removed directory '4'
[root@ JAVED Desktop]# ls
5 D1 file3
================================================================================

##########: The Use of ls Command :##########


LS command is used to list the files and folders
It has multiple options like:

ls-l ===== Long list including Attributes


ls-a ===== All Files & Directories including Hidden
ls-d ===== For a Particular Directory
ls-R ===== Recursive to See the Tree Structure
ls-h ===== Human Readable With -l & -s, Print Size
ls-i ===== Print the Index Number of Each File
ls-r ===== Reverse Order While Shorting

The ls command lists directory contents for the specified directory or,
if no directory is given, for the current working directory.

[root@JAVED Desktop]# ls (Show


in Short List)
Data Parwez
[root@JAVED Desktop]# ls -l (Show in
Long List)
drwxr-xr-x. 2 root root 6 Dec 16 10:20 Data
-rw-r--r--. 1 root root 21 Dec 16 10:06 Parwez
[root@JAVED Desktop]# ls -a (Show Hidden
Files)
. .. Data Parwez .Test1 .Test2

[root@JAVED Desktop]# ls -la (Show Hidden


Files in long List)
drwxr-xr-x. 3 root root 60 Dec 16 10:20 .
dr-xr-x---. 18 root root 4096 Dec 16 10:04 ..
drwxr-xr-x. 2 root root 6 Dec 16 10:20 Data
-rw-r--r--. 1 root root 21 Dec 16 10:06 Parwez

[root@JAVED Desktop]# touch .Tabrez.txt (Create


Hidden Blank File)
[root@JAVED Desktop]# ls (List
Directory)
Data Parwez
[root@JAVED Desktop]# ls -a (Check
Hidden Directory)
. .. Data Parwez .Tabrez.txt

[root@JAVED Desktop]# ll -a (Check


Hidden Directory)
drwxr-xr-x. 2 root root 6 Dec 11 01:14 . (Present
Working Directory)
drwxr-xr-x. 4 root root 111 Dec 11 01:32 .. (Parent
Directory)

[root@JAVED Desktop]# mkdir .Salman (Create Hidden


Folder)

[root@JAVED Desktop]# ls -a (Check


Hidden Directory)
. .. Data Parwez .Salman .Tabrez.txt

[root@JAVED Desktop]# ls -ld .Salman/ (Check


Hidden Directory)
drwxr-xr-x. 2 root root 6 Dec 16 10:46 .Salman/ (Show Permission
Directory)

[root@JAVED Desktop]# ls -l Parwez (Show Permission


of File)
-rw-r--r--. 1 root root 21 Dec 16 10:06 Parwez

[root@JAVED ~]# ls -l Jetking.txt (Check File


Size Also)
-rw-r--r--. 1 root root 1938 Dec 16 10:09 Jetking.txt

[root@JAVED ~]# ls -lh Jetking.txt (Check File Size


in Kb,Mb,GB)
-rw-r--r--. 1 root root 1.9K Dec 16 10:09 Jetking.txt

[root@JAVED ~]# ls -li Jetking.txt (Show Index


Number)
102119670 -rw-r--r--. 1 root root 1938 Dec 16 10:09 Jetking.txt

[root@JAVED ~]# ls (List


Directory)
Course Jetking.txt ping.sh
Data Hello.sh MNA
Desktop Hello.txt Music
[root@JAVED ~]# ls /root/MNA/ (Show Data
in Folder)
sem1 sem2
[root@JAVED ~]# ls
Course Folder.sh Jetking.txt
Data Hello.sh MNA
Desktop Hello.txt Music

[root@JAVED ~]# ls -r MNA/ (Show Only


Subdirectories)
sem2 sem1
[root@JAVED ~]# ls -R MNA/ (Show
Subdirectories & Files)
MNA/:
sem1 sem2
MNA/sem1:
ccna office windows
MNA/sem1/ccna:
MNA/sem1/office:
MNA/sem1/windows:
MNA/sem2:
redhat server
MNA/sem2/redhat:
MNA/sem2/server:
[root@JAVED ~]# tree MNA/ (Show in
Tree Format)
[root@JAVED ~]# ls -lt (Show Newest
First Modification)

[root@JAVED ~]# ls -Z MNA/ (Print


Security Context)
unconfined_u:object_r:admin_home_t:s0 sem1
unconfined_u:object_r:admin_home_t:s0 sem2

[root@JAVED ~]# ls -lZ MNA/ (Print


Security Context)
drwxr-xr-x. 5 root root unconfined_u:object_r:admin_home_t:s0 47 Dec 12 15:25 sem1
drwxr-xr-x. 4 root root unconfined_u:object_r:admin_home_t:s0 34 Dec 12 15:25 sem2
[root@JAVED ~]#
================================================================================

##########: The Use of cd Command :##########

The cd command is used to change your shell's current working directory.


Not specify any arguments to the command, it will change to your home directory.
The prompt displays the tilde character (~) when your current working directory is
your home directory.

Change Directory ( Arguments / Direcrory/Folder Name )

[root@JAVED ~]# cd Desktop/ (Change


Directory)
[root@JAVED Desktop]# pwd (Present
Working Directory)
/root/Desktop

[root@JAVED Desktop]# cd . (Present


Working Directory)
[root@JAVED Desktop]# cd .. (Go One Step
Back / Parent Direncory)
[root@ JAVED Database]# cd ../.. (Go To 2
Step Back Directory)
[root@ JAVED Data]# cd - (Go To
Previous Directory)
/root/Desktop

[root@JAVED Desktop]# cd - (Go To


Previous Directory)
/root/Desktop/Data

[root@JAVED Desktop]# man pwd (Details


info Regarding pwd)

The cd means Change Directory


This command is used to move in another directory

The cd Options:===========

User will Move To Home Directory ===== cd


User will Move Back To Last Working Directory ===== cd -
User wil Move Back One Level ===== cd ..
User will Move Back Two Level ===== cd ../..
Means, Represent to present Directory ===== .
Means, represent to Parent Directory ===== ..
Means, represent to Home directory ===== ~
================================================================================

##########: The Use of CAT Command in Linux :##########

Stands for Concatenate

It is used to create single or multiple files, view the contents


of files, multiple files, and redirect contents of the file to a terminal or files.
Create a File ===== cat > <filename>
To See an Existing File Content ===== cat <filename>
To Append an Existing File ===== Cat >> <filename>
To Combine the data of Two or More ===== Cat <first file> <Second
file> >> <Third file>
Files into a Third File

[root@JAVED ~]# cat /etc/passwd (To Check


File Content)
[root@JAVED ~]# cat /etc/passwd /etc/group | grep -w user1
user1:x:6016:6020::/home/user1:/bin/bash
user1:x:6020:
[root@JAVED ~]# ls
(List Directory)
[root@JAVED ~]# >Parwez (Create
File)
[root@JAVED ~]# ls
Parwez
[root@JAVED ~]# cat >Parwez (Create
File)
My Name is MD.Parwez.
My Big Brother Name is MD.JAVED.
CTRL+D For (Save)
[root@JAVED ~]# cat Parwez (To
Check File Content)
My Name is MD.Parwez.
My Big Brother Name is MD.JAVED.
CTRL+D
================================================================================

##########: The Use of touch Command :##########

The touch command normally updates a file's timestamp to the current


date and time without otherwise modifying it. Useful for creating Blank/empty files

[root@JAVED Desktop]#touch Parwez (Create


Blank File)
[root@JAVED Desktop]#ls (List
Directory)
Parwez
[root@JAVED Desktop]#file Parwez (To Check
File Type)
Parwez: empty
[root@JAVED Desktop]#echo "My Name is MD.Parwez" > Parwez
[root@JAVED Desktop]#cat Parwez (To Check
File Content)
My Name is MD.Parwez
[root@JAVED Desktop]#file Parwez (To Check
File Type)
Parwez: ASCII text
[root@JAVED Desktop]#
[root@JAVED ~]# ll
(Show in Long List)
-rw-r--r--. 1 root root 114 Dec 13 16:43 Course
[root@JAVED ~]# touch Course (To Change
Timestamp)
[root@JAVED ~]# ll
(Show in Long List)
total 60
-rw-r--r--. 1 root root 114 Dec 16 10:10 Course
[root@JAVED ~]# ll
(Show in Long List)
drwxr-xr-x. 4 root root 30 Dec 12 15:25 MNA
[root@JAVED ~]# touch MNA (To
Change Timestamp)
[root@JAVED ~]# ls -ld MNA/
drwxr-xr-x. 4 root root 30 Dec 16 10:12 MNA
[root@JAVED ~]#
================================================================================

##########: The Use of echo Command :##########

echo Command is use for Print Text, String, Variable. Mostly use in Shell
Scripting.

[root@ JAVED Data]# echo This is Test File (Print Text)


This is Test File
[root@ JAVED Data]#

[root@ JAVED Data]# echo "This is Test File" (Print Text)


This is Test File
[root@ JAVED Data]# echo This is Test File >echo_file.txt (Create File)
[root@ JAVED Data]# cat echo_file.txt (To
Check File Content)
This is Test File
[root@ JAVED Data]# echo This is Test File2 >>echo_file.txt(Append File)
[root@ JAVED Data]# echo This is Test File3 >>echo_file.txt(Append File)
[root@ JAVED Data]# echo This is Test File4 >>echo_file.txt(Append File)
[root@ JAVED Data]# cat echo_file.txt (To
Check File Content)
This is Test File
[root@ JAVED Data]# echo $USER (Print
User)
root
[root@ JAVED Data]# echo USER (Print
Text)
USER
[root@ JAVED Data]# echo $HOSTNAME (Print
Hostname)
JAVED
[root@ JAVED Data]# man echo (More
Details of echo)
================================================================================

##########: The Use of cp Command :##########

The cp command copies a file, creating a new file either in the current
directory or in a specified directory. It can also copy multiple files to a
directory.

Cp File Folder/Directory ===== <Source Address> <Destination Address>

The cp options:=====
-r :- Recursive ( To Copy the Directory Along with its Content)
-v :- Verbose
-f :- Forcefully
-p :- Copy with Permission
[root@ JAVED Desktop]# ls (List
Directory)
Hr Marketing Notes

[root@ JAVED Desktop]# ls /root/Pictures/ (List Directory)


[root@ JAVED Desktop]# cp Notes /root/Pictures/ (To Copy File Only)
[root@ JAVED Desktop]# ls /root/Pictures/ (List Directory)

Note:- If you want to Copy Directory Then We Need To Use Command cp -r

[root@ JAVED Desktop]# cp Hr /root/Pictures/ (Need To User cp -


r)
cp: -r not specified; omitting directory 'Hr'
[root@ JAVED Desktop]# cp -r Hr /root/Pictures/ (Copy Directory)
[root@ JAVED Desktop]# ls /root/Pictures/ (Check Directory)
Hr Notes
[root@ JAVED Desktop]# ls /root/Music/ (Change Directory)
[root@ JAVED Desktop]# ll
drwxr-xr-x. 2 root root 6 Dec 17 12:07 Hr
drwxr-xr-x. 2 root root 6 Dec 17 12:07 Marketing
-rw-r--r--. 1 root root 0 Dec 17 12:20 Notes

[root@ JAVED Desktop]# cp -vp Notes /root/Music/ (Copy File Old


Time Stamp)
'Notes' -> '/root/Music/Notes'

[root@ JAVED Desktop]# ls -l /root/Music/Notes


-rw-r--r--. 1 root root 0 Dec 17 12:20 /root/Music/Notes
[root@ JAVED Desktop]# cp /etc/passwd . (Copy Source To
Present D)
[root@ JAVED Desktop]# ls
Data Database passwd
================================================================================

##########: The Use of mv Command :##########

The mv command moves files from one location to another.


Also used to rename the files and directory
File contents remain unchanged.

Mv Command is use for Move / Cut or Rename File And Directories.

mv ===== <Source Address> <Destination Address>

[root@JAVED ~]# ls (List


Directory)
Desktop Downloads
[root@JAVED ~]# cd Desktop/ (Change
Directory)
[root@ JAVED Desktop]# ls (List
Directory)
5 D1 file3
[root@ JAVED Desktop]# ls /root/Downloads/ (List
Directory)
[root@ JAVED Desktop]# mv 5 /root/Downloads/ (Move File &
Folder)
[root@ JAVED Desktop]# ls /root/Downloads/
5
[root@ JAVED Desktop]# ls (List
Directory)
D1 file3
[root@ JAVED Desktop]# mv D1 /root/Downloads/Data1 (Move Folder With
Rename)
[root@ JAVED Desktop]# ls /root/Downloads/
5 Data1
[root@ JAVED Desktop]# ls (List
Directory)
file3
[root@ JAVED Desktop]# mkdir Sale (Create
Directory)
[root@ JAVED Desktop]# mkdir Hr (Create
Directory)
[root@ JAVED Desktop]# ls (List
Directory)
Hr Sale
[root@ JAVED Desktop]# mv Sale Marketing (Rename File &
Folder)
[root@ JAVED Desktop]# ls (List
Directory)
Hr Marketing
[root@ JAVED Desktop]#
================================================================================

##########: Managing Link Between The Files :##########

=====: What is Hard Link :=====

SoftLink Link ID :- 1
Hardlink Link ID :- 2

Every file starts with a single hard link, from its initial name to the data
on the file system. Create a new hard link to a file, you create another name
that points to that same data. The new hard link acts exactly like the original
file name. Once created, you cannot tell the difference between the new hard link
and the original name of the file. It reports is each file's link count, the number
of hard links the file has. Use the ln command to create a new hard link (another
name)
that points to an existing file. The command needs at least two arguments, a path
to the existing file, and the path to the hard link that you want to create.
If you want to find out whether two files are hard links of each other, one way
is to use the -i option with the ls command to list the files' inode number.

Note:-

All hard links that reference the same file will have the same link count,
access permissions, user and group ownerships, time stamps, and file content.
Each hard link points to the same data on the storage device.

Even if the original file gets deleted, the contents of the file are still
available as long as at least one hard link exists.
Data is only deleted from storage when the last hard link is deleted.

=====: Limitation of Hard Link :=====

The hard links can only be used with regular files.


Cannot use ln to create a hard link to a directory or special file.
Secondly, hard links can only be used if both files are on the same file system.

[root@JAVED ~]# echo "This is a Sample File for Hard Link" >File01.txt
[root@JAVED ~]# cat File01.txt (To Check
File Content)
This is a Sample File for Hard Link
[root@JAVED ~]# ls (List
Directory)
File01.txt
[root@JAVED ~]# ln File01.txt Hard_File01.txt (Create Hard Link)
[root@JAVED ~]# ll
(List Directory)
-rw-r--r--. 2 root root 36 Dec 19 14:58 File01.txt
-rw-r--r--. 2 root root 36 Dec 19 14:58 Hard_File01.txt

[root@JAVED ~]# ls -li (To


Check Inode Number)
102119652 -rw-r--r--. 2 root root 36 Dec 19 14:58 File01.txt
102119652 -rw-r--r--. 2 root root 36 Dec 19 14:58 Hard_File01.txt

[root@JAVED ~]# cat File01.txt (To Check


File Content)
This is a Sample File for Hard Link
[root@JAVED ~]# cat Hard_File01.txt (To Check
File Content)
This is a Sample File for Hard Link

[root@JAVED ~]# rm File01.txt (Delete


File)
rm: remove regular file 'File01.txt'? y
[root@JAVED ~]# ll
(List Directory)
-rw-r--r--. 1 root root 36 Dec 19 14:58 Hard_File01.txt Now it is Regular File
[root@JAVED ~]# ls
Hard_File01.txt

[root@JAVED ~]# cat Hard_File01.txt (To Check


File Content)
This is a Sample File for Hard Link
[root@JAVED ~]# ls
MNA Course Parwez
Data Hard_File01.txt
[root@JAVED ~]# ln Data Hard_Data (Create Hard
Link)
ln: Data: hard link not allowed for directory
[root@JAVED ~]#
================================================================================

==========: Diffrence Between Copy & Hard Link :==========

[root@JAVED ~]# echo "Hello" >Tabrez


[root@JAVED ~]# ls
Tabrez
[root@JAVED ~]# ls -li Tabrez (To Check
Inode Number)
102119673 -rw-r--r--. 1 root root 6 Dec 19 15:45 Tabrez
[root@JAVED ~]# cd Desktop/

[root@ JAVED Desktop]#ln /root/Tabrez Tabrez_New (Create Hard Link)


[root@ JAVED Desktop]#ls -li Tabrez_New (To Check Inode
Number)
102119673 -rw-r--r--. 2 root root 6 Dec 19 15:45 Tabrez_New

[root@ JAVED Desktop]#date


Sun Dec 19 15:48:44 IST 2021

[root@ JAVED Desktop]#ls -li /root/Tabrez (To Check Inode


Number)
102119673 -rw-r--r--. 2 root root 6 Dec 19 15:45 /root/Tabrez

[root@ JAVED Desktop]#cp /root/Tabrez Tabrez_cp


[root@ JAVED Desktop]#ls -li Tabrez_cp (To Check Inode
Number)
50151988 -rw-r--r--. 1 root root 6 Dec 19 15:50 Tabrez_cp

[root@ JAVED Desktop]#cp /root/Tabrez Tabrez_cp1


[root@ JAVED Desktop]#ls -li Tabrez_cp1 (To Check Inode
Number)
50151989 -rw-r--r--. 1 root root 6 Dec 19 15:58 Tabrez_cp1

[root@ JAVED Desktop]#ls -li (To Check


Inode Number)
50151988 -rw-r--r--. 1 root root 6 Dec 19 15:50 Tabrez_cp
50151989 -rw-r--r--. 1 root root 6 Dec 19 15:58 Tabrez_cp1
102119673 -rw-r--r--. 2 root root 6 Dec 19 15:45 Tabrez_New

[root@ JAVED Desktop]#ls -li (To Check


Inode Number)
50151988 -rw-r--r--. 1 root root 6 Dec 19 15:50 Tabrez_cp
50151989 -rw-r--r--. 1 root root 6 Dec 19 15:58 Tabrez_cp1
102119673 -rw-r--r--. 2 root root 6 Dec 19 15:45 Tabrez_New Hard Link
[root@ JAVED Desktop]#

[root@ JAVED Desktop]#cat /root/Tabrez (To Check File


Content)
Hello
[root@ JAVED Desktop]#rm /root/Tabrez (Delete File)
rm: remove regular file '/root/Tabrez'? y

[root@ JAVED Desktop]#ls -li


50151988 -rw-r--r--. 1 root root 6 Dec 19 15:50 Tabrez_cp
50151989 -rw-r--r--. 1 root root 6 Dec 19 15:58 Tabrez_cp1
102119673 -rw-r--r--. 1 root root 6 Dec 19 15:45 Tabrez_New [Now it is Regular
File]

[root@ JAVED Desktop]#ln Tabrez_New h1 (Create Hard Link)


[root@ JAVED Desktop]#ln Tabrez_New h2 (Create Hard Link)
[root@ JAVED Desktop]#ln Tabrez_New h3 (Create Hard Link)

[root@ JAVED Desktop]#ll


-rw-r--r--. 4 root root 6 Dec 19 15:45 h1
-rw-r--r--. 4 root root 6 Dec 19 15:45 h2
-rw-r--r--. 4 root root 6 Dec 19 15:45 h3
-rw-r--r--. 4 root root 6 Dec 19 15:45 Tabrez_New

[root@ JAVED Desktop]#rm h2 (Delete


File)
rm: remove regular file 'h2'? y
[root@ JAVED Desktop]#ll
-rw-r--r--. 3 root root 6 Dec 19 15:45 h1
-rw-r--r--. 3 root root 6 Dec 19 15:45 h3
-rw-r--r--. 3 root root 6 Dec 19 15:45 Tabrez_New

[root@ JAVED Desktop]#rm h3 (Delete


File)
rm: remove regular file 'h3'? y

[root@ JAVED Desktop]#ll


-rw-r--r--. 2 root root 6 Dec 19 15:45 h1
-rw-r--r--. 2 root root 6 Dec 19 15:45 Tabrez_New
[root@ JAVED Desktop]#
================================================================================

##########: What is Soft Link :##########

The ln -s command creates a soft link, which is also called a "symbolic link."
A soft link is not a regular file, but a special type of file that points to
an existing file or directory. Soft links have some advantages over hard links:
Can link two files on different file systems. Can point to a directory or
special file, not just a regular file.

When the original regular file gets deleted, the soft link will still point
to the file but the target is gone. A soft link pointing to a missing file
is called a "dangling soft link."

Note:-

One side-effect of the dangling soft link in the preceding example is that if
you later create a new file with the same name as the deleted file
(/home/user/newfilelink2.txt), the soft link will no longer be "dangling" and
will point to the new file.

##########: Hard Link do Not Work Like This :##########

If you delete a hard link and then use normal tools (rather than ln) to
create
a new file with the same name, the new file will not be linked to the old
file.

A soft link can point to a directory.


The soft link then acts like a directory.

Soft Link Command ===== ln -s <Original File Name> <Soft_link_name>

[root@JAVED ~]# echo "This is a Sample File" > Sample_File01.txt


[root@JAVED ~]# ls
Sample_File01.txt

[root@JAVED ~]# cat Sample_File01.txt (To Check File


Content)
This is a Sample File

[root@JAVED ~]# ln -s Sample_File01.txt Soft_Sample_file01.txt (Create Soft


Link)
lrwxrwxrwx. 1 root root 17 Dec 17 13:11 Soft_Sample_file01.txt ->
Sample_File01.txt
Note:- When you Delete Original File Then File Will Not Open & Work

[root@JAVED ~]# mkdir Parwez


[root@JAVED ~]# ls
Parwez
[root@JAVED ~]# ln -s Parwez Soft_Parwez (Create Soft Link)
[root@JAVED ~]# ll
drwxr-xr-x. 2 root root 6 Dec 19 14:30 Parwez
lrwxrwxrwx. 1 root root 6 Dec 19 14:33 Soft_Parwez -> Parwez

[root@JAVED ~]# cd Soft_Parwez/

[root@ JAVED Soft_Parwez]# touch Test.txt (Create Blank


File)
[root@ JAVED Soft_Parwez]# ls /root/Parwez/ (List Directory)
Test.txt
[root@JAVED ~]# mkdir Jetking (Create
Directory)
[root@JAVED ~]# ls Jetking/
[root@JAVED ~]# cd Desktop/ (Change
Directory)
[root@ JAVED Desktop]# ln -s /root/Jetking/ /root/Desktop/Jet (Create Soft Link)
[root@ JAVED Desktop]# ll
lrwxrwxrwx. 1 root root 14 Dec 19 14:41 Jet -> /root/Jetking/

[root@ JAVED Desktop]# cd Jet/ (Change


Directory)
[root@ JAVED Jet]# touch File{1..10}.txt

[root@ JAVED Jet]# ls (List


Directory)
File10.txt File2.txt File4.txt File6.txt File8.txt
File1.txt File3.txt File5.txt File7.txt File9.txt

[root@ JAVED Jet]# ls /root/Jetking/ (List


Directory)
File10.txt File2.txt File4.txt File6.txt File8.txt
File1.txt File3.txt File5.txt File7.txt File9.txt

Note:-If You Want to Create Softlink in Desktop Then you have to go Desktop
Frist

[root@ JAVED Desktop]# ln -s /etc/hosts Myhosts (Create Soft Link)


[root@ JAVED Desktop]# ll
lrwxrwxrwx. 1 root root 10 Dec 19 14:47 Myhosts -> /etc/hosts

[root@ JAVED Desktop]# cat Myhosts (To Check File


Content)
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

[root@ JAVED Desktop]#


[root@ JAVED Desktop]# ls -l /etc/hosts (List File)
-rw-r--r--. 1 root root 158 Sep 10 2018 /etc/hosts

[root@ JAVED Desktop]# cat /etc/hosts (To Check


File Content)
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@ JAVED Desktop]#

==================================Completed=====================================

You might also like