Mastering Linux Commands: A Step-By-Step Guide
Linux is known for its powerful command-line interface, providing users with commands to perform various tasks. In this guide, we will explore several essential Linux commands to perform common file and directory operations.
Viewing the Contents of a File
To view the contents of a file in Linux, you can use the cat command.
For example, to view the contents of a file called sample.txt, you would run:
eg:- cat sample.txt
Changing File Access Permissions
Linux allows you to modify file permissions using the chmod command.
For instance, to make a file file.txt readable, writable, and executable for the owner, but only readable for others, you can use:
eg:- chmod 744 file.txt
Checking Command History
To check the list of commands you've previously executed in the terminal, you can use the history command. It will display a numbered list of your command history.
eg:- history
Removing a Directory or Folder
The rm command can be used to remove files and directories. To delete a directory named my_folder and its contents, use the -r (recursive) flag:
eg :- rm -r my_folder
Creating and Viewing File Content
To create a new text file, you can use the touch command. For example, to create a file named fruits.txt and view its content, use the following commands:
eg:- touch fruits.txt
cat fruits.txt
Adding Content to a File
You can use the echo command to add content to a file. To add fruits to a file named devops.txt, one per line, you can do:
eg:- echo -e "Apple\nMango\nBanana\nCherry\nKiwi\nOrange\nGuava" > devops.txt
Displaying the Top and Bottom Three Lines of a File
To display the top three lines of a file (e.g., fruits.txt), you can use the head command:
eg:- head -n 3 fruits.txt
To show the bottom three lines of a file, use the tail command:
eg:- tail -n 3 fruits.txt
Creating and Viewing Another File
To create another file, such as Colors.txt, and view its content, you can use the same touch and cat commands as before:
eg :- touch Colors.txt
cat Colors.txt
Adding Content to the "Colors.txt" File
You can use the echo command to add colour names to the Colors.txt file, one per line:
eg:- echo -e "Red\nPink\nWhite\nBlack\nBlue\nOrange\nPurple\nGrey" > Colors.txt
Finding Differences Between Files
To find the differences between two files, you can use the diff command. For example, to compare fruits.txt and Colors.txt, run:
eg:- diff fruits.txt Colors.txt