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

Linux Command Line

linux commands

Uploaded by

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

Linux Command Line

linux commands

Uploaded by

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

Linux Command Line Cheat Sheet:

All the Commands You Need


May 16, 2024 / By Nathan House

230
SHARES

Listen to the article


You may need to open a compressed file, but you've forgotten the TAR commands. Or
you’re new to Linux and need to know the top ten terminal commands that open and
modify files and folders. The sheer volume of Linux terminal commands can overwhelm
beginners, not to mention server administrators, IT professionals, and hobbyists.

Therefore, we’ve prepared this essential Linux command line cheat sheet to help you get
familiar with Linux security commands categorized by the scope of their actions. We’re
confident that this compilation can help you master Linux quickly.
Feel free to download a copy of this cheat sheet here and scroll down to find the
commands you need today.

Linux Command Line Cheat Sheet Search

Search our Linux Command Line cheat sheet to find the right cheat for the term you're
looking for. Simply enter the term in the search bar and you'll receive the matching
cheats available.

Essential Commands
We recommend that you memorize these commands. You’ll need them the most when
operating Linux.

File Management

In the following commands: X may refer to a single file, a string containing a wildcard
symbol referring to a set of multiple files, e.g., file*.txt, or the stream output of a
piped command (in which case the syntax would be X | command instead
of command X); Y is a single directory; A and B are path strings of files/directories.

COMMAND DESCRIPTION

Wildcard symbol for variable length, e.g., *.txt refers to all files
*
extension

Wildcard symbol referring to a single character, e.g., Doc?.docx can


?
to Doc1.docx, DocA.docx, etc.

ls List the names of files and subfolders in the current directory. Option
COMMAND DESCRIPTION

l, -a, -t which you may combine, e.g., -alt.

Also show details of each item displayed, such as user permissions and
ls -l
when the item was last modified

ls -a Also display hidden files/folders. May be combined with ls -l to for

Sort the files/folders according to the last modified time/date, starti


ls -t
most recently modified item

ls X List the files

Change directory to Y.

cd Y Special instances of Y:
. — current directory
.. — parent directory

cd To the $HOME directory

cd .. Up one level to enclosing folder or parent directory

cd /etc To the /etc directory

Compare two files A and B for sameness. No output if A and B ar


cmp A B
outputs character and line number otherwise.

diff A
Compare two files A and B for differences. Outputs the difference.
B

pwd Display the path of the current working directory.

mkdir X Make a new directory named X inside the current directory.

mv A B Move a file from path A to path B. Also used for renaming files.
COMMAND DESCRIPTION

Examples:
- Moving between directories folder1 and folder2:
mv ./folder1/file.txt ./folder2

The file name will remain unchanged, and its new path will
be ./folder2/file.txt.
- Renaming a file: mv new_doc.txt expenses.txt
The new file name is expenses.txt.

Copy a file from path A to path B. Usage similar to mv both in mov

directory and simultaneously renaming the file in its new location.


cp A B
Example: cp ./f1/file.txt ./f2/expenses.txt simultaneously copi
file file.txt to the new location with a new name expenses.txt.

Recursively copy a directory Y and its contents to Z. If Z exists,


cp -r Y
source Y into it; otherwise, create Z and Y becomes its subdirect
Z
with Y’s contents

rm X Remove (delete) X permanently.

rm -r Y Recursively delete a directory Y and its contents

rm -f X Forcibly remove file X without prompts or confirmation

rm -rf
Forcibly remove directory Y and its contents recursively
Y

rmdir Y Remove a directory Y permanently, provided Y is empty.

open X Open X in its default program.

open -e
Opens X in the default text editor (macOS: TextEdit)
X
COMMAND DESCRIPTION

touch X Create an empty file X or update the access and modification times of

cat X View contents of X.

cat -b
Also display line numbers as well.
X

wc X Display word count of X.

Display the first 10 lines of X. If more than a single file is specifi


head X is preceded by a header consisting of the string "==> X <=='' where "X

name of the file.

head -n
Show the first 4 lines of X.
4 X

