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:
- command
lsis the name of the command - option(s)
-lhis the option that configures command behavior, starting with- - arguments
/user/binis 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.
⬞ ls¶
Use this command to see the content of the current folder. It has several options that allow you to configure view mode.
-lshow the content in vertical list format-ashow everything ( including hidden files and folders )-hdisplay output file size in human-readable format
You can use these options individually or together as follows:
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:
pathfull or relative file path~home directory location.current folder location..parent folder location of current folder-last folder location
⬞ mkdir¶
Use this command to create a new empty directory with a specified name. It stands for "make directory."
-pcreate folder with sub folders
⬞ rmdir¶
Use this command to remove a directory with a specified name. Note that the folder to be removed must be empty.
⬞ 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.
⬞ 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.
-vprints details after the removal-iprompt before every deletion, respond withnfor no,yfor yes-rrecursive deletion, removes a directory and its content
⬞ mv¶
Use this command to move and rename files and folders.
⬞ cp¶
Use this command to make a copy of the file
-rrecursive copying, copies entire directory and its content
⬞ nano¶
Use this command to open a file in edit mode. You can edit any file with this terminal text editor.
-ledit the file with line numbers enabled
- 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
-ndisplay the content with line numbers
⬞ head¶
Use this command to display the first 10 lines of a file to the terminal. Number of line it should display can be configured.
-n5displays the first 5 lines, note that the number must be bigger than 0
⬞ 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.
-n5displays the first 5 lines, note that the number must be bigger than 0