Everyday commands: ls, grep, tar
The commands you type every day: list files with ls and ll, search text with grep, pack and extract archives with tar.
ls and ll: list files
ll is not a separate program. It is an alias Ubuntu defines in ~/.bashrc for ls -alF.
| What | Command |
|---|---|
| List files | ls |
Long list incl. hidden files (= ll) | ls -alF |
| Human-readable sizes | ls -lh |
| Newest first | ls -lt (oldest first: ls -ltr) |
| Biggest first | ls -lS |
| Only directories | ls -d */ |
| Recursively | ls -R (better: tree, if installed) |
| Show numeric UID/GID | ls -n |
Reading the long format (-rw-r----- 1 anna developers ...) is explained in How permissions work.
grep: search text
grep PATTERN FILE prints every line that contains the pattern.
| What | Command |
|---|---|
| Search a file | grep "error" app.log |
| Ignore case | grep -i "error" app.log |
| Search a whole folder | grep -r "listen" /etc/nginx/ |
| Show line numbers | grep -n "error" app.log |
| Only the file names | grep -rl "password" /etc/ |
| Lines that do not match | grep -v "DEBUG" app.log |
| Whole word only | grep -w "root" /etc/passwd |
| Several patterns | grep -E "error|fail|denied" app.log |
| 3 lines of context around each hit | grep -C 3 "panic" app.log (-A after, -B before) |
| Count hits | grep -c "Failed password" /var/log/auth.log |
| Search compressed (rotated) logs | zgrep "error" /var/log/syslog.2.gz |
| Filter another command’s output | ps aux | grep nginx |
-E turns on extended regular expressions: ^ = start of line, $ = end, . = any char, .* = anything.
tar: archives
A .tar file bundles many files into one. .tar.gz / .tgz is the same, compressed with gzip; .tar.xz and .tar.zst use other compressors. Modern tar detects the compression by itself when extracting.
| What | Command |
|---|---|
| Extract (any compression) | tar -xf archive.tar.gz |
| Extract into a folder | tar -xf archive.tar.gz -C /opt/app |
| Show what’s inside, without extracting | tar -tf archive.tar.gz |
| Extract a single file | tar -xf archive.tar.gz path/in/archive.conf |
| Create a .tar.gz | tar -czf backup.tar.gz /etc/nginx |
| Create with xz (smaller, slower) | tar -cJf backup.tar.xz folder/ |
| Create, keeping owners and permissions | sudo tar -czpf etc-backup.tar.gz /etc |
| Exclude something | tar -czf app.tar.gz --exclude='*.log' /srv/app |
How to remember: x = extract, c = create, t = list (table), f = file name follows, z = gzip, v = verbose (show each file).
Other formats:
| File | Extract with |
|---|---|
.zip | unzip file.zip (sudo apt install unzip) |
.gz (single file) | gunzip file.gz |
.7z | 7z x file.7z (sudo apt install 7zip) |
Other daily commands
| What | Command |
|---|---|
| Show a file | cat file, long files: less file |
| First / last lines | head -n 20 file, tail -n 20 file |
| Follow a growing file | tail -f file |
| Copy / move / rename | cp -a src dst, mv old new |
| Delete a folder with content | rm -r folder (careful: no trash bin) |
| Create an empty file / update its time | touch file |
| Where am I? | pwd |
| Disk usage of a folder | du -sh folder |