0% found this document useful (0 votes)
9 views

Linux Commands Cheat Sheet

Uploaded by

Saumya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Linux Commands Cheat Sheet

Uploaded by

Saumya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Linux Commands Cheat Sheet

What is Linux?

Linux is an open-source UNIX-like operating system (OS). An operating


system is a software that directly manages a system’s hardware and
resources, like CPU, memory, and storage. OS acts as a GUI through which
user can communicate with the computer. The OS sits between applications
and hardware and makes the connections between all of your software and
the physical resources that do the work.

Linux Commands List – Table of Content


File and Directory Operations Commands
File Permission Commands
File Compression and Archiving Commands
Process Management Commands
System Information Commands
Networking Commands
IO Redirection Commands
Environment Variable Commands
User Management Commands
Shortcuts Commands List
Bash Shortcuts Commands
Nano Shortcuts Commands
VI Shortcuts Commands
Vim Shortcuts Commands
FAQs on Linux Commands Cheat Sheet

Basic Linux Commands with Examples


In this Linux cheat sheet, we will cover all the most important Linux
commands, from the basics to the advanced. We will also provide some tips
on how to practice and learn Linux commands. This cheat sheet is useful for
Beginners and Experience professionals.

1. File and Directory Operations Commands


File and directory operations are fundamental in working with the Linux
operating system. Here are some commonly used File and Directory
Operations commands:

Command Description Options Examples


HTML Cheat Sheet CSS Cheat Sheet JS Cheat Sheet Bootstrap Cheat Sheet jQuery Cheat Sheet Angular C
ls ls -l
displays files and
-l: Long format
directories with
listing.
detailed information.
-a: Include
ls -a
List files and hidden files
shows all files and
directories. hidden ones
directories, including
-h: Human-
ls -lh
readable file
displays file sizes in a
sizes.
human-readable
format.

cd cd /path/to/directory
Change changes the current
directory. directory to the
specified path.

pwd Print current pwd


working displays the current
directory. working directory.

mkdir mkdir my_directory


Create a new creates a new
directory. directory named
“my_directory”.

rm Remove files -r: Remove rm file.txt


and directories. directories deletes the file named
recursively. “file.txt”.
-f: Force rm -r my_directory
removal deletes the directory
without “my_directory” and its
confirmation. contents.
Command Description Options Examples

rm -f file.txt
forcefully deletes the
file “file.txt” without
confirmation.

cp cp -r directory
destination
copies the directory
“directory” and its
-r: Copy
Copy files and contents to the
directories
directories. specified destination.
recursively.
cp file.txt destination
copies the file “file.txt”
to the specified
destination.

mv mv file.txt
new_name.txt
renames the file
Move/rename “file.txt” to
files and “new_name.txt”.
directories. mv file.txt directory
moves the file “file.txt”
to the specified
directory.

touch Create an
touch file.txt
empty file or
creates an empty file
update file
named “file.txt”.
timestamps.

cat View the cat file.txt


contents of a displays the contents
file. of the file “file.txt”.

head Display the -n: Specify the head file.txt


first few lines of number of shows the first 10
a file. lines to lines of the file
display. “file.txt”.
Command Description Options Examples

head -n 5 file.txt
displays the first 5
lines of the file
“file.txt”.

tail tail file.txt


shows the last 10
-n: Specify the lines of the file
Display the last
number of “file.txt”.
few lines of a
lines to tail -n 5 file.txt
file.
display. displays the last 5
lines of the file
“file.txt”.

ln ln -s source_file
link_name
-s: Create
Create links creates a symbolic link
symbolic (soft)
between files. named “link_name”
links.
pointing to
“source_file”.

find find /path/to/search -


-name: Search name “*.txt”
Search for files by filename. searches for all files
and directories. -type: Search with the extension
by file type. “.txt” in the specified
directory.

2. File Permission Commands


