10 Linux Commands For Developers

10 Linux Commands For Developers

Following are the  list of ten commands you will learn about in this post

Lets get started.

1. Man

The first command you should learn in Linux is “man”. Using this command you can get the usage and description of all  Linux commands. For example, if you want to know about “ls” command and its options, just execute “man ls” command in the terminal to list its usage and description.

2. Touch, Cat And Less

Touch command is used to create any type of file in Linux systems with “0” size. As a developer , when working with Linux you might want to create files in the server. You can make use of touch command to do that.

Cat command is used to view the contents of a file. You cannot edit the contents of the file using cat. It just gives a view of the file. cat doesn’t support scrolling using keyboard.

Note: To work the commands given below, copy any content to the demo.txt file using your favorite vi or nano editor. For our examples, i have copied the boot.log file contents on to demo.txt file using ” cat /var/log/boot.log >> demo.txt” command. You can also do the same.

Less command also gives the view of a file. less is very fast and you can use the arrow keys to scroll up and down to know the start and end of the file. There is also “more” command, which is used to view the file but it allows only forward scrolling using “enter” key. It doesn’t support backward scrolling.

3. Sort And Grep

Sort is used to sort the contents of a file. Create a file named test.txt and copy the following contents on to the file to test the sort command.

In the above example, second column has the names. So if you want to sort the names alphabetically use “-k” flag with the column location. It would be “-k2”.

The first column has numbers. If you want to sort numbers, use  “-h” flag. If the numbers are in different column, you can use the “-k” flag along with “-h” flag.

READ  How To Setup GUI For Amazon EC2 Linux Instance

The last column has months. You can sort a file based on month using “-M” flag.

Note: If you want to eliminate the duplicate lines , you can use “-u” flag along with the sort command.

To sort the file in descending order, use “-r” flag.

Grep:

Grep is a powerful command and will be used by sysadmins quite often. Grep command is used for searching specific string patterns in a file as well as the standard output (STDIN). We will look into few file based operations . Other uses of grep is out of scope of this post.

The above command gives the output including the sub-string. If you want to search for individual words, you need to add “-i” flag to the grep command. Also you can search for a string or a pattern in multiple files using a single grep command. For example,

You can also use regular expressions for matching the string.

4. Cut

Cut command is used for extracting a portion of a file using columns and delimiters. If you want to list everything in a selected column, use the “-c” flag with cut command. For example, lets select the first two columns from our test.txt file.

If you want to extract specific strings from a file, you can used the delimiter “-d” flag and “-f” flag to select the field. For example, if you wan to extract all the names from our test.txt file you can use the following command.

The following example extracts the users from /etc/passd file using ‘:” delimiter.

5. Sed

sed is a text-editor which can perform editing operations in a non-interactive way. Sed command gets its input from a standard input or a file to perform the editing operation on a file. Sed is a very powerful utility and you can do a lot of file manipulations using sed. I will explain the important operation you might want to do with text file.

I you want to replace a text in a file by searching it in a file, you can use the  sed  command with substitute “s” flag to search for the specific pattern and change it.

READ  How To Install Latest Sonatype Nexus 3 on Linux

For example, lets replace  “mike” in test.txt file to “michael

In the above example we used “/” as a delimiter for string substitution. You can use any character as a delimiter for substitution. For example, if you want to make changes to a url, you need to have a different delimiter because the url already have slashes. So you can substitute like the following.

You can also replace a line by matching a string pattern in the line. “-c” flag is used for replacing text using sed. Lets replace the first line in our test.txt file using the following command.

6. Tar

tar command is used to create and extract archive files. “-cf” and “-xf” flags are used for creating and extracting archives.

Lets  create a tar archive out of test.txt file

Lets extract the test.tar archive to the destination folder “demo” using “-C” flag.

7. Find

find command is used for finding files. You can find the files using its name with “-name” flag.

You can also find folder using its name by using”/ -name” flag.

8. Diff

diff command is used to find the difference between two files. Diff command analyses the files and prints the lines which are different. Lets say we have two files test and test1. you can find the difference between the two files using the following command.

9. Uniq

uniq command is used for filtering out the duplicate line in a file.

10. Chmod

chmod command is used for changing the read/write/execute permissions of a file. Permissions are represented in numbers as follows.

To give all permissions on test.txt file, you can use the following chmod command.

Leave comment

Your email address will not be published. Required fields are marked with *.