-
Move files or directories from one place to another
-
Rename files or directories
mv [options] source target
| Command | Description |
|---|---|
mv file1 file2 |
Renames file1 to file2. Overwrites file2 if it exists. |
mv file folder/ |
Moves file into folder/ if folder exists |
mv folder1 folder2 |
If folder2 doesn’t exist → renames folder1 to folder2 |
If folder2/ exists → moves folder1 inside folder2/ |
|
mv *.txt folder/ |
Moves all .txt files into folder/ |
mv file1 file2 folder/ |
Moves both file1 and file2 into folder/ |
mv dir1/* dir2/ |
Moves all contents of dir1/ into dir2/ |
mv *.sh ../ |
✅ Moves all .sh files to parent directory. |
mv folder1/* . |
Moves all contents of folder1/ to current directory |
Mv folder1/* “$(pwd)” |
Also moves all contents of folder1/ to current directory |
- If
file2exists → Overwrites it silently
Use -i to confirm before overwrite:
mv -i file1 file2
-
If
folder2exists →folder1is moved intofolder2/ -
If
folder2does not exist →folder1is renamed tofolder2
| Option | Description |
|---|---|
-i |
Prompt before overwrite (interactive) |
-n |
Never overwrite existing files |
-v |
Verbosely show files as they are moved/renamed |
-u |
Move only if source is newer than destination |
-
mv file file→ No effect (source and target are the same) -
You can't move a folder into a file
-
You can move a file into a folder
-
Overwriting is silent by default, so use
-ifor safety
cd stands for "change directory", and it's used to navigate between directories in the terminal.
cd [directory_path]
| Command | Meaning |
|---|---|
cd folder1 |
Go into folder1 (a subdirectory of current directory) |
cd .. |
Move one level up (to parent directory) |
cd ../.. |
Move two levels up |
cd /home/user |
Go to an absolute path |
cd ~ or just cd |
Go to your home directory |
cd - |
Go to your previous working directory |
-
Use
pwdto confirm where you are. -
Use
lsto list what’s in the current directory.
-
Full form:
chmodstands for "change mode" -
Purpose: Modifies file/directory permissions in Linux/Unix systems
-
Permissions apply to:
-
u→ user (owner) -
g→ group -
o→ others -
a→ all (u,g,o)
-
| Command | Meaning |
|---|---|
chmod +x file |
Adds execute permission to user, group, and others (same as a+x) |
chmod =x file |
Sets permission to only execute for all (ugo), removes read/write |
chmod u+x file |
Adds execute permission to user (owner) only |
chmod g+r file |
Adds read permission to group only |
Each permission is represented by a 3-digit number:
-
r = 4,w = 2,x = 1 -
Format:
USER GROUP OTHERS
| Command | Meaning |
|---|---|
chmod 100 file |
Gives execute to user, no permissions for group/others (--x------) |
chmod +100 file |
Adds execute permission to user (owner) only |
-
chmod +x→ shorthand forchmod a+x -
chmod =x→ removes all permissions except execute -
Numeric mode is strict:
chmod 755 file→rwxr-xr-x
-
Full form:
lsstands for "list directory contents" -
Purpose: Displays files/directories with details, sorted by modification time (oldest first)
| Flag | Meaning |
|---|---|
-l |
Long listing format — shows file permissions, owner, size, time, name |
-t |
Sort by modification time (most recent first by default) |
-r |
Reverse the order — shows oldest modified first |
-rw-r--r-- 1 user group 1234 Aug 2 12:34 example.txt
| Field | Description |
|---|---|
-rw-r--r-- |
File permissions |
1 |
Number of hard links |
user |
File owner |
group |
Group owner |
1234 |
File size (in bytes) |
Aug 2 12:34 |
Last modification date and time |
example.txt |
File name |
ls -ltr lists files with detailed info, sorted by modification time (oldest first).
-
Lists only directories, not files
-
*/is a glob pattern matching all names ending in/(i.e., directories)
ls -d */
| Part | Meaning |
|---|---|
ls |
List files and directories |
-d |
List the name of the directory, not its contents |
*/ |
Match all directories in current path |
If you have:
file1.txt dirA/ dirB/
Then:
ls -d */
Returns:
dirA/ dirB/
-
You want a clean list of just directories
-
Without
-d,ls */would show the contents inside each directory
-
Shows a brief, one-line description of a command
-
It’s like a quick version of
man
whatis [command_name]
whatis ls
Output:
ls (1) - list directory contents
| Field | Description |
|---|---|
ls |
Command name |
(1) |
Section of the manual (man 1) |
| Description | Short explanation of the command |
-
Quickly check what a command does
-
Confirm if a command exists
-
See the relevant man section
- If it shows nothing, update the manual database:
sudo mandb
| Command | Purpose |
|---|---|
man ls |
Full manual page for ls |
whatis ls |
One-line summary |
man -f ls |
Same as whatis ls |
man -k list |
Search all man pages containing "list" |
-
Displays the manual (help/documentation) for a given Linux command
-
Great for understanding usage, flags, and options of a command
man [command_name]
man ls
→ Shows the manual page for the ls command
Linux manuals are divided into sections:
| Section | Contents |
|---|---|
| 1 | User commands (e.g., ls, cd) |
| 2 | System calls |
| 3 | Library functions |
| 4 | Device files |
| 5 | File formats |
| 6 | Games and screensavers |
| 7 | Miscellaneous |
| 8 | System administration commands |
man 5 crontab
→ Opens section 5 (file format) of crontab
| Key | Action |
|---|---|
Up/Down |
Scroll line by line |
Space |
Next page |
q |
Quit manual |
/text |
Search for "text" in the man page |
n |
Go to next search match |
If unsure what a command does, try:
man <command>
To find where a command is documented (all sections):
man -f printf # same as `whatis printf`
man -k network # search all man pages with "network" keyword
-
Creates empty files if they don’t exist
-
Updates the access and modification time of existing files
touch filename
-
Creates the file
filenameif it doesn’t exist -
If it exists, updates its modified and access time to the current time
touch test.txt
-
Creates
test.txtif missing -
Updates timestamp if it already exists
touch (1) - change file timestamps
| Part | Explanation |
|---|---|
touch (1) |
Section 1 of the manual — user command |
| Description | Short explanation: modifies file times |
🔸 Unlike mkdir, there is no touch (2) because touch is not a system call — it uses underlying system calls like utime() or open() internally.
View the full man page:
man touch
| Command | Description |
|---|---|
touch file.txt |
Create or update file.txt |
touch file1 file2 |
Create/update multiple files |
touch -t 202508021530 file.txt |
Set custom timestamp (YYYYMMDDhhmm) |
-
vimstands for Vi IMproved -
An enhanced version of the classic
vitext editor -
Used to create and edit text/code files in the terminal
-
Popular among programmers for its speed, power, and customizability
sudo apt update
sudo apt install vim
vim filename
-
Opens
filenamefor editing -
Creates the file if it doesn’t exist
vim (1) – Vi IMproved, a programmers text editor
| Mode | Purpose | Enter Mode With |
|---|---|---|
| Normal | Navigate & run commands | Esc |
| Insert | Insert text | i, a, o |
| Visual | Select and manipulate text | v, V, Ctrl + v |
| Command-line | Save, quit, search, etc. | : (in normal mode) |
| Task | Command |
|---|---|
| Enter insert mode | i |
| Save file | :w |
| Quit Vim | :q |
| Save and quit | :wq or ZZ |
| Quit without saving | :q! |
| Delete current line | dd |
| Undo | u |
| Redo | Ctrl + r |
| Search for text | /text |
| Move to next match | n |
| Move to previous match | N |
| Copy (yank) line | yy |
| Paste | p |
| Cut (delete) word Enable syntax highlighting (if disabled) | dw :syntax on |
- User config:
~/.vimrc
Can be used to set things like:
set number " Show line numbers
syntax on " Enable syntax highlighting
set tabstop=4 " Set tab width to 4
| Feature | vi |
vim (Vi IMproved) |
|---|---|---|
| Syntax Highlighting | ❌ No | ✅ Yes |
| Undo levels | Single | Multiple |
| Plugin support | ❌ No | ✅ Yes |
| Help system | Minimal | Full :help command |
| User customization | Limited | Rich .vimrc config file |
-
nanois a beginner-friendly, easy-to-use text editor for the terminal. -
It works in modeless style: you can type, edit, and save without switching modes (unlike Vim).
-
Great for quick edits, config files, or for those new to Linux.
nano (1) – simple text editor
nano filename
-
Opens
filenameinnano -
Creates the file if it doesn’t exist
| Task | Shortcut |
|---|---|
| Save (write) file | Ctrl + O then Enter |
| Exit | Ctrl + X |
| Cut line | Ctrl + K |
| Paste line | Ctrl + U |
| Copy line | Alt + 6 |
| Undo | Alt + U |
| Redo | Alt + E |
| Search text | Ctrl + W |
| Go to line number | Ctrl + _ then enter number |
| Show help | Ctrl + G |
- User config file:
~/.nanorc
Example settings:
set linenumbers
set softwrap
set tabsize 4
| Feature | nano |
vim (Vi IMproved) |
|---|---|---|
| Ease of Use | ✅ Very beginner-friendly | ❌ Steep learning curve (initially) |
| Modes | ❌ None (modeless) | ✅ Yes (normal, insert, visual) |
| Commands | On-screen shortcuts | Keyboard commands (in modes) |
| Syntax Highlighting | Limited | ✅ Advanced |
| Undo/Redo | Basic (Alt + U / Alt + E) |
✅ Powerful (u, Ctrl + r) |
| Customization | Minimal (.nanorc) |
Extensive (.vimrc) |
| Speed & Power | Good for simple tasks | ✅ Excellent for programming |
| Plugin Support | ❌ None | ✅ Yes (with plugin managers) |
| Best for | Quick edits, config files | Complex coding, scripting |
-
Use
nanoif you're editing config files, logs, or doing a quick change and want a no-hassle interface. -
Use
vimif you're coding, doing power-user work, or need powerful editing features like macros, search-replace, etc.
-
View file content
-
Combine multiple files
-
Create new files (in simple cases)
cat filename
| Task | Command |
|---|---|
| View file content | cat file.txt |
| View multiple files | cat file1.txt file2.txt |
| Create a new file (quickly) | cat > new.txt (then type, Ctrl+D to save) |
| Append file content | cat file1 >> file2 |
- Permanently deletes files or directories
rm filename
| Task | Command |
|---|---|
| Delete a file | rm file.txt |
| Delete multiple files | rm file1.txt file2.txt |
| Force delete (no prompt) | rm -f file.txt |
| Delete an empty directory | rmdir mydir |
| Delete directory + contents | rm -r mydir/ |
| Force delete directory | rm -rf mydir/ |
rm permanently deletes — use carefully, especially with -rf
-
The first line of a script:
#! interpreter_path -
Tells the OS which shell (or interpreter) to use to execute the script
Example:
#!/bin/bash
| Shebang Line | Uses Which Shell | Path Points To | Shell Type |
|---|---|---|---|
#!/bin/bash |
Bash (Bourne Again Shell) | /bin/bash |
Full-featured shell (most common on Linux) |
#!/bin/dash |
Dash (Debian Almquist Shell) | /bin/dash |
Lightweight, POSIX-compliant, fast |
#!/bin/ksh |
Korn Shell (ksh) | /bin/ksh |
Advanced shell, scripting friendly |
#!/bin/sh |
POSIX shell (linked) | Often linked to bash or dash |
Legacy-compatible (portable) |
-
/bin/sh→ symbolic link to/bin/dash(for performance) -
/bin/bash→ standalone full Bash shell -
/bin/dash→ minimalist POSIX-compliant shell -
/bin/ksh→ may need to be installed separately
🔧 You can check this with:
ls -l /bin/sh
| Feature | /bin/bash |
/bin/sh (dash) |
/bin/dash |
/bin/ksh |
|---|---|---|---|---|
| Speed | Moderate | ✅ Fast | ✅ Fast | Fast |
| Bash-only features | ✅ Yes | ❌ No | ❌ No | ❌ No |
| POSIX-compliant | Mostly | ✅ Yes | ✅ Yes | ✅ Yes |
Arrays, [[ ]], etc. |
✅ Yes | ❌ No | ❌ No | Partial/Yes |
| Script portability | ❌ Low | ✅ High | ✅ High | ✅ High |
| Use Case | Recommended Shebang |
|---|---|
| Advanced scripting (arrays, regex) | #!/bin/bash |
| Fast, POSIX-compliant init scripts | #!/bin/dash or #!/bin/sh |
| Legacy or system-wide compatibility | #!/bin/sh |
| Older systems or ksh-specific features | #!/bin/ksh |
-
Displays a list of previously executed shell commands in the terminal
-
Helps you recall or reuse past commands
history
- Lists all past commands with line numbers (most recent at the bottom)
| Command | Description |
|---|---|
history |
Show entire command history |
history 10 |
Show last 10 commands |
!n |
Run command number n from history |
!-1 |
Run the last command again |
!! |
Shortcut for !-1 (repeat last command) |
!string |
Run most recent command starting with string |
!sudo |
Run the last command that started with sudo |
history -c |
❗ Clear the history (in current session) |
history -w |
Write current history to ~/.bash_history |
Bash stores history in:
~/.bash_history
- You can view or edit it with
cat,nano, orvim
| Shortcut | Action |
|---|---|
Ctrl + r |
Reverse search through history, To search and reuse past commands interactively: (Ctrl + r) Then type part of the command — it finds matches instantly! |
↑ / ↓ |
Navigate through history one by one |
!! |
Repeat previous command |
- Clears all previous output from the terminal window
clear
- Just type and hit Enter — screen looks fresh, but command history is still accessible (via
↑arrow orCtrl + r).
- Shows the current directory you’re in
pwd
Output will be something like:
/home/username/projects
free
-
Displays RAM & swap usage in bytes
-
Columns: total, used, free, shared, buff/cache, available
free -m # in megabytes
free -g # in gigabytes
nproc
-
Shows the number of logical CPU cores available
-
Example output:
8 -
Useful for parallel tasks or compilation optimization (e.g.,
make -j$(nproc))
df -h
-
Displays available and used disk space per mounted filesystem
-
-h= human-readable (MB/GB) -
Columns: Filesystem, Size, Used, Avail, Use%, Mounted on
top
-
Shows live summary of CPU, memory usage, running processes
-
Default columns:
- PID, USER, %CPU, %MEM, TIME+, COMMAND
-
Press
qto quit -
Press
M(uppercase) to sort by memory usage -
Press
Pto sort by CPU usage
| Command | Purpose | Common Flag | Output Units |
|---|---|---|---|
free |
Memory usage | -m, -g |
MB/GB |
nproc |
Number of CPU cores | None | Integer |
df |
Disk usage per filesystem | -h |
MB/GB |
top |
Live process & system monitoring | (interactive) | Real-time display |
-
Enables debugging mode in Bash
-
Prints each command and its arguments to the terminal before it executes
set -x
# your commands
set +x
-
set -x→ turns on command tracing -
set +x→ turns off command tracing
#!/bin/bash
set -x
a=5
b=10
c=$((a + b))
echo "Sum is $c"
set +x
Output:
+ a=5
+ b=10
+ c=15
+ echo 'Sum is 15'
Sum is 15
-
Helps debug complex scripts by showing step-by-step execution
-
Especially useful inside
if,for, orwhileblocks -
Works in both interactive shell and scripts
The ps command is used to display information about active processes on the system.
ps -ef
-
-e→ Show all processes (not just those owned by current user) -
-f→ Full-format listing (more detailed info)
| Column | Meaning |
|---|---|
| UID | User who owns the process |
| PID | Process ID |
| PPID | Parent Process ID |
| C | CPU utilization |
| STIME | Start time of the process |
| TTY | Terminal associated (if any) |
| TIME | Total CPU time the process has used |
| CMD | Command that started the process |
ps -ef | grep firefox
Finds all running firefox processes.
The pipe (|) is used to connect two commands, sending the output of the first command as the input to the second.
command1 | command2
- Think of it as: “Take the result of command1 and pipe it into command2.”
🔹 Lists files in long format and scrolls the output.
-
ls -l→ Lists files with permissions, size, owner, date, etc. -
less→ Opens output in a scrollable viewer (up/down with arrow keys).
✅ Useful when there are many files and the output overflows the terminal.
🔹 Searches for running bash processes.
-
ps -ef→ Shows all processes with full details. -
grep bash→ Filters lines that contain “bash”.
✅ Helps you find if bash (or any other process) is currently running.
🔹 Counts how many lines are in file.txt.
-
cat file.txt→ Outputs the content of the file. -
wc -l→ Counts the number of lines.
✅ A cleaner and more readable way than using wc -l file.txt.
🔹 Shows the last few kernel/system log messages.
-
dmesg→ Displays kernel ring buffer messages (boot, hardware, drivers). -
tail→ Shows only the last 10 lines by default.
✅ Useful for debugging hardware or boot issues.
🔹 Counts all regular files in the current directory and subdirectories.
-
find . -type f→ Finds all files (not directories) starting from.(current dir). -
wc -l→ Counts the number of lines in the output = number of files.
✅ Helps quickly find how many files exist recursively.
-
Filtering: e.g.,
grep,awk,cut -
Pagination: e.g.,
less,more -
Counting: e.g.,
wc -
Sorting: e.g.,
sort,uniq -
Chaining logic: combine multiple steps without temporary files
- Pipes only work with stdout to stdin (standard output/input).
You can chain multiple pipes:
cat file.txt | grep "error" | sort | uniq -c
A pipe (|) connects the output of one command (stdout) to the input of another command (stdin).
For example:
ls | less
-
lssends its output to the pipe -
lessreads that output from the pipe and displays it interactively
This works because less reads from standard input (stdin).
echo something
-
echosimply prints whatever arguments you pass to it. -
It does not read from standard input (stdin).
-
It does not care what is coming through the pipe.
So when you run:
date | echo
-
date→ produces something likeSat Aug 2 16:32:58 +06 2025 -
That output is sent into the pipe
| -
echoignores it — becauseechodoesn't read from stdin
So the result is just:
(an empty line)
date is effectively discarded.
date | cat
✔ This will output the current date using the pipe.
d=$(date)
echo "Current date is: $d"
| Command | Reads from stdin? | Works with pipes as input? |
|---|---|---|
echo |
❌ No | ❌ No effect from pipe input |
cat |
✅ Yes | ✅ Yes |
less |
✅ Yes | ✅ Yes |
grep |
✅ Yes | ✅ Yes |
awk, sort |
✅ Yes | ✅ Yes |
awk is a powerful command-line text processing tool used to analyze, extract, and format structured data from files or command output.
It processes input line by line and field by field, using patterns and actions.
whatis awk
awk (1) - pattern scanning and text processing language
(The full meaning of awk comes from the initials of its creators:
Aho, Weinberger, and Kernighan)
-
Filtering and transforming text
-
Extracting specific fields from lines
-
Doing calculations while reading files
-
Creating reports from structured text
It’s like a mini programming language designed for:
pattern { action }
awk 'pattern {action}' filename
-
pattern: Optional. Specifies the condition to match.
-
action: What to do when the pattern is matched.
-
filename: File to read from (or input from pipe
|).
| Variable | Description |
|---|---|
$0 |
Entire line |
$1 |
First field |
$2 |
Second field |
$NF |
Last field (NF = Number of Fields) |
NF |
Total number of fields in a line |
NR |
Current line number (Number of Records) |
FS |
Field separator (default: space/tab) |
awk '{print $1, $3}' file.txt
Prints the first and third columns of each line.
awk '{sum += $2} END {print "Total:", sum}' file.txt
Adds all numbers in the second column.
awk '{print NR, $0}' file.txt
Adds line numbers before each line.
awk '{print $NF}' file.txt
awk 'END {print NR}' file.txt
awk -F, '{print $1, $3}' data.csv
-F, tells awk to use a comma as the field separator.
ps -ef | grep amazon | awk -F" " '{print $2}'
-
ps -ef: Shows all processes -
grep amazon: Filters lines related to "amazon" -
awk -F" " '{print $2}': Extracts the second field (PID)
awk '{printf "%-10s %-5s\n", $1, $2}' file.txt
Formats fields nicely using printf.
awk '$3 > 1000' file.txt
Prints lines where the third field is greater than 1000.
awk 'NF > 3' file.txt
ps -ef | grep power | awk '{sum+=$2} END {print "Total:", sum}'
| Task | Command |
|---|---|
| Extract 2nd column | awk '{print $2}' |
| Use comma separator | awk -F, '{print $1}' |
| Sum values in column | awk '{sum+=$1} END{print sum}' |
| Print line numbers | awk '{print NR, $0}' |
| Print last field | awk '{print $NF}' |
| Only lines with >3 columns | awk 'NF>3' |
Great! Let’s break down set -e, set -o, and how they work in shell scripting — especially in bash.
Exit immediately if a command exits with a non-zero status.
It’s like saying:
💣 “If anything fails, stop the script right there.”
set -e
echo "Step 1"
false # This fails (exit status 1)
echo "Step 2" # This will NOT run
The set -o pipefail option in bash (or set -pipefail in zsh) makes your pipeline safer by making the entire pipeline fail if any command fails, not just the last one.
Only the last command in a pipeline determines the pipeline’s exit status.
false | grep "something"
echo $? # Outputs: 0 ✅ (success) — misleading!
Here, even though false failed, grep didn’t fail, so the pipeline exit status is 0.
That hides errors! In a script, it might continue even if an earlier step failed.
Now, the pipeline fails if any command fails.
set -o pipefail
false | grep "something"
echo $? # Outputs: 1 ❌ — correctly shows failure
curl stands for Client URL. It's used to fetch or send data using URLs — via protocols like HTTP, HTTPS, FTP, etc.
It’s commonly used to:
-
Download web pages or files
-
Interact with REST APIs
-
Send POST or GET requests
-
Test endpoints
curl [options] [URL]
curl -O https://example.com/file.txt
-Osaves the file with the original filename.
curl https://example.com
curl -X POST -d "name=Tamim&email=test@example.com" https://httpbin.org/post
-
-X POSTtells curl to use POST -
-dsends form data
curl -H "Authorization: Bearer <token>" https://api.example.com/data
curl -F "file=@example.txt" https://httpbin.org/post
-Fmeans form-data
curl -I https://example.com
| Option | Description |
|---|---|
-O |
Save file with remote name |
-o filename |
Save output to a file manually |
-L |
Follow redirects |
-X |
Specify request type (GET, POST…) |
-d |
Send POST data |
-F |
Send multipart/form-data |
-H |
Add custom header |
-I |
Fetch headers only |
-s |
Silent mode (no progress/output) |
-
-Xstands for "request command/method". -
It tells
curlto use a specific HTTP method (likeGET,POST,PUT,DELETE, etc.).
curl -X POST https://httpbin.org/post
This sends a POST request instead of the default GET.
📌 Without -X, curl uses GET by default.
Use https://httpbin.org to test all curl features safely.
Here's your final note on wget — a powerful command-line tool for downloading files from the web.
wget stands for Web Get — it’s a non-interactive command-line tool to download files from the web using protocols like HTTP, HTTPS, and FTP.
wget [options] [URL]
| Use Case | Command Example |
|---|---|
| 📥 Download a file | wget https://example.com/file.zip |
| 💾 Save with custom name | wget -O myfile.zip https://example.com/file.zip |
| 🔁 Resume a partially downloaded file | wget -c https://example.com/bigfile.zip |
| 🕵️ Download in background | wget -b https://example.com/file.iso |
| Option | Description |
|---|---|
-O FILE |
Save download as specific filename |
-c |
Continue incomplete download (resume) |
-P DIR |
Save file(s) to specific directory |
--limit-rate=200k |
Throttle download speed to 200 KB/s |
--user, --password |
HTTP authentication login info |
--mirror |
Mirror entire website |
--no-check-certificate |
Skip SSL certificate verification |
| Feature | curl |
wget |
|---|---|---|
| Protocols | HTTP, FTP, SCP, etc. | HTTP, FTP |
| Resuming downloads | Manual (-C -) |
Built-in (-c) |
| Recursive download | ❌ | ✅ Yes (--recursive, --mirror) |
| POST/PUT requests | ✅ | Limited |
| Default install | Common on macOS/Linux | Common on Linux |
Try downloading from:
wget https://www.gnu.org/software/wget/manual/wget.txt
Here’s a concise and practical final note on find — one of the most powerful tools in Linux to search for files and directories.
find is used to search files/directories recursively in a directory hierarchy, based on name, type, time, size, permissions, etc.
find [path] [options] [expression]
| Purpose | Command Example |
|---|---|
| 🔍 Find by name | find . -name "file.txt" |
| 🔍 Case-insensitive name | find . -iname "file.txt" |
| 📂 Find only directories | find . -type d -name "folder*" |
| 📄 Find only files | find . -type f -name "*.sh" |
| 🔒 Find by permissions | find . -type f -perm 644 |
| 🕒 Find by modification time | find . -mtime -1 (within last 1 day) |
| 📏 Find by size | find . -size +10M (files larger than 10 MB) |
| ❌ Find and delete files | find . -name "*.tmp" -delete |
| 🧩 Find and execute command | find . -name "*.log" -exec rm {} \; (delete logs) |
| 🔄 Find and use xargs | `find . -name "*.txt" |
| Option | Meaning |
|---|---|
. |
Current directory |
-name |
Match file name exactly |
-iname |
Match name case-insensitively |
-type f |
Files only |
-type d |
Directories only |
-mtime -n |
Modified less than n days ago |
-size +nM |
Bigger than n megabytes |
-perm 644 |
Files with exact permission |
-exec CMD \; |
Run command on found files (e.g., delete) |
# All .c files in /home
find /home -type f -name "*.c"
# Delete all temp files
find . -name "*.tmp" -delete
# List all directories
find . -type d
# Find large files >100MB
find / -type f -size +100M
# Find modified in last 2 days
find . -mtime -2