File permissions on Linux and Unix systems control access to files and
directories. There are three basic permissions: read, write, and execute. Each
permission can be granted or denied to three different categories of users:
the owner of the file, the members of the file’s group, and everyone else.

Here are some file permission commands:


Command Description Options Examples

chmod u: User/owner
permissions.
g: Group
permissions.
o: Other
chmod u+rwx file.txt
permissions.
Change file grants read, write, and
+: Add
permissions. execute permissions to
permissions.
the owner of the file.
–: Remove
permissions.
=: Set
permissions
explicitly.

chown chown user file.txt


Change file changes the owner of
ownership. “file.txt” to the specified
user.

chgrp chgrp group file.txt


Change group changes the group
ownership. ownership of “file.txt”
to the specified group.

umask umask 022


sets the default file
Set default file permissions to read and
permissions. write for the owner, and
read-only for group and
others.

3. File Compression and Archiving Commands


Here are some file compression and archiving commands in Linux:
Commands Description Options Examples

tar -c: Create a


new archive.
-x: Extract
files from an
archive.
tar -czvf archive.tar.gz
-f: Specify the
files/
archive file
Create or creates a compressed
name.
extract archive tar archive named
-v: Verbose
files. “archive.tar.gz”
mode.
containing the files in
-z: Compress
the “files/” directory.
the archive
with gzip.
-j: Compress
the archive
with bzip2.

gzip gzip file.txt


-d:
Compress compresses the file
Decompress
files. “file.txt” and renames it
files.
as “file.txt.gz”.

zip zip archive.zip file1.txt


file2.txt
Create -r: Recursively
creates a zip archive
compressed include
named “archive.zip”
zip archives. directories.
containing “file1.txt”
and “file2.txt”.

4. Process Management Commands


In Linux, process management commands allow you to monitor and control
running processes on the system. Here are some commonly used process
management commands:
Commands Description Options Examples

ps ps aux
shows all running
Display running -aux: Show all processes with
processes. processes. detailed
information.

top top
displays a dynamic
Monitor system
view of system
processes in real-
processes and
time.
their resource
usage.

kill kill PID


terminates the
Terminate a -9: Forcefully
process with the
process. kill a process.
specified process
ID.

pkill pkill
Terminate process_name
processes based terminates all
on their name. processes with the
specified name.

pgrep pgrep
process_name
List processes
lists all processes
based on their
with the specified
name.
name.

grep used to search for -i: Ignore case grep -i “hello”


specific patterns or distinctions file.txt
regular while grep -v “error”
expressions in text searching. file.txt
files or streams -v: Invert the grep -r “pattern”
match, directory/
Commands Description Options Examples

and display displaying grep -l “keyword”


matching lines. non-matching file.txt
lines. grep -n “pattern”
-r or -R: file.txt
Recursively In these examples
search we are extracting
directories for our desirec output
matching from filename
patterns. (file.txt)
-l: Print only
the names of
files
containing
matches.
-n: Display
line numbers
alongside
matching
lines.
-w: Match
whole words
only, rather
than partial
matches.
-c: Count the
number of
matching lines
instead of
displaying
them.
-e: Specify
multiple
patterns to
search for.
-A: Display
lines after the
matching line.
-B: Display
lines before
Commands Description Options Examples

the matching
line.
-C: Display
lines both
before and
after the
matching line.

5. System Information Commands


In Linux, there are several commands available to gather system information.
Here are some commonly used system information commands:

sudCommand Description Options Examples

uname uname -a
Print system -a: All system
displays all system
information. information.
information.

whoami whoami
Display current
shows the current
username.
username.

df df -h
-h: Human-
Show disk space displays disk space
readable
usage. usage in a human-
sizes.
readable format.

du -h: Human-
du -sh directory/
readable
Estimate file and provides the total
sizes.
directory sizes. size of the
-s: Display
specified directory.
total size only.

free free -h
Display memory -h: Human-
displays memory
usage readable
usage in a human-
information. sizes.
readable format.
sudCommand Description Options Examples

