Table of contents
- Viewing File Contents:
- Changing Access Permissions of Files:
- Checking Command History:
- Removing a Directory/Folder:
- Creating and Viewing File Contents:
- Adding Content to devops.txt:
- Showing Top Three Fruits:
- Showing Bottom Three Fruits:
- Creating and Viewing the Colors.txt File:
- Adding Content to Colors.txt:
- Finding the Difference Between fruits.txt and Colors.txt:
Introduction: In the world of command-line interfaces (CLI), file and directory management is an essential skill for any developer or system administrator. In this blog post, we will explore various commands and techniques to perform common file operations. We will cover viewing file contents, changing access permissions, removing directories, creating and editing files, as well as comparing file differences. So, let's dive in!
Viewing File Contents:
To view the contents of a file, we can use the cat
command followed by the file name. For example, to view the contents of a file called "fruits.txt," run the following command:
cat fruits.txt
Changing Access Permissions of Files:
To modify access permissions for files, we can use the chmod
command. The command requires a numeric code or symbolic representation for permission settings. For example, to give read and write permissions to the owner of a file, use the following command:
chmod u+rw file_name
Checking Command History:
To view the list of commands you have executed previously, you can use the history
command. It displays a numbered list of recently executed commands. Run the following command to see your command history:
history
Removing a Directory/Folder:
- To remove a directory or folder, use the
rmdir
command followed by the directory name. Note that the directory must be empty for this command to work. For example, to remove a directory named "my_folder," use the following command:
rmdir my_folder
Creating and Viewing File Contents:
To create a new file, you can use the touch
command followed by the file name. For instance, to create a file called "fruits.txt," run the following command:
touch fruits.txt
To view the content of the newly created file, you can use the cat
command mentioned earlier.
Adding Content to devops.txt:
To add content to a file, you can use a text editor or use the echo
command to append content to the end of the file. For example, to add fruits to the "devops.txt" file, run the following commands:
echo "Apple" >> devops.txt
echo "Mango" >> devops.txt
echo "Banana" >> devops.txt
echo "Cherry" >> devops.txt
echo "Kiwi" >> devops.txt
echo "Orange" >> devops.txt
echo "Guava" >> devops.txt
Showing Top Three Fruits:
To display the top three fruits from the file, we can use the head
command with the -n
flag to specify the number of lines to show. For example, to display the top three fruits from "devops.txt," run the following command:
head -n 3 devops.txt
Showing Bottom Three Fruits:
To show the bottom three fruits from the file, we can use the tail
command with the -n
flag. The -n
flag allows us to specify the number of lines to display from the end of the file. For example, to show the bottom three fruits from "devops.txt," run the following command:
tail -n 3 devops.txt
Creating and Viewing the Colors.txt File:
Similar to the process of creating "fruits.txt," we can create another file called "Colors.txt" using the touch
command:
touch Colors.txt
To view the content of the file, you can use the cat
command mentioned earlier.
Adding Content to Colors.txt:
To add content to the "Colors.txt" file, follow the same process as adding content to "devops.txt." Run the following commands:
echo "Red" >> Colors.txt
echo "Pink" >> Colors.txt
echo "White" >> Colors.txt
echo "Black" >> Colors.txt
echo "Blue" >> Colors.txt
echo "Orange" >> Colors.txt
echo "Purple" >> Colors.txt
echo "Grey" >> Colors.txt
Finding the Difference Between fruits.txt and Colors.txt:
To find the difference between two files, we can use the diff
command. In this case, we want to compare "fruits.txt" and "Colors.txt." Run the following command:
diff fruits.txt Colors.txt
The diff
command will display the differences between the files, highlighting added and removed lines.
Conclusion: Mastering file and directory management in the terminal is crucial for efficient development and system administration. In this blog post, we covered various commands, including viewing file contents, changing access permissions, checking command history, removing directories, creating and viewing files, adding content to files, and comparing file differences.