Bash essentials for navigating, searching, piping and managing files.
Navigation
| Command |
Description |
pwd |
Print current directory |
ls -la |
List all files with details |
cd <dir> |
Change directory |
cd - |
Go to previous directory |
cd ~ |
Go home |
tree -L 2 |
Folder tree, 2 levels deep |
Files and folders
| Command |
Description |
touch file.txt |
Create empty file |
mkdir -p a/b/c |
Create nested folders |
cp -r src dest |
Copy folder |
mv old new |
Move or rename |
rm file |
Delete file |
rm -r folder |
Delete folder |
ln -s target link |
Symbolic link |
Viewing files
| Command |
Description |
cat file |
Print file |
less file |
Scroll through file (q to quit) |
head -n 20 file |
First 20 lines |
tail -n 20 file |
Last 20 lines |
tail -f app.log |
Follow a file as it grows |
wc -l file |
Count lines |
Searching
| Command |
Description |
grep "text" file |
Lines containing text |
grep -rn "text" . |
Recursive with line numbers |
grep -i / -v |
Case insensitive / invert match |
find . -name "*.js" |
Find files by name |
find . -type d -name node_modules |
Find folders |
find . -mtime -1 |
Modified in last 24h |
which node |
Location of a command |
Pipes and redirection
| Syntax |
Description |
a | b |
Pipe output of a into b |
> file |
Write output to file (overwrite) |
>> file |
Append output to file |
2> file |
Redirect errors |
&> file |
Redirect output and errors |
a && b |
Run b if a succeeds |
a || b |
Run b if a fails |
a ; b |
Run b after a regardless |
cat access.log | grep 404 | sort | uniq -c | sort -rn | head
find . -name "*.tmp" | xargs rm
Processes
| Command |
Description |
ps aux |
All processes |
top / htop |
Live process view |
kill <pid> |
Stop a process |
kill -9 <pid> |
Force kill |
command & |
Run in background |
jobs / fg |
List / resume background jobs |
Ctrl+C / Ctrl+Z |
Interrupt / suspend |
Permissions
| Command |
Description |
chmod +x script.sh |
Make executable |
chmod 644 file |
rw-r–r– |
chmod 755 dir |
rwxr-xr-x |
chown user:group file |
Change owner |
sudo <command> |
Run as root |
Shortcuts and history
| Shortcut |
Description |
Ctrl+R |
Search command history |
!! |
Repeat last command (sudo !!) |
Ctrl+A / Ctrl+E |
Start / end of line |
Ctrl+U / Ctrl+K |
Delete before / after cursor |
Ctrl+L |
Clear screen |
history |
Show command history |
Network
| Command |
Description |
curl -I <url> |
Response headers only |
curl -X POST -d '{}' -H "Content-Type: application/json" <url> |
POST JSON |
wget <url> |
Download file |
ssh user@host |
Connect over SSH |
scp file user@host:/path |
Copy file over SSH |