10 Useful Unix Commands – Introduction:
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.)
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)
$ find . -type f -name 'nameofthefile.ext'
1b. To find file names with 755 file permissions
$ find . -type f -name '*.txt'
1c. Find all empty directories under certain path.
$ find . -type f -perm 0777 -print
For more details, you can type:
$ find /tmp -type d -empty
$ man find
Grep can be used when you need to search for content within a file.
To search for the given string in a single file
To search for the given string in multiple files.
$ grep "literal_string" filename
To search a string in file using a regular expression. The below example returns all the lines with “sample[anychars]empty” string:
$ grep "this" demo_*.*
$ grep "sample.*empty" demo_file
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
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.
$ sed -i '1d' /home/username/sample.txt
$ sed -i '4,10d' /home/username/sample.txt
To delete empty lines in the file
$ sed '/^$/d' /home/username/sample.txt
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.
To print the first ‘n’ number of lines: head -n number filepath
To print the ‘n’ number of bytes, you can use -c option
$ head -n 6 sample.txt
To ignore the last ‘n’ lines of a file
$ head -c 13 sample.txt
$ head -n -5 sample.txt
To skip printing last n bytes
To display the first four lines of every file in the working directory
$ head -c -30 sample.txt
$ head -n 4 -q *.*
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’.
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.
$ locate 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 -c samplefile.txt
$ locate -e samplefile.txt
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.
$ alias myrs='sudo service mysql restart'
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.
This will unset the name assigned to this command.
$ unalias myrs='sudo service mysql restart'
“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).
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/
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:
$ tar -cvfz photos-11-11-12.tar.gz /home/sampleusers/photos
$ tar -xvf photos-11-11-12.tar.gz
To extract in specific folder,
$ tar -xvf photos-11-11-12.tar.gz /home/newuser/photos
This is a file transfer command. It is very useful to transfer file(s) from one server to another.
To transfer a file from the local to a server:
To transfer a file from a server to the local machine:
$ scp mytarball.tar.gz email@example.com:/home/username/foldername
To transfer a file from local to a server by authorized private key file (secure transfer):
$ scp firstname.lastname@example.org:/home/username/foldername ./mytarball.tar.gz
9. df & du
$ scp -i serverkey.pem mytarball.tar.gz email@example.com:/home/username/foldername
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.
$ df -h
$ df -h /
$ df -h /home/myuser/*
The lsof command lists all the open files of all the active processes.
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:
$ sudo lsof -i -P -n | grep LISTEN
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…
$ sudo lsof -u anyusername