Linux is probably the most used operating system in the world. It runs more than 90% of the internet, about 70% of world’s smart-phones (Android is technically a flavor of Linux, in case you did not know) and it has made its headway into desktop market too. If you are interested in using Linux or have it installed already, here are 10 terminal commands you will find useful while fiddling around with the OS:
df
df
stands for Disk Free and reports the amount of free space on each disk mounted. The output looks something like this:
Showing the free spaces on the disk
Those 1K-block counts are a little cumbersome to read. You’d have to calculate the size in MBs and GBs yourself. To make df
print human readable sizes, pass it the ‘h’ parameter, like so: df -h
Showing the human-readable free space on the disk
You can now see that it now shows human-readable sizes.
alias
As you go on working on Linux, you would come to realise that there are commands that you need to run pretty often. For example, updating your Linux distribution is a fairly regular task (unless you are using it on a Server). You could save some time by creating aliases for those commands.
An alias is a name given to another command, sometimes with parameters. The format for creating an alias is: alias shortname="longcommand"
If you find yourself running ls -alh
pretty regularly, you can alias it to llh
like this:
You can alias other commands with switches into individual commands as per your desire
In the image above, we can see:
- The shell/teminal refuses to recognise the
llh
command. - We run
ll -alh
in directory. - Then we make an alias
llh
whose value is set tols - alh
- We run
llh
and this time, the terminal responds with output of the assigned command for the alias. You can verify that by looking at the output ofllh
.
pwd
This is probably one command which you would use the least but in a time when you are boggled down by too many terminals and a deadline where you need to fix issues in one directory by referring to 10 others (such a thing is common when you are configuring something on a remote server), you want to know which directory you are working under. pwd
, standing for present working directory tells you the present directory in which you are working.
Most Linux distributions make a decent effort to show you the directory you are working in by adding them as part of the prompt. However, that is not a mandate. If you are greeted by a prompt which says: 14:34:10 machine203 $
then you would have no idea which directory you are under. pwd
is a life savior in such cases.
cat
No, this command was not named after the animal. cat
is named after the word concatenation and was primarily intended to concatenate (or join) two or more files and show their output to terminal (or redirect the output to a file).
{% include figure image_path=“/assets/images/posts/useful-commands-in-linux/linux-cat-command.png” alt=“cat command output” caption=”cat
can be used to show the contents of a single file or join multiple files into one!” %}
ls
ls
stands for list and is used to list files and folders in the present directory (or you can pass the directory path as an argument).
By default, ls shows a list of file names only. If you want to list more details, add the -l
to perform a long listing which shows more info about the files.
less
less
is used to vertically scroll through a file (or an output of a command ) in the terminal. For example, if you run the command dmesg
to look into what the OS kernel has done since bootup, you would be presented with a very long list of strings. To browse through such a long array of text, you would have to scroll the terminal - which is neither very user friendly nor always available as a feature, especially in remote logins.
Try piping that piping the output to less
and you will see that not only can you browse through the content but also search through it.
Quick Tip: To exit the view presented by less
, press Q key on the keyboard.
cut
There are a number of commands which give their output in a pretty formatted manner. We have already seen such a command - ls -l
. Suppose you want to show only the list of file and directory owners in the current directory, then you can run ls -l | cut -d' ' -f3
and that should show you the list of file or directory owners.
cut
can be used to cut output vertically!
What you did was to pipe the output of ls -l
to cut
and you asked cut
to use a single space as a delimiter (-d' '
) and display field number 3 (-f3
).
du
du
stands for disk usage and is one of the most useful tools you can have. It reports the size of a directory - i.e. the cumulative size of all files and directories nested underneath the concerned directory. When run without any arguments, it reports size of files and folders in the current directory. Let’s see the default output:
Output of the du
command
Numbers on the left are file or directory sizes and most certainly they are not very friendly to us, who would rather prefer the sizes reported in Kilo, Mega and GigaBytes instead. Like the df
command, add the -h
switch to the command.
Output of the df
command reporting human readable size
Now it is much better, right?
NOTE: The bottom line tells the cumulative size of the current directory. You might ask Hey, why don’t those numbers add up? The answer is: du
does not report the size of files in the list. Now, a number of times, you would want to know just the total size of all files and folders in a particular directory. For those cases, use the -sh
switch. The h
stands for human, while s
now stands for summary. This is how it would look like:
Output of the du
command reporting directory size summary in human readable size
And in case you are trying to drill down the issue of “What is taking up space on the computer?”, you would need to look for the largest file or folder in the current directory. Append a ./*
to the previous command, like this: du -sh ./*
and du will tell you the sizes of folders and files in the current directory. For folders, it would not tell the size of all files and folders it finds nested within, only the summarized size of all folders in the current directory:
Output of the du
command reporting directory listing with size summary in human readable size
grep
grep
standing for Global Regular Expression Print is a tool used to search for text within some text. It comes in handy when you are searching for text within a file or from the output of another program. For example, you have a large log file and you want to find out a particular incident, it can be painful to do so without grep
.
By default, grep
prints the lines which it finds what you want it to search. Also, by default, grep
performs a case-sensitive search.
grep
is case sensitive by default
Here we are passing (piping) the output of the command dmesg
to grep
for searching. You can see that searching for link is down results in 0 finds but when we pass the -i
switch along, grep
does perform a case-insensitive search and finds the lines where “Link is down” is mentioned.
adding the -n
switch causes grep
to dump the line numbers where it found the required text
We have but scratched the surface of grep
. It is best used as a tool to find patterns in the text. That, however is beyond the scope of this article.
sort
sort
does what its name says: it sorts. You feed it a list of something and it will spit out a sorted list of those items.
It is often used to sort a list of files or numbers outputted by another program. For example, you have a list of names in a file which you want to sort alphabetically. Or you want to sort the output of du
to see which files and folders are consuming the most space.