ls *.c
| head Display the first 5 items of a list of *.c files in the current direc
-n 5

Display the last (10, by default) lines of X. If more than a single fi


tail X specified, each file is preceded by a header consisting of the string "
where "X'' is the name of the file.

tail -n Display entire contents of the file(s) X specified, with header of re


+1 X names

Display the last 10 lines of the file(s) X specified, and track chang
tail -f
them at the end. Overwriting X or modifying X with a text editor su
X
would mess up this command’s output.

Read a file with forward and backward navigation. Often used with pipe,
less
file.txt | less
COMMAND DESCRIPTION

ln -s A
Create symbolic link of path A to link name S.
S

Start Your Cyber Security


Career with StationX

• 1000+ Classes & Virtual Labs


• Top Certification Prep
• Mentorship & Career Support
• Community Access
• Advanced Training & Networking

MEMBERSHIP →

00

DAYS

02

HRS

01

MINS
53

SECS

Input/Output Redirection

These are helpful for logging program output and error messages.

COMMAND DESCRIPTION

echo TEXT Display a line of TEXT or the contents of a variable.

echo -e Also interprets escape characters in TEXT, e.g., \n → new line


TEXT → backslash, \t → tab.

echo -n
Omits trailing newline of TEXT.
TEXT

| is the pipe character; feeds the output of the command cmd1


cmd1 | cmd2
to the command cmd2, e.g., ps aux | grep python3.

Redirect output of a command cmd to a file file. Overwrites pr


cmd > file
content of file.

Redirect output of cmd to file. Overwrites pre-existing conten


cmd >& file
Suppresses the output of cmd.

cmd >
Suppress the output of cmd.
/dev/null

cmd >> file Append output of cmd to file.

cmd < file Read input of cmd from file.

cmd << Read input of cmd from the standard input with the delimiter
delim character delim to tell the system where to terminate the input
COMMAND DESCRIPTION

counting the number of lines of ad-hoc input:


wc -l << EOF
> I like
> apples
> and
> oranges.
> EOF
4
Hence there are only 4 lines in the standard input delimited by E

cmd <<<
Input a text string to cmd.
string

cmd 2> foo Redirect error messages of cmd to foo.

cmd 2>> foo Append error messages of cmd to foo.

cmd &> file Redirect output and error messages of cmd to file.
Search and Filter

These commands help you find the files you want.

COMMAND DESCRIPTION

Search for a text pattern patt in X. Commonly used with


grep patt
e.g., ps aux | grep python3 filters out the processes
/path/to/src
containing python3 from all running processes of all use

grep -r patt Search recursively (the target directory /path/to/src a


/path/to/src subdirectories) for a text pattern patt.

grep -v patt X Return lines in X not matching the specified patt.


COMMAND DESCRIPTION

grep -l patt X Write to standard output the names of files containing pat

grep -i patt X Perform case-insensitive matching on X. Ignore the case of

find Find files.

find /path/to/src Find all files in /path/to/src matching the pattern "*.
-name "*.sh" file name.

find /home -size


Find all files in the /home directory larger than 100MB.
+100M

locate name Find files and directories by name.

sort X Arrange lines of text in X alphabetically or numerically.


Archives

These commands are for unpacking compressed files


(.zip, .gz, .bz2, .tar, etc.) with large or complex contents, such as
programs. TAR commands, in particular, can be tricky to remember.

COMMAND DESCRIPTION

tar Manipulate archives with .tar extension.

Get verbose output while manipulating TAR archives. May


tar -v
option with others, e.g., tar -tvf.

tar -cf archive.tar


Create a TAR archive named archive.tar containing
Y

tar -xf archive.tar Extract the TAR archive named archive.tar.

tar -tf archive.tar List contents of the TAR archive named archive.tar.

tar -czf Create a gzip-compressed TAR archive


COMMAND DESCRIPTION

archive.tar.gz Y named archive.tar.gz containing Y.

tar -xzf
Extract the gzip-compressed TAR archive named archive
archive.tar.gz

tar -cjf Create a bzip2-compressed TAR archive


