Linux File Editing, Viewing, and Searching Cheat Sheet
1. File Editing
nano filename -> Easy editor for beginners
vim filename -> Powerful modal editor
vi filename -> Classic version of vim
Nano Shortcuts:
CTRL + O -> Save
CTRL + X -> Exit
CTRL + K -> Cut line
CTRL + U -> Paste line
Vim Basics:
i -> Insert mode
Esc -> Command mode
:w -> Save
:q -> Quit
:wq -> Save and quit
dd -> Delete line
/word -> Search word
n -> Next match
2. File Viewing
cat filename -> Show file contents
more filename -> Paginate through large file
less filename -> Scrollable view, /search
head -n 10 filename -> First 10 lines
tail -n 10 filename -> Last 10 lines
tail -f filename -> Follow live updates (logs)
3. Searching
Search Inside Files:
grep "text" filename -> Search for text in file
grep -i "text" filename -> Case-insensitive search
grep -r "text" /path/dir -> Recursive search
grep -n "text" filename -> Show line numbers
grep -A3 -B2 "text" filename -> Show context lines
Search Files/Dirs:
find /path -name "file.txt" -> Find by name
find /path -iname "*.log" -> Case-insensitive search
find . -type f -mtime -1 -> Modified in last 1 day
locate filename -> Fast file search (needs updatedb)
which command -> Path of a command
4. Power Tips
sed 's/old/new/g' file -> Replace text
grep -c "word" file -> Count matches
grep --color "word" file -> Highlight matches
grep "text" *.log -> Search in multiple files
grep -rn "error" /var/log/ -> Search + show line number