As a programmer, you will use Linux more or less in your software development career and will probably use Linux commands to retrieve the information you need. This article will share 10 useful Linux commands for developers that you will find helpful.
Here are the Linux commands we are going to introduce today:
man
touch, cat and less
sort and grep
cut
sed
tar
find
diff
uniq
chmod
Let’s go through them one by one.
1, the man command
The first Linux command you need to know is the man command, which displays the usage and description of a given 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 lsroot@devopscube:~# man lsLS(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 specia
fied.
Mandatory arguments to long options are 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 with size 0 in Linux. As a developer, you can use the touch command when you need to create a file on a Linux server:
Syntax: touch <filename>touch demo.txt
root@devopscube:~# touch demo.txtroot@devopscube:~# lsdemo.txt
The cat command is used to view the contents of a file. However, the cat command does not edit the contents of a file. It only browsed the contents of a file. The cat command does not support page turning by pressing up and down keys on the keyboard.
Syntax: cat <filename>cat demo.txt
The same less command allows you to browse through files. Less is very fast and supports up and down keys to see the beginning and end of a file. However, the more command is similar to this command, except that the more command can only use the Enter key to move files forward and does not support rollback.
Syntax: less <filename>
more <filename>
less demo.txt
more demo.txt
3. Sort and grep commands
Using the sort command, you can sort the file contents. Create a file named test.txt and copy the following contents into it:
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 above example, 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” :
Grammar: sort
sort -k2 test.txt
Sorting result
root@devopscube:~# sort -k2 test.txt45 Dave level expert dec4 dennis start beginner jul10 lucy level beginer mar58 Mathew Head CEO nov7 Megan employee trainee feb1 mike level intermediate jan
The first column is a number, and if you want to sort by number, you can use the “-h” option. If the number is in a different column, you can use the -k option after the -h option:
root@devopscube:~# 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:
root@devopscube:~# sort -k5 -M test.txt1 mike level intermediate jan7 Megan employee trainee feb10 lucy level beginer mar4 dennis start beginner jul58 Mathew Head CEO nov45 Dave level expert dec
Note: If you want to eliminate duplicate lines, you can use the “-u” option after the sort command.
Use the “-r” option to order the files in reverse order:
root@devopscube:~# sort -h -r test.txt58 Mathew Head CEO nov45 Dave level expert dec10 lucy level beginer mar7 Megan employee trainee feb4 dennis start beginner jul1 mike level intermediate jan
The Grep command:
The Grep command is powerful and often used by system administrators. The grep command can search for a string in a specified format in a file and output it as standard output.
Syntax: grep “<search string>” <filename>
grep “Mathew” test.txt
root@devopscube:~# grep “dennis” test.txt4 dennis start beginner jul
The output of the command above contains the substring, and if you want to retrieve the full word, you need to add the “-i” option. At the same time, you can use the grep command to search for strings in multiple files. The command code is as follows:
grep “dennis” test1.txt test2.txt test3.txt
Of course, you can also use regular expressions to match strings.
4. Run cut
The cut command lets you extract a specified portion of a file with columns or separators. If you want to list the entire contents of a column in a file, use the “-c” option. For example, the following will extract the entire contents of columns 1 and 2 from the test.txt file.
cut -c1-2 test.txtroot@devopscube:~# cut -c1-2 test.txt110454758
If you want to extract the specified string from the file, you can select the column using the separator option “-d” and the “-f” option. For example, we can extract the names column with the cut command:
root@devopscube:~# cut -d’ ‘ -f2 test.txtmike
lucyDavedennisMeganMathew
The following example extracts the Users column from /etc/passd file:
cut -d’:’ -f1 /etc/passwd
5. Sed
Sed is an online editor that processes content one line at a time. During processing, the current line is stored in a temporary buffer, called the pattern space. The contents of the buffer are then processed with the sed command. When the processing is complete, the contents of the buffer are sent to the screen. Proceed to the next line and repeat until you reach the end of the file. The contents of the file do not change unless you store the output using redirection.
If you want to replace the specified content in the file by searching for it, 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 test.txt:
sed ‘s/mike/michael/’ test.txtroot@devopscube:~# 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. 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.txtroot@devopscube:~# tar -cf test.tar test.txtroot@devopscube:~# lstest.tar test.txt
Use the “-c” option to unpack the test.tar file to the “demo” directory:
tar -xf test.tar -C /root/demo/root@devopscube:~# tar -xf test.tar -C /root/demo/root@devopscube:~# cd demo/root@devopscube:~/demo# lstest.txt
7, find command
The find command is used to search for files. You can use the “-name” option to search for files with a specified name:
find -name find -name test.txtroot@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 passwdroot@devopscube:~# find / -name passwd/etc/cron.daily/passwd/etc/pam.d/passwd/etc/passwd/usr/share/lintian/overrides/passwd
Diff command
The diff command is used to find the differences between two files. The diff command analyzes the contents of the file and prints out the different lines. The following example shows the differences between two files, test and test1:
Syntax: diff <filename1> <filename2>
diff test.txt test1.txt
root@devopscube:~# diff test.txt test1.txt7c7< 59 sdfsd—> 59 sdfsd CTO dec
9. Uniq command
Using the uniq command, you can filter out duplicate lines in a file.
Grammar: uniq
uniq test.txt
root@devopscube:~# uniq test.txt1 mike level intermediate jan10 lucy level beginer mar45 Dave level expert dec4 dennis start beginner jul7 Megan employee trainee feb58 Mathew Head CEO nov
10. Chmod command
Using the chmod command, you can change the read/write/execute permission of a file. The permission value is as follows:
4 – read permission2 – write permission1 – execute permission0 – no permission
The following command can give the highest permissions to the test. TXT file:
chmod 755 test.txt