archiave.tar.bz2 Y named archive.tar.bz2 containing Y.

tar -xjf
Extract the bzip2-compressed TAR archive named archiv
archive.tar.bz2

gzip Manipulate archives with .gz extension.

gzip Y Create a gzip archive named Y.gz containing Y.

gzip -l Y.gz List contents of gzip archive Y.gz.

gzip -d Y.gz
Extract Y.gz and recover the original file Y.
gunzip Y.gz

bzip2 Manipulate archives with .bz2 extension.

bzip2 Y Create a bzip2 archive named Y.bz2 containing Y.

bzip2 -d Y.gz
Extract Y.bz2 and recover the original file Y.
bunzip2 Y.gz

zip -r Z.zip Y Zip Y to the ZIP archive Z.zip.

unzip Z.zip Unzip Z.zip to the current directory.

unzip Z.zip List contents of Z.zip.

File Transfer
These commands are for logging in to local and remote hosts, and for uploading and
downloading files, transferring them between devices. Remember to omit the square
brackets "[" and "]" when you input the optional parameters they enclose.

COMMAND DESCRIPTION

ssh user@access Connect to access as user.

ssh access Connect to access as your local username.

ssh -p port user@access Connect to access as user using port.

Login to hostN as userN via secure copy pr


for N=1,2.

Example usage:
scp alice@pi:/home/source bob@arduino:/d
scp [user1@]host1:[path1]
[user2@]host2:[path2] path1 and path2 may be local or remote, but

they’re absolute rather than relative paths,


