Linux Commands CheatSheet

Are you looking to learn Linux / revise the basic Linux Commands? If you do, this article is for you.

programmedraj
5 min readFeb 23, 2023
Linux Commands Cheatsheet for Beginners
Photo by Gabriel Heinzer on Unsplash

Linux has a CLI (command line interface). In this tutorial, we are going to cover the basic commands that we use in the shell of Linux.

To open the terminal, press Ctrl+Alt+T in Ubuntu, or press Alt+F2, type in gnome-terminal, and press enter.

Create multiple folders at once

mkdir folder1 folder2 folder3

Creates child folder if your folder exists else error

mkdir your/child-folder

Creates your folder and then child folder too

mkdir -p your/child

Move backwards by one directory

cd .. 
cd
#both works same.

Get present working directory path

pwd

To remember the last working directory to traverse across multiple directories

pushd #adds last accessed folder at top of stack.
popd #removes & traverses back to folder at top of stack.

Move directory or file

mv /source_path /destination_path
#can be used to rename the directory
mv /munbai /mumbai

Copies files from source to destination path

cp /asia/ind/test.txt  /usa/California

Delete file/directory

rm /ind/test.txt  #delete "test.txt" file. 
rm -r /india #delete files in the folder "india".

to copy or delete directory use -r (stands for repetitive tasks)

Create new file

touch ind/tester.txt

Read file content

cat ind/tester.txt     #can't scroll content.
more ind/tester.txt #scrollable view for content in file.
  • Spacebar to scroll by screen
  • Enter to scroll by line
  • b to scroll back by screen
  • / to search by text

Open & Edit file

cat > ind/tester.txt

Use CTRL + D to save & exit

List Command for directories & files

ls       #just lists files
ls - l #lists files with details
ls -a #lists all files including hidden
ls - lt #lists files in order created
ls - ltr #lists files in reverse order of creation

Retrieve OS version.

/etc/os-release

Retrieves kernel version

uname -r 

List PCI devices (ethernet cards, video cards)

lspci

List block devices (physical disks)

lsblk

Search city.txt throughout linux (stored in database)

locate city.txt

Update database for new file so that can be searched using locate.

updatedb

Search file in specific directory

find  /specific/directory -name filename.format

grep command:

grep text-to-find filename.format          #find string in file
grep -i text-to-find filename.format #for case insensitive search
grep -r "text-to-find" /specific/directory #for searching across file in folder

#search lines where text not found in line
grep -v "text-to-find" filename.format
#search exact word
grep -w text-to-find filename.format

#print lines where exact word not found
grep -vw text-to-find filename.format


grep -A1 texttofind filename.format
#prints line with text & 1 line after the sentence having keyword.

grep -A2 texttofind filename.format
#prints line with text & 2 lines after the sentence having keyword.

grep -B1 texttofind filename.format
#prints line with text & 1 line before the sentence having keyword.

grep -A1 -B2 texttofind filename.format
#for printing both conditions merged grep command.

To change ownership of file

chown owner:group filename

Go to root folder

cd ~ 

To list the output of commands in columns instead of row format

command | column -t

Some more important features & commands offered by Linux:-

BASH SHELL:-

Use history command to get past commands history, the number associated to each command can be used to directly run that command using !number

Use tab to auto complete commands/ directory.

alias dt = date to give own name to command.

echo $SHELL to print variable value, $SHELL is a variable.

create global variable using export

example:- export OFFICE=caller

Declaring local variable in shell: OFFICE = caller

which tool-name provides location for tool

To add new path variable: export PATH=$PATH:/opt/obs/bin

VI editor:-

modes: command, insert, last line.

vi filename.format To open file in command mode
type “i” to open in insert mode (i.e.to edit content)
type “:” to switch in last line mode. (i.e. to save or discard changes and Exit)
tap “esc” to shift back from any mode to command mode.

Command mode guide for VI / VIM editor:-
to copy line use: y y
paste: p
delete a letter: x
delete a line using: d d
delete 3 lines using: d 3 d
To undo changes: u
To redo changes: ctrl + r

Insert mode guide for VI / VIM editor:-

/line to search for 1st occurrence of “line” using up-down approach & drags cursor there.
?line to search for 1st occurrence of “line” using down-up approach & drags cursor there.
n to find next occurrence
N previous occurrence

Last Line mode guide for VI / VIM editor:-

Save file :w
Exit file :q
Save & Exit file :wq
Exit file without saving :q!

Package management in linux:-

debian based — dpkg, apt, apt-get
red hat based- rpm, yum
• Always use yum / rpm based distros for auto package dependency resolution.
rpm & dpkg are low-level package manager.
• yum & apt/apt-get are high-level package manager.

Tape Archives in linux:-

tar -cf test.tar file1 file2 file3 creates new tape archive with all 3 files.
ls -ltr test.tar get tape archive details
tar -tf test.tar to view contents in tar file.
tar -xf test.tar extract contents from tar file.
tar -zcf test.tar file1 file2 file3 creates compressed tar including all 3 files.

STDOUT:-

echo “tree” > test.txt to rediect to test.txt file
cat “tree” >> test.txt appends tree in existing test.txt file.

STDERR:-

cat missing_file 2> error.txt to rediect error to error.txt
cat missing_file 2>> error.txt to append error to error.txt

cat missing_file 2> /dev/null

Here, /dev/null means bit bucket & is used to dump values we don’t want to print on screen.

| (command line pipe) used to merge multiple commands.
Example:- grep Hello sample.text | less (to print 1 line with Hello occurrence even if others exists)

tee command:-
echo “the” | tee -a shell.txt prints content in console & then appends to file.

chmod uses symbolic & numeric method to change file permissions.

Symbolic method example: chmod u+rwx test-file to provide full access.
Numeric method example: chmod 777 test-file to provide full access

File permissions representation alongwith octal notation:-

r read with O.N = 4
w write with O.N = 2
x execute with O.N = 1
- no permission with O.N = 0

rwx can be represented as 4+2+1 = 7
rw- can be represented as 4+2+0 = 6

SYSTEMD Service (Service Management Service):-

systemctl used to manage services — start/stop/status/restart
journalctl used to figureout issues/ logging & debugging.

crontab -e for running scripts and commands at regular intervals, and at specific times and dates.

SSH & SCP

SSH has password based & password-less mechanism.

use ssh-keygen to generate key or password-less SSH access.
example:- ssh-keygen -t rsa
ssh-copy-id user@hostname stores passw for future use of key for auth.

Use SCP to copy files from remote server to local system.

example:- scp /home/dir/r.tar.gz user@rgrp: /home/r
To preserve ownership of files & copy directory use: scp -pr

Real-time use cases:

grep -o [string-to-be-searched] [filename]
#to print the occurences in errorlog file having string.

grep [string-to-be-searched] [filename]
#to print the lines in errorlog file having string.

du -sh img/filename.format #gives filesize

ls -lh filename.format #gives file details including access rights.

sudo chmod a+rwx /var/DirectoryName
#provides full access to directory for all users.

Feel free to add more commands and make this the go-to cheatsheet for every Linux Newbie ;-)

--

--