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>
Mans LS
[Email protected]:~# 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 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:
Grammar: Touch <filename>
Touch Demo.txt
[Email protected]:~# Touch Demo.txt
[Email protected]:~# ls
Demo.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.txt
More 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 Jan
Lucy level Beginer Mar
Dave level expert Dec
4 Dennis start Beginner Jul
7 Megan Employee Trainee Feb
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: sort
Sort-k2 Test.txt
Sort results
[Email protected]:~# sort-k2 test.txt
Dave level expert Dec
4 Dennis start Beginner Jul
Lucy level Beginer Mar
Mathew Head CEO, Nov
7 Megan Employee Trainee Feb
1 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 Jan
4 Dennis start Beginner Jul
7 Megan Employee Trainee Feb
Lucy level Beginer Mar
Dave level expert Dec
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.txt
1 Mike Level intermediate Jan
7 Megan Employee Trainee Feb
Lucy level Beginer Mar
4 Dennis start Beginner Jul
Mathew Head CEO, Nov
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.txt
Mathew Head CEO, Nov
Dave level expert Dec
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 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.txt
4 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.txt
1
10
45
4
7
58
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.txt
Mike
Lucy
Dave
Dennis
Megan
Mathew
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.txt
1 Michael Level intermediate Jan
Lucy level Beginer Mar
Dave level expert Dec
4 Dennis start Beginner Jul
7 Megan Employee Trainee Feb
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]:~# ls
Test.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# ls
Test.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.txt
7c7
< 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.txt
1 Mike Level intermediate Jan
Lucy level Beginer Mar
Dave level expert Dec
4 Dennis start Beginner Jul
7 Megan Employee Trainee Feb
Mathew Head CEO, 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 permission
2-write permission
1-execute permission
0-no permission
The following command can give the Test.txt file the highest permissions:
chmod 755 Test.txt
10 Linux commands that every programmer needs to know