Introduction to 10 Useful Unix Commands :

In this article, I am going to list out the 10 most useful Unix commands. But first, let us see why the need for this list.

If we can split people into groups based on the OS they use, a recent statistical survey shows that more than 80% of users (both desktop and laptop) use Windows-based machines. But the same survey also shows that more than 80% of internet public servers are Unix or Unix-like OS, which means that most of the web applications are deployed in Unix based servers. Therefore, irrespective of the OS they use in their local/personal machines, most of them are working with Unix based machines indirectly.

So, in my opinion, it is only necessary that every developer have some knowledge in Unix and its basic commands. Following are the 10 most useful Unix commands, which will help your everyday needs while development:

(Also I assume, you have some idea of basic Unix commands like ls, cd, cp, mv, mkdir, etc., and so I will skip those commands here.)

1. find:

Locates a file by name inside the current directory and all subdirectories

$ find . -type f -name 'nameofthefile.ext' 1 $ find . - type f - name 'nameofthefile.ext'

1a. You can also use the * meta character if you want to find every file ending in .txt (or any other extension)

Example 1:

$ find . -type f -name '*.txt' 1 $ find . - type f - name '*.txt'

1b. To find file names with 755 file permissions

$ find . -type f -perm 0777 -print 1 $ find . - type f - perm 0777 - print

1c. Find all empty directories under certain path.

$ find /tmp -type d -empty 1 $ find / tmp - type d - empty

For more details, you can type:

$ man find 1 $ man find

2. grep:

Grep can be used when you need to search for content within a file.

Usages:

To search for the given string in a single file

$ grep "literal_string" filename 1 $ grep "literal_string" filename

To search for the given string in multiple files.

$ grep "this" demo_*.* 1 $ grep "this" demo_* . *

To search a string in file using a regular expression. The below example returns all the lines with “sample[anychars]empty” string:

$ grep "sample.*empty" demo_file 1 $ grep "sample.*empty" demo_file

3. sed:

sed is a “stream-oriented” editor, and a very powerful one at that. It allows to perform complex search and replace functions across a set of files.

A file “sample.txt”, for example, contains the text “This is only for testing purposes, learner”. The following command would alter the word “test” to “beta”.

$ sed -i '' s/test/beta/g /home/username/sample.txt 1 $ sed - i '' s / test / beta / g / home / username / sample . txt

Usages:

To delete the first line of a file: The below command will delete first line of the file. You can mention the number in the command:

$ sed -i '1d' /home/username/sample.txt 1 $ sed - i '1d' / home / username / sample . txt

To delete a range of lines in a file: Below command will delete 5th to 10th lines in the file.

$ sed -i '4,10d' /home/username/sample.txt To delete empty lines in the file $ sed '/^$/d' /home/username/sample.txt 1 2 3 4 $ sed - i '4,10d' / home / username / sample . txt To delete empty lines in the file $ sed '/^$/d' / home / username / sample . txt

4. head:

This command prints the first ‘n’ number of data of the given input. By default, it prints the first 10 lines of each given file.

Usages:

To print the first ‘n’ number of lines: head -n number filepath

Example:

$ head -n 6 sample.txt 1 $ head - n 6 sample . txt

To print the ‘n’ number of bytes, you can use -c option

$ head -c 13 sample.txt 1 $ head - c 13 sample . txt

To ignore the last ‘n’ lines of a file

$ head -n -5 sample.txt 1 $ head - n - 5 sample . txt

To skip printing last n bytes

$ head -c -30 sample.txt 1 $ head - c - 30 sample . txt

To display the first four lines of every file in the working directory

$ head -n 4 -q *.* 1 $ head - n 4 - q * . *

5. locate

As the name suggests, the locate command is used to locate specified files. With the help of this command, you can perform a quick search for the location of a specific file or a group of files. The locate command uses the database created by ‘updatedb’.

Usages:

To list all the files having the word ‘apache’, it will list the files with full dir path.

$ locate samplefile.txt 1 $ locate samplefile . txt

To list only the number of matching files.

$ locate -c samplefile.txt 1 $ locate - c samplefile . txt

To list only the number of matching files, which exist. This is because, if we delete a file and if have not updated the updatedb, the locate command will return the deleted files also in the list.

