Every programmer needs to understand 10 Linux commands.
As a programmer, Linux systems are more or less used in software development, and Linux commands may be used to retrieve the required information. This article will share with developers 10 useful Linux commands, hoping to help you.
The following are the Linux commands we will introduce today:
Man
Touch, cat and less
Sort and grep
Cut
Sed
Tar
Find
Diff
Uniq
Chmod
Next, let's give a detailed introduction.
1. man command
The first Linux Command you need to know is the man command, which can display the usage and description of the specified command. For example, if you want to know the usage and options of the ls command, you can run "man ls" on the terminal ":
Syntax: man <command name>
Man ls
Root @ devopscube :~ # Man ls
LS (1) User Commands LS (1)
NAME
Ls-list directory contents
SYNOPSIS
Ls [OPTION]... [FILE]...
DESCRIPTION
List information about the FILEs (the current directory by default ).
Sort entries alphabetically if none of-cftuvSUX nor -- sort is speciâ
Fied.
Mandatory arguments to long options are mandatory for short options
Too.
-A, -- all
Do not ignore entries starting.
2. touch, cat, and less commands
The touch command can create any type of files with a size of 0 in Linux. As a program developer, you can use the touch command to create files on a Linux Server:
Syntax: touch <filename>
Touch demo.txt
Root @ devopscube :~ # Touch demo.txt
Root @ devopscube :~ # Ls
Demo.txt
The cat command is used to view the content of a file, but the cat command cannot edit the content of the file. It can only browse the content of the file. The cat command does not support keyboard up/down key paging.
Syntax: cat <filename>
Cat demo.txt
The same less command allows you to browse files. The less command is very fast and supports up/down keys to view the beginning and end of a file. However, the more command is similar to it, except that the more command can only use the enter key to flip forward the file, and does not support rollback.
Syntax: less <filename>
More <filename>
Less demo.txt
More demo.txt
3. sort and grep commands
The sort command is used to sort the file content. Create a file named test.txt and copy the following content to the file:
1 mike level intermediate jan
10 lucy level beginer mar
45 Dave level expert dec
4 dennis start beginner jul
7 Megan employee trainee feb
58 Mathew Head CEO nov
In the above example, the second column is the name, so if you want to sort the name column in alphabetical order, you can use the "-k" option and mark the column number, for example, "-k2 ":
Syntax: sort
Sort-k2 test.txt
Sorting result
Root @ devopscube :~ # Sort-k2 test.txt
45 Dave level expert dec
4 dennis start beginner jul
10 lucy level beginer mar
58 Mathew Head CEO nov
7 Megan employee trainee feb
1 mike level intermediate jan
The first column is numbers. If you want to sort by numbers, you can use the "-h" option. If the number is in different columns, you can use the "-k" option after the "-h" option:
Root @ devopscube :~ # Sort-h test.txt
1 mike level intermediate jan
4 dennis start beginner jul
7 Megan employee trainee feb
10 lucy level beginer mar
45 Dave level expert dec
58 Mathew Head CEO nov
The last column is the month. You can use the "-M" option to sort the file content by month:
Root @ devopscube :~ # Sort-k5-M test.txt
1 mike level intermediate jan
7 Megan employee trainee feb
10 lucy level beginer mar
4 dennis start beginner jul
58 Mathew Head CEO nov
45 Dave level expert dec
Note: If you want to remove duplicate rows, you can use the "-u" option after the sort command.
Use the "-r" option to arrange objects in reverse order:
Root @ devopscube :~ # Sort-h-r test.txt
58 Mathew Head CEO nov
45 Dave level expert dec
10 lucy level beginer mar
7 Megan employee trainee feb
4 dennis start beginner jul
1 mike level intermediate jan
Grep command:
The Grep command is very powerful and is often used by system administrators. The grep command searches for strings in the specified format in the file and outputs the strings in the standard format.
Syntax: grep "<search string>" <filename>
Grep "Mathew" test.txt
Root @ devopscube :~ # Grep "dennis" test.txt
4 dennis start beginner jul
The output result of the preceding command contains the sub-string. to retrieve the complete word, you need to add the "-I" option. You can also use the grep command to search strings in multiple files. The command code is as follows:
Grep "dennis" test1.txt test2.txt test3.txt
You can also use regular expressions to match strings.
4. cut command
The cut command allows you to extract specified parts of the file using columns or delimiters. If you want to list all the content of a column in the file, you can use the "-c" option. For example, the following section extracts all the content in columns 1st and 2 from the test.txt file.
Cut-C1-2 test.txt
Root @ devopscube :~ # Cut-C1-2 test.txt
1
10
45
4
7
58
If you want to extract the specified string from the file, you can use the delimiter options "-d" and "-f" to select columns. For example, we can use the cut command to extract names columns:
Cut-d ''-f2 test.txt
Root @ devopscube :~ # Cut-d ''-f2 test.txt
Mike
Lucy
Dave
Dennis
Megan
Mathew
The following example extracts the users column from/etc/passd file:
Cut-d': '-f1/etc/passwd
5. sed command
Sed is an online editor that processes a row of content at a time. During processing, the currently processed rows are stored in the temporary buffer, called the pattern space. Then, the sed command is used to process the content in the buffer, send the buffer content to the screen. Next, process the next row, and repeat until the end of the file. The file content is not changed unless you use the redirection storage output.
If you want to search for the specified content in the replacement file, you can use the "s" option to retrieve it and then replace it.
Syntax: sed's/<old-word>/<new-word>/'test.txt
For example, replace "michael" with "mike" in the test.txt file ":
Sed's/mike/michael/'test.txt
Root @ devopscube :~ # Sed's/mike/michael/'test.txt
1 michael level intermediate jan
10 lucy level beginer mar
45 Dave level expert dec
4 dennis start beginner jul
7 Megan employee trainee feb
58 Mathew Head CEO nov
6. tar command
The tar command is used to compress and decompress files. The "-cf" and "-xf" options are often used.
Syntax: tar <options> <archive-name> <file/folder name>
Let's package the test.txt file:
Tar-cf test.tar test.txt
Root @ devopscube :~ # Tar-cf test.tar test.txt
Root @ devopscube :~ # Ls
Test.tar test.txt
Decompress the test.tar file that has just been packaged into the "demo" directory using unzip-c‑scripts:
Tar-xf test.tar-C/root/demo/
Root @ devopscube :~ # Tar-xf test.tar-C/root/demo/
Root @ devopscube :~ # Cd demo/
Root @ devopscube :~ /Demo # ls
Test.txt
7. find command
The find command is used to retrieve files. You can use the "-name" option to retrieve files with the specified name:
Find-name test.txt
Root @ devopscube:/home/ubuntu # cd ~
Root @ devopscube :~ # Find-name test.txt
./Demo/test.txt
./Test.txt
You can also use "/-name" to retrieve the folder with the specified name:
Find/-name passwd
Root @ devopscube :~ # Find/-name passwd
/Etc/cron. daily/passwd
/Etc/pam. d/passwd
/Etc/passwd
/Usr/share/lintian/overrides/passwd
8. diff command
The diff command is used to identify the differences between the two files. The diff command analyzes the file content and prints different lines. The following example shows the differences between the two files test and test1:
Syntax: diff <filename1> <filename2>
Diff test.txt test1.txt
Root @ devopscube :~ # Diff test.txt test1.txt
7c7
<59 sdfsd
---
> 59 sdfsd CTO dec
9. Uniq command
The uniq command is used to filter duplicate lines in the file:
Syntax: uniq
Uniq test.txt
Root @ devopscube :~ # Uniq test.txt
1 mike level intermediate jan
10 lucy level beginer mar
45 Dave level expert dec
4 dennis start beginner jul
7 Megan employee trainee feb
58 Mathew Head CEO nov
10. chmod command
The chmod command is used to change the read, write, and execute permissions of a file. The permission values are as follows:
4-read permission
2-write permission
1-execute permission
0-no permission
The following command can grant the highest permission to the test.txt file:
Chmod 755 test.txt