Table of contents
- Command to view what's written inside a file?
- Command to change the access permission of a file?
- Command to check which commands you have run till now.
- Command to remove Directory/Folder in Linux?
- Command to create a fruits.txt file and to view the content?
- Command to Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.
- Command to show only top three fruits from the file?
- Command to Show only the bottom three fruits from the file.
- Command to create another file Colors.txt and to view the content.
- Command to add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey.
- Command to find the difference between fruits.txt and Colors.txt files.
Command to view what's written inside a file?
cat <filename>
The "cat" command shows the content written inside the file.
Example: Suppose, we have one file named "file1.txt" and inside this file "Hello DevOps" is written. So, to view what is written inside the file command should be like this
cat file1.txt
Command to change the access permission of a file?
chmod 777 file1.txt
The Above command gives all permissions i.e. read, write and execute permissions to all ( Users, Groups and Others ).
In the above screenshot, file1.txt has all the permission ( Read, Write, Execute )
Let's Understand how to provide permissions to a file in detail,
Octal | File mode |
1 | --x, executable permission |
2 | --w, write permission |
4 | --r, read permission |
So, suppose you want to provide executable permission to groups, write permission to users and read permission to others, then the command should be like this,
executable permission to groups i.e. octal "1"
write permission to users i.e. octal "2"
read permission to others i.e. octal "4"
command: chmod 124 file1.txt
Command to check which commands you have run till now.
history
history command shows us what all commands we have you till now.
Command to remove Directory/Folder in Linux?
To remove an empty directory,
rmdir <directory-name>
To remove non-empty directories,
rm -r <directory-name>
Here, -r means recursively.
Command to create a fruits.txt file and to view the content?
To create a file "touch" command is used
touch <filename>
To view the content of a file, the "cat" command is used,
cat <filename>
Command to Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.
First create a file with "touch" command,
Example: touch devops.txt
Open devops.txt file with vim editor,
vim, nano editors are used to add content to a file.
Example: vim devops.txt
Press "i" to go into insert mode and add contents to the file,
and press "Esc + :wq" to save and exit from the file.
Command to show only top three fruits from the file?
head -3 <filename>
In the above command, the "head" command gives us first part of a file.
-3, --shows the top 3 lines of a file.
Command to Show only the bottom three fruits from the file.
tail -3 <filename>
In the above command, the "tail" command gives us last part of a file.
-3, --shows the bottom 3 lines of a file.
Command to create another file Colors.txt and to view the content.
touch <filename>
To see the content of a file,
cat <filename>
Command to add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey.
Open colors.txt file with vim editor,
vim, nano editors are used to add content to a file.
Example: vim colors.txt
Press "i" to go into insert mode and add contents to the file,
and press "Esc + :wq" to save and exit from the file.
Command to find the difference between fruits.txt and Colors.txt files.
diff <file1> <file2>
"diff" command is used to check difference between two files.
Let's understand with the help of the below screenshot,
Here, in "devops.txt" file we have content,
Apple
red
blue
yellow
green
maroon
and in "colors.txt" file we have,
red
blue
yellow
green
maroon
diff command shows the difference between two files