Basic Linux Commands
Task: What is the linux command to
1. To view what's written in a file.
The command to view the contents of a file in Linux is "cat". Here's how to use it:
For example, if you want to view the contents of a file named "mytextfile.txt" located in your home directory, you would type:
cat ~/mytextfile.txt
This will display the contents of the file in the terminal window.
2. To change the access permissions of files.
In Linux, you can use the "chmod" command to change the access permissions of files. The syntax of the "chmod" command is as follows:
chmod [options] mode file(s)
Where "options" are optional parameters you can use to modify the behavior of the command, "mode" is the new permission mode you want to set, and "file(s)" are the name(s) of the file(s) you want to change the permissions of.
The "mode" parameter is a combination of three digits, each representing a different set of permissions:
Each digit can be a value between 0 and 7, representing a different set of permissions:
Here are some examples of how to use the "chmod" command:
chmod 700 myfile.txt
chmod 640 myfile.txt
Note that you need to have the appropriate permissions to change the permissions of a file. If you are not the owner of the file or a superuser, you may need to use the "sudo" command to run the "chmod" command with elevated privileges.
3. To check which commands you have run till now.
In Linux, you can use the "history" command to view a list of the commands you have run in the current session. Here's how to use it:
This will display a numbered list of the commands you have run in the current session, starting with the most recent. The output will look something like this:
history
1? ls
2? cd Documents
3? nano myfile.txt
4? sudo apt-get update
5? ping google.com
6? history
Each command is preceded by a number, which you can use to run the command again by typing "!" followed by the command number. For example, to run the "ls" command again, you would type "!1" and press Enter.
You can also use the "history" command with the "-c" option to clear the command history. This can be useful if you don't want others to see the commands you have run, or if you want to start a new session with a clean slate. To clear the command history, type "history -c" and press Enter.
4. To remove a directory/ Folder.
In Linux, you can use the "rmdir" or "rm" command to remove a directory/folder. The "rmdir" command is used to remove an empty directory, while the "rm" command is used to remove a directory and its contents. Here's how to use both commands:
rmdir directoryname
Where "directoryname" is the name of the directory you want to remove. For example, to remove a directory called "mydir", you would type:
rmdir mydir
Note that you must first remove any files or subdirectories within the directory before you can remove it using the "rmdir" command. If the directory is not empty, you will get an error message.
rm -r directoryname
Where "directoryname" is the name of the directory you want to remove. The "-r" option stands for "recursive", which tells the "rm" command to remove the directory and all of its contents, including subdirectories and files. For example, to remove a directory called "mydir" and all of its contents, you would type:
rm -r mydir
Be very careful when using the "rm" command with the "-r" option, as it will permanently delete all files and subdirectories within the specified directory without prompting for confirmation. Always double-check that you have specified the correct directory before running this command.
5. To create a fruits.txt file and to view the content.
To create a file called "fruits.txt" in Linux, you can use the "touch" command, which will create an empty file with the specified name. Here's how to use it:
touch fruits.txt
This will create an empty file called "fruits.txt" in the current directory. To add content to the file, you can use a text editor such as "nano" or "vim", or you can use the "echo" command to write text to the file directly from the command line.
Here's an example of how to use the "echo" command to add some text to the "fruits.txt" file:
领英推荐
echo "apples" >> fruits.txt
echo "oranges" >> fruits.txt
echo "bananas" >> fruits.txt
This will add the words "apples", "oranges", and "bananas" to the "fruits.txt" file, with each word on a separate line. The ">>" symbol is used to append the output of the "echo" command to the end of the file, rather than overwriting the existing contents.
To view the contents of the "fruits.txt" file, you can use the "cat" command, as I explained in a previous question. Here's how to use it:
cat fruits.txt
This will display the contents of the "fruits.txt" file in the terminal window.
6. Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.
To add the fruits "Apple", "Mango", "Banana", "Cherry", "Kiwi", "Orange", and "Guava" to a file called "devops.txt", with each fruit on a separate line, you can use 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
Each command uses the "echo" command to write the name of a fruit to the end of the "devops.txt" file, with the ">>" symbol used to append the text to the end of the file. When you are finished, you can view the contents of the "devops.txt" file using the "cat" command:
cat devops.txt
This will display the contents of the "devops.txt" file in the terminal window, with each fruit on a separate line.
7. To Show only top three fruits from the file.
To show only the top three fruits from the "devops.txt" file, you can use the "head" command to display the first few lines of the file, like this:
head -n 3 devops.txt
The "-n" option specifies the number of lines to display, so "-n 3" tells the "head" command to display the first three lines of the "devops.txt" file. The output will look like this:
Apple
Mango
Banana
This will show only the top three fruits from the "devops.txt" file, which in this case are "Apple", "Mango", and "Banana".
8. 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 to display the last few lines of the file, like this:
tail -n 3 devops.txt
The "-n" option specifies the number of lines to display, so "-n 3" tells the "tail" command to display the last three lines of the "devops.txt" file. The output will look like this:
Kiwi
Orange
Guava
This will show only the bottom three fruits from the "devops.txt" file, which in this case are "Kiwi", "Orange", and "Guava".
9. To create another file Colors.txt and Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey.
To create a file called "Colors.txt" and To add the colors "Red", "Pink", "White", "Black", "Blue", "Orange", "Purple", and "Grey" to a file called "Colors.txt", with each color on a separate line, you can use the following commands:
touch Colors.txt # creates an empty file called "Colors.txt"
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
Each command uses the "echo" command to write the name of a color to the end of the "Colors.txt" file, with the ">>" symbol used to append the text to the end of the file. When you are finished, you can view the contents of the "Colors.txt" file using the "cat" command:
cat Colors.txt
This will display the contents of the "Colors.txt" file in the terminal window, with each color on a separate line.
10. To find the difference between fruits.txt and Colors.txt file.
To find the difference between two files, you can use the "diff" command in Linux. In this case, to find the difference between the "fruits.txt" and "Colors.txt" files, you can use the following command:
diff fruits.txt Colors.txt
This command will compare the contents of the two files and display any differences between them. If there are no differences, the command will produce no output.
If there are differences, the output will show which lines are different. For example, if the "fruits.txt" file contains the line "Apple" and the "Colors.txt" file does not, the output might look like this:
1,3c1,8
< apples
< oranges
< bananas
---
> Red
> Pink
> White
> Black
> Blue
> Orange
> Purple
> Grey
The first line of the output, "1,3c1,8", indicates the range of lines that are being compared. In this case, lines 1 to 3 of the first file ("fruits.txt") are being compared to lines 1 to 8 of the second file ("Colors.txt").
The lines that begin with "<" indicate lines that are present only in the first file ("fruits.txt"). In this case, the lines "< apples", "< oranges", and "< bananas" are present only in "fruits.txt".
The "---" line separates the changes made to the first file from the changes made to the second file. The lines that begin with ">" indicate lines that are present only in the second file ("Colors.txt"). In this case, the lines "> Red", "> Pink", "> White", "> Black", "> Blue", "> Orange", "> Purple", and "> Grey" are present only in "Colors.txt".
So the output indicates that "fruits.txt" contains the lines "apples", "oranges", and "bananas", while "Colors.txt" contains the lines "Red", "Pink", "White", "Black", "Blue", "Orange", "Purple", and "Grey". This information can be helpful in identifying the differences between the two files.