0% found this document useful (0 votes)
21 views13 pages

Basic Linux Commands

This document provides a comprehensive overview of basic Linux commands, including file management, user management, networking, process management, and system information commands. It details the syntax and usage of various commands, such as 'ls', 'cd', 'mkdir', 'chmod', and 'scp', along with their options and examples. Additionally, it covers file permission commands and VI editing commands, making it a valuable resource for Linux users.

Uploaded by

sloki222006
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views13 pages

Basic Linux Commands

This document provides a comprehensive overview of basic Linux commands, including file management, user management, networking, process management, and system information commands. It details the syntax and usage of various commands, such as 'ls', 'cd', 'mkdir', 'chmod', and 'scp', along with their options and examples. Additionally, it covers file permission commands and VI editing commands, making it a valuable resource for Linux users.

Uploaded by

sloki222006
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Basic Linux commands

Command Description
ls Lists all files and directories in the present working directory
ls -R Lists files in sub-directories as well
ls -a Lists hidden files as well
ls -al Lists files and directories with detailed information like permissions,size, owner, etc.
cd or cd ~ Navigate to HOME directory
cd .. Move one level up
cd To change to a particular directory
cd / Move to the root directory
cat > filename Creates a new file
cat filename Displays the file content
cat file1 file2 > file3 Joins two files (file1, file2) and stores the output in a new file (file3)
mv file "new file path" Moves the files to the new location
mv filename new_file_name Renames the file to a new filename
sudo Allows regular users to run programs with the security privileges of the superuser or root
rm filename Deletes a file
man Gives help information on a command
history Gives a list of all past commands typed in the current terminal session
clear Clears the terminal
mkdir directoryname Creates a new directory in the present working directory or a at the specified path
rmdir Deletes a directory
mv Renames a directory
pr -x Divides the file into x columns
pr -h Assigns a header to the file
pr -n Denotes the file with Line Numbers
lp -nc , lpr c Prints “c” copies of the File
lp-d lp-P Specifies name of the printer
apt-get Command used to install and update packages
mail -s 'subject'
-c 'cc-address'
Command to send email
-b 'bcc-address'
'to-address'
mail -s "Subject"
Command to send email with attachment
to-address < Filename

File Permission commands


Command Description
ls -l to show file type and access permission
r read permission
w write permission
x execute permission
-= no permission
Chown user For changing the ownership of a file/directory
Command Description
Chown user:group
change the user as well as group for a file or directory
filename

User management commands of linux


Command Description
sudo adduser username To add a new user
sudo passwd -l 'username' To change the password of a user
sudo userdel -r 'username' To remove a newly created user
sudo usermod -a -G
To add a user to a group
GROUPNAME USERNAME
sudo deluser USER
To remove a user from a group
GROUPNAME
finger Shows information of all the users logged in
finger username Gives information of a particular user

Networking command
Command Description
SSH username@ip-address or
login into a remote Linux machine using SSH
hostname
Ping hostname="" or ="" To ping and Analyzing network and host connections
dir Display files in the current directory of a remote computer
cd "dirname" change directory to “dirname” on a remote computer
put file upload ‘file’ from local to remote computer
get file Download ‘file’ from remote to local computer
quit Logout

Process command
Command Description
bg To send a process to the background
fg To run a stopped process in the foreground
top Details on all Active Processes
ps Give the status of processes running for a user
ps PID Gives the status of a particular process
pidof Gives the Process ID (PID) of a process
kill PID Kills a process
nice Starts a process with a given priority
renice Changes priority of an already running process
df Gives free hard disk space on your system
free Gives free RAM on your system

VI Editing Commands
Command Description
i Insert at cursor (goes into insert mode)
a Write after cursor (goes into insert mode)
A Write at the end of line (goes into insert mode)
ESC Terminate insert mode
u Undo last change
U Undo all changes to the entire line
o Open a new line (goes into insert mode)
dd Delete line
3dd Delete 3 lines
D Delete contents of line after the cursor
C Delete contents of a line after the cursor and insert new text. Press ESC key to end insertion.
dw Delete word
4dw Delete 4 words
cw Change word
x Delete character at the cursor
r Replace character
R Overwrite characters from cursor onward
s Substitute one character under cursor continue to insert
S Substitute entire line and begin to insert at the beginning of the line
~ Change case of individual character

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

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

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

• pwd
Print current
displays the current working
working directory.
pwd directory.
Command Description Options Examples

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

• rm [Link]
deletes the file named
• -r: Remove
“[Link]”.
directories
• rm -r my_directory
recursively.
Remove files and deletes the directory
• -f: Force
directories. “my_directory” and its
removal
contents.
without
• rm -f [Link]
confirmation.
forcefully deletes the file
rm “[Link]” without confirmation.

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

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

Create an empty • touch [Link]


file or update file creates an empty file named
touch timestamps. “[Link]”.

• cat [Link]
View the contents
displays the contents of the file
of a file.
cat “[Link]”.

• head [Link]
• -n: Specify shows the first 10 lines of the
Display the first the number file “[Link]”.
few lines of a file. of lines to • head -n 5 [Link]
display. displays the first 5 lines of the
head file “[Link]”.
Command Description Options Examples

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

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

• -name: • find /path/to/search -name


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

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

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

• chown user [Link]


Change file changes the owner of
ownership. “[Link]” to the specified
chown user.

Change group • chgrp group [Link]


chgrp ownership. changes the group
Command Description Options Examples

ownership of “[Link]” to
the specified group.

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

File Compression and Archiving Commands


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

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

• gzip [Link]
Compress • -d: Decompress compresses the file
files. files. “[Link]” and renames it as
gzip “[Link]”.

• zip [Link] [Link]


Create • -r: Recursively [Link]
compressed include creates a zip archive named
zip archives. directories. “[Link]” containing
zip “[Link]” and “[Link]”.
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 aux
shows all running
Display running • -aux: Show all
processes with
processes. processes.
detailed information.
ps

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

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

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

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

• -i: Ignore case • grep -i “hello”


distinctions [Link]
while searching. • grep -v “error”
• -v: Invert the [Link]
match, • grep -r “pattern”
used to search for
displaying non- directory/
specific patterns or
matching lines. • grep -l “keyword”
regular expressions
• -r or -R: [Link]
in text files or
streams and display
Recursively • grep -n “pattern”
search [Link]
matching lines.
directories for In these examples
matching we are extracting
patterns. our desirec output
• -l: Print only the from filename
grep names of files ([Link])
Commands Description Options Examples

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 the
matching line.
• -C: Display lines
both before and
after the
matching line.

System Information Commands


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

Command Description Options Examples

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

• whoami
Display current
shows the current
username.
whoami username.

Show disk space • -h: Human- • df -h


df usage. readable sizes. displays disk space
Command Description Options Examples

usage in a human-
readable format.

• -h: Human- • du -sh directory/


Estimate file and readable sizes. provides the total size
directory sizes. • -s: Display total of the specified
du size only. directory.

• free -h
Display memory • -h: Human- displays memory usage
usage information. readable sizes. in a human-readable
free format.

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

• lscpu
Display CPU provides detailed CPU
information. information.
lscpu

• lspci
List PCI devices.
lspci List PCI devices.

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

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

Display network • ifconfig


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

• ping [Link]
Send ICMP echo requests
sends ICMP echo requests to “[Link]” to
to a host.
ping check connectivity.
Command Description Examples

Display network
• netstat -tuln
connections and
shows all listening TCP and UDP connections.
netstat statistics.

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

• ssh user@hostname
Securely connect to a
initiates an SSH connection to the specified
remote server.
ssh hostname.

• scp [Link]
Securely copy files user@hostname:/path/to/destination
between hosts. securely copies “[Link]” to the specified remote
scp host.

Download files from the • wget [Link]


wget web. downloads “[Link]” from the specified URL.

• curl [Link]
Transfer data to or from
retrieves the content of a webpage from the
a server.
curl specified URL.

VI Shortcuts Commands:
Command Description

Change the current word. Deletes from the cursor position to the end of the current word
cw
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 position until you press
R
the Escape key.

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

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.

r Replace the character under the cursor with a new character entered 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.

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

~ Switch the case of the character under the cursor.

a Switch to insert mode after the cursor.

C Delete from the cursor position to the end of the line and switch to insert mode.
Vim Shortcuts Commands:
Normal Command Visual
Mode Description Mode Description Mode Description

Enter insert mode at Enter visual


i the current cursor :w Save the file. v mode to select
position. text.

Delete the character Copy the


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

Delete the current Quit Vim without Delete the


dd :q! d
line. saving changes. selected text.

:wq Paste the


Copy the current
yy or Save and quit Vim. p copied or
line.
deleted text.

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

Undo the last :set nu Display line


u or
change. numbers.
:set number

Ctrl +
Redo the last undo.
R
Nano Shortcuts Commands:
Search
File and
Operation Descriptio Navigatio Descriptio Editin Replac Descriptio
s n n n g Description e n

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

Exit Nano Search and


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

Read a file Go to a Mark a block


Repeat the
into the specific Ctrl + of text for
Ctrl + R Alt + \ Alt + R last
current line 6 copying or
search.
buffer. number. cutting.

Go to the
Justify the beginning Cut/delete
Ctrl +
Ctrl + J current Alt + , of the the marked
K
paragraph. current block of text.
line.

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

You might also like