$ cat linux-fundamentals/part-01.md
Linux File Management

Linux File Management
I’m learning DevOps and Linux, and I want to share my notes about working with files and folders. I keep things simple because I’m still learning.
What You’ll Learn
- How the Linux file system works
- How to move around folders
- How to create, copy, move, and delete files
- How to use paths and wildcards
- How to read and search file contents
- How to use grep to find text
- How to compare files
- How to work with file editors
1. The Linux File System
In Linux, everything starts from the root folder /. It is like C:\ in Windows, but Linux does not use drive letters.
Example structure:
/
├── home/
│ ├── reza/
│ └── john/
├── etc/
├── var/
└── usr/Important folders to remember: • /home → user folders like /home/reza • /root → admin user’s home folder (need sudo) • /etc → configuration and system files • /var → logs and temporary data • /usr → programs and libraries • /tmp → temporary files (deleted after restart)
2. Moving Around
pwd → shows where I am now
pwdls → lists files and folders
ls
ls -l # long format
ls -a # show hidden filescd → move to another folder
cd Documents
cd .. # go up one level
cd ~ # go to my home folder3. Understanding Paths
Absolute path starts from /:
cd /home/reza/DocumentsRelative path starts from where I am:
cd Documents
cd ../johnSpecial symbols to remember:
- . → current folder
- .. → one level up
- ~ → my home folder
4. Creating Files and Folders
mkdir → create a new folder
The -p option stands for parents. It tells Linux to automatically create all missing folders in the path.
mkdir my_folder
mkdir -p projects/web/reacttouch → create an empty file
touch file.txtAdd text to a file:
echo "Hello World" > file.txt
echo "More text" >> file.txt5. Copying Files
cp → copy files
cp file.txt backup.txt
cp file.txt ~/Documents/Copy folders (use -r for recursive):
cp -r folder1 folder26. Moving or Renaming
mv → move or rename files
mv oldname.txt newname.txt
mv file.txt Documents/No need for -r when moving folders.
7. Deleting Files and Folders
rm → delete files
rm file.txtrm -r → delete folders and everything inside
rm -r folder_nameBe careful, there’s no trash bin. Safe way:
rm -i file.txtrmdir → removes empty folders only
rmdir empty_folder8. Reading Files
cat – Show Whole File
The simplest way to see a file:
cat file.txtShow multiple files:
cat file1.txt file2.txt- Good for: Small files
- Bad for: Big files (floods the screen!)
- Warning: Never use cat on binary files (images, executables) – it breaks the terminal!
head – Show Beginning
See first 10 lines:
head file.txtSee first 5 lines:
head -n 5 file.txtWhen I use this: Checking log files, seeing file headers
tail – Show End
See last 10 lines:
tail file.txtSee last 20 lines:
tail -n 20 file.txtWatch file live (great for logs!):
tail -f /var/log/syslogThis keeps showing new lines as they’re added. Press Ctrl+C to stop.
When I use this: Watching log files in real-time, checking recent entries
less – View Page by Page
A program to read files page by page. Much better than cat for big files.
less bigfile.txtNavigation in less:
- Space → Next page
- b → Previous page
- Arrow keys → Move up/down
- g → Go to beginning
- G → Go to end
- q → Quit
Searching in less: Press / then type search term:
# Inside less:
/error # Find "error"
n # Next match
N # Previous matchWhy I Use less:
- Doesn’t load entire file at once (fast!)
- Can search • Can go back and forth •
- Doesn’t mess up my terminal
9. Searching in Files with grep
What is grep?
Searches for text patterns in files. Super useful!
grep = Global Regular Expression Print
Basic Usage
grep "search_term" file.txtExample:
grep "error" logfile.log
# Shows all lines with "error"Useful grep Options
grep -i "error" file.txt # Ignore case (Error, ERROR, error)
grep -n "error" file.txt # Show line numbers
grep -v "error" file.txt # Show lines WITHOUT "error"
grep -r "error" /var/log/ # Search recursively in folder
grep -l "error" *.log # Show only filenames with matches
grep -c "error" file.txt # Count matching linesSearch Multiple Files
grep "error" *.log # Search all .log files
grep "error" file1.txt file2.txt # Search specific filesCommon Patterns I Use
# Find errors in logs
grep -i "error" /var/log/syslog
# Find my IP address
grep "192.168" /etc/hosts
# Find failed login attempts
grep "Failed password" /var/log/auth.log
# Count errors
grep -c "error" application.logAdvanced grep
Using Regular Expressions:
grep "^error" file.txt # Lines starting with "error"
grep "error$" file.txt # Lines ending with "error"
grep "error.*warning" file.txt # Lines with error then warning
grep "[0-9]" file.txt # Lines with numbersGrep with Pipes:
ps aux | grep nginx # Find nginx processes
ls -la | grep "txt" # Find txt files in listing
cat file.txt | grep "error" # Same as: grep "error" file.txtContext Lines:
grep -A 3 "error" file.txt # Show 3 lines After match
grep -B 2 "error" file.txt # Show 2 lines Before match
grep -C 2 "error" file.txt # Show 2 lines of Context (before and after)10. Wildcards (Working with Many Files at Once)
Wildcards help you work with many files or folders using one command instead of typing each name. They are very useful when you have many files with similar names.
* (Asterisk) – Matches Anything
The star * means “any number of characters.” You can use it anywhere in a file name.
Examples:
ls *.txt
# Shows all files that end with .txt (for example: notes.txt, report.txt, list.txt)
ls file*
# Shows all files that start with "file" (for example: file1.txt, fileA.txt, file_backup.log)
rm *.log
# Deletes all files that end with .log (for example: system.log, app.log, error.log)
cp *.jpg Pictures/
# Copies all image files that end with .jpg into the Pictures folder.? (Question Mark) – Matches One Character
The question mark ? replaces only one single character.
Examples:
ls file?.txt
# Matches files like: file1.txt, fileA.txt, fileb.txt
# But not: file10.txt (because it has two extra characters)
ls ???.txt
# Matches any file name that has exactly three characters before .txt
# (for example: cat.txt, dog.txt, log.txt)Practice Tip: You can test your wildcard command with ls first to make sure it matches what you expect. For example:
ls *.txtThen if it looks correct, you can safely run:
rm *.txtThis helps avoid deleting the wrong files.
11. Finding Files (Search Anywhere in Linux)
When your folders have many files, it’s easy to lose something. The find command helps you search for a file or folder by name, type, or size.
The basic format is:
find [where_to_search] [option] [what_to_find]For example:
find . -name "file.txt"
# . means start searching in the current folder
# -name "file.txt" means look for a file with this exact nameCommon Examples
Find a file in the current folder and subfolders:
find . -name "notes.txt"Find all .log files inside /home:
find /home -name "*.log"Find all .txt files (case insensitive):
find . -iname "*.txt"
# -iname ignores upper or lower case (so it matches both FILE.TXT and file.txt)Find only files (not folders):
find . -type f -name "*.sh"
# -type f means look for regular files.Find only folders:
find . -type d -name "backup"
# -type d means look for directories (folders).Advanced Examples
Find large files (more than 10MB):
find . -type f -size +10M
# +10M means larger than 10 megabytes
# You can also use -10M for smaller than 10MBFind files changed in the last 2 days:
find . -mtime -2
# -mtime checks when the file was last modified
# -2 means in the last two daysFind and delete specific files:
find . -name "*.tmp" -delete
# Deletes all temporary .tmp files under the current folder.
# Use this carefully, it does not ask for confirmation.Practice Tip: When using find, start with small areas like . (current folder) before searching your whole system. Example:
find . -name "*.log"is faster and safer than
find / -name "*.log"which searches the entire computer.
12. Comparing Files
diff – Compare Two Files
See differences between files:
diff file1.txt file2.txtOutput symbols:
- < = line from first file
- > = line from second file
- a = added
- d = deleted
- c = changed
Side-by-Side View
diff -y file1.txt file2.txtIgnore Case and Whitespace
diff -i file1.txt file2.txt # Ignore case
diff -w file1.txt file2.txt # Ignore whitespace13. Text Editors in Terminal
nano – Easy Editor
Simple text editor:
nano file.txtControls (shown at bottom):
- Ctrl+O = Save (Write Out)
- Ctrl+X = Exit
- Ctrl+K = Cut line
- Ctrl+U = Paste
- Ctrl+W = Search
I use nano when: Quick edits, simple config changes
vim – Advanced Editor
Powerful but harder to learn:
vim file.txtTwo modes:
- Normal mode (default) – for commands
- Insert mode – for typing (press i)
Basic vim commands:
- i = Enter insert mode
- Esc = Back to normal mode
- :w = Save
- :q = Quit
- :wq = Save and quit
- :q! = Quit without saving
Quick Comparison
- nano: Easy to learn, usually installed, basic features, my choice for quick edits
- vim: Hard to learn, always installed, very powerful, my choice for serious work
14. File Analysis Tools
file – Identify File Type
file document.pdf
# Output: PDF document
file image.jpg
# Output: JPEG image data
file script.sh
# Output: Bash scriptUseful when file has wrong extension or no extension.
wc – Word Count
wc file.txt
# Output: lines words bytes filenameCount lines only:
wc -l file.txtCount words only:
wc -w file.txtCount characters:
wc -m file.txtPractical Examples
# Count files in directory
ls | wc -l
# Count log entries
grep "error" app.log | wc -l
# Count lines of code
find . -name "*.py" | xargs wc -l15. Sorting and Unique Lines
sort – Sort Lines
sort file.txt # Sort alphabetically
sort -r file.txt # Reverse sort
sort -n file.txt # Sort numerically
sort -u file.txt # Sort and remove duplicatesuniq – Remove Duplicate Lines
Important: File must be sorted first!
sort file.txt | uniq # Remove duplicates
sort file.txt | uniq -c # Count occurrences
sort file.txt | uniq -d # Show only duplicatesPractical Example
Find most common errors in log:
grep "error" app.log | sort | uniq -c | sort -rnThis:
- Finds all error lines
- Sorts them
- Counts duplicates
- Sorts by count (highest first)
16. File Permissions (Basic Idea)
Example:
-rw-r--r-- 1 reza reza 1234 Oct 19 10:30 file.txtWhat it means: • rw- → owner can read and write • r– → group can read only • r– → others can read only
17. Command Quick Reference
Navigation
- pwd → Show current folder
- ls → List files
- cd folder → Go to folder
- cd .. → Go up one level
- cd ~ → Go home
File Actions
- touch file.txt → Create file
- mkdir folder → Create folder
- cp file1 file2 → Copy file
- cp -r dir1 dir2 → Copy folder
- mv old new → Move or rename
- rm file → Delete file
- rm -r folder → Delete folder
Reading Files
- cat file.txt → Show whole file
- head file.txt → Show first 10 lines
- tail file.txt → Show last 10 lines
- tail -f file.txt → Watch file live
- less file.txt → View file page by page
Searching
- grep “text” file → Find text in file
- grep -i “text” file → Case insensitive search
- grep -r “text” folder/ → Search in folder
- grep -n “text” file → Show line numbers
- grep -v “text” file → Show lines without text
File Analysis
- wc -l file → Count lines
- diff file1 file2 → Compare files
- file filename → Show file type
- sort file → Sort lines
- uniq file → Remove duplicates
What I Learned
- Everything starts from /
- Use pwd, ls, and cd to move around
- Use absolute or relative paths
- Add -r when copying folders
- rm deletes permanently
- Wildcards save time
- Always check your path before removing files
- Use less for large files, cat for small ones
- grep is super powerful for searching
- tail -f is perfect for watching logs
- Always pipe through sort before using uniq
- nano for quick edits, vim for power users
- Regular expressions make grep even more useful
Common Commands
# Watch logs
tail -f /var/log/nginx/error.log
# Find errors
grep -i "error" /var/log/syslog | less
# Count errors today
grep "$(date +%Y-%m-%d)" /var/log/app.log | grep -c "ERROR"
# Find config files
find /etc -name "*.conf"
# Check file before editing
less /etc/nginx/nginx.conf
# Quick edit
vim ~/.bashrc
# Find large files
find . -type f -size +100M
# Most common errors
grep ERROR app.log | sort | uniq -c | sort -rn | head
# Test wildcard commands safely
ls *.txt
# Find and count specific file types
find . -name "*.py" | wc -lTips
- Always check your current folder with pwd.
- Use rm -i to confirm before deleting.
- Test wildcard commands with ls first.
- Practice in test folders before using real files.
- Keep backups of important data.
- Use less instead of cat for large files.
- Learn a few grep patterns, they’re incredibly useful.
- Start with nano for editing, learn vim later.
- When in doubt, check the manual with man command.
That’s my complete guide to Linux file management! Now I can navigate, create, read, search, and manage files.