17 Terminal Commands Every Programmer Should Know | by Mahbub Zaman | Towards Data Science

Most computer users interact via a graphical user interface because it’s an easy way to manipulate a machine. Even though GUI applications remain popular among vast users, a command-line interface gives users more control over their computer by running text commands. It’s a powerful way to interact with a computer. For example, you can run text commands to create 100 folders, but it will take more time and effort to achieve the same result via a GUI application.
mkdir testcd testfor i in {1..100}do   mkdir "$i"done
To be able to run these commands, we need to use the Terminal application. This Terminal provides you with the shell. Shell is an environment where we can run our shell commands and shell scripts. It allows us to control command-line tools. The macOS X comes with the bash shell (Bourne Again shell), but you can change your shell to csh (C shell), zsh (Z shell), ksh (Korn Shell), tcsh (TENEX C Shell), etc. Now let’s go through some basic shell commands to see what is happening. List all available shells in macOS X.
cat /etc/shells
To see which shell you’re running.
echo $0
To change to bash shell.
chsh -s /bin/bash
To change to zsh shell.
chsh -s /bin/zsh
Depending on which shell you’re using, you’ll get different features. My choice of the shell is zsh. You’ll learn some basic shell commands from this post to improve daily workflow by saving time and effort.
The pwd command stands for Print Working Directory. It will print the full path to the current working directory.
pwd# /Users/universe/Desktop
The shell ignores a word if it starts with # (hash) and any remaining characters on that line.
# /Users/universe/Desktop
The mkdir command stands for Make Directory. Here we are creating a directory on my Desktop called neptune.
mkdir neptune
We can create multiple directories using the -p option.
mkdir -p space/neptune
The above command will create a folder called space and a nested folder or subfolder neptune. If we want to create our third folder naiad under neptune, we can use the following command. We don’t have to provide the -p option for this command because the path space/neptune exists already.
mkdir space/neptune/naiad
The cd command stands for Change Directory. Let’s use this command to access the space directory we have created previously.
cd spacecd neptunecd naiad
Or you can use the following command to access the naiad directory.
cd space/neptune/naiad
Both commands will produce the same result. Now we should be inside the naiad directory.
The .. (two periods) represents or points to the directory that contains the current directory, also known as the parent directory. Here the parent directory of naiad is neptune. Let’s use the following command to move one level up in the directory hierarchy.
pwd# /Users/universe/Desktop/space/neptune/naiadcd ..pwd# /Users/universe/Desktop/space/neptune
Moreover, we can move to multiple levels. In this case, let’s move 2 levels up.
cd ../..pwd# /Users/universe/Desktop
Now, we are back to the Desktop directory.
The . (single periods) represents or points to the current working directory. It’s helpful when we don’t want to write the full path when running a command. For example, if our command-line tool is in the current directory, we can use the following command.
./space-robot
The ~ (tilda) or $HOME represents or points to the home directory. For macOS X, the user’s home directory is under the /Users directory.
cd ~pwd# /Users/universecd $HOMEpwd# /Users/universe
By using this shorthand, you don’t have to type the full path to the home directory.
Also, we can see a / (forward-slash) is used to separate the directories in a pathname. By using the following command, we can move to the root directory.
cd /
Using the touch command, we can create a file.
cd ~/Desktop/neptune touch todo.txt
The ls command stands for List. We can use it to list all the contents of the specified directory, if no path is specified it will list everything in the current directory.
cd ~/Desktop/neptunemkdir todols# todo     todo.txt
Use -a flag to list hidden files and directories.
ls -a
Use -l flag to list with detailed information.
ls -l
Or use it together.
ls -al
We have been writing commands for a while, so let’s clear the Terminal screen.
clear
The mv command stands for Move. We can use this command to move files and directories from one place to another. Also, we can use it to rename files and directories. The following command will move the todo.txt file from its current directory to its child directory todo.
cd ~/Desktop/neptunemv todo.txt todo/todo.txt
Now let’s rename the file. Here we are renaming the todo.txt file to my-todo.txt.
cd todomv todo.txt my-todo.txt
The cp command stands for Copy. Now, let’s copy our my-todo.txt file to the parent directory.
cd ~/Desktop/neptunecp todo/my-todo.txt my-todo-bu.txt
We can use the -r flag to copy a directory. The following command will copy everything that is inside the todo folder to a folder called bu.
cp -r todo bu
The rm command stands for Remove, and rmdir command stands for Remove Directory. The rmdir command can only delete a directory if it is empty. Now let’s remove a file and directory.
cd ~/Desktop/neptunerm my-todo-bu.txtmkdir emptyrmdir empty
We can use the -r flag to delete a non-empty folder.
rm -r burm -r todo
The > can be used to redirect stdout (Standard output). Here, we are adding a task to our todo.txt file. The echo command writes its arguments to stdout. This > command will overwrite the todo.txt if exists already.
cd ~/Desktop/neptuneecho 1. Make space robot > todo_today.txt
If we want to append data we need to use the >> command.
echo 2. Make space robot v2 >> todo_today.txt
The < can be used to redirect stdin (Standard input). Here, we are redirecting the contents of our todo.txt file to the wc command to print the number of lines, word count, and character count.
wc < todo_today.txt#        2       9      43
The * (asterisk) can be used as a wildcard to match zero or more characters.
ls *.txt# todo_today.txt
The ? (question mark) can be used to match a single character.
ls ????_?????.txt# todo_today.txt
The [] (square brackets) can be used to match any number of characters inside them.
ls t[o]do_*.???# todo_today.txt
The ; (semicolon) can be used to write multiple commands on a single line, all we have to do is separate each command with a semicolon.
cd ~/Desktop/neptune ; wc < todo.txt#        2       9      43
The $ (dollar) can be used to create and store values.
echo $HOME# /Users/universeMyValue=99echo $MyValue# 99
The \ (backslash) can be used to escape a special character.
echo \$MyValue# $MyValueecho \$$MyValue# $99
The cat command stands for Concatenate. It prints the contents of a file or files to stdout. Print content of a single file.
cd ~/Desktop/neptunecat todo.txt# 1. Make space robot# 2. Make space robot v2
Print content of multiple files.
cd ~/Desktop/neptuneecho FILE 1 > file_1.txtecho FILE 2 > file_2.txtecho FILE 3 > file_3.txtcat file_1.txt file_2.txt file_3.txt# FILE 1# FILE 2# FILE 3
The grep command stands for Global Regular Expression Print. It searches text inside the file specified with the given search pattern. By default, grep is case-sensitive. For case-incentive search, we can use the -i flag.
cd ~/Desktop/neptunegrep "FILE" file_1.txt# FILE 1grep -i "file" file_1.txtFILE 1
The -r flag is used to search inside a directory recursively. Here it will search everything inside the current directory and its child directories.
grep -r "FILE" .# ./file_1.txt:FILE 1# ./file_2.txt:FILE 2# ./file_3.txt:FILE 3
There are other flags that can be useful, like the -n flag will print matching lines with the line number, the -c flag will print the count of the matching lines.
The | command or the pipe command is used to add two or more commands, where the output of the previous command is used as the input for the next command.
command_1 | command_2 | .................. | command_N
Here we are sorting the todo.txt file in reverse order by combining the cat and sort command.
cat todo.txt | sort -r# 2. Make space robot v2# 1. Make space robot
The commands head and tail can be used to print the content of a file to standard output. The flag -n is used to define how many lines we want to output. Now let’s print the first 1 line of a file.
cd ~/Desktop/neptunehead -n 1 todo.txt# 1. Make space robot
Print the last 1 line of a file.
tail -n 1 todo.txt# 2. Make space robot v2
These commands can be useful when working with large files.
The find command is used to search files.
find dir -name search_pattern
Let’s find all the text files in the current directory.
cd ~/Desktop/neptunefind . -name "*.txt"# ./file_1.txt# ./todo.txt# ./file_2.txt# ./file_3.txt
The open command can be used to open a file and open the directory using the Finder application.
cd ~/Desktop/neptuneopen .open todo.txt
Finally, we have the man command, which stands for Manual. It prints the user manual for a command.
man cd
Use up and down arrow keys to navigate through the documentation. Use the f key to move forward one page and the b key to go back one page — press q to quit.
Congratulations! Now you know all these cool shell commands, which will help you to save time and effort. You can find all the shell commands I've discussed on my GitHub. Happy coding!