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

Linux-Bash Commands

Uploaded by

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

Linux-Bash Commands

Uploaded by

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

Basic Linux Commands with Examples

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:

Comman Description Options Examples


d

ls  -l: Long
format
listing.  ls -l
 -a: Include displays files and directories with detailed
hidden information.
List files and files  ls -a
directories. hidden shows all files and directories, including
ones  ls -lh
 -h: displays file sizes in a human-readable
Human- format.
readable
file sizes.

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

pwd Print current


 pwd
working
displays the current working directory.
directory.

mkdir  mkdir my_directory


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

rm Remove files  -r: Remove  rm file.txt


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

confirmatio forcefully deletes the file “file.txt” without


n. confirmation.

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

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

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

cat View the


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

head  head file.txt


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

tail  tail file.txt


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

ln  -s: Create  ln -s source_file link_name


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

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

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:

Comman Descriptio Options Examples


d n

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

chown Change  chown user file.txt


Comman Descriptio Options Examples
d n

file changes the owner of “file.txt” to the


ownership. specified user.

chgrp Change  chgrp group file.txt


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

umask Set  umask 022


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

3. File Compression and Archiving Commands


Here are some file compression and archiving commands in Linux:

Comman Description Options Examples


ds

tar  -c: Create a


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

gzip  -d:  gzip file.txt


Compress
Decompress compresses the file “file.txt” and
files.
files. renames it as “file.txt.gz”.
Comman Description Options Examples
ds

zip  zip archive.zip file1.txt


 -r:
Create file2.txt
Recursively
compressed creates a zip archive named
include
zip archives. “archive.zip” containing “file1.txt”
directories.
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 Descriptio Options Examples


n

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

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

kill

 kill PID
Terminate a  -9: Forcefully kill a terminates the process
process. process. with the specified
process ID.
Commands Descriptio Options Examples
n

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

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

grep used to search  -i: Ignore case grep -i “hello” file.txt


for specific distinctions while grep -v “error” file.txt
patterns or searching. grep -r “pattern”
regular  -v: Invert the match, directory/
expressions in displaying non-matching grep -l “keyword”
text files or lines. file.txt
streams and  -r or -R: Recursively grep -n “pattern”
display search directories for file.txt
matching lines. matching patterns. In these examples we are
 -l: Print only the names extracting our desirec
of files containing output from filename
matches. (file.txt)
 -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.
Commands Descriptio Options Examples
n

 -B: Display lines before


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:

sudComma Description Options Examples


nd

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

whoami
Display current  whoami
username. shows the current username.

df

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

du  -h: Human-
 du -sh directory/
Estimate file and readable sizes.
provides the total size of the
directory sizes.  -s: Display total
specified directory.
size only.
sudComma Description Options Examples
nd

free

Display memory  free -h


 -h: Human-
usage displays memory usage in a
readable sizes.
information. human-readable format.

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

lscpu  lscpu
Display CPU provides detailed CPU
information. information.

lspci  lspci
List PCI devices.
List PCI devices.

lsusb List all


connected USB  lsusb
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:

Comma Description Examples


nd

ifconfig Display network


 ifconfig
interface
shows the details of all network interfaces.
information.

ping  ping google.com


Send ICMP echo
sends ICMP echo requests to “google.com” to
requests to a host.
check connectivity.
Comma Description Examples
nd

netstat Display network


 netstat -tuln
connections and
shows all listening TCP and UDP connections.
statistics.

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

ssh  ssh user@hostname


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

scp  scp file.txt


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

wget Download files  wget http://example.com/file.txt


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

curl  curl http://example.com


Transfer data to or
retrieves the content of a webpage from the
from a server.
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
Output of cmd2 is used as the input file for cmd1.
<(cmd2)

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

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 environment


export -p
variables.

env VAR1=value Sets the value of an environment variable for a


COMMAND specific command.

printenv Displays the values of all environment 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, including


finger username 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. The
username
-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.

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 user
GROUPNAME is added to the group without removing them from
USERNAME 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:

Navigati Description Editin Description Histor Description


on g y

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

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

Move back Cut/delete the Go to the previous


Ctrl + Ctrl +
Ctrl + B one word before command in
W P
character. the cursor. history.

Move Go to the next


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

Move back Ctrl + Clear the Ctrl + Terminate the


Alt + B
one word L screen. C current command.

Move
Alt + F forward one
word.

10.2: Nano Shortcuts Commands:


File Descripti Navigat Descript Editi Description Search Descri
Operati on ion ion ng and ption
ons Replac
e

Search
Cut/delete from
Scroll up for a
Ctrl + Save Ctrl + Ctrl the cursor Ctrl +
one string
O the file. Y +K position to the W
page. in the
end of the line.
text.

Search
Exit
and
Nano Scroll
replace
Ctrl + (prompt Ctrl + down Ctrl Uncut/restore the Alt +
a
X to save if V one +U last cut text. W
string
modified page.
in the
).
text.

Read a
Go to a Repeat
file into Mark a block of
Ctrl + specific Ctrl the
the Alt + \ text for copying Alt + R
R line +6 last
current or cutting.
number. search.
buffer.

Go to
Justify
the
the Cut/delete the
Ctrl + beginnin Ctrl
current Alt + , marked block of
J g of the +K
paragrap text.
current
h.
line.

Go to
the end
Alt + Copy the marked
Alt + . of the
6 block of text.
current
line.

10.3: VI Shortcuts Commands:


Comma Description
nd

Change the current word. Deletes from the cursor position to the end
cw
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 cursor


R
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.

s Substitute the character under the cursor and switch to insert 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.

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 entered
r
from the keyboard.

i Switch to insert mode before the cursor.

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

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

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

~ 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:

Norma Description Comman Descriptio Visua Description


l d Mode n l
Mode Mode

Enter insert
mode at the Save the Enter visual mode
i :w v
current cursor file. to select text.
position.

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

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

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

Paste the Replace all


copied or occurrence
:s/old/
p deleted text s of “old”
new/g
below the with “new”
current line. in the file.
Norma Description Comman Descriptio Visua Description
l d Mode n l
Mode Mode

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

Ctrl Redo the last


+R undo.

You might also like