Shell Commands You Should Have Memorized Yesterday

Master the Terminal to be a productive developer

Shashank Katte
Geek Culture

--

Being a developer means spending an awful lot of time in the terminal, especially Linux based terminal. You may fear this minimal dark text window, but it is your most potent ally to be a productive developer.

Terminals (a.k.a Console a.k.a command prompt) can be found everywhere. You find them integrated into your code editors like Visual Studio code or as a CLI (command line interface) to services like GitHub or AWS.

Being able to fire commands at will in the terminal saves you more time and energy than dragging your mouse pointer around clicking away at the GUI. Interestingly it’s not all that daunting to get started.

Let's look at the must-know commands…

1. Get to know where you are with “pwd”

The pwd command stands for the present working directory. It tells you exactly which folder/directory you are in the file system. Think of this as your — find my location service on google maps. Here’s how to use it: just type pwd at the cursor in the terminal, hit return and it outputs the path to your current directory.

$ pwd

2. Listing files and directories with “ls”

ls is a very powerful command that lists all the contents of a directory. You can use different flags along with it to choose what information is shown.

When you use it without any options, it displays the names of all the unhidden files and directories in the current directory you are in.

$ ls

Adding the -l flag along with ls will list all the unhidden contents of your current directory in a tabular format with more details

$ ls -l

Now, if we want to display all the hidden files and directories we use the -a flag. If we use it along with -l flag it lists all the contents including hidden ones in a nice tabular format

I highly recommend that you use this format of the ls command, to list out all the contents of directories.

$ls -al

In Linux/mac hidden files and directory names start with . , for example .my-secret-directory is a hidden directory.

3. Change directories with “cd”

cd stands for change directory. As you might have already guessed it helps us move around directories. Hitch a ride!

When you use cd without any flags, it takes you to your home directory/root directory. For example, if I try this command on my mac it will take me to my home directory /Users/shashank

$ cd

To get into a particular directory you use cd followed by the name of the directory

$ cd my-directory

you can also do multiple jumps per se and get into a directory nested into another directory (subdirectory) using the path. For example, if I want to jump into my projects folder I would do this

$ cd shashank/projects

4. Understand the dot “.” and double dots “..”

The single dot . is an alias for the current directory and the double dots .. is an alias for the parent directory.

So if you do a $ cd . You will still stay in the current directory. However, the single dot alias is much useful with other commands like open

$ open .

This launches your default file explorer at the current directory. If you are on mac this launces the finder displaying contents of the current directory.

now if you use

$ cd ..

This will take you one level higher or to the parent directory of your current directory.

Yes the alias for current and parent directory . and .. are also hidden. You will see them listed when you use ls with -a flag

5. Create Directories with “mkdir”

mkdir stands for make directory. To create a directory use mkdir with the directory name

$ mkdir my-new-directory

You can also use the long path to create directories provided the path until the new directory exists. For example, you can create my-new directroy like so:

$ mkdir /users/shashank/my-new-directory

This will work provided the directory structure /users/shashank already exists.

6. Crete a new file with “touch”

touch lets you quickly create an empty file or multiple files. This command is very useful when you need to create files without any content to test out logs etc. to use it you use touch followed by the filename(s) with extension.

$ touch log.txt dump.csv 

7. Add content to file with “>” and “>>”

> lets you overwrite and push content into a file. For example, you can push the contents from file1 to file2. The contents of file2 will be overwritten if it already has any content

$ file1 > file2

However, if you want to append content to an existing file without overwriting use >>

$ file1 >> file2

8. View the file contents with “cat”

The quickest way to display file contents on the terminal is to use the cat command. Similar to touch you can provide multiple file names.

$ cat file1 file2

9. Rename or move with “mv”

mv is used to rename files/directories or move files/directories from one place to another

To move a file

$ mv file1 my-directory

To rename a directory or file, provide the existing file/directory name followed by a new file name/directory name.

$ file1 newfile

If the second parameter is a new folder, the mv command moves the file/directory in the first parameter into the folder in the second parameter. Otherwise, it will rename the file.

10. Delete files and folders with “rm” and “rmdir”

You can delete a file using rm command.

$ rm filename

If the file is write-protected, you will be prompted for confirmation before deleting it. If it's not protected the command goes ahead to delete the file

To delete an empty folder use the rmdir command

$ rmdir my-directory

Danger zone: To delete a folder that contains files without any prompt use the rm command with flags r and f

$ rm -rf my-directory

11. Use the power of wildcards

Wildcards (also referred to as meta characters) are symbols or special characters that represent other characters. You can use them with any command like ls or rm to list or remove files matching given criteria.

  • An asterisk (*) – matches one or more occurrences of any character, including no character.
  • Question mark (?) – represents or matches a single occurrence of any character.
  • Bracketed characters ([ ]) – matches any occurrence of character enclosed in the square brackets. It is possible to use different types of characters (alphanumeric characters): numbers, letters, other special characters, etc.

Here we list all files with a .txt extension

$ ls -al *.txt

The following example matches all files with names beginning with l followed by any single character and ending with st.sh (which is the suffix).

$ ls l?st.sh

and below we match all files with names starting with l followed by any of the characters in the square bracket but ending with st.sh.

$ ls l[abdcio]st.sh

Caution: Be extra careful while using wildcards with rm or rmdir. It can prove disastrous

10. Clean up the terminal window with “clear”

With all the commands and inputs the terminal gets cluttered pretty fast. clear does what it says; it clears up the screen and takes your cursor right to the top so that you have a fresh clean area to fire more commands!

$ clear

All is not lost when you use the clear command, If you want to look back at the previous commands and outputs, just scroll up and it’s right there.

11. Use the “man” command to list a manual for any command

Use man the command to print the complete manual of any command that you can run in the terminal. It gives details on usage options, flags, error codes, etc. When in doubt man it!

Here man command will print the entire manual for ls command

$ man ls

Bonus: Understand the power of TAB autocomplete and Arrow keys history

These are not commands but short-cuts to reduce your typing in the terminal.

Use the TAB key for autocompletion when you are typing file or directory names. You can type a single character and hit TAB. You can cycle through all the options that start with the character

Also, you can cycle through the history of recently used commands using the up-Arrow and/or Down- arrow keys.

Where to go from here

We have covered the bare minimum commands here, however, they are enough to get you going. As you start using the terminal more often and dip your hands into various CLIs you will pick up the commands essential in your context. I encourage you to explore the linux.org documentation for more commands.

Thanks for reading and Happy hacking.

--

--

Shashank Katte
Geek Culture

Full-Stack Developer and Agile-DevOps Coach from Toronto. I write about programming, technology and leadership | katte.io