Basic linux commands with Examples (Post-04)
Table of contents
- 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.
To view what's written in a file.
more
: Used to view a text file one page at a time, press the spacebar to go to the next page. π½- Example:
more filename
- Example:
less
: Similar tomore
but with additional features like navigating pages, searching for strings, and using functions similar to the vi editor. β¬- Example:
less filename
- Example:
head
: Displays the first few lines of a file (by default, ten lines). β¬οΈExamples:
head myfile.txt
displays the first ten lines of myfile.txt.head -15 myfile.txt
displays the first fifteen lines of myfile.txt.
tail
: Displays the last part of a file. β¬οΈ- Example:
tail filename
- Example:
cat
: Used to concatenate files or display file contents. π±Examples:
cat 01.txt
displays the contents of file 01.txt.cat 01.txt 02.txt
displays the contents of both files.cat file1.txt file2.txt > file3.txt
combines file1.txt and file2.txt into file3.txt.cat note5 >> notes
appends note5 to notes.cat >> file1
adds additional data to file1.
These commands offer different ways to view and manipulate file content in Linux. Feel free to use the command that suits your specific needs.
To change the access permissions of files.
To change the access permissions of files in Linux, you can use the chmod
command. The chmod
command allows you to modify the read, write, and execute permissions for the owner, group, and other users. Here's how you can use the chmod
command:
Symbolic Mode: You can use symbols (+, -, =) to add, remove, or set specific permissions. The symbols are represented by:
β
+
: Adds permissionsβ
-
: Removes permissionsβ‘οΈ
=
: Sets permissions explicitly
Examples:
To add read and write permissions for the owner:
chmod +rw filename
To remove execute permission for the group:
chmod g-x filename
To set read and execute permissions for others, removing all other permissions:
chmod o=rx filename
Numeric Mode: You can use a numeric value to set permissions explicitly using the octal representation.
π’
4
: Read permissionβοΈ
2
: Write permissionβΆοΈ
1
: Execute permission
Examples:
To set read and write permissions for the owner and read-only permissions for the group and others:
chmod 644 filename
To grant full permissions to the owner, read and execute permissions to the group, and read-only permissions to others:
chmod 750 filename
Remember, you need appropriate permissions to modify the access permissions of a file. The chmod
command can be used with various options and modifiers to change permissions recursively or for multiple files simultaneously. Refer to the chmod
command's manual page (man chmod
) for more details and additional options.
Please exercise caution when changing access permissions, as improper settings may compromise the security or functionality of your files and system.
To check which commands you have run till now.
To check the commands that you have run so far in a Linux terminal, you can use the history
command. The history
command displays a list of previously executed commands along with their corresponding line numbers. π
Here's how you can use the history
command:
Open a terminal. π»
Type
history
and press Enter. β¨οΈ
The command history will be displayed, showing the list of commands you have run during your current terminal session. Each command will be listed with a line number, making it easier to reference specific commands if needed. π
You can also use the history
command with options to modify its behavior. For example, you can use the -c
option to clear the command history or use the -n
option to display command numbers. βοΈ
Please note that the command history is specific to each terminal session, and it may not reflect the complete command history across multiple sessions or if you have started a new terminal session. β οΈ
Now you can easily keep track of the commands you've used in your Linux terminal! β¨
To remove a directory/ Folder.
To remove a directory or folder in Linux, you can use the rmdir
or rm
command. The rmdir
command is used to remove empty directories, while the rm
command can remove both files and directories, including non-empty directories.
Here's how you can use these commands:
rmdir
command:rmdir directory_name
This command will remove an empty directory. If the directory is not empty, you will receive an error.
rm
command:rm -r directory_name
The
-r
option stands for "recursive" and allows therm
command to remove directories and their contents recursively, including non-empty directories. Be cautious when using this command, as it permanently deletes the specified directory and its contents.
Remember to exercise caution when using the rm
command, as it permanently deletes files and directories without confirmation.
Now, go ahead and remove the directory or folder you want to delete! ποΈ
To create a fruits.txt file and to view the content.
To create a fruits.txt
file, you can use the touch
command to create an empty file, and then you can use a text editor like nano
or vi
to add content to the file. Here's how you can do it:
Create the
fruits.txt
file:touch fruits.txt
This command creates an empty file named
fruits.txt
.Open the
fruits.txt
file with a text editor:nano fruits.txt
This command opens the
fruits.txt
file in thenano
text editor.Add the content to the file: Inside the text editor, you can add the names of fruits. For example:
Apple Banana Orange
Save and exit the text editor: In the
nano
text editor, pressCtrl + O
to save the file andCtrl + X
to exit.
Now, you have created the fruits.txt
file with the desired content.
To view the content of the fruits.txt
file, you can use the cat
command:
cat fruits.txt
This command displays the content of the fruits.txt
file in the terminal.
πππ Enjoy exploring the content of your fruits.txt
file!
Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.
To add the specified content to the devops.txt
file, you can use a text editor like nano
or vi
. Here's how you can do it:
Open the
devops.txt
file with a text editor:nano devops.txt
This command opens the
devops.txt
file in thenano
text editor. πAdd the content to the file: Inside the text editor, add the following content, one item per line:
Apple Mango Banana Cherry Kiwi Orange Guava
Save and exit the text editor: In the
nano
text editor, pressCtrl + O
to save the file andCtrl + X
to exit. πΎ
Now, you have added the specified content to the devops.txt
file, with each item on a separate line.
To view the content of the devops.txt
file, you can use the cat
command:
cat devops.txt
This command displays the content of the devops.txt
file in the terminal. π
ππ₯πππ₯ππ Enjoy exploring the content of your devops.txt
file!
To Show only top three fruits from the file.
o show only the top three fruits from the devops.txt
file, you can use the head
command. The head
command allows you to display the first few lines of a file. Here's how you can do it:
head -3 devops.txt
This command displays the first three lines of the devops.txt
file, which will show the top three fruits.
ππ₯π Enjoy exploring the top three fruits from your devops.txt
file!
To Show only bottom three fruits from the file.
To show only the bottom three fruits from the devops.txt
file, you can use the tail
command. The tail
command allows you to display the last few lines of a file. Here's how you can do it:
tail -3 devops.txt
This command displays the last three lines of the devops.txt
file, which will show the bottom three fruits.
ππ₯π Enjoy exploring the bottom three fruits from your devops.txt
file!
To create another file Colors.txt and to view the content.
To create the Colors.txt
file, you can use a text editor like nano
or vi
to create and add content to the file. Here's how you can do it:
Create the
Colors.txt
file using a text editor:nano Colors.txt
This command opens the
Colors.txt
file in thenano
text editor.Add the desired content to the file: Inside the text editor, you can add the names of colors or any other content you prefer. For example:
Red Blue Green Yellow Orange Purple
Save and exit the text editor: In the
nano
text editor, pressCtrl + O
to save the file andCtrl + X
to exit.
Now, you have created the Colors.txt
file with the desired content.
To view the content of the Colors.txt
file, you can use the cat
command:
cat Colors.txt
This command displays the content of the Colors.txt
file in the terminal.
π¨ Enjoy exploring the content of your Colors.txt
file!
Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey.
To add the specified content to the Colors.txt
file, you can use a text editor like nano
or vi
. Here's how you can do it:
Open the
Colors.txt
file with a text editor:nano Colors.txt
This command opens the
Colors.txt
file in thenano
text editor.Add the content to the file: Inside the text editor, add the following content, one item per line:
Red Pink White Black Blue Orange Purple Grey
Save and exit the text editor: In the
nano
text editor, pressCtrl + O
to save the file andCtrl + X
to exit.
Now, you have added the specified content to the Colors.txt
file, with each color on a separate line.
To view the content of the Colors.txt
file, you can use the cat
command:
cat Colors.txt
This command displays the content of the Colors.txt
file in the terminal.
π΄πΈβͺοΈβ«οΈπ΅πππ«οΈ Enjoy exploring the content of your Colors.txt
file!
To find the difference between fruits.txt and Colors.txt file.
To find the difference between the fruits.txt
and Colors.txt
files, you can use the diff
command. The diff
command compares two files and shows the differences between them.
Here's how you can use the diff
command to compare the contents of the fruits.txt
and Colors.txt
files:
diff fruits.txt Colors.txt
This command compares the contents of the two files and displays the lines that differ between them, if any. If the files are identical, no output will be displayed.
By running the diff
command, you can identify any variations between the fruits.txt
and Colors.txt
files and understand the differences in their contents.
ππππ΄πΈβͺοΈβ«οΈπ΅πππ«οΈ Explore and discover the differences between your fruits.txt
and Colors.txt
files!
Thank you for giving your precious time for reading this blog/article and also follow Shahaji Kadam for more such blogs/articles.
HashTags: #90daysofdevops #devops #cloud #aws #awscloud #awscommunity #docker #linux #kubernetes #k8s #ansible #grafana #terraform #github #opensource #90days #challenge #learning
Subscribe to my newsletter
Read articles from Shahaji Kadam directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by