As a programmer, a Linux system is used more or less in a software development career, and Linux commands may be used to retrieve the required information. This article will help you to share 10 useful Linux commands for your developers.
Here's the Linux command we're going to cover today:
Mans Touch, cat and less sort and grep cut sed tar find diff uniq chmod
Let's go through this in more detail.
1. Man command
The first Linux command you need to know is the man command, which displays the usage and description of the specified command. For example, if you want to know the use and options of the LS command, you can perform "man ls" at the terminal:
Syntax: Man <command name>man ls
[Email protected]:~# man Lsls (1) User Commands ls (1) NAME ls-list directory contentssynopsis 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 is Mandatory for short options too. -A,--all do not ignore entries starting with.
2, Touch,cat and less commands
The touch command can create any type of file of size 0 on a Linux system, and as a program developer, you can use the Touch command when you need to create files on a Linux server:
Syntax: Touch <filename>touch demo.txt[email protected]:~# touch demo.txt[email protected]:~# lsdemo.txt
The cat command is used to view the contents of a file, but using the cat command does not edit the contents of the file, it is only possible to browse the contents of the file. The cat command does not support paging the keyboard up and down keys.
Syntax: Cat <filename>cat Demo.txt
The same less command allows you to browse through the file, the lower command is very fast, and the up and down keys are supported to view the beginning and end of the file. The more command, however, is similar to it, except that the more command only uses the ENTER key to page forward the file and does not support fallback.
Syntax: less <filename> more <filename>
Less Demo.txtmore Demo.txt
3. Sort and grep commands
The sort command is used to order the contents of a file. Create a file named Test.txt, and copy the following to the file:
1 Mike level intermediate JAN10 Lucy level Beginer mar45 Dave level expert DEC4 Dennis start beginner JUL7 Megan Employee Trainee feb58 Mathew Head CEO, Nov
In the example above, the second column is the name, so if you want to sort the name column alphabetically, you can use the "-k" option and label the column number, such as "-K2":
Syntax: Sortsort-k2 test.txt
Sort results
[Email protected]:~# sort-k2 test.txt45 Dave level expert DEC4 Dennis start beginner jul10 Lucy level Beginer mar58 mathe W Head CEO nov7 Megan Employee Trainee FEB1 Mike level intermediate Jan
The first column is numbers, and if you want to sort by numbers, you can use the "-H" option. If the number is on a different column, you can use the "-k" option after the "-H" option:
[Email protected]:~# sort-h test.txt 1 Mike level intermediate JAN4 Dennis start beginner JUL7 Megan Employee Trainee FEB10 Lucy level Beginer mar45 Dave level expert DEC58 Mathew Head CEO Nov
The last column is the month, and you can use the "-M" option to sort the contents of the file by month:
[Email protected]:~# sort-k5-m test.txt1 Mike level intermediate JAN7 Megan employee trainee FEB10 Lucy level Beginer MA R4 Dennis start beginner jul58 Mathew Head CEO nov45 Dave level expert Dec
Note: If you want to eliminate duplicate rows, you can use the "-u" option after the sort command.
Using the "-r" option, the file is in reverse order:
[Email protected]:~# sort-h-R test.txt58 Mathew Head CEO nov45 Dave level expert dec10 Lucy level beginer MAR7 Megan EMP Loyee Trainee feb4 Dennis start beginner JUL1 Mike level intermediate Jan
grep command:
The grep command is very powerful and is often used by system administrators. The grep command can search a file for a string in a specified format, while also making standard output for it.
Syntax: grep "<search string>" <filename> grep "Mathew" Test.txt
[Email protected]:~# grep "Dennis" Test.txt4 Dennis start Beginner Jul
The output of the above command contains the substring, and if you want to retrieve the complete word, you need to add the "-i" option. You can also use the grep command to search for strings in multiple files, with the following command code:
grep "Dennis" Test1.txt Test2.txt Test3.txt
Of course you can also use regular expressions to match strings.
4. Cut command
The Cut command allows you to extract the specified part of a file with a column or delimiter. If you want to list the entire contents of a column in a file, you can use the "-C" option. For example, the following will extract the entire contents of columns 1th and 2 from the Test.txt file.
Cut-c1-2 Test.txt
[Email protected]:~# cut-c1-2 test.txt110454758
If you want to extract the specified string from the file, you can use the delimiter option "-D" and the "-F" option to select the column. For example, we can use the Cut command to extract names columns:
Cut-d '-f2 test.txt
[Email protected]:~# cut-d '-f2 test.txtmikelucydavedennismeganmathew
The following example extracts the Users column from the/ETC/PASSD file:
Cut-d ': '-f1/etc/passwd
5. SED command
Sed is an online editor that processes a single line of content at a time. When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then the next line is processed, so it repeats until the end of the file. The file content does not change unless you use redirection to store the output.
If you want to replace the specified content in the file by searching, 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 "Mike" with "Michael" in the Test.txt file:
Sed ' s/mike/michael/' test.txt
[Email protected]:~# sed ' s/mike/michael/' test.txt1 Michael level intermediate jan10 Lucy level Beginer mar45 Dave level Expert DEC4 Dennis start beginner JUL7 Megan Employee trainee feb58 Mathew Head CEO, Nov
6. Tar command
The tar command is used to compress and decompress files, often using the "-CF" and "-XF" options.
Syntax: Tar <options> <archive-name> <file/folder name>
Let's package the Test.txt file:
TAR-CF Test.tar Test.txt
[Email protected]:~# tar-cf test.tar test.txt[email protected]:~# lstest.tar test.txt
Use the "-C" option to extract the Test.tar file you just packaged to the "demo" Directory:
TAR-XF test.tar-c/root/demo/
[Email protected]:~# tar-xf test.tar-c/root/demo/[email protected]:~# cd demo/[email protected]:~/demo# lstest.txt
7. Find command
The Find command is used to retrieve the file, and you can use the "-name" option to retrieve the file with the specified name:
Find-name find-name Test.txt
[Email protected]:/home/ubuntu# cd ~[email protected]:~# 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
[Email protected]:~# 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 find the different points of 2 files. The diff command analyzes the contents of the file and then prints the different lines, and the following example identifies the differences between the two file test and the Test1:
Syntax: diff <filename1> <filename2> diff test.txt Test1.txt
[Email protected]:~# diff test.txt test1.txt7c7< sdfsd---> sdfsd CTO Dec
9. Uniq command
The Uniq command is used to filter duplicate rows in a file:
Syntax: Uniq uniq test.txt
[Email protected]:~# uniq test.txt1 Mike level intermediate jan10 Lucy level Beginer mar45 Dave level expert DEC4 Dennis S Tart Beginner JUL7 Megan Employee trainee feb58 Mathew Head CEO of Nov
10.chmod Command
The chmod command is used to change the file's read/write/execute permissions, and the value of the permissions is as follows:
4-read permission2-write permission1-execute permission0-no permission
The following command can give the Test.txt file the highest permissions:
chmod 755 Test.txt
If you have any thoughts and comments on this article, please give your comment, I am looking forward to it!
10 Linux commands that every programmer needs to know