Basic Linux Commands Everyone Must Know

Hey developers! This article is devoted to those people who are NOT afraid of the Black Screen of Death. In this article, we will be looking at some basic Linux commands everyone must know. Especially for those who are involved in Cloud Services or generally from an IT background.  

So, open up your console or terminal, and let’s go through this Linux commands cheat sheet.

*Note: These commands are Linux Bash Script commands and would not work on Windows CMD. 

Prerequisite

General Commands

1. man Manual pages

With the man command, you can retrieve the information in the manual and display it as text output on your screen. 

				
					man ftp 
man -k mail | more 
man man 
				
			

2. passwd  

Change your login password. Depending on a privilege, one can change user’s and group passwords as well as real name, login shell, etc. 

				
					passwd
man passwd
q
				
			

‘q’ is used to quit the manual

3. date

Displays dates in various formats 

				
					date 
date +"Year: %Y, Month: %m, Day: %d" 
date "+DATE: %D %nTIME: %T" 
man date 
				
			

%D – Display date as mm/dd/yy
%Y – Year (e.g., 2020)
%m – Month (01-12)
%B – Long month name (e.g., November)
%b – Short month name (e.g., Nov)
%d – Day of month (e.g., 01)
%j – Day of year (001-366)
%u – Day of week (1-7)
%A – Full weekday name (e.g., Friday)
%a – Short weekday name (e.g., Fri)
%H – Hour (00-23)
%I – Hour (01-12)
%M – Minute (00-59)
%S – Second (00-60)

4. cal

Calendar, for a month and for an entire year. Years range: 1 9999 (but not 0) 

				
					cal
# current month 

cal 2 2000  
# Feb 2000, leap year 

cal 2 2100  
# not a leap year 

cal 2 2400  
# leap year 

cal 9 1752  
# 11 days skipped 

cal 0  
# error 

cal 2019  
# whole year 
				
			

5. clear

Clears the terminal screen.

				
					clear 

CTRL + L 
# It is an alias for clear 
				
			

6. sleep

“Sleeping” is doing nothing for some time. Usually used for delays in shell scripts 

				
					sleep 10  
# 10 second pause 

sleep 5h 30m 1s  
# sleep for 5 hours, 30 minutes and 1 second as ’h’ denotes hours, ‘m’ for minutes and ‘s’ for seconds. 

sleep 1d  
# for days 
				
			

7. time

It works in conjunction with another command. The time command will execute your normal command but then calculates and displays the time it took to complete it. 

				
					time cal 

time date 

time wget https://programatically.com/ 

time sleep 5 
				
			

8. which

Displays a path name of a command. Searches a path environmental variable for the command and displays the absolute path. 

To find which tcsh and bash are actually in use, type: 

				
					which tcsh 

which bash 

which python cpp java 

man which  
# for more details
				
			

9. whereis

Display all locations of a command (or some other binary, man page, or source file). Searchers all directories to find commands that match whereis argument 

				
					whereis tcsh 

whereis java 

whereis ftp 

whereis mv 
				
			

10. alias / unalias 

Removes alias. Requires an argument.

				
					alias  
# Prints list of all alias 

alias c='clear' 
alias d='df -H'
# Creating alias 

c
d
# Call or execute commands through alias name c or d 

unalias c 
# Removes alias 
				
			

11. history and ! 

Display a history of recently used commands. 

				
					history  
# all commands in the history 

history 10  
# last 10 

!!  
# repeat last command 

!-1 
# repeat last command = !! 

!-2 
# repeat second last command 

!ca 
# repeat last command that begins with ‘ca’ 
				
			

12. whoami

Display a history of recently used commands. 

				
					whoami  

echo $(whoami)  
# Common in scripting practice 
				
			

13. df

df stands for disk free. It fetches details, of all the mounted disks in the system, about their used and available space. 

				
					df  
# Shows the details about space for all mounted disks. 

df -h 
# Makes it a bit more easily readable as it prints sizes in power of 1000 

df -h -a 
				
			

-a, -all : includes pseudo, duplicate and inaccessible file systems.
-h, -human-readable : print sizes in power of 1024
-H, -si: print sizes in power of 1000
-i, -inodes : list inode information instead of block usage
-l, -local : limit listing to local file systems
-P, -portability : use POSIX output format
-T, -print-type : print file system type

14. exit / logout 

Exit from your login session.  

				
					exit
				
			

15. shutdown

Causes the system to shut down or reboot cleanly. May require superuser privileges, so just use sudo before the commands 

				
					shutdown -h now  
# stop 

shutdown -r now  
# reboot 

reboot 
# reboot 
				
			

Commands for Files & Directories

1. ls

In Linux, a folder is called a directory. To list all the content of a directory we use the Linux ls command as follows: 

				
					ls  
# Lists directory’s contents 

ls -a 
# Lists all of the directory’s contents 

ls -A 
# Lists all without ".." 

ls -l 
# Long format 

ls -al 

ls -lt 
# Sort by modification time (latest - earliest) 

ls -ltr 
# reverse 
				
			

2. cat

It is used to display and concatenate files. 

				
					cat  
# Will read from STDIN and print to STDOUT every line you enter. Press CTRL + c to exit. 

cat file1.txt
#Will print out all the content of file1.txt

cat file1.txt file2.txt 
# Will concatenate all files in one and print them to STDOUT. 

