Absolute path & Relative path
Absolute path: An absolute path is a complete path to a file or directory from the
root directory. The root directory is the top-level directory of the file system and is
represented by a forward slash (/). Absolute paths always start with the root
directory and provide the full path to the file or directory.
For example, the absolute path to the home directory of a user named "ajmain"
would be "/home/ajmain".
Relative path: Relative path is a path to a file or directory that is relative to the
current directory. It specifies the location of the file or directory in relation to the
current directory. Relative paths do not start with the root directory and are usually
shorter than absolute paths.
For example, if you are currently in the home directory of "ajmain" and want to
access a file named "file01.txt" in a subdirectory called "dir01", the relative path
would be "dir01/file01.txt".
The main difference between absolute and relative paths is how they are interpreted.
Absolute paths always point to the same location regardless of the current directory,
whereas relative paths may point to different locations depending on the current
directory.
20 Commands for LINUX
1. cd:
Change directory.
cd ~ // go to home directory
cd .. // go 1 directory back
cd .. /.. // go 2 directory back
2. cp:
Copy file and directory.
cp <main file name> <the name which i want to copy>
Command:
cp file01.txt file02.txt
Comment: Here file01.txt will copy and file02.txt will be also created with
file01.txt content.
cp <main file name> <path of the directory/name of the file>
Command:
cp file01.txt ~/class02/file02.txt
Comment: Here file01.txt will copy in another directory which is
home/class02/ and it will save the file by file02.txt name.
cp -r <main directory name> <the directory name which i want to copy>
Command:
cp -r dir01 dir02
Comment: Here dir01 directory copy with it's own content and paste by dir02
directory name.
3. mv:
cut or rename a file or directory
mv <existing file name> <new file name>
Command:
mv file01.txt file02.txt
Comment: Here file01.txt will be rename with file02.txt
4. rm:
remove the file and directory
rm <file name>
Command:
rm file01.txt
Comment: The file01.txt will be removed
rm -r <directory name>
Command:
rm -r dir01
Comment: The dir01 directory will be removed
5. pwd:
show directory path
6. tar:
Archive files and directories.
tar czfv <compress directories name> <main directories name>
c = compress
z = zip
f = forcefully
v=
Command:
tar czfv dir01.tar.gz dir01
Comment: Here dir01 will be archive or compress by dir.tar name.
7. gzip:
Compress files.
gzip <file name>
Command:
gzip file01
Comment: file01 will compress
8. gunzip:
Decompress files.
gunzip <file name>
Command:
gunzip file01
9. History:
Show the previous used commands
10. If we want to show some specific line from file01.txt file
head -n<line number> <file name>
Command:
head -n1 file01.txt
Comment: Here output will show us the 1st line of the file01.txt
tail -n<line number> <file name>
Command:
tail -n1 file01.txt
Comment: Here output will show us the last line of the file01.txt
11. grep:
grep something from file which we want to see.
grep "<content which we want to see>" <file name>
Command:
grep "hello" file01.txt
Comment: The output will show us the line by filter where the hello word exist.
grep "<content which we want to see>" <file name> -i
grep "hello" file01.txt -i
Comment: The output will show us the line by filter where the hello and Hello
word exist. (-i is ignore case sensitive)
12. kill:
Terminate a process.
13. awk: Text processing language.
14. cat:
show internal content of any file.
cat <file name>
Command:
cat file01.txt
Comment:
It will show the all content of file01.txt
Exercises 1:
1. go to home and display current directory
Command for home directory:
cd ~
Command for display current directory:
pwd
Or, cd ~ ; pwd
2. change directory to /bin using absolute path
Command:
cd /bin
3. change directory to /etc using relative path
Suppose my current directory is /home/Downloads/PNR
Command: cd .. .. .. ; cd /etc
4. display the long listing of /bin from current directory
Command:
ls /bin
5. list the contents of ~
Command:
cd ~ ; ls
6. create a directory ‘temp’ in your home from current directory
Suppose right now i am in /home/class01
Command:
cd ~ ; mkdir temp
7. remove the directory ‘temp’ from your home
Command:
rm -r temp
8. try to get help on pushd and popd commands
Get help on pushd
Get help on popd
9. go to your home and display the directory
Command:
cd ~ ; pwd
10. push the directory /etc
11. display the current directory
Command:
pwd
12. pop the directory and display the directory
Exercises 2:
1. display first 12 lines of /etc/passwd
Command:
head -n12 /etc/paasswd
2. display last line of /etc/passwd
Command:
tail -n1 //etc/passwd
3. create a file me.txt using cat command with contents
me
him
her
Command:
cat > me.txt
Input content:
me
him
her
ctrl + d to back in command mode
4. display contents of file me.txt in reverse order
Command:
head me.txt | rev
5. use more to display /var/log/messages file
6. display readable strings from binary /usr/bin/passwd
Command:
Strings /usr/bin/passwd
7. use ls to find biggest file in /etcdirectory
Command:
ls -lsS
8. create m1.txt, m2.txt, m3.txt and list only .txt files using ls command
Command:
touch m1.txt m2.txt m3.txt ; ls -X | grep ‘\.txt’
# create file filea.txt as
1. India
2. US
# create file fileb.txt as
1. New Delhi
2. New York
Shell Programming
#1
Program:
User Input:
Output:
# 2 : Make element list 1-5:
Program:
Output: