Unlocking Linux Mastery: Advanced Commands for Power Users
Day 3: Basic Linux Commands
Table of contents
- To View the contents of a file
- Change access permissions of files
- Check your command history
- Remove a directory/folder
- Create an animals.txt file and view its content
- Add content in animals.txt
- To Show only the top five animals from the file
- To Show only the bottom five animals from the file
- To create another file with content in flowers.txt and to view the content.
- To find the difference between animals.txt and flowers.txt file
- Conclusion
To View the contents of a file
Cat
- This Linux command allows you to view the contents of a file
Simply type
cat filename
and PressEnter
Replace "filename" with the name of the file you want to view.
Change access permissions of files
chmod
-To modify the access permissions of a file, use the chmod
command followed by the permission code and the file name.
The permission code consists of three digits representing read (4)
, write (2),
and execute (1)
permissions for the owner, group, and others, respectively.
Simply type
chmod 644 filename
and PressEnter
For example - Here 735 is represented as,
7: Hundred's place denotes owner permission
3: Ten's place denotes group permission
5: Unit's place denotes everyones permission
Furthermore, chmod digits give the following kind of permissions:
1 = Execute
2 = Write
3 = Write & Execute
4 = Read
5 = Read & Execute
6 = Read & Write
7 = Read, Write & ExecuteNow, when we put it all together, we can see what the chmod 735 command means and what it will do to your file permissions:
7: Owner can read, write and execute
3: Group can write and execute
5: Everyone can read and execute
Check your command history
history
-To view the list of commands you have run in the terminal session history.
Simply type
history
and PressEnter
This will display a numbered list of commands you executed during the current session.
Remove a directory/folder
rm
- To remove a directory/ Folder.
Simply type
rm filename/directoryname
and PressEnter
Create an animals.txt file and view its content
touch
- To create an empty file named "animal.txt,"
cat
-To view the contents of the file (we discussed earlier)
Simply type
touch filename
and PressEnter
then ,type
cat filename
and PressEnter
Add content in animals.txt
Vim
- To open a file to add content
Simply type
vim filename
PressEnter
Inserting Text: To start inserting or editing text within the file, press
i
for insert mode. You can then type or modify text as needed.Saving and Exiting: To save the changes and exit vim, press
Esc
to ensure you are in normal mode, and then type:wq
orZZ (Shift + zz)
PressEnter
To Show only the top five animals from the file
head
- To display the top lines of the file
Simply type
head -5 filename
PressEnter
you can also use the
head
command with the-n
option
To Show only the bottom five animals from the file
tail
-To display the last lines of the file,
Simply type
tail -5 filename
PressEnter
you can also use the
tail
command with the-n
option
To create another file with content in flowers.txt and to view the content.
We already discussed this earlier
touch
,vim
andcat
command
To find the difference between animals.txt and flowers.txt file
diff
- To compare the contents of two files and find the differences
Simply type
diff filename1 filename2
PressEnter
The
diff
command will display the lines that are different between the two files, if any.
Conclusion
In summary, mastering a few fundamental Linux commands empowers users to efficiently manage files and directories within the terminal. Commands like cat
allow for easy viewing of file contents, while chmod
facilitating the adjustment of access permissions, ensuring secure file management. Monitoring and reviewing past commands is made simple by history
providing a comprehensive list of previous actions within the session.
Basic file operations, such as creation, editing, and deletion, are streamlined through commands like touch
, vim
, and rm
enabling users to manipulate files swiftly and effectively. Furthermore, commands like 'head' and 'tail' offer quick insights by displaying the top or bottom lines of a file, respectively. For more advanced tasks, such as comparing file contents, diff
provides a straightforward solution, highlighting any discrepancies between files. By familiarizing oneself with these commands, users gain greater control and efficiency in navigating the Linux environment.
I hope you found this blog helpful!
Happy learning!