Those who are familiar with the terminal application part of Unix based systems, have probably done some work with the help of it. Being a terminal user for some time now, I automate a lot of stuff on my Mac OS X operating system by making use of such a wonderful computer utility.
Although the graphical user interfaces offered by the modern computer operating systems out there make the terminal application seem like a nightmare to the beginner computer user, based on my personal experience I am completely sure that by guiding one through the right path of becoming a command line user, terminal becomes their first choice when interacting with their own operating system of choice.
With the main purpose of helping those in the beginning of their journey with the terminal application, I have compiled a list of ten Unix based commands which I am going to share through this article. As far as my wisdom goes with the command line, Unix based commands work on both Mac OS X and Linux based distributions.
What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.
Note: Anyone running Mac OS X or a Linux distribution will be able to execute on their terminal the commands being shared through this article.
The cd command
The cd command is a very important one and everyone who wants to learn how to use the command line must be aware of it. This utility is crucial when it comes to navigating the filesystem of your own operating system. Based on my personal experience, you have to provide the path of the directory you want to move to as the argument to the cd command.
The path can be a relative one, or an absolute one. The following example tells the shell to move to the home directory.
cd /Users/$USERNAME
For those of you who don’t know, any path which starts with a / is considered an absolute path by Unix or any other system which is built on top of it.
The echo command
The echo command comes as a builtin within the shell of Unix based operating systems. Based on my personal experience with this command, it is very useful especially when one wants to print the values of environment variables. To make things more practical, launch a new terminal application on your own operating system and run the following command.
echo ‘this is a test’
Once the above command has finished executing on your terminal application, the following should be displayed on your own console.
this is a test
And to print the value of an environment variable, type the echo command followed by the name of the environment variable like shown below.
echo $HOME
Once the above command has finished executing in your current shell session, the absolute path of your own home directory should come as an output.
The output which came on my own terminal application is being shown below.
/Users/oltjano
Try to remove the $ symbol from the command which we demonstrated in the above example and see what happens. In other words, type the command shown below inside the terminal application of your own operating system.
echo HOME
The following output should come out on the console.
HOME
The ls -l command
The ls command is perfect when one wants to list files and directories. It becomes even more powerful when combined with the -l option, which stands for the long output. The ls -l command provides detailed output of the files inside the directory which you are listing.
The following is an example that lists the contents of your home directory.
ls -l $HOME
The output coming out on the console after the above command is being executed, is really long.
The following is mine.
total 4088
drwx—— 3 oltjano staff 102 Sep 26 2016 Applications
drwxr-xr-x 7 oltjano staff 238 May 23 16:17 Calibre Library
-rw-r–r– 1 oltjano staff 1906638 Apr 5 2017 Deskto
drwx——+ 33 oltjano staff 1122 Nov 19 22:58 Desktop
drwx——+ 8 oltjano staff 272 Oct 27 18:27 Documents
drwx——+ 354 oltjano staff 12036 Nov 18 03:30 Downloads
drwx——@ 56 oltjano staff 1904 Jun 1 15:31 Library
drwx——+ 6 oltjano staff 204 May 14 2018 Movies
drwx——+ 5 oltjano staff 170 May 23 16:07 Music
drwx——+ 31 oltjano staff 1054 Nov 6 22:21 Pictures
drwxr-xr-x 5 oltjano staff 170 Apr 5 2017 Projects
drwxr-xr-x+ 6 oltjano staff 204 Feb 21 2018 Public
drwxr-xr-x 6 oltjano staff 204 Oct 28 18:38 PycharmProjects
drwxr-xr-x 2 oltjano staff 68 Feb 10 2018 Sites
drwxr-xr-x 3 oltjano staff 102 Apr 29 2018 VirtualBox VMs
drwxr-xr-x 4 oltjano staff 136 Sep 26 2016 Virtualenvs
-rw-r–r– 1 oltjano staff 234 Feb 13 2018 code.py
-rw-r–r– 1 oltjano staff 284 Jul 14 13:27 code.pyc
-rw-r–r– 1 oltjano staff 452 May 18 2017 practical.py
-rw-r–r– 1 oltjano staff 263 May 15 2017 sugar.py
-rw-r–r– 1 oltjano staff 162945 Apr 6 2017 test.txt
-rw-r–r– 1 oltjano staff 432 May 15 2017 thecodeship.py
The output which comes as a result of the ls -l command gives information on the type of file being listed, on the access permissions and also details the owner of the file.
This command is really useful when the user is interested in detailed information on the files being stored in his or her directories.
The pwd command
This command is very useful when you need to know the current working directory while navigating the filesystem. Based on my personal experience with this command, it prints the current working directory.
It takes no arguments. Run the command pwd like shown below.
pwd
Once I managed to execute the above command in the shell of my own operating system, I got the following output.
/Users/oltjano
So as you have probably guessed, the pwd command is necessary when one wants to navigate their filesystem with the help of the cd command. It is important you are aware of the path of the current working directory so you can move through the entire filesystem by providing relative or absolute paths.
The printenv command
This command can be used to print the variables predefined within the environment of the builtin shell. According to the official documentation, the printenv command displays one variable per line, in a name/value format.
To make things more concrete, run the command printenv like shown below.
printenv
The output of the above command is being shown below.
TERM_PROGRAM=Apple_Terminal
SHELL=/bin/bash
TERM=xterm-256color
TMPDIR=/var/folders/vt/nqlv_f8j27j4c136rkjgjd580000gn/T/
Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.b0OKtrgxjf/Render
TERM_PROGRAM_VERSION=361.1
TERM_SESSION_ID=2D652FD1-50B5-48AD-A802-A5B7814B369D
USER=oltjano
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.GFci9jGNSt/Listeners
__CF_USER_TEXT_ENCODING=0x1F5:0x0:0x0
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/metasploit-framework/bin:/opt/metasploit-framework/bin
PWD=/Users/oltjano
LANG=en_US.UTF-8
XPC_FLAGS=0x0
XPC_SERVICE_NAME=0
SHLVL=1
HOME=/Users/oltjano
LOGNAME=oltjano
_=/usr/bin/printenv
Once you have managed to execute the command printenv on your terminal console, you should get an output similar to the one shown above.
The clear command
This command is very useful as it clears the screen. Every time your terminal application gets ‘dirty’ you can run this command to clear it.
Just type the following command and you will get a fresh console.
clear
The rm -r command
When it comes to deleting entire directory structures from the terminal application, this command is perfect. The option -r tells the command rm to remove the entire hierarchy of the directory file which is provided as an argument.
The following command removes the entire files in the directory Test, including the subdirectories within it.
rm -r /Users/oltjano/Desktop/Test
The top command
The top command helps the user to list the current running processes on their own Unix based operating system. According to the official documentation, the top utility periodically displays a list of system processes.
PID COMMAND %CPU TIME #TH #WQ #PORT MEM PURG CMPRS PGRP PPID
9918 top 2.1 00:00.60 1/1 0 21 1992K 0B 0B 9918 9305
9916 Google Chrom 0.0 00:00.10 13 0 82 13M 0B 0B 252 252
9914 Google Chrom 0.0 00:00.36 13 0 99 15M 0B 0B 252 252
9913 Google Chrom 0.3 01:07.45 24 2 205 260M 0B 0B 252 252
9880 ocspd 0.0 00:00.03 3 0 32 1288K 0B 0B 9880 1
9656 Google Chrom 0.0 00:09.10 8 0 60 52M 0B 0B 252 252
9655 Google Chrom 0.0 00:43.74 14 1 119 48M 0B 0B 252 252
9653 Google Chrom 0.0 00:02.23 14 0 109 16M 0B 0B 252 252
9601 netbiosd 0.0 00:00.08 2 1 39 2816K 0B 0B 9601 1
9514 Google Chrom 0.0 00:16.87 18 1 126 59M 0B 12M 252 252
9433 mdworker 0.0 00:00.07 3 0 40 908K 0B 2072K 9433 1
9374 Google Chrom 0.4 18:41.88 18 0 140 166M 60K 10M 252 252
9347 mdworker 0.0 00:04.43 4 0 49 9516K 0B 1064K 9347 1
As you can see from the above console output, the default sorting key is pid, the process id for each process.
The grep command
The grep command searches input files for a given pattern. Based on my personal experience it selects and returns to the user the lines that match the pattern.
A real world scenario in which I mostly make use of the command grep is when I am interested in the existence of a particular file on my filesystem. This real world example involves piping too.
ls /Users/oltjano/Desktop | grep webmaster
If the webmaster text is being found in the output produced by the ls /Users/oltjano/Desktop part of the command, grep will select and return it to the console like shown below.
webmaster
The man command
And last, but not least; the command man. It stands for manual. This command is very useful, especially if you are eager on learning new command line utilities. Just type the command man followed by the name of the command you are interested in as an argument to it in your console.
The following example prints the manual of the ls command on the console.
man ls
Final thoughts
It takes a lot of time to become an advanced user of the terminal application offered by Unix based operating systems. Not only does it take time, but periodically practice too.