Linux Commands(Day 03) : Advanced Command-Line Techniques and Automation

Table of contents
- File and Directory Management:
- File Viewing and Editing:
- Permissions and Ownership
- Networking:
- Task: What is the linux command to
- To view what's written in a file.
- To change the access permissions of files.
- To check which commands you have run till now.
- To remove a directory/ Folder.
- To create a fruits.txt file and to view the content.
- Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.
- To Show only top three fruits from the file.
- To Show only bottom three fruits from the file.
- To create another file Colors.txt and to view the content.
- Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey.
- To find the difference between fruits.txt and Colors.txt file.

File and Directory Management:
ls:
Purpose: Lists directory contents.
Example: ls -la (shows details, including hidden files).
ls -la
cd:
Purpose: Changes current directory.
Example: cd directoryname.
cd dir2
pd:
Purpose: Prints current directory path.
Example : pwd.
output: /home/user
pwd
mkdir:
Purpose: Creates a new directory.
Example: mkdir directory_name.
mkdir directory_name
r/rmdir :
- Purpose: Deletes files or directories.
Examples:
rm-rf :- Forcefully removes even non empty files and directory.
rm-rp :- Removes non empty directory including parent and sub directory.
rm-r :- Removes empty directory.
rm-rf*:- Removes all directory & files except hidden files.
rmdir :- Removes the specified directory.
rmdir-p :- Remove parent & child directory.
rmdir-pv :- Remove all parent & sub directories along with verbose.
rmdir directory_name
cp:
Purpose: Copies files or directories.
Example : cp file_name (Source) file_name (destination).
Data of file1 will be overwritten to file2.
cp file1 file2
mv:
Purpose: Moves files into directories or renames files.
Note: Directory can also be move to directory but file can’t move to file.
Examples:
mv File_name (Source) Directory_name(Destination). {used to move file to directory}
mv File_name (oldname) File_name(Newname). {used to rename file or directory}
mv File1 Dir1 mv File1 Myfile
touch:
Purpose: Creates a single or many empty File and used to change timestamp of a file or update any one type of time of a file.
Example : touch file_name
Time Stamp: Every file has three types of time associated with it.
Access time:- Last time when a file was accessed.
Example : touch-a (it will change access time to current time)
Modify time:- Last time when a file was modified.
Example : touch -m (it will change modify time to current time)
Change time:- Last time when file meta data was changed.
Example : touch -c (it will change change time to current time)
touch file1 touch -a touch -m touch -c
File Viewing and Editing:
cat:
Purpose: Creates Single File , Combine two or more files to make one , To add new content in same file , To display file content.
Example :
cat > file_name {used to create file}
cat file_name {used to show content inside file}
cat file1_name file2_name » file3_name {Data of file1 & file2 will be combined and data will be shown into file3}
cat » file_name { since content can’t be edit but new content can be added}
cat > file1 cat file1 cat file1 file2 » file3 cat » file1
less:
Purpose: Views files page by page.
Example: less file_name.
less file1
more:
Purpose: Views files (simpler than less).
Example: more file_name.
more file1
nano:
Purpose: Edits text files (beginner-friendly).
Example: nano file_name (Ctrl+O → Ctrl+X → Y to exit).
nano file1
vim:
Purpose: Edits text files (advanced).
Example: vim file_name(i to edit, :wq to save/quit).
vim file1
head:
Purpose: Shows first 10 lines of a file.
Example: head file_name (first 10 lines).
head file1
tail:
Purpose: Shows last 10 lines of a file.
Example: tail file_name(last 10 lines).
tail file1
Permissions and Ownership
chmod:
Purpose: Modifies file/directory permissions.
Example: chmod 777 directory_name.
chown:
Purpose: Changes file/directory ownership.
Example: chown username file_name/directory_name.
chgrp:
Purpose: Changes file/directory group.
Example: chgrp groupname file_name/directory_name
ls -l:
Purpose: Displays permissions and details.
Example: ls -l.
chmod 777 dir1 chown username file1 chgrp groupname file
Networking:
ping:
Purpose: Tests network connectivity.
Example: ping google.com.
curl:
Purpose: Fetches data from URLs.
Example: curl https://example.com.
wget:
Purpose: Downloads files from the web.
Example: wget <url>.
ifconfig:
Purpose: Shows same as windows ip config tells us ip & ethernet and Nid of our machine.
Example: ifconfig.
ip:
Purpose: Manages network settings (modern).
Example: ip addr (shows IP addresses).
ping google.com. curl https://example.com wget https://example.com/file.txt ifconfig ip addr
Task: What is the linux command to
To view what's written in a file.
cat – View Entire File.
cat filename
To change the access permissions of files.
chmod - change mode.
chmod mode filename
mode: can be numeric (755) or symbolic (u+rwx).
filename is the file or directory whose permissions you want to change.
To check which commands you have run till now.
- history :- This will display a numbered list of all previously executed commands.
history
To remove a directory/ Folder.
Use the rmdir command if the directory is empty.
Use the rm-r command.
rmdir myfolder rm -r dir1
To create a fruits.txt file and to view the content.
Using touch.
Using cat.
Using vim/vi .
Using nano.
touch fruits.txt cat > fruits.txt vim fruits.txt nano fruits.txt # To view : cat fruits.txt
Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.
Using echo with \n.
Using cat.
echo -e "Apple\nMango\nBanana\nCherry\nKiwi\nOrange\nGuava" > devops.txt cat >> devops.txt # now editor will open after cat command and we'll put manually Apple Mango Banana Cherry Kiwi Orange Guava
To Show only top three fruits from the file.
head command will be used.
head -n 3 devops.txt
To Show only bottom three fruits from the file.
tail command will be used.
tail -n 3 devops.txt
To create another file Colors.txt and to view the content.
Using touch.
Using cat.
Using vim/vi .
Using nano.
touch Colors.txt cat > Colors.txt vim Colors.txt nano Colors.txt # To view : cat Colors.txt
Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey.
Using echo with \n.
Using cat.
echo -e "Red\nPink\nWhite\nBlack\nBlue\nOrange\nPurple\nGrey" > Colors.txt cat >> Colors.txt # now editor will open after cat command and we'll put manually Red Pink White Black Blue Orange Purple Grey
To find the difference between fruits.txt and Colors.txt file.
Using diff to Compare Files.
diff fruits.txt Colors.txt
Subscribe to my newsletter
Read articles from Aditya Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
