Skip to content

Fundamental Bash Commands

The commands are essential to begin working at the Terminal with Bash.

pwd

When working in the Terminal it is possible to be located at different locations within the folder or directory heirarchy.

The pwd command is used to print out the current directory.

Example:


# print the directory currently inside
pwd


ls

The ls command is used to list the files and folders in a directory.

Example:


# show files and folders in current directory
ls


# see more details for each item listed including permissions and filesize
ls -l

# also show hidden files in the list
ls -a

# it is possible to combine the previous 2 commands into a single one
ls -la

A directory path can be passed to ls to display the files and folders in that directory

Example:


# `ls` can be used to list the files not in the current directory
# if "/home/fuzzy/Downloads" is the file path to my downloads folder 
# we can see the files and folders in it with
ls '/home/fuzzy/Downloads'


cp

The cp command can be used to copy files and folders.

Examples for copying files:


# copy old.csv into a file called new_copy.csv
cp old.csv new_copy.csv

# copying files that may not be in the current directory

cp 'home/fuzzy/Downloads/old.csv' 'home/fuzzy/MyCoolProject/new_copy.csv'

Examples for copying folders:


# copy the contents of the downloads folder into another folder
cp -r 'home/fuzzy/Downloads' 'home/fuzzy/a_folder_for_backups'


cd

The cd command is used to navigate around to different locations in the filesystem

Example:


# Navigate into the Downloads folder
cd 'home/fuzzy/Downloads'


nano

While working on the terminal it will likely be necessary to work with text files within a text editor.

As with text editors, only plain text files are able to be opened with nano. Excel files, zip files and mp4 files will not work with nano.

nano is a text editor in the terminal that can be used.

Example:


# open a file called data.csv located in the current directory with nano
nano 'data.csv'

# open a file in the downloads folder with nano
nano 'home/fuzzy/Downloads/my_file.txt'


The head command allows for the printing of the first few lines of a text file to the terminal.

Example:


# by default `head` will print the first 10 lines of a file.
head data.csv

# print the first 20 lines of a text file
head -n 20 data.csv


tail

The tail command allows for the printing of the last few lines of a text file to the terminal.


# by default `tail` will print the last 10 lines of a file
tail data.csv

# running tail on a file to show last 20 lines
tail -n 20 data.csv

# running tail on a file not in the current working directory

tail -n 20 '/home/fuzzy/Downloads/data.csv'


cat

The cat command is used to print all the lines of a file to the terminal.


# print the contents of a file
cat data.csv

# and if the file is not in the current working directory
cat '/home/fuzzy/Downloads/data.csv'