e.g., /var/www/*.html, /usr/bin.

If user1 and user2 are not specified, scp

your local username.

scp -P port [user1@]host1:


[path1] [user2@]host2: Connect to hostN as userN using port for
[path2]

scp -r [user1@]host1:
Recursively copy all files and directories
[path1] [user2@]host2:
from path1 to path2.
[path2]

sftp [user@]access Login to access as user via secure file tr


protocol. If user is not specified, your loca
COMMAND DESCRIPTION

will be used.

sftp access Connect to access as your local username.

sftp -P port user@access Connect to access as user using port.

Synchronize [path1] to [path2], preserving


rsync -a [path1] [path2] links, attributes, permissions, ownerships, and
settings.

Synchronize [path1] on the remote host host


rsync -avz host1:[path1] local path [path2], preserving symbolic links,
[path2] permissions, ownerships, and other settings. It
compresses the data involved during the transfer

Start Your Cyber Security


Career with StationX

• 1000+ Classes & Virtual Labs


• Top Certification Prep
• Mentorship & Career Support
• Community Access
• Advanced Training & Networking

MEMBERSHIP →

00
DAYS

02

HRS

01

MINS

53

SECS

File Permissions
Not all files are equally accessible. To prevent unwanted tampering, some files on your
device may be read-only. For more information about file permissions on Linux, refer to
our Linux File Permissions Cheat Sheet.

COMMAND DESCRIPTION

chmod Change permissions of a file or directory. Permissions may be


permission form [u/g/o/a][+/-/=][r/w/x] (see examples below) or a t
file octal number.

chown user2 Change the owner of a file to user2.


COMMAND DESCRIPTION

file

chgrp group2
Change the group of a file to group2.
file
Usage examples:

 chmod +x testfile → allow all users to execute the file


 chmod u-w testfile → forbid the current user from writing or
changing the file
 chmod u+wx,g-x,o=rx testfile → simultaneously add write
and execute permissions to user, remove execute permission from
group, and set the permissions of other users to only read and
write.
Numeric Representation

The table below compares Linux file permissions in octal form and in the
format [u/g/o/a][+/-/=][r/w/x].

OCTAL PERMISSION(S) EQUIVALENT TO APPLIC

0 No permissions -rwx

1 Execute permission only =x

2 Write permission only =w

3 Write and execute permissions only: 2 + 1 = 3 =wx

4 Read permission only =r

5 Read and execute permissions only: 4 + 1 = 5 =rx

6 Read and write permissions only: 4 + 2 = 6 =rw

7 All permissions: 4 + 2 + 1 = 7 =rwx


Examples
 chmod 777 testfile → allow all users to execute the file
 chmod 177 testfile → restrict current user (u) to execute-only,
while the group (g) and other users (o) have read, write and
execute permissions
 chmod 365 testfile → user (u) gets to write and execute only;
group (g), read and write only; others (o), read and execute only.
Other Links You Might Like:

 Linux File Permissions Cheat Sheet


 Unix Commands Cheat Sheet
 Tmux Cheat Sheet
 Tcpdump Cheat Sheet
 Bash Cheat Sheet
 Vi Cheat Sheet
 Our Linux Courses Collection
System Information
These commands come in handy when you’re developing new applications for Linux or
troubleshooting your Linux machine.

General

These provide information about your Linux machine and perform administrative tasks.

COMMAND DESCRIPTION

uname Show the Linux system information.

uname -a Detailed Linux system information

uname -r Kernel release information, such as kernel version

uptime Show how long the system is running and load information.

su Superuser; use this before a command that requires root access e


sudo shutdown

cal Show calendar where the current date is highlighted.


COMMAND DESCRIPTION

date Show the current date and time of the machine.

halt Stop the system immediately.

shutdown Shut down the system.

reboot Restart the system.

last reboot Show reboot history.

man COMMAND Shows the manual for a given COMMAND. To exit the manual, pre

hostname Show system host name

hostname -I Display IP address of host

cat /etc/*- Show the version of the Linux distribution installed. For exampl
release using Red Hat Linux, you may replace * with redhat.
Hardware

These commands provide details about the hardware supporting your Linux machine.

COMMAND DESCRIPTION

Display messages in kernel ring buffer (data structure that


dmesg messages related to the operation of the program running th
system)

cat /proc/cpuinfo Display information about the central processing unit (CPU)

cat /proc/meminfo Display memory information

lspci -tv Displays information about each Peripheral Component Interc


device on your system.
The option -t outputs the information as a tree diagram,
COMMAND DESCRIPTION

for verbose output.

Display information about Universal Serial Bus (USB) device


devices connected to them.
lsusb -tv
The option -t outputs the information as a tree diagram,

for verbose output.

dmidecode Display system hardware components, serial numbers, and BIO

hdparm -i
Display information about the disk sda
/dev/sda

hdparm -tT
Perform a read speed test on the disk sda
/dev/sda

badblocks -s
Test for unreadable blocks on the disk sda
/dev/sda
Disk Usage

These commands provide storage details regarding your Linux machine.

COMMAND DESCRIPTION

df Display free disk space.

du Show file/folder sizes on disk.

du -ah Disk usage in human readable format (KB, MB etc.)

du -sh Total disk usage of the current directory

du -h Free and used space on mounted filesystems

du -i Free and used inodes on mounted filesystems


COMMAND DESCRIPTION

fdisk -l List disk partitions, sizes, and types

free -h Display free and used memory in human readable units.

free -m Display free and used memory in MB.

free -g Display free and used memory in GB.


Process Management and Performance
Monitoring
The following is redolent of functions in Windows Task Manager, but on the command
line.

Table of Contents Table of Contents

 Linux Command Line Cheat Sheet Search  Linux Command Line Cheat Sheet S

 Essential Commands  Essential Commands

 File Transfer  File Transfer

 File Permissions  File Permissions

 System Information  System Information

 Process Management and Performance Monitoring  Process Management and Performan

 User Management  User Management

 Networking  Networking

 Installing New Programs  Installing New Programs

 Conclusion  Conclusion

 Frequently Asked Questions  Frequently Asked Questions


Table of Contents Table of Contents

Level Up in Cyber Security:Join Our Level Up in Cyber Security:Jo


Membership Today! Membership Today!

LEARN MORE LEARN MORE


Table of Contents Table of Contents

 COURSES  COURSES
Table of Contents Table of Contents

 BLOG  BLOG

 RESOURCES  RESOURCES
 

 

 

User Management
These commands give information on the system’s users and allows superuser
administrators to change user settings.

COMMAND DESCRIPTION

who Display who is logged in

w Display what users are online and what they are doing

users List current users

whoami Display what user you are logged in as

id Display the user ID and group IDs of your current user

last Display the last users who have logged onto the system

groupadd gp1 Create a group named gp1

useradd -c "Alice Create an account named ab1, with a comment of "Alice


Bob" -m ab1 create the new user’s home directory

userdel ab1 Delete the account named ab1


COMMAND DESCRIPTION

usermod -aG gp1 ab1 Add the account ab1 to the group gp1

Networking
These commands regulate how your Linux machine communicates with other computers,
such as the local area network (LAN) router or external websites.

COMMAND DESCRIPTION

ifconfig Display all network interfaces with IP addresses

ifconfig -a Display all network interfaces, even if any of them is down, with

ifconfig
Display IP addresses and details of the eth0 interface
eth0

ip a Another way to display all network interfaces with IP addresses

ethtool
Query or control network driver and hardware settings of the inter
eth0

Print open sockets of network connections, routing tables, interfa


netstat masquerade connections, and multicast memberships.
Pipe with the less command: e.g., netstat -a | less

netstat -a Show both listening and non-listening sockets

netstat -l Show only listening sockets

netstat -
Show listening TCP and UDP ports and corresponding programs
nutlp

Send ICMP echo request to host, which may be a symbolic name, do


ping host
IP address

whois Display whois information for domain


COMMAND DESCRIPTION

domain

dig domain Display DNS information for domain

dig -x addr Do a reverse lookup on an IPv4 or IPv6 address addr

host domain Display DNS IP address for domain

wget LINK Download from location LINK

curl LINK Display the HTML source of LINK. Check out our curl Cheat Sheet

Start Your Cyber Security


Career with StationX

• 1000+ Classes & Virtual Labs


• Top Certification Prep
• Mentorship & Career Support
• Community Access
• Advanced Training & Networking

MEMBERSHIP →

00

DAYS

02
HRS

01

MINS

53

SECS

Installing New Programs


A package file is an archive containing compiled software and other resources it uses.
The package file extension and the package installer (a utility for installing new
programs) depend on the Linux distribution.

Know your system’s Linux distribution to understand the correct installation commands
tailored to it. If the package comes with a README component, it should contain
application-specific installation instructions such as extracting TAR files (refer to this
article’s Archives section), ./setup.sh, and make install.

Package Management Overview

The following table is on package management in popular Linux distributions.

PACK
LINUX DISTRIBUTION PACKAGE FILE EXTENSION
INST

Debian / Ubuntu .deb ap

Rocky / Fedora / Red Hat


.rpm yu
Enterprise Linux

Arch Linux / Manjaro /


.pkg, .pacman, .pkg.tar(.xz/.zst/.gz) pa
Garuda / Chakra
Package Management Commands

Here are the commands for package management in popular Linux distributions.
ROCKY / FEDORA /
ARCH LINUX / MAN
LINUX DISTRIBUTION DEBIAN / UBUNTU RED HAT ENTERPRISE
GARUDA / CHAKRA
LINUX

The command pa
Update list of packages
sudo apt dnf check- Syy achieves
available from remote
update update but may damage
repositories
Use pacman -

Upgrade installed sudo apt sudo dnf


pacman -Syu
packages upgrade upgrade

Find a package with apt search dnf search


pacman -Ss k
keyword in the name keyword keyword

View description and


apt show dnf info
summary information about pacman -Si p
package package
a package

Install a package (with


sudo dnf
appropriate file sudo dpkg -i
install pacman -S pa
extension) on the local package.deb
package.rpm
file system

sudo apt
Remove / uninstall a sudo dnf erase
remove pacman -R pa
package package
package

Conclusion
Learning basic Linux commands is a great way to kickstart your education in IT and
cyber security. You’re now ready to practice them on any Linux distribution you fancy. If
you want to focus on cyber security, go for Kali Linux and Parrot OS. Also, don't forget
to check out our Unix Commands Cheat Sheet here.

Take your Linux skills to the next level with comprehensive Linux training modules
included in our StationX Membership.

You might also like