Introduction
Hello everyone! Today, we'll learn some basic Linux commands that can help you manage files, view their content, and perform various operations on them. Don't worry, I'll guide you through each step.
Let's get started!
View the contents of a file: In Linux, you can easily view the contents of a file using the "cat" command. For example, to see what's written in a file named "sample.txt", you can use the following command:
Change access permissions of files: To change the permissions of a file, you can use the "chmod" command. It allows you to set read, write, and execute permissions for the file's owner, group, and others. Here's a simple example:
Check your command history: The "history" command is handy for checking the list of commands you have run in the past. Just type:
Remove a directory/folder: To delete a directory (also known as folder) and its contents, you can use the "rmdir" command. For example,
Please note that the directory must be empty for this command to work. If it contains files or other directories, you can use the "rm" command with the "-r" option like this:
Create a new file and view its content: To create a new file, you can use the "touch" command. For example, to create a file named "fruits.txt",
type: touch fruits.txt
Next, you can use the "cat" command to view the content of the file:
Add content to a file: To add content to a file, you can use the "echo" command and redirect its output to the file using the ">>" operator. Let's add fruits to the "devops.txt" file, one fruit per line:
Show only the top three fruits from the file: To display only the top three fruits from the "devops.txt" file, you can use the "head" command:
This will show the first three lines of the file.
Show only the bottom three fruits from the file: To see only the bottom three fruits from the "devops.txt" file, you can use the "tail" command:
This will display the last three lines of the file.
Create another file and view its content: Similarly, you can create another file named "Colors.txt" using the "touch" command:
To view the content of this file, use the "cat" command:
Add content to the new file: Let's add some colors to the "Colors.txt" file, one color per line:
Find the difference between two files: To find the difference between the "fruits.txt" and "Colors.txt" files, you can use the "diff" command:
This command will highlight the differences between the two files, if any.
Conclusion
That's it! You've learned some essential Linux commands to manage files and directories. Practice them, and soon you'll become a Linux command pro! Happy learning!