Terminal commands are the easiest way to handle your Linux system, and learning to use them properly will do wonders for your productivity. Bash is one type of shell used in Linux terminals, and by default, most Linux distributions come with bash shell. This post will walk you through some of the most useful beginner-level bash commands you may want to start using.
Also read: 8 Useful and Interesting Bash Prompts
File and Directory-Related Commands
File and directory manipulation in bash is very easy and intuitive. These are some of the most-used commands that you need to learn.
Print Text with echo
To print out any string in your terminal, you can use the echo
command. If you wish to print out “Hello world” in your terminal, use the following:
echo "Hello world"
You can add escape characters like \n
for new lines using -e
flag.
echo -e "hello \n world"
Create Files Using touch
The touch
command is used to create files. You can create single or multiple files as follows.
touch file1.txt
To create multiple files, append their file names one after another.
touch file2.txt file3.txt
Also read: How to Create LaTeX Documents with Emacs
Write on Files Using nano Text Editor
You can write or modify file contents using the nano editor. To open a file in write mode, use:
nano file1.txt
The above command opens a text editor in the terminal. You can write whatever you want and save the file using Ctrl + O and exit the text editor using Ctrl + X .
See Content of a File With cat
To see what is written on a file without opening it, you can use the cat
command.
cat file1.txt
List Files and Directories Using ls
In the steps above, you’ve created three files: file1.txt, file2.txt and file3.txt. You can list those files using the ls
command.
ls
Also read: How to Install Git and Git Bash in Windows
Create Directories With mkdir
We use the mkdir
command to create directories. it is an acronym for “make directories.”
mkdir [directory name]
You can create a “greetings” directory like this.
Change Directory With cd
You can change and navigate directories using the cd
command.
cd [directory path]
Go to the previous directory using the ../
path parameter.
cd ../
You can directly go to your home folder using the ~
path parameter.
cd ~
Also read: The Beginner’s Guide to the pwd Command in Linux
Get to Your Working Directory Using pwd
If you want to know the working directory of any file or directory, you can use the pwd
command.
pwd
Copy a File or Directory Using cp
The cp
command requires two arguments. The first should be the file or directory path you want to copy, while the second is the target location path. In the following example we are copying our “file1.txt” file to the “greetings” directory.
cp file1.txt greetings/
Also read: How to Copy and Paste Text, Files and Folders in Linux Terminal
Move and Rename a File With mv
If you want to move your file, you’ll have to use the mv
command. It also needs two arguments: source file location and destination location.
mv file2.txt greetings/
You can also rename a file using the mv
command. The trick is to leave the source path the same, but in the destination path you’ll need to write the modified name with the same file extension.
mv file3.txt modified-file3.txt
Remove a File Using rm
You can use the rm
command with the file location to remove or delete a file.
rm file2.txt
If you want to delete a directory, use the -rf
flag. It allows you to recursively delete all the files inside the directory.
rm -rf greetings/
Also read: How to Use Bash For Loop Commands
Delete a Directory with rmdir
Add the directory path after the rmdir
command to delete an empty directory. You can also delete multiple directories in a single command.
rmdir greetings/ rmdir dir1 dir2 dir3
Permission Management Commands
Each file in Linux has various permissions. For instance, you may not be permitted to view or use a file. Fortunately, this list of commands allows you to change file permissions in Linux.
Modify File Permissions With chmod
You can add or remove file permissions using the chmod
command. To make a file readable, add the +r
flag after chmod
. To make a file executable, add +x
after the chmod
command.
In Linux, we often need to make files executable to run them. To solve this issue, you can run this command in your terminal:
chmod +x filename
The executable can be run as:
./filename
Also read: Bash Tips and Tricks to Work Smarter in the Terminal
Become Superuser Using su and sudo
To install new software and modify files and folders beyond your user directory, you need superuser permissions. Superuser can change anything in your operating system.
There are two popular ways to temporarily become superuser in Linux. Using su
, you’ll get a superuser prompt, and your commands will run as a superuser inside that prompt. You can use su
like this:
sudo su
You can leave the superuser prompt by running the exit
command.
Modern Linux distributions use sudo
instead of su
. You can escalate your permission to superuser only by prefixing sudo
in your command. After the command executes, your permission will return to normal. This is a more secure option than su
.
sudo apt update
Also read: What Are Bash Variables and How Can You Use Them?
Networking-Related Commands
As Linux is very popular in the server space, it comes with various tools to work with networking. Below you’ll find some commands which can give you the basic idea about networking in Linux.
Download Files Using wget
You can download files and interact with any REST APIs using the wget
command. wget
supports HTTP, HTTPS, FTP and FTPS protocols. You can download a file by simply adding the link of the source after the wget
command.
In this example, we are downloading a video using wget
:
wget https://archive.org/download/BigBuckBunny_328/BigBuckBunny.ogv
wget
automatically fetches the video file and downloads it.
Curl Basics
Curl is a networking utility that is used to transfer data to and from servers. It provides a number of options allowing you to resume transfers, limit the bandwidth, proxy support, user authentication etc.
To download and save a file using curl, use the -O
flag.
curl -O https://archive.org/download/BigBuckBunny_328/BigBuckBunny.ogv
If the connection drops during the download, you can resume the download by using -C
flag.
curl -C -O https://archive.org/download/BigBuckBunny_328/BigBuckBunny.ogv
Also read: Using find, locate, which and whereis Commands to Search for Files in Linux
Utilities Commands
These are some of the utilities you can use in your day-to-day activities. Knowing them will help you save a lot of time and become more efficient.
Date
Get today’s date using the date
command.
date
Output contains date, month, year, weekdays and your timezone.
Mon Aug 15 04:39:43 PM IST 2022
Search With grep
grep
is a very powerful searching tool that you can use to search for words and sentences in directories.
You can also employ grep
to find a software package of your choice. For example, if you need to install VLC player and don’t know its package name, you can combine apt
and grep
to search for the software package.
apt search vlc | grep "vlc"
Find Package Installation Path Using which
You can use which
to see the location where packages are installed. For example, to see the location of your bash installed binary use:
which bash
Get Previous Command via History
Sometimes we forget commands. Fortunately, you can use history
to see all the previous commands you ran and refresh your memory.
history
Also read: How to Resize and Optimize Images From the Linux Terminal
Close a GUI Application Using kill
Once you’ve opened a GUI application from your terminal, you get the ID of the application. This is like a handle to that application.
To close this application, you can use kill command.
kill [application ID]
Read CLI User Manual via man
You can look at the documentation of a CLI tool using the man
command.
man echo
The above command will give you the manual for the echo
command.
Here you can learn about all the available flags for the echo
command. You should try different flags to customize the command to suit your needs. To exit from the manual page, hit the q button.
Clean the Terminal
If your terminal is filling with clutter, you can wipe it clean by using the clear
command.
clear
You can also use the Ctrl + l shortcut the clean the terminal window.
Also read: 5 Useful Tips When Compiling Your Own Linux Kernel
Show Off to Your Friends
If you’d like to have some fun and print some random cool stuff on your screen just for the sake of it or to show off to your friends, you can employ a tool called cmatrix
.
To install cmatrix
, run these commands.
sudo apt update sudo apt install cmatrix
In your terminal, run cmatrix
.
cmatrix
The output will be like a snapshot of the matrix.
You can also display a moving train using a simple bash command: sl
. You can install this using the following command.
sudo apt update sudo apt instal sl
Run this utility in the terminal.
sl
Also read: 12 of the Best Linux Distros in 2022
Frequently Asked Questions
What are some other shells for Linux apart from bash?
There are many options in Linux shells apart from bash. Z-shell, fish shell and c shell are among the most popular. Keep in mind that each shell has its own particularities. Therefore, scripts made for a specific shell won’t run on other shells.
What is the purpose of bash shell?
Bash shell or generally shells help us to efficiently manipulate files and directories. Shell has many essential tools like cp
, mv
, touch
, etc., that help us create and modify files very easily in the terminal.
What are some limitations of the bash shell?
Unlike other scripting languages, bash is very slow and error prone. The main drawback of bash is the lack of any debugging framework. This makes bash not suitable for large scripts and automation.
Image credit: Gabriel Heinzer via Unsplash. All screenshots are by Hrishikesh Pathak
Our latest tutorials delivered straight to your inbox