Contents

Introduction

A Unix shell provides a text-based user interface, known as a command line interface (CLI), for Unix-like operating systems. The shell is both an interactive command language and a scripting language, and it is used by the operating system to control the execution of the system using shell scripts.

This guide will walk you through some Unix shell commands that will be useful to know as you progress on your coding journey. This guide is not an exhaustive list.

How to Execute a Unix Shell Command

A Unix shell command can be executed through a CLI application running a Unix shell instance, or it can be executed as part of a Unix shell script.

Once you have opened a CLI application, type the command into the console and press ENTER or RETURN to execute it. Some commands will print text as a part of their execution, and some will not. An error message will usually be printed if something has gone wrong during the program execution or if the command and its arguments were not structured properly. Additional information about CLI applications can be found in the Unix Installation guide.

Additional information about creating, writing, and executing shell scripts can be found in the Unix Shell Scripts guide.

Unix Shell Shortcuts

Use the TAB key to autocomplete file, directory, and command names.

Use the UP arrow key to scroll through previous commands.

Use the DOWN arrow key to scroll forward through previous commands.

Commands

pwd

Print Working Directory / Present Working Directory

The pwd command prints the absolute file path of your current location in the shell. Your current location will correspond to some folder in your computer’s file system.

pwd

whoami

Print Your Username

The whoami command prints your username. This command can be used to check your username when you are connected to a remote server.

whoami

date

Print the Current Date and Time

The date command prints the current date and time.

date

echo

Print Something

The echo command will print the given string.

echo STRING_HERE


Tip

Fun fact: Want to make your computer beep? Try the following: echo -e "\a"


When given an environment variable name, such as $HOME or $PATH, the echo command will print the value of the given variable.

echo $VARIABLE_NAME_HERE


For additional information about environment variables, the following resources may be helpful:

Red Hat Blog - Linux environment variable tips and tricks

echo Examples

echo "Hello, World!"
echo $HOME

clear

Clear the Shell Window

The clear command clears the shell window.

clear

ls

List the Directory Contents

The ls command prints all the visible files and folders in our working directory.

ls


When we add the -l flag to the ls command, the visible files and folders will be printed in a long list format.

ls -l


When we add the -a flag to the ls command, the command will list all the files and folders in our working directory, including hidden files and folders.

Hidden files and folders will always have a name that begins with . (e.g. .bash_profile).

ls -a


When we add both the -a and -l flags to the ls command, the command will list all files in our working directory, including hidden files, in a long list format.

ls -al


We can use the ls command to print the contents of a different directory by providing the absolute or relative path to the directory.

ls DIRECTORY_PATH_HERE


Additional information about paths can be found in the Unix Paths guide.

cd

Change the Working Directory

The cd command will change the working directory to the directory with the given absolute or relative path.

cd DIRECTORY_PATH_HERE


Additional information about paths can be found in the Unix Paths guide.

touch

Create a File

The touch command creates a file.

touch FILENAME_HERE


When executing the touch command, the filename argument can also be a relative or absolute path to the file you are creating.

touch FILE_PATH_HERE


Additional information about paths can be found in the Unix Paths guide.

touch Examples

touch my-text-file.txt
touch ./my-directory/MyJavaClass.java

mkdir

Create a Directory (Folder)

The mkdir command creates a folder, also known as a directory. The directory path argument can be an absolute path or a relative path.

mkdir DIRECTORY_PATH_HERE


Additional information about paths can be found in the Unix Paths guide.

chmod

Change File Permissions

The chmod command updates the permissions of the given file using the given permissions.

chmod PERMISSIONS_HERE FILE_PATH_HERE


Permissions can be provided in symbolic mode or octal mode.

chmod Examples

chmod 444 my-text-file.txt
chmod ugo+w my-text-file.txt

export

Set the Value of an Environment Variable

The export command will set the given variable to the given value. When executed in a Unix shell, this change will only be active for the current shell session.

export VARIABLE_NAME_HERE=VARIABLE_VALUE_HERE


If an environment variable needs to have some value in every shell session, it is recommended to set that variable in the appropriate shell profile file or shell RC file. Additional information about shell profile files and RC files can be found in the Unix Shell Configuration guide.

To confirm the variable has been set properly, you can print its value using the echo command.

echo $VARIABLE_NAME_HERE

export Examples

export SOME_VARIABLE="some value"
echo $SOME_VARIABLE

which

Print the Path of this Command

The which command prints the absolute path to the location of the given command, provided as an argument.

which COMMAND_NAME_HERE


When we add the -a flag to the which command, the command will print all paths to the given command. A command may have multiple paths if there are multiple versions of the command installed on the machine.

which -a COMMAND_NAME_HERE


The which -a commands give you all locations of the given command. The which command tells you which one will be used when you execute that command.

which Examples

which ruby
which python
which -a ruby
which -a python

bash

Start a Bash Shell Instance

The bash command initializes an instance of the Bash shell inside the current shell instance.

bash

zsh

Start a Z Shell Instance

The zsh command initializes an instance of the Z shell inside of the current shell instance.

zsh

exit

Exit the Current Shell Instance

The exit command can be used to exit the current shell instance.

exit

Resources and References

For additional information about Unix shell commands, the following resources may be helpful:

explainshell

GeeksforGeeks - Essential Unix Commands

freeCodeCamp - The Linux Command Handbook – Learn Linux Commands for Beginners

Tutorials Point - Linux Commands Reference

SitePoint - 15 Little-Known Unix Commands

Red Hat Blog - Linux file permissions explained

W3Schools - What is Command Line Interface (CLI)?

AWS - What is a CLI?

Wikipedia - Unix shell


Back to top.