cat file1.txt file2.txt > file3.txt 
# Will concatenate and insert file1 and file2 content into file3 

cat > file1.txt 
# Will take whatever you type from STDIN and will put it into the file1.txt  
				
			

3. head / tail

Head shows the top 10 lines of the content. 

				
					head file1.txt 

head -n 5 file1.txt  
# -n 5 is used to specifically mention how many lines you want to show from the top. 
				
			

Tails shows the last 10 lines of the content. 

				
					tail file1.txt 

tail -n 5 file1.txt 
# -n 5 is used to specifically mention how many lines you want to show from the bottom. 
				
			

4. more / less

To display contents of large files, page by page or scroll, line by line up and down. It is interactive.  

				
					less file1.txt 

less -N file1.txt 
# To display line numbers 

less /var/log/auth.log  

less +5 /var/log/auth.log 
# Starts off at line 5 

more /var/log/auth.log 
# Same as less command (3) 

more -10 /var/log/auth.log 
# Will show the content with 10 lines in each page 

man less 
# For more information 
				
			

Use the following screen navigation commands while viewing large log files.
1. CTRL+F
forward one window
2. CTRL+B
backward one window
3. CTRL+D
forward half window
4. CTRL+U
backward half window

In a smaller chunk of data, where you want to locate particular error, you may want to navigate line by line using these keys:
1. j – navigate forward by one line
2. k – navigate backward by one line

5. touch 

Is used to create files.

				
					touch file1.txt
# touch <filename>.<extention>

touch Script.sh 
# generate a shell script with ‘sh’ extension 

touch file1.txt file2.txt file3.txt
# can create multiple files at once

man touch  
# see manual pages for touch command 
				
			

6. cp 

Copies files / directories 

				
					cp file1.txt file2.txt
# cp <source> <destination>
# will copy content from file1 to file2
    
cp file1.txt /home/user/directory 

cp file1.txt file2.txt file3.txt /home/user/directory
# Will copy multiple files to the same destination
				
			

Useful option: -i to prevent overwriting existing files and prompt the user to confirm 

Both files must be in the same working directory. If they are in various directories, the absolute path must be given 

7. mv

Moves or renames files/directories. 

				
					mv file1 /home/user/Desktop/ 
# mv <source> <destination> 

mv file1 file2 file3 /home/user/Desktop/
#move multiple files at once

mv index.html /web/index.html
				
			

8. rm

This is a Linux delete file command. To removes file(s) and/or directories, we use the Linux rm command. 

				
					rm file1 file2

rm -r dir1 dir2
# remove directory and all internal files

rm -r file1 dir1 dir2 file4

rm h*d 
# remove all files beginning with h and ending with d which are in working directory

rm *
# erase all files from your working directory
				
			

9. find

This is a Linux find command which looks up a file in a directory tree. 

				
					find . -name 'file1.txt' 
# find file1.txt in the currect working directory

find ./home/Desktop/ -name '*.txt'
# will find all the text files in home directory

find ./home -empty
# this will find all the empty files and directories
				
			

10. mkdir

Creates a directory. 

				
					mkdir newFolder 

mkdir /home/user/newFolder 
# absolute path of the new directory in one command
				
			

11. rmdir

This is Linux remove directory command. It removes or deletes a directory.

				
					rmdir newFolder 
# Removes a directory. 

rm -r newFolder 
# Equivalent. -r means recursively 
				
			

12. cd

Changes your current directory to a new one.

				
					cd /home/user/Desktop  
# Changing directory with absolute path 

cd subdir  
# Assuming subdir is in the current directory. 

cd  
# Returns you to your home directory 

cd ..  
# Moves to the superior directory 
				
			

13. pwd

Displays personal working directory, i.e. your current directory.  

				
					pwd  

echo $(pwd)  
# common in scripting 
				
			

14. grep

Searches its input for a pattern. The pattern can be a simple substring or a complex regular expression. If a line matches, it’s directed to STDOUT; otherwise, it’s discarded. 

				
					echo "OS-19" | grep 19  
# Will print the line where '19' matches

echo "OS-19" | grep 20  
# Will not print the line, since '20' is not matching 

grep "text you want search" file1.txt 

cat filename | grep "text you want search" 
				
			

15. pipes

What’s a pipe?

  • Is a method of inter-process communication (IPC)
  • In shells a ‘|’ symbol used
  • It means that the output of one program (on one side of a pipe) serves as an input for the program on another end.
  • A set of “piped” commands is often called a pipeline
				
					man man | less  

cat Script.sh | head 

cat file1.txt | grep "hello world"
				
			

16. nano

There are a few text editor tools in Linux. I personally prefer the nano tool. Type in the following command to open a file in edit mode: 

				
					nano newFile.txt 

CTRL + s 
# To save the content in the file.  

CTRL + x 
# To jump out of the text editor mode. 
				
			

If it is not letting you save it, then open the file with sudo command.

And that’s all! 

Again, these commands will only work on a LINUX terminal. And a common way to run Linux with Windows is to start a Virtual Machine using VMware. 

I hope this article helped you with Basic Linux Commands Everyone Must Know. You may also want to read about How to add Users, Groups and Assign Permissions in Linux. Please like, and leave your reviews in the comment section below.

Have a great one! 

One reply on “Basic Linux Commands Everyone Must know”

  • student
    14 October, 2022 at 12:06 PM

    v helpful

Comments are closed.