TABLE OF CONTENTS
a) To view the content of the file
b) To change the access permissions of files
c) To check which commands you have run till now.
d) To remove a directory/ Folder.
e)To create a fruits.txt file and view the content
f) Add content in fruit's.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.
g) To Show only the top three fruits from the file.
h) Show only the bottom three fruits from the file.
i) To create another file Colors.txt and to view the content.
j)Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, and Grey.
k) To find the difference between fruits.txt and Colors.txt files.
To view the content of the file
Cat: Cat(concatenate) command is very frequently used in Linux. It reads data from the file and gives its content as output. It helps us to create, view, and concatenate files.
1) To view a single file
Syntax:
cat file_name
2) To view the content of Multiple files
cat file_name1 file_name2
3)To Create and add content to the file
cat > newfile_name
4)To Append the content in a file
cat >> file_name
5)To copy the content from old to a new file
cat (older file name) > (newer file name)Display number of lines in front of each line in a file.
6)To count number of lines in a file
cat -n <filename>
To change the access permissions of files:
Chmod: chmod command is used to change the access permissions of files and directories. The name is an abbreviation of change mode. The chmod
command is used to modify this permission so that it can grant or restrict access to directories and files.
In the Linux file system, each file is associated with a particular owner and have permission access for different users. The user classes may be:
owner
group member
Others (Everybody else)
The file permissions in Linux are the following three types:
r: Read permissions. The file can be opened, and its content viewed.
w: Write permissions. The file can be edited, modified, and deleted.
x: Execute permissions. If the file is a script or a program, it can be run (executed).
For example:
---
means no permissions have been granted at all.rwx
means full permissions have been granted. The read, write, and execute indicators are all present.
Syntax:
chmod [options] [mode] [File_name]
This mode can be specified in several ways, we will discuss two modes: Symbolic and Octal mode.
1) Symbolic mode
In symbolic mode, File permissions are specified in letters and operators to set or tell what to do with permissions
Operators | Definition |
+ | Add permissions |
- | Remove permissions |
= | Set the permissions to the specified values |
Reference | Class |
u | Owner |
g | Group |
o | Others |
a | All (owner, groups,others) |
Examples of Using the Symbolic mode:
Adding permissions to the file owner/group/users:
chmod g+w[file_name]
Remove write permission for the group and others:
chmod go-w [file_name]
2) Octal mode
It is also a method for specifying permissions. In this method, we specify permission using a three-digit number. Where
First digit specifies the permission of Owner.
Second digit specifies the permission for Group.
Third digit specifies the permission for Others.
Value | Permission |
4 | Read Permission |
2 | Write Permission |
1 | Execute Permission |
Examples of Using the Octal mode:
chmod 674 [file_name]
All the permissions are given to the file.
chmod 777 <filename>
c) To check which commands you have run till now.
History: history command is used to view the previously executed command. In Bash shell history command shows the whole list of the command.
syntax:
$ history
d) To remove a directory/ Folder.
rmdir: This command is used to delete a directory. But will not be able to delete a directory including a sub-directory. It means a directory has to be empty to be deleted.
Syntax:
rmdir <dirname>
e) To create a fruits.txt file and view the content
touch file.txt # To create a file
echo "Content to be added in file" > file.txt #To add content in a file
cat filename.txt # To view the content of any text file.
f) Add content in fruits.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.
vi fruits.txt
cat fruits.txt
g) To Show only the top three fruits from the file.
syntax:
head -n 3 <filename> To show top three content in the file
h) To Show only the bottom three fruits from the file.
syntax:
tail -n 3 <filename> To show bottom three content in the file
i)To create another file Colors.txt and to view the content.
touch <file.txt> # To create a file
cat <filename.txt> # To view the content of any text file.
j)Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, and Grey.
echo -e "Red\nPink\nWhite\nBlack\nBlue\nOrange\nPurple\nGrey" > filename.txt
k) To find the difference between fruits.txt and Colors.txt files.
diff <firstfile.txt> <secondfile.txt> #The diff command will show
# the lines that are different
#between the two files,
#highlighting any changes made.
These are some basic Linux commands that will prove helpful throughout your DevOps journey.
If you found this blog/article post helpful, please share it with others who might benefit from it and share your valuable feedback.
Thank you, Shubham Londhe
#Devops #devopsjourney #trainwithshubham #linux #90daysofchallenge #90daysofdevopschallenge #devopslearning