$ locate -e samplefile.txt 1 $ locate - e samplefile . txt

6. alias

alias is a built-in shell command that lets you assign a name (as a substitute or nickname!) for another command. Reasons to do this can mainly be because either the command is too long, and needs a shorter name, or it is frequently used. In both cases, you might feel the need for a simpler name to substitute a command for faster use.

For example, you can set,

$ alias myrs='sudo service mysql restart' 1 $ alias myrs = 'sudo service mysql restart'

Now, if you run $ myrs, it will restart the mysql service. It is that simple. You do not have to type the entire long command again and again.

This command can also be reversed using the ‘unalias’ commad. This means that at a later point, if you do not want the alias name assigned to the particular command, you can break or reverse the association using the ‘unalias’ command. The usage is exactly similar to the ‘alias’ command.

Exampls:

$ unalias myrs='sudo service mysql restart' 1 $ unalias myrs = 'sudo service mysql restart'

This will unset the name assigned to this command.

7. tar:

“tar” is an acronym for tape archive. The tar command is used to rip a collection of files and directories into highly compressed archive files commonly known as tarball or tar, (gzip and bzip in Linux).

Usages:

To create a tar archive file sample-01-01-17.tar for a directory /home/sampleuser in current working directory:

$ tar -cvf sample-11-11-16.tar /home/sampleuser/ 1 $ tar - cvf sample - 11 - 11 - 16.tar / home / sampleuser /

To compress files into a gzip archive file we can use the option as z: The below command will create a compressed photos-11-11-12.tar.gz file for the directory /home/sampleusers/photos. (note : tar.gz and tgz are both similar).

$ tar -cvfz photos-11-11-12.tar.gz /home/sampleusers/photos 1 $ tar - cvfz photos - 11 - 11 - 12.tar.gz / home / sampleusers / photos

To uncompress tar.gz archive file, you can run the following command and it will extract it in current folder:

$ tar -xvf photos-11-11-12.tar.gz 1 $ tar - xvf photos - 11 - 11 - 12.tar.gz

To extract in specific folder,

$ tar -xvf photos-11-11-12.tar.gz /home/newuser/photos

8. scp:

This is a file transfer command. It is very useful to transfer file(s) from one server to another.

Usages:

To transfer a file from the local to a server:

$ scp mytarball.tar.gz username@someservername.com:/home/username/foldername 1 $ scp mytarball . tar . gz username @ someservername . com : / home / username / foldername

To transfer a file from a server to the local machine:

$ scp username@someservername.com:/home/username/foldername ./mytarball.tar.gz 1 $ scp username @ someservername . com : / home / username / foldername . / mytarball . tar . gz

To transfer a file from local to a server by authorized private key file (secure transfer):

$ scp -i serverkey.pem mytarball.tar.gz username@someservername.com:/home/username/foldername 1 $ scp - i serverkey . pem mytarball . tar . gz username @ someservername . com : / home / username / foldername

9. df & du

These two are commands associated with the disk. The df & du commands will help to check disk free space & files space usage respectively.

The following command will list all the disks names, its size, usage, available information & where these disks are mounted, etc.

$ df -h 1 $ df - h

The following command will list all the files, directories & sub-directories occupying space. This will help you to identify which folders have occupied more space in the server disk.

$ df -h / $ df -h /home/myuser/* 1 2 $ df - h / $ df - h / home / myuser / *

10. lsof

The lsof command lists all the open files of all the active processes.

Usages:

To list the open files of logged-in users:

$ lsof 1 $ lsof

If the same is used with sudo, it will list all the open files of all users.

To list all open ports and its listening processes: It will display all the services running on the different port, and thereby see which services are running in which ports. :

$ sudo lsof -i -P -n | grep LISTEN 1 $ sudo lsof - i - P - n | grep LISTEN

By specifying the user name, we can list all the open files by the specific user in the system:

$ sudo lsof -u anyusername 1 $ sudo lsof - u anyusername

These are, in my opinion, some of the most important and useful Unix commands all developers should know, so as to ease the development process for themselves. We, at Agira, insist that all our developers be aware of these Unix commands. They might be helpful at various situations, sometimes when least expected! Happy learning…