Skip to content

Latest commit

 

History

History
254 lines (176 loc) · 3.26 KB

File metadata and controls

254 lines (176 loc) · 3.26 KB

Advanced Linux Commands

Search for a pattern in a file (case-sensitive)

grep "pattern" filename.txt

Search for a pattern (case-insensitive)

grep -i "pattern" filename.txt

Recursively search for a string in all files in a directory

grep -r "pattern" /path/to/directory

Show line numbers with matched text

grep -n "pattern" filename.txt

Replace text in a file (does not modify original file)

sed 's/old/new/' filename.txt

Replace all occurrences in a file

sed 's/old/new/g' filename.txt

Replace and save changes to the same file (in-place)

sed -i 's/old/new/g' filename.txt

Print specific fields from a file or command output

awk '{print $1}' filename.txt

Print lines that match a pattern

awk '/pattern/ {print}' filename.txt

Securely copy a file to a remote server

scp file.txt user@remote:/path/to/destination/

Copy a directory to a remote server (with -r flag)

scp -r folder/ user@remote:/path/

Create a hard link to a file

ln original.txt hardlink.txt

Create a symbolic (soft) link

ln -s original.txt symlink.txt

ZIP Command (Compress Files)

Install zip (if not installed)

sudo yum install zip     # RHEL/CentOS
sudo apt install zip     # Ubuntu/Debian

Create a Zip File

zip archive.zip file.txt

Zip Multiple Files

zip archive.zip file1.txt file2.txt

Zip a Directory

zip -r archive.zip myfolder

-r = recursive (include all files inside directory)

Extract Zip File

unzip archive.zip

Extract to specific folder:

unzip archive.zip -d /path/to/directory

View Zip Content

unzip -l archive.zip

NOHUP Command (Run Process in Background)

What is nohup?

  • nohup = No Hang Up
  • Runs a command even after logout
  • Useful for long-running jobs

Basic Syntax

nohup command &

Example:

nohup python script.py &

Output File

By default, output goes to:

nohup.out

Redirect Output to Custom File

nohup python script.py > output.log 2>&1 &

Explanation:

  • > → redirect output
  • 2>&1 → redirect errors
  • & → run in background

Check Running Background Jobs

ps -ef | grep script.py

Stop Background Process

Find PID:

ps -ef | grep script.py

Kill it:

kill PID

Force kill:

kill -9 PID

FIND Command (Search Files & Directories)

Basic Syntax

find /path -option value

Find File by Name

find /home -name file.txt

Case insensitive:

find /home -iname file.txt

Find by File Type

Type Command
File -type f
Directory -type d

Example:

find /home -type f -name "*.txt"

Find by Size

find /home -size +10M
Symbol Meaning
+ Greater than
- Less than

Examples:

find / -size +100M
find / -size -1G

Find by Permission

find /home -perm 755

Find by Owner

find /home -user john