uptime uptime
Show system
shows the current
uptime.
system uptime.

lscpu lscpu
Display CPU provides detailed
information. CPU information.

lspci lspci
List PCI devices.
List PCI devices.

lsusb lsusb
List USB lists all connected
devices. USB devices.

6. Networking Commands
In Linux, there are several networking commands available to manage and
troubleshoot network connections. Here are some commonly used
networking commands:

Command Description Examples

ifconfig Display network ifconfig


interface shows the details of all network
information. interfaces.

ping Send ICMP echo ping google.com


requests to a sends ICMP echo requests to
host. “google.com” to check connectivity.

netstat Display network netstat -tuln


connections and shows all listening TCP and UDP
statistics. connections.
Command Description Examples

ss ss -tuln
Display network
shows all listening TCP and UDP
socket
connections.
information.

ssh Securely connect ssh user@hostname


to a remote initiates an SSH connection to the
server. specified hostname.

scp scp file.txt


Securely copy
user@hostname:/path/to/destination
files between
securely copies “file.txt” to the specified
hosts.
remote host.

wget wget http://example.com/file.txt


Download files
downloads “file.txt” from the specified
from the web.
URL.

curl curl http://example.com


Transfer data to
retrieves the content of a webpage from
or from a server.
the specified URL.

7. IO Redirection Commands
In Linux, IO (Input/Output) redirection commands are used to redirect the
standard input, output, and error streams of commands and processes. Here
are some commonly used IO redirection commands:

Command Description

cmd < file Input of cmd is taken from file.

cmd > file Standard output (stdout) of cmd is redirected to file.

cmd 2> file Error output (stderr) of cmd is redirected to file.

cmd 2>&1 stderr is redirected to the same place as stdout.


Command Description

cmd1 <(cmd2) Output of cmd2 is used as the input file for cmd1.

cmd > /dev/null Discards the stdout of cmd by sending it to the null device.

cmd &> file Every output of cmd is redirected to file.

cmd 1>&2 stdout is redirected to the same place as stderr.

cmd >> file Appends the stdout of cmd to file.

8. Environment Variable Commands


In Linux, environment variables are used to store configuration settings,
system information, and other variables that can be accessed by processes
and shell scripts. Here are some commonly used environment variable
commands:

Command Description

export
Sets the value of an environment variable.
VARIABLE_NAME=value

Displays the value of a specific environment


echo $VARIABLE_NAME
variable.

Lists all environment variables currently set in


env
the system.

unset VARIABLE_NAME Unsets or removes an environment variable.

Shows a list of all currently exported


export -p
environment variables.

Sets the value of an environment variable for


env VAR1=value COMMAND
a specific command.
Command Description

Displays the values of all environment


printenv
variables.

9. User Management Commands


In Linux, user management commands allow you to create, modify, and
manage user accounts on the system. Here are some commonly used user
management commands:

Command Description

who Show who is currently logged in.

sudo adduser Create a new user account on the system with the
username specified username.

Display information about all the users currently


finger logged into the system, including their usernames,
login time, and terminal.

sudo deluser USER


Remove the specified user from the specified group.
GROUPNAME

last Show the recent login history of users.

Provide information about the specified user,


finger username including their username, real name, terminal, idle
time, and login time.

Delete the specified user account from the system,


sudo userdel -r
including their home directory and associated files.
username
The -r option ensures the removal of the user’s files.

sudo passwd -l Lock the password of the specified user account,


username preventing the user from logging in.
Command Description

Switch to another user account with the user’s


su – username
environment.

sudo usermod -a -G Add an existing user to the specified group. The


GROUPNAME user is added to the group without removing them
USERNAME from their current groups.

10. Shortcuts Commands


There are many shortcuts commands in Linux that can help you be more
productive. Here are a few of the most common ones:

10.1: Bash Shortcuts Commands:

Navigation Description Editing Description History Description

Cut/delete
from the Search
Move to the cursor command
Ctrl + A beginning of Ctrl + U position to Ctrl + R history
the line. the (reverse
beginning of search).
the line.

Cut/delete
from the
Move to the Escape from
cursor
Ctrl + E end of the Ctrl + K Ctrl + G history
position to
line. search mode.
the end of
the line.

Cut/delete Go to the
Move back
Ctrl + the word previous
Ctrl + B one Ctrl + P
W before the command in
character.
cursor. history.
Navigation Description Editing Description History Description

Go to the
Move
Paste the next
Ctrl + F forward one Ctrl + Y Ctrl + N
last cut text. command in
character.
history.

Terminate
Move back Clear the
Alt + B Ctrl + L Ctrl + C the current
one word screen.
command.

Move
Alt + F forward one
word.

10.2: Nano Shortcuts Commands:

File Description Navigation Description Editing Descrip


Operations

Cut/del
from t
Save the Scroll up one curso
Ctrl + O Ctrl + Y Ctrl + K
file. page. position
the end
the lin

Exit Nano
Uncut/re
(prompt to Scroll down
Ctrl + X Ctrl + V Ctrl + U the last
save if one page.
text
modified).

Read a file Mark a b


Go to a
into the of text
Ctrl + R Alt + \ specific line Ctrl + 6
current copying
number.
buffer. cuttin
File Description Navigation Description Editing Descrip
Operations

Go to the
Justify the Cut/del
beginning of
Ctrl + J current Alt + , Ctrl + K the mar
the current
paragraph. block of
line.

Go to the Copy t
Alt + . end of the Alt + 6 marked b
current line. of tex

10.3: VI Shortcuts Commands:

Command Description

Change the current word. Deletes from the cursor position to the
cw
end of the current word and switches to insert mode.

dd Delete the current line.

x Delete the character under the cursor.

Enter replace mode. Overwrites characters starting from the


R
cursor position until you press the Escape key.

o Insert a new line below the current line and switch to insert mode.

u Undo the last change.

Substitute the character under the cursor and switch to insert


s
mode.

dw Delete from the cursor position to the beginning of the next word.

D Delete from the cursor position to the end of the line.


Command Description

4dw Delete the next four words from the cursor position.

A Switch to insert mode at the end of the current line.

S Delete the current line and switch to insert mode.

Replace the character under the cursor with a new character


r
entered from the keyboard.

i Switch to insert mode before the cursor.

3dd Delete the current line and the two lines below it.

Exit from insert or command-line mode and return to command


ESC
mode.

Restore the current line to its original state before any changes
U
were made.

~ Switch the case of the character under the cursor.

a Switch to insert mode after the cursor.

Delete from the cursor position to the end of the line and switch to
C
insert mode.

10.4: Vim Shortcuts Commands:

Normal Description Command Description Visual Description


Mode Mode Mode

Enter insert
mode at the Enter visual
i current :w Save the file. v mode to
cursor select text.
position.
Normal Description Command Description Visual Description
Mode Mode Mode

Delete the
Copy the
character
x :q Quit Vim. y selected
under the
text.
cursor.

Quit Vim
Delete the
Delete the without
dd :q! d selected
current line. saving
text.
changes.

:wq
Paste the
Copy the Save and
yy or p copied or
current line. quit Vim.
:x deleted text.

Paste the Replace all


copied or occurrences
p deleted text :s/old/new/g of “old” with
below the “new” in the
current line. file.

:set nu
Undo the or Display line
u
last change. numbers.
:set number

Ctrl + Redo the


R last undo.

Conclusion
In conclusion, Linux is a widely used operating system for development, and
as a developer, you should have knowledge of Linux and its basic
commands. In this Cheat Sheet, we covered all commands like creating
directories, file compression and archiving, process management, system
information, networking and more. In addition to that, this Linux Cheat Sheet

You might also like