Skip to content

Using Terminal

You might be wondering why the command line (terminal) is still in use when we have human-friendly GUI and tools available. Every developer would agree that the command line is far more powerful than any GUI, and it provides speed and expressiveness that no GUI can offer.

In this code note, we will go over all the commands that Software Engineers in Test would use in their daily test automation activities one by one. Please make sure to practice them often.



› Fire up the Terminal

Let's look into how we can invoke the terminal application from our system. Windows and Mac users utilize different terminal apps, so make sure to see the instructions for your corresponding system.

Once your terminal is open, you should see a Dollar Sign ($) or similar symbol (%). This is the Prompt.

It prompts you to input some commands so that bash can process them. Each command will be prepended by this prompt ($), but you should not type it. Instead, your computer will do it for you.

Please keep in mind that when you are using a terminal, you are always inside some folder. By default, when you open the terminal, you start with the home folder denoted by a tilde symbol (~).



› Command Structure

Every command that you type in the terminal has defined structures and components. Its general format is as follows

For example, for the following command:

$  ls   -lh   /user/bin    
Its structure details are as follows:

  • command    ls is the name of the command
  • option(s)    -lh is the option that configures command behavior, starting with -
  • arguments    /user/bin is the argument (data) that command operates on


let's cover commands that the tester often uses 😁



pwd

Use this command to display the current folder's full file path. It is easy to get lost while using the terminal. Therefore, this command comes in handy when you want to know where you are located in the file system.

pwd 


ls

Use this command to see the content of the current folder. It has several options that allow you to configure view mode.

  • -l show the content in vertical list format
  • -a show everything ( including hidden files and folders )
  • -h display output file size in human-readable format

You can use these options individually or together as follows:

1
2
3
4
ls        # display current folder content
ls -l     # display current folder contents with list format 
ls -al    # display current folder contents with list format 
ls -alh   # display all folder contents with list format, with readable data size 

In the above example, each item in the folder has prefix characters such as drwxr-xr-x; these characters represent file permissions. If the first character is d, the file is a folder ( directory ). If the first character is - , it is a regular file.


cd

Use this command to change the current directory you are on to a specific folder. For example, we can use this command as follows:

  • path   full or relative file path
  • ~     home directory location
  • .     current folder location
  • ..   parent folder location of current folder
  • -     last folder location
1
2
3
4
5
6
cd  /c/user/nijat/dowloads/musics  # full path 
cd  downloads/musics               # relative path, if you are already on [nijat] folder
cd  ~                              # goes to [nijat] folder regardless of where you are
cd  .                              # goes to the current folder 
cd  ..                             # goes to the parent folder of current folder
cd  -                              # goes to the last folder location 


mkdir

Use this command to create a new empty directory with a specified name. It stands for "make directory."

  • -p     create folder with sub folders
1
2
3
mkdir  src               # creates a folder with title [src]
mkdir  earth mars pluto  # creates folders [earth], [mars], [pluto] in current folder
mkdir -p code/test/java  # creates folder structures [code]->[test]->[java]


rmdir

Use this command to remove a directory with a specified name. Note that the folder to be removed must be empty.

1
2
3
rmdir  src              # removes [src] empty folder
rmdir  earth mars pluto # removes empty folders [earth], [mars], [pluto]
rmdir -p code/test/java # removes emptry folder strcutures [code]->[test]->[java]


touch

Use this command to create an empty file with a specified file name and extensions. Every file has its name and extension that helps identify the file type.

file extension

file extension is the ending part of a file that specifies that file type. Without a file extension, your operating system cannot identify which program can work with the file.

1
2
3
touch data.json                # creats an empty json file titled "data"
touch note.txt screenshot.png  # creates a text file and a image file 
touch space/Demo.java          # creates "Demo.java" in existing [space] folder 


rm

Use this command to delete file(s). Please note that deletion is final and cannot undo it. You can also delete a folder with this command with -r option.

  • -v   prints details after the removal
  • -i   prompt before every deletion, respond with n for no, y for yes
  • -r   recursive deletion, removes a directory and its content
1
2
3
4
rm -v Demo.java    # removes "Demo.java" file and display details
rm -i Demo.java    # prompts the deletion of "Demo.java", type n or y
rm -r code         # removes [code] folder and all of its content
rm *.java          # delete all java files 


mv

Use this command to move and rename files and folders.

1
2
3
mv pom.xml   backup/    # move pom.xml file to the backup/ folder
mv *.java    code/      # move all java files to the code/ folder
mv Demo.java User.java  # renames java file "Demo.java" → "User.java"


cp

Use this command to make a copy of the file

  • -r   recursive copying, copies entire directory and its content
1
2
3
cp info.txt backup/info.txt     # overwrites files if exists in backup/ 
cp info.txt backup/bp_info.txt  # copy info.txt as backup_info.txt to backup/
cp -r code/ backup/             # copy dir1 to the dir2


nano

Use this command to open a file in edit mode. You can edit any file with this terminal text editor.

  • -l   edit the file with line numbers enabled

nano Demo.java    # opens the "Demo.java" file in edit mode 
nano -l Demo.java # opens the "Demo.java" file in edit mode with line numbers
After you have edit the file, press following keys to save and exit from the editor to the terminal

  • Ctrl+O   to save the edit
  • Ctrl+X   to exit the nano editor to the terminal


cat

Use this command to display the entire content of a file in terminal

  • -n   display the content with line numbers
cat Demo.java    # display the entire content of "Demo.java" 
cat -n Demo.java # display the entire content of "Demo.java" with line numbers


Use this command to display the first 10 lines of a file to the terminal. Number of line it should display can be configured.

  • -n5    displays the first 5 lines, note that the number must be bigger than 0
head Demo.java     # display the first 10 lines of "Demo.java" file
nano -n4 Demo.java # display the first 4 lines of "Demo.java" file


tail

Use this command to display the last 10 lines of a file to the terminal. Number of line it should display can be configured.

  • -n5    displays the first 5 lines, note that the number must be bigger than 0
tail Demo.java     # display the last 10 lines of "Demo.java" file
tail -n4 Demo.java # display the last 4 lines of "Demo.java" file