FILES AND FILE PROCESSING
TASKS
SS
ss (Socket Statistics) is a command-line utility to display network sockets on Unixlike operating systems. It can show more detailed information compared to netstat
●
●
●
Display All Sockets:
ss
●
Display All UDP Sockets:
●
ss -u
●
Display Listening Sockets:
●
Display All TCP Sockets:
●
ss -l
●
ss -t
●
●
Display All Unix Domain
Sockets:
ss -x
●
Display Summary Statistics:
●
Display Numeric Output:
●
ss -s
●
ss -n
●
●
Display Listening UDP
Sockets Sorted by Port:
ss -lu
●
●
Display Established TCP
Connections:
ss -t state established
●
●
Display Process Information
for Sockets:
ss -p
●
●
●
●
Display Listening TCP
Sockets Sorted by Port:
ss -lt
Display Established TCP and
UDP Connections:
ss state established
Basic Commands - MKDIR
●
●
SAR (System Activity Reporter) is a utility for monitoring system performance. It provides
a command-line interface to collect, report, and save system activity data such as CPU
usage, memory usage, disk I/O, and more.
Create new directory:
●
mkdir directory_name
●
Removing a directory:
●
●
●
●
rmdir directory_name
●
Create nested directories:
mkdir -p
/home/user/documents
If the directory is not empty, you'll encounter an
error. In that case, you can use the rm command
with the -r option to recursively remove the
directory and all its contents:
rm -r directory_name
SFTP
●
Connect to a server:
●
sftp username@hostname
●
Navigate directories:
●
List files:
●
cd remote_directory
●
ls
●
lcd local_directory
●
lls
●
Connect to a server:
●
Connect to a server:
●
Connect to a server:
●
sftp username@hostname
●
sftp username@hostname
●
sftp username@hostname
●
●
●
●
Upload files:
●
Download files
●
Create directories:
●
put file.txt
●
get file.txt
●
mkdir new_directory
SFTP
●
Connect to a
server:
sftp
username@hostna
●
Delete
me
files/directories:
●
rm file.txt
●●
Upload files:
rmdir
● directory_name
put file.txt
●
●
Navigate
directories:
cd
remote_directory
●
Rename files:
●
lcd local_directory
●
rename old_name
new_name
●
Download files
●
●
●
List files:
●
ls
●
lls
●
Exit SFTP
session:
exit
Create
directories:
●
●
get file.txt
●
mkdir
new_directory
SCP
●
Copy a file from
local to remote:
scp
/path/to/local/file
●
Copy a directory
username@hostna
from remote to
me:/path/to/remote
local:
/directory
●
● scp -r
Verbose mode
username@hostna
(display detailed
me:/path/to/remote
information
/directory
during the copy
/path/to/local/direct
process):
ory
●
●
Copy a file from
remote to local:
●
Copy a directory
from local to
remote:
scp
username@hostna ● scp -r
●
●
Preserve
Specify a
me:/path/to/remote
/path/to/local/direct
modification
different SSH
/file
ory
times, access
port (e.g 2222):
/path/to/local/direct
username@hostna
times and modes ● me:/path/to/remote
ory
●
●
scp mode
-P 2222
Limit
Queit
frombandwidth
the original
/directory
/path/to/local/file
used
in
kilobits
(disable
progress
file or directory
●
username@hostna
per
second:
meter
and nonwhen copying:
me:/path/to/remote
error
messages):
●
scp
-l
1000
●
scp -p
/directory
●
/path/to/local/file
scp -q
/path/to/local/file
●
SCP (2)
●
●
Use compression
during the file
transfer:
●
scp -C
/path/to/local/file
username@hostna
me:/path/to/remote
/directory
●
Preserve
modification
times, access
times and modes
from the original
file or directory
when copying
recursively:
scp -rp
/path/to/local/direct
SED
●
●
Substitute the
Global
Delete lines
first occurrence
substitution
matching a
of the word
(substitute all
pattern:
●
● occurences of
apples
withbefore
Insert text
Append text after ●● Print
specific line
sed '/banana/d'
oranges:
apples
with 3):
a line (before line
a line (line
(lines
2 to 5):
fruits.txt
oranges):
●
3):
●
●
sed
sed
'3a\This
is
an
sed -n '2,5p'
●
Delete
specific
●
●
●
Substitute
Append text to
●
's/apples/oranges/'
sed
sed
'3i\This istext
a in
appended
line'
fruits.txt
lines (line 2-5):
specific
line
(line
the end of every
fruits.txt
's/apples/oranges/
new line' fruits.txt
fruits.txt
●
2):
line:
sed
'2,5d' fruits.txt
g' fruits.txt
●
●
sed ● Remove leading and trailing whitespace:
sed 's/$/ ●
'2s/apples/oranges
delicious/' fruits.txt
sed 's/^[ \t]*//;s/[ \t]*$//' fruits.txt
/' fruits.txt
●
VI
●
Switch to insert
mode:
Search and
●
Press i to insert
replace:
text before the
● cursor.
Visual mode:
●
●
Press
Press /ato
tostart
insert
searching
forward.
text after the
●
Press? vtotostart
enter
●
cursor.
Press
visual mode.
●
searching
Press o to insert
● backward.
keys
text Use
on aarrow
new line
or navigation
keys
●
below
the current
●
Navigate within
the file:
●
●
Save and exit:
Press
Esc
to
Copy,
cut,
and
Undo
and
redo:
●
Use arrow keys
exit insert mode.
paste:
or h (left), j (down), ●
Type :w to save
●
●
k (up),
l (right) to
Split
windows:
Press u(write).
to undo
●
changes
Execute
shell
● move the cursor.
To copy a line, ● the
last action.
commands:
Type :q to quit
position
the
cursor
●
●
Use
w to
move
Press Ctrl + r to
●
Vi.
To
split
the
on the line
and
forward
one
word.
redo the last
window
●
type yy.
Type
:wq toa
To
execute
●
undone
action.
Use b to move
horizontally,
save command,
and quit Vi.
shell
●
To
cut
a
line,
backward
type
:split. one
type
:! followed
by
●
position
the
cursor
Type
:q!
to
force
word.
●
●
●
LESS
●
Move Up and
Down:
●
Search:
●
/patternLine
:
●
Go
to
Line:
Display
●
j or ↓ : Move
Search
forward for
Numbers:
●
:8 : one
Enter
a
down
line.
the next
●
colon
followed
by
-N : Toggle
line
●
occurrence
of
Control
Text
● ●
k ornumber
↑File
: Move
a View
line
toup
numbers on and
pattern.
Display:
one
line.
Contents
jump
to that line
off.
●
?pattern :
Forward/Backwar
●
(line
8).
Spacebar
:
●
●
Marking and
Toggle
Wrapping:
Search
backward
d:
●
Move
forward
one
Ctrl
+
L:
Jumping:
●
for
the
previous
●
-S : Toggle
page.
F : Scroll
Redraw
the
occurrence
of
wrapping
long
forward,
similar
to
screen.
●
b : Move
pattern.
lines
● tail -f. Press Ctrl +
●
●
●
●
Quit and Exit:
q : Quit less
(exit).
View File
Information:
Ctrl + G :
View File
Display
current file
Contents
name,
line from a
Specificand
Position:
number,
total
● number of lines.
+ : Start viewing
the file from the
end.
●
●
HEAD
●
Default (display
Display the first
the first 10 lines
part of multiple
of a file):
files:
●
head -n
●
●
Display
files with
●
Display
the first ●● Display
the first
[number_of_lines]
head
[filename]
head [file1]
a
specific
part of a file and
part
[filename]
[file2]of...files with
delimiter:
continue to
headers:
●
Display
all but
●
●
output
Display
bytes
or
Display
the first ●
●
head -d ';' file.csv
head
-n
[number]
the
last n lines as
of
continuously
kilobytes instead
part
of [file2]
a file ...
q
[file1]
a new
file: lines are
of lines:
excluding the
●
●
added
(like
tailand
- continue
first m to
lines:
head
-n of
-[number]
Display
the
first
part
a file
●
head -c [number]
f):
[filename]
as new lines are
●
added
(like
[filename]output continuously
head
-n +[number]
●
tail -f):
head -f [filename]
[filename]
●
Display the first n
lines of a file
●
●
TAIL
●
●
Display the last n lines of a
file:
tail -n [number] [filename]
●
tail -n [number] -q [file1] [file2] ...
●
●
●
Follow a file and display new lines
as they are appended (like tail -f):
tail -f [filename]
●
●
●
●
tail [filename]
●
Display the last part of files with
headers:
●
Display the last 10 lines of a
file (default behavior):
●
●
Display bytes or kilobytes
instead of lines:
tail -n [number] -d [delimiter] [filename]
tail [file1] [file2] ...
●
tail -c [number] [filename]
Display the last part of a file with a
specified delimiter:
Display the last part of
multiple files:
●
●
●
Display all but the first n lines
of a file:
tail -n +[number] [filename]
Output appended data as the file grows
(similar to tail -f) but exit after N
iterations:
tail -f -n [number] [filename]
SSH
●
Connect to a remote host:
●
ssh username@hostname
●
●
Securely copy files between two remote
hosts:
scp username1@hostname1:/path/to/file
username2@hostname2:/path/to/directory
Terminate an SSH session:
After connecting to a remote host via SSH,
you can simply type exit to terminate the
SSH session and return to your local shell.
Connect to a remote host on a specific
port:
●
●
●
●
●
ssh -p port_number
username@hostname
●
ssh username@hostname command
●
●
●
Execute a command on a remote
host:
●
●
●
●
scp /path/to/local/file
username@hostname:/path/to/remote/di
rectory
ssh -L local_port:remote_host:remote_port
username@hostname
ssh -i path/to/private_key
username@hostname
●
Copy files from local to remote:
Forward a local port to a remote host and
port:
Specify a private key for authentication:
●
●
Copy files from remote to local:
scp
username@hostname:/path/to/remote/fil
e /path/to/local/directory
Forward a remote port to a local host
and port:
ssh -R remote_port:local_host:local_port
username@hostname
SPLIT
●
●
Split a file into smaller files with a
specified number of lines:
split -l [number_of_lines] [filename]
[prefix]
●
●
●
●
●
split -a [length] [filename] [prefix]
split -d [filename] [prefix]
split -b [size] [filename] [prefix]
●
Specify the suffix length for the
output files:
Use a numeric suffix instead of
alphabetic:
Split a file into smaller files with
a specified number of bytes:
●
●
●
●
Split a file into chunks of a specific size
with a specified suffix length:
split -a [length] -b [size] [filename]
[prefix]
Split a file based on a specific
delimiter:
csplit [filename] /delimiter/ {*}
●
●
Split a file into a specified number
of smaller files:
split -n [number] [filename] [prefix]
●
●
Don't split a file but output only the
number of lines or bytes:
split --verbose -l [number_of_lines]
[filename]
GREP
●
Search for a pattern in a file:
●
grep 'apple' fruits.txt
●
●
●
●
Case-insensitive search:
Search for a pattern in multiple
files:
grep 'apple' fruits.txt vegetables.txt
●
grep -i 'apple' fruits.txt
●
●
●
●
●
Search for lines matching any one
of multiple patterns:
grep -e 'pattern1' -e 'pattern2' filename
Count the number of lines
matching a pattern:
grep -c 'pattern' filename
●
●
Search for lines starting with
a specific pattern:
grep '^pattern' filename
Search for a pattern recursively
in directories:
grep -r 'apple' /path/to/directory
●
grep -n 'apple' fruits.txt
grep -E 'pattern' filename
●
●
Search for lines matching a pattern
and display line numbers:
Search for lines matching an
extended regular expression pattern:
●
●
●
●
●
●
●
Search for lines not
matching a pattern:
grep -v 'apple' fruits.txt
Display only the matched pattern
(excluding the entire line):
grep -o 'pattern' filename
Search for lines ending with
a specific pattern:
grep 'pattern$' filename
USER AND PERMISSIONS TASKS
Create user
●
sudo useradd
username
●
●
To create a user with additional options like specifying
the home directory or the user's primary group, you can
use flags with the useradd command. For example:
sudo useradd -m -d /home/username -s /bin/bash -g
groupname username
●
In details::
●
-m: Creates the user's home directory if it doesn't exist.
●
-d /home/username: Sets the home directory to /home/username.
●
-s /bin/bash: Sets the user's login shell to /bin/bash.
●
-g groupname: Sets the user's primary group to groupname.
Remove user
●
sudo userdel
username
●
●
To remove the user's home directory
along with their account, you can use
the -r flag:
sudo userdel -r username
List user
getent passwd
●
●
●
●
awk -F: '{print $1}' /etc/passwd
This command uses awk to print the first
field (username) from the /etc/passwd
file, using : as the field separator.
●
●
cat /etc/passwd
cut -d: -f1 /etc/passwd
This command extracts the usernames
from the /etc/passwd file using : as the
delimiter.
●
●
getent passwd | cut -d: -f1
This command retrieves entries from the
system user database, passwd, and then
extracts the usernames using cut.
CHMOD -change permissions of
files and directories
●
chmod [options] [who][operator][permissions] file/directory
permissions: Specifies the permissions to be added, removed, or set.
It can be:
●
●
who: Specifies who the permission change applies to. It can be:
●
operator: Specifies how the permissions should be modified. It can be:
●
●
●
u for user/owner.
g for group.
●
o for others.
●
a for all (equivalent to ugo).
●
# Add execute permission for the owner
●
chmod u+x myfile.txt
●
●
●
+ to add permissions.
- to remove permissions.
= to set permissions.
●
r for read.
●
w for write.
●
x for execute.
X for execute only if the file is a directory or already has execute
permission for some user.
●
●
Additional Tips:
●
●
# Remove write permission for the group
●
chmod g-w myfile.txt
●
●
●
●
# Set read and execute permissions for others
●
chmod o=rx myfile.txt
●
●
# Set read, write, and execute permissions for all
●
chmod a=rwx myfile.txt
●
●
●
Recursive Changes: Use the -R option to apply permissions recursively to all files and directories within a
directory.
Symbolic Links: By default, chmod dereferences symbolic links. Use the -h option to change permissions of
symbolic links themselves rather than their target.
Special Permissions: You can set special permissions using symbolic mode or absolute mode. These
include the setuid (s), setgid (s), and sticky (t) bits.
●
●
●
In absolute mode, permissions are
represented numerically:
chmod [options] mode file/directory
●
mode: Represents the permissions numerically using the
following format:
●
4 for read.
●
2 for write.
●
1 for execute.
●
# Set read, write, and execute permissions for the owner, and read-only
permissions for group and others
chmod 744 myfile.txt
●
●
# Set read and write permissions for the owner, read-only for group and others
●
chmod 644 myfile.txt
CHOWN -change ownership of files
and directories
●
chown owner:group file/directory
●
●
owner: Specifies the new owner of the
file/directory.
group: Specifies the new group of the
file/directory.
●
●
# Change the owner of myfile.txt
to user1 and the group to group1
chown user1:group1 myfile.txt
●
3. Change Group Only:
●
2. Change Owner Only:
●
chown :group file/directory
●
chown owner file/directory
●
●
# Change the group of myfile.txt to group2 while keeping
the owner unchanged
chown :group2 myfile.txt
●
●
●
# Change the owner of myfile.txt to user2 while keeping the
group unchanged
chown user2 myfile.txt
Additional Tips:
●
●
Recursive Changes: Use the -R option to apply ownership changes recursively to all files and directories within a directory.
●
Symbolic Links: By default, chown does not follow symbolic links. Use the -h option to change the ownership of symbolic links themselves rather than their target.
●
Numeric Representation: You can use numeric values for both owner and group. For example, chown 1001:1001 myfile.txt will change the owner and group to the user and group
with UID and GID 1001, respectively.
●
●
Always be cautious when changing ownership, especially for system files and directories, as incorrect settings can render your system unusable or compromise its security.
Add sudo to users
Here's how you can add a user to the sudo group:
●
●
●
Run the following command to edit the /etc/sudoers file: sudo visudo
Once file is open, look for the two lines that says ‘# Members of the sudo
group may gain root privileges %sudo ALL=(ALL:ALL) ALL’
To add a user to the sudo group, you need to add a line below this section.
Replace username with the username of the user you want to add (example):
●
%sudo ALL=(ALL:ALL) ALL
●
username ALL=(ALL:ALL) ALL
GROUPADD -create groups and
assign users to groups
●
sudo groupadd groupname
●
The -aG options have the following meanings:
●
●
●
●
●
●
2. Adding existing Users to a Group:
sudo usermod -aG groupname
username
Add new user to a group:
sudo useradd -G
group1,group2 username
●
●
●
●
●
-a: Append the user to the supplementary
group(s) specified by the -G option without
removing them from other groups.
-G: The groups specified by the -G option will
replace the current list of supplementary groups.
Check Group Membership: You can check the groups a user belongs to using the
groups command:
groups username
Remove User from Group: To remove a user from a group, you can use the gpasswd
command:
sudo gpasswd -d username groupname
●
●
●
Check Group Information: You can check information about a specific group using the
getent command:
getent group groupname
Password changing
sudo passwd username
UTILITIES TASKS
NOHUP
●
●
●
●
●
●
●
●
nohup is a Unix command used to run another command or script in such a way
that it continues to run even after you log out or close the terminal session.
Things you can run with nohup command:
- scripts, commands, programs, background
processes, shell built-in commands, pipelines,
commands with arguments and scheduled
commands.
Running Commands: Any commandline command can be executed with
nohup. For example:
nohup python my_script.py &
Running Shell Built-in Commands:
Built-in commands of the shell can also
be run with nohup. For example:
nohup echo "Hello, World!" &
Running Commands Scheduled with at:
Commands scheduled using the at command
can be run with nohup. For example:
echo "nohup ./my_script.sh > output.log &" | at
now + 1 hour
●
●
Basic Syntax:
nohup command [options]
[arguments] &
●
●
Running Programs: Any executable
program can be started with nohup.
For example:
nohup ./my_program &
●
●
Running Pipelines: Commands
connected by pipes can also be run
with nohup. For example:
nohup command1 | command2 &
●
●
Running Scripts: Any shell script or
executable file can be run with
nohup. For example:
nohup ./my_script.sh &
●
●
Running Background Processes: You can use
nohup to send a process into the background
even if it's interactive or foreground by default.
For example:
nohup ./my_long_running_process &
●
●
Running Commands with Arguments:
Any command with arguments can be
executed using nohup. For example:
nohup ./my_program arg1 arg2 &
NOHUP (2)
●
●
●
nohup is a Unix command used to run another command or script in such a way
that it continues to run even after you log out or close the terminal session.
Example 2: Redirecting Output to a
Custom File:
nohup ./my_script.sh > output.log &
●
How to kill a nohup session:
●
Find the process id (PID) using: ps aux | grep 'nohup'
●
Kill the process forcefully using the PID: kill -9 12345
●
●
●
●
Example 3: Redirecting Both Standard
Output and Standard Error to a File:
nohup ./my_script.sh > output.log 2>&1
&
Alternatively, if you know the name of the script or
command you ran with nohup, you can use tools
like pkill to terminate it directly without looking up
the PID. For example:
pkill -f 'nohup my_script.sh'
●
●
Example 5: Checking the
Progress:
tail -f nohup.out
SCREEN
●
●
●
●
●
Screen is a terminal multiplexer that allows you to run
multiple terminal sessions within a single window.
Starting a New Screen
Session:
screen
Detaching from a Screen
Session:
Press Ctrl + A followed by d.
●
Switching between Windows:
●
Press Ctrl + A followed by n to switch to the next window.
●
Press Ctrl + A followed by p to switch to the previous window.
●
●
●
Listing Available Screen
Sessions:
screen -ls
●
●
●
screen -S [session_name]
Scrolling in Screen:
●
Press Ctrl + A followed by [ to enter scrollback
mode. Then you can use the arrow keys or Page
Up/Down keys to scroll through the output. Press
Esc to exit scrollback mode.
●
●
Attaching to a Screen Session:
screen -r [session_name or
session_ID]
●
●
Creating a New Window
within a Screen Session:
Press Ctrl + A followed by c.
●
Splitting the Screen Vertically:
●
Splitting the Screen Horizontally:
●
Press Ctrl + A followed by |.
●
Press Ctrl + A followed by S.
●
Press Ctrl + A followed by the window number to switch
directly to a specific window.
●
Creating a Named Screen
Session:
●
This key combination splits the current
window vertically.
Terminating a Screen
Session:
exit
●
●
●
This key combination splits the current
window horizontally.
Terminating a Screen
Session:
screen -X -S [session_ID] quit
TELNET
●
●
●
Telnet is a network protocol used to establish a connection to a remote server or device.
Once connected, you can interact with the server through a command-line interface.
Connecting to a Remote Host:
●
Exiting Telnet:
telnet [hostname or IP address]
●
quit
[port]
●
●
Displaying Telnet Help:
?
●
Toggling Character Echo:
●
toggle echo
Closing the Connection:
●
Ctrl + ] (press Ctrl and the right bracket simultaneously)
●
●
Changing Telnet Escape Character:
●
Ctrl + ] (press Ctrl and the right bracket simultaneously)
●
●
This key combination puts Telnet in command mode.
Then you can type quit to close the connection and exit
Telnet.
If you want to change the Telnet escape character from the
default (Ctrl + ]), you can use this key combination to enter
command mode, then type escape followed by the desired
character.
●
●
Displaying Current Telnet
Settings:
show term
●
Changing Terminal Type:
●
set term vt100
TCPDUMP
●
●
●
●
●
●
●
●
●
tcpdump is a powerful command-line packet analyzer that
allows you to capture and analyze network traffic.
Capture Traffic on a Specific
Interface:
tcpdump -i eth0
Capture Traffic for a
Specific Protocol:
tcpdump icmp
Capture Traffic and Display
in ASCII Format:
tcpdump -A
Reading from a Saved
Capture File:
tcpdump -r [filename]
●
●
Capture Traffic for a Specific
Host:
tcpdump host 192.168.1.100
●
●
●
Capture Traffic for a Specific Source or
Destination:
●
tcpdump src 192.168.1.100
●
tcpdump dst 192.168.1.100
●
●
Capture Traffic and Display
Timestamps:
tcpdump -tttt
Capture Traffic for a Specific
Port:
tcpdump port 80
●
●
Capture Traffic for a Specific
Network:
tcpdump net 192.168.1.0/24
●
●
Capture Traffic and Save to a
File:
tcpdump -w capture.pcap
NETSTAT
netstat is a command-line tool that displays network connections, routing tables,
interface statistics, masquerade connections, and multicast memberships.
●
Display All Active Network
Connections:
●
netstat -a
●
●
Display Routing Table:
●
netstat -r
●
Display Numeric Output:
●
netstat -n
●
Display UDP Connections:
●
netstat -au
●
●
Display Listening Server
Sockets:
netstat -l
●
●
Display PID and Program Name
for Network Connections:
netstat -p
●
●
●
●
●
Display Network Statistics:
●
netstat -s
●
●
Display All Listening and
Non-listening Sockets:
netstat -a -l
Display Unix Domain
Sockets:
netstat-lx
●
●
Display Kernel Interface
Table:
netstat -i
●
Display TCP Connections:
●
netstat -at
Display Listening TCP
Sockets Sorted by Port:
netstat -lt
NETSTAT (2)
●
●
●
netstat is a command-line tool that displays network connections, routing tables,
interface statistics, masquerade connections, and multicast memberships.
Display Listening UDP
Sockets Sorted by Port:
netstat -lu
●
●
Display Network Statistics for
IPv6:
netstat -s -6
●
●
Display Network Information in
Continuous Refresh Mode:
netstat -c
●
●
Display Network Statistics for
IPv4:
netstat -s -4
SS
ss (Socket Statistics) is a command-line utility to display network sockets on Unixlike operating systems. It can show more detailed information compared to netstat
●
●
●
Display All Sockets:
ss
●
Display All UDP Sockets:
●
ss -u
●
Display Listening Sockets:
●
Display All TCP Sockets:
●
ss -l
●
ss -t
●
●
Display All Unix Domain
Sockets:
ss -x
●
Display Summary Statistics:
●
Display Numeric Output:
●
ss -s
●
ss -n
●
●
Display Listening UDP
Sockets Sorted by Port:
ss -lu
●
●
Display Established TCP
Connections:
ss -t state established
●
●
Display Process
Information for Sockets:
ss -p
●
●
●
●
Display Listening TCP
Sockets Sorted by Port:
ss -lt
Display Established TCP and
UDP Connections:
ss state established
SS (2)
●
ss (Socket Statistics) is a command-line utility to display network sockets on Unixlike operating systems. It can show more detailed information compared to netstat
●
Display IPv4 Sockets: ● Display IPv6 Sockets:
●
ss -4
●
●
●
ss -6
Display Information about
Specific Ports:
●
ss sport = :port_number
●
Display TCP and UDP Sockets
with Process Information:
ss -tup
●
●
Display Listening Sockets
with Process Information:
ss -l -p
LOCATE
locate is a command-line utility used to quickly search for files in a directory hierarchy. It
uses a pre-built database of files and directories, which makes searching for files much
faster compared to using the find command.
●
Update the Locate Database: ● Search for a File by Name:
●
sudo updatedb
●
●
Search for Case-Insensitive
Matches:
●
locate example.txt
●
●
Limit Search Results to Specific
Directory:
locate -r '/path/to/directory/*pattern*'
locate -i [pattern]
●
●
Display Only Exact Matches:
●
locate -b [pattern]
●
●
Display File Information
Along with Matches:
locate -l [pattern]
●
Display Paths Relative to Root
Directory:
locate -r '^/path/to/search/*pattern*'
●
●
Search for Files Matching a
Pattern:
locate *.log
●
●
Display the Number of
Matches:
locate -c [pattern]
●
●
Display Paths Relative to Current
Directory:
locate -r '^./path/to/search/*pattern*'
PS
The ps command is used to display information about
processes running on a Unix-like operating system.
●
Display All Processes:
●
ps -e
●
ps -u [username]
ps -ejH
●
ps -ejHf
Display Full Format Listing:
●
ps -ef
ps -e -o
pid,user=USERNAME,command
Display Processes in a
Hierarchical Format:
●
ps -axjf
Display Processes in UserOriented Format:
●
●
Display Process Group ID
and Session ID:
ps -eH
Display Processes with Top
CPU Usage:
Display Processes with Real User ID:
●
ps aux
●
Display Process Tree with
Command Arguments:
Display Process Tree:
●
Display Detailed Process
Information:
Display Processes Owned by
a Specific User:
ps -e --sort=-pcpu | head
●
ps -o pid,ppid,pgid,sid,comm
Display Processes with Top
Memory Usage:
●
ps -e --sort=-rss | head
TAR
tar is a command-line utility used for archiving files and directories into a
single file, often called a "tarball", and for extracting files from a tarball.
●
Create a Tarball:
●
tar -cvf archive.tar file1 file2 directory1
This command creates a new tarball named archive.tar containing
file1, file2, and directory1. The options -c specifies creating a new
archive, -v enables verbose output, and -f specifies the filename of
the archive.
●
●
●
Extract Files from a
Compressed Tarball:
tar -xvf archive.tar -C /path/to/directory
●
●
●
●
Create a Tarball (with Compression):
●
tar -czvf archive.tar.gz file1 file2
directory1
Extract Files from a Tarball:
●
tar -xvf archive.tar
●
List Contents of a Tarball:
●
tar -tvf archive.tar
●
The option -t displays the table of contents.
tar -xzvf archive.tar.gz
Extract Files to a Specific
Directory:
●
●
Compress a Directory into a
Tarball Directly:
tar -czvf directory.tar.gz directory1
Extract Specific Files from a
Tarball:
●
tar -xvf archive.tar file1 file2
●
●
Append Files to an Existing Tarball:
●
Update Files in an Existing Tarball:
●
tar -rvf archive.tar newfile
●
tar -uvf archive.tar updatedfile
●
The option -r specifies appending files to an
archive.
●
The option -u specifies updating files in an
archive
GZIP
gzip is a command-line utility used for file compression
and decompression.
●
Compress a File:
●
●
●
Decompress a File:
gzip file.txt
●
gzip -d file.txt.gz
This command compresses file.txt and
replaces it with a compressed file file.txt.gz.
●
This command decompresses file.txt.gz and replaces
it with the original uncompressed file file.txt.
●
Decompress Without Removing the Original File:
Specify Compression Level:
●
●
gzip -dk file.txt.gz
gzip -9 file.txt
●
●
●
●
●
This command decompresses file.txt.gz but keeps
the original compressed file.
●
Keep Original File When
Compressing:
gzip -k file.txt
This command compresses file.txt using the highest
compression level (slowest but best compression ratio).
You can specify compression levels from 1 to 9.
Compress Multiple Files:
gzip file1.txt file2.txt
●
Decompress Multiple Files:
gzip -d file1.txt.gz file2.txt.gz
●
●
Force Compression, Overwrite Existing File:
●
gzip -f file.txt
●
This command forces compression of file.txt and
overwrites the existing compressed file if it already
exists.
●
Display Compression Ratio:
gzip -l file.txt.gz
●
Test Compressed File Integrity:
●
●
gzip -t file.txt.gz
●
●
his command tests the integrity of the compressed file
file.txt.gz. It checks whether the compressed file can be
successfully decompressed without errors.
This command displays information about the
compressed file file.txt.gz, including compression ratio
and original size.
SYSTEMCTL
systemctl is a command-line utility used to manage systemd services and
other units (such as sockets, targets, devices, and timers) on Linux systems.
●
Start a Service:
●
systemctl start servicename ●
Stop a Service:
systemctl stop servicename
●
Reload Configuration of a Service:
●
systemctl reload servicename
Enable a Service to Start on Boot:
●
●
systemctl enable servicename
●
This command reloads the configuration of
the specified service without stopping it.
●
Check Status of a Service:
●
systemctl status servicename
●
●
This command displays the status of the specified
service, including whether it is running, enabled, or
disabled.
●
This command configures the specified
service to start automatically during boot.
●
●
Display Unit Dependencies:
systemctl list-dependencies
servicename
●
Restart a Service:
●
systemctl restart servicename
Disable a Service from
Starting on Boot:
●
systemctl disable servicename
●
●
List All Loaded Units:
●
systemctl list-units
●
This command lists all loaded units (services,
sockets, targets, devices, timers, and more).
SYSTEMCTL (2)
Reload systemd Manager
Configuration:
List All Failed Units:
●
systemctl --failed
●
Show Logs for a Service Since a
Specific Time:
●
journalctl -u servicename --since
"YYYY-MM-DD HH:MM:SS"
systemctl daemon-reload
Show Logs for a Service:
●
journalctl -u servicename
SUDO ACTIONS
sudo is a command-line utility used to execute commands
with elevated privileges on Unix-like operating systems.
Edit a File with Elevated
Privileges:
Execute a Command with
Elevated Privileges:
●
sudo apt update
●
sudo nano /path/to/file
Execute a Command as
Another User:
Edit the sudoers File:
●
sudo visudo
●
sudo -u username [command]
Run a Command in a New
Login Shell:
Change to Another User's Account:
●
●
sudo -i -u username
This command starts a login shell as the specified user,
providing an environment similar to logging in directly
as that user.
Run a Command with a Custom Environment:
●
●
sudo -E [command]
This command preserves the user's environment
variables when executing the specified command with
elevated privileges.
●
sudo -i
Switch to the Root User:
sudo su
●
List Active sudo Sessions:
●
●
sudo -l
This command lists the user's sudo privileges, showing
which commands the user is allowed to execute with
elevated privileges.
View sudo Log Entries:
●
●
sudo journalctl _COMM=sudo
This command displays log entries related to
sudo commands executed on the system.
NCAT
ncat is a powerful networking utility that is part of the Nmap suite. It provides
a wide range of functionalities for interacting with network connections.
Connect to a Remote Host
and Port:
Listen on a Specific Port:
●
●
●
●
●
●
ncat -l [port]
●
●
ncat [hostname/IP] [port] > output.txt
●
This command connects to the specified remote host
and port, redirecting the server's output to output.txt.
ncat [hostname/IP] [port] -e [command]
This command connects to the specified remote host
and port, executing the specified command on the
server.
This command connects to the specified remote host
and port, sending the contents of file.txt to the server.
Create a Chat Server:
●
●
ncat -l -p [port] --chat
This command makes ncat listen on the specified port
and allows for bidirectional communication with clients.
SSL-Encrypted Connection:
●
.This command connects to a remote host through a proxy server
specified by [proxy_host:port] with the specified proxy type (http,
socks4, socks4a, socks5).
●
ncat --ssl [hostname/IP] [port]
This command establishes an SSL-encrypted
connection to the specified remote host and port.
File Transfer using netcat:
UDP Mode:
ncat -u [hostname/IP] [port]
This command establishes a UDP connection
to the specified remote host and port.
ncat [hostname/IP] [port] < file.txt
Execute a Command on a Remote Host:
Redirect Output to a File:
ncat --proxy [proxy_host:port] --proxy-type [type] [hostname/IP]
[port]
●
●
ncat [hostname/IP] [port]
Proxy Connections:
●
Transfer Data from Standard Input to a Server:
●
●
ncat -l [port] < file.txt
Port Scanning:
●
●
ncat -zv [hostname/IP] [start_port]-[end_port]
This command performs a TCP port scan on
the specified range of ports for the given host..
TROUBLESHOOTING TASKS
SAR
SAR (System Activity Reporter) is a utility for monitoring system performance. It provides
a command-line interface to collect, report, and save system activity data such as CPU
usage, memory usage, disk I/O, and more.
Display memory usage
statistics:
Display CPU usage
statistics:
●
sar -u
●
Display network statistics:
●
sar -n DEV
●
sar -o output_file
sar -b
Display process creation
statistics:
●
sar -c
Display swap space
utilization:
●
sar -S
Display file system
statistics:
sar -B
Save SAR data to a file:
●
sar -r
Display paging statistics:
●
Display disk I/O statistics:
●
sar -f /var/log/sa/saDD
Display real-time CPU
usage:
●
sar 1 3
IOSTAT
IOstat is a command-line utility used to monitor system input/output (I/O) device load. It
provides information about CPU utilization, disk I/O activity, and network filesystems
(NFS).
Display disk I/O statistics:
●
iostat
●
Display disk utilization and
throughput statistics:
●
iostat -m
iostat 5
iostat -d
●
iostat -x
Display NFS statistics:
●
iostat -n
Display only disk utilization
statistics:
●
Display extended statistics including
average request size and queue
length:
Display disk I/O statistics for
a specific interval:
Display only CPU statistics:
●
iostat -c
Display only NFS statistics:
●
Display disk I/O statistics for
a specific interval and count:
iostat -n -x
●
iostat -d 5 3
LSOF
lsof stands for "list open files" and is a command-line utility used in Unix and Unixlike systems to list information about files that are opened by various processes
List all open files:
●
List files opened by a
specific user:
lsof
●
List files opened by a
specific process name:
●
●
●
●
●
lsof -c command_name
lsof -u username -c process_name
lsof -p PID
List network connections
and listening ports:
●
lsof -i
List files opened by a
specific file descriptor:
lsof -i :port_number
List files opened by a specific
user and process:
●
List files opened by a
specific command or binary:
lsof -c process_name
List files opened by a
specific TCP or UDP port:
lsof -u username
List files opened by a
specific process ID (PID):
●
lsof -d file_descriptor
List files opened by a
specific path:
●
lsof /path/to/file
TRACEPATH
tracepath is a command-line utility used to trace the path that packets take from the local
host to a destination host. It is similar to the traceroute command but with a simplified
output format.
●
●
●
●
Trace the path to a
destination host:
tracepath example.com
Trace the path using a
specific interface:
●
tracepath -i eth0 example.com
●
tracepath -U example.com
tracepath -s source_ip example.com
●
tracepath -p destination_port
example.com
Trace the path and display
verbose output:
●
tracepath -v example.com
tracepath -6 example.com
Trace the path and display
timestamps:
●
tracepath -T example.com
Trace the path and specify the
destination port number:
Trace the path and specify the
source IP address:
●
●
Trace the path using UDP
instead of ICMP:
Trace the path using IPv4 and specify
the maximum number of hops:
tracepath -n -m 20 example.com
Trace the path using IPv6:
Trace the path and set the initial
TTL (Time to Live):
●
tracepath -t initial_ttl example.com
Trace the path of one of Google’s
DNS servers IP address:
●
tracepath 8.8.8.8
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )