googleads
10 Useful Unix Commands for Web Developers | Essential Commands
Technical Tips & Tricks Unix

10 Useful Unix Commands for Every Web Developers

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

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

1b. To find file names with 755 file permissions

1c. Find all empty directories under certain path.

For more details, you can type:

 

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

To search for the given string in multiple files.

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

 

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

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:

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

 

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:

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

To ignore the last ‘n’ lines of a file

To skip printing last n bytes

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

 

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.

 

To list only the number of matching files.

 

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.

 

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,

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:

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:

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

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

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:

 

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

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

 

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.

 

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.

 

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:

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

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

 

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…

The following two tabs change content below.

One thought on “10 Useful Unix Commands for Every Web Developers”

Leave a Reply

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