FoxuTech

Linux Commands Every Developer Should Know

Linux Commands Every Developer Should Know

Now days Linux is growing very fast compare to other operating systems, so all application getting developed in cross platform, so that time, developer are required to work on linux. Not an administrator level but some random commands should aware to work on linux. Here we have listed linux commands developer should know.

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

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.

Syntax:

# man <command name>

# man ls

# 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 with .

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.

Syntax:

# touch <filename>

# touch any.txt

root@foxutech:~# touch any.txt

root@foxutech:~# ls

any.txt

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.

Syntax: cat <filename>

# cat anyname.txt

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.

Syntax: less <filename>

more <filename>

# less anyname.txt

# more anyname.txt

sort

Sort command is helpful to sort/order lines in text files. You can sort the data in text file and display the output on the screen, or redirect it to a file. Based on your requirement, sort provides several command line options for sorting data in a text file.

Sort Command Syntax:

# sort [-options]

# cat test

aa aa zz

aa aa ff

aa aa tt

aa aa kk

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”.

# sort -k3 test

aa aa ff

aa aa kk

aa aa tt

aa aa zz

The following sort command sorts lines in test file on the 3rd word of each line and displays sorted output.

# sort -k3 test

aa aa ff

aa aa kk

aa aa tt

aa aa zz

# cat test

aa aa zz

aa aa ff

aa aa tt

aa aa kk

Grep:

grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines.

In addition, two variant programs egrep and fgrep are available. egrep is the same as grep -E. fgrep is the same as grep -F. Direct invocation as either egrep or fgrep is deprecated, but is provided to allow historical applications that rely on them to run unmodified.

Syntax: grep “<search string>” <filename>

grep “aa” test

root@foxutech:~# grep “aa” test

aa aa zz

aa aa ff

aa aa tt

aa aa kk

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,

# grep “dennis” test1.txt test2.txt test3.txt

You can also use regular expressions for matching the string.

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.

cut -c1-2 test.txt

root@foxutech:~# cut -c1-2 test.txt

1

10

45

4

7

58

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 want to extract all the names from our test.txt file you can use the following command.

cut -d’ ‘ -f2 test.txt

root@foxutech:~# cut -d’ ‘ -f2 test.txt

mike

lucy

Dave

dennis

Megan

Mathew

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

cut -d’:’ -f1 /etc/passwd

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.

Syntax: sed ‘s/<old-word>/<new-word>/’ test

For example, lets replace  “aa” in test file to “bb”

root@foxutech:~# sed ‘s/aa/bb/’ test

bb aa zz

bb aa ff

bb aa tt

bb aa kk

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.

echo “http://www.foxutech.us/main.html” | sed ‘s_us/main_com/index_’

root@foxutech:~# echo “http://www.foxutech.us/main.html” | sed ‘s_us/main_com/index_’

http://www.foxutech.com/index.html

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

tar

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

Syntax: tar <options> <archive-name> <file/folder name>

Let’s  create a tar archive out of test.txt file

root@foxutech:~# tar -cf test.tar test.txt

root@foxutech:~# ls

test.tar test.txt

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

root@foxutech:~# tar -xf test.tar -C /root/test/

root@foxutech:~# cd test/

root@foxutech:~/test# ls

test.txt

find

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

root@foxutech:/home/ubuntu# cd ~

root@foxutech:~# find -name test

./demo/test

./test

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

root@foxutech:~# find / -name passwd

/etc/cron.daily/passwd

/etc/pam.d/passwd

/etc/passwd

/usr/share/test/overrides/passwd

diff

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

Syntax: diff <filename1> <filename2>

diff test.txt test1.txt

root@foxutech:~# diff p1.pl p1.pl.in

19c19

< use constant DEBUG_LOG_PATH         => ‘${prefix}/var’ ;

> use constant DEBUG_LOG_PATH         => ‘@LOGDIR@’ ;

Uniq

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

Syntax: uniq

uniq test.txt

root@foxutech:~# uniq test

aa aa zz

aa aa ff

aa aa tt

aa aa kk

chmod

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

4 – read permission

2 – write permission

1 – execute permission

0 – no permission

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

chmod 755 test.txt

Exit mobile version