ubuntu, updated

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.

WhatCommand
List filesls
Long list incl. hidden files (= ll)ls -alF
Human-readable sizesls -lh
Newest firstls -lt (oldest first: ls -ltr)
Biggest firstls -lS
Only directoriesls -d */
Recursivelyls -R (better: tree, if installed)
Show numeric UID/GIDls -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.

WhatCommand
Search a filegrep "error" app.log
Ignore casegrep -i "error" app.log
Search a whole foldergrep -r "listen" /etc/nginx/
Show line numbersgrep -n "error" app.log
Only the file namesgrep -rl "password" /etc/
Lines that do not matchgrep -v "DEBUG" app.log
Whole word onlygrep -w "root" /etc/passwd
Several patternsgrep -E "error|fail|denied" app.log
3 lines of context around each hitgrep -C 3 "panic" app.log (-A after, -B before)
Count hitsgrep -c "Failed password" /var/log/auth.log
Search compressed (rotated) logszgrep "error" /var/log/syslog.2.gz
Filter another command’s outputps 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.

WhatCommand
Extract (any compression)tar -xf archive.tar.gz
Extract into a foldertar -xf archive.tar.gz -C /opt/app
Show what’s inside, without extractingtar -tf archive.tar.gz
Extract a single filetar -xf archive.tar.gz path/in/archive.conf
Create a .tar.gztar -czf backup.tar.gz /etc/nginx
Create with xz (smaller, slower)tar -cJf backup.tar.xz folder/
Create, keeping owners and permissionssudo tar -czpf etc-backup.tar.gz /etc
Exclude somethingtar -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:

FileExtract with
.zipunzip file.zip (sudo apt install unzip)
.gz (single file)gunzip file.gz
.7z7z x file.7z (sudo apt install 7zip)

Other daily commands

WhatCommand
Show a filecat file, long files: less file
First / last lineshead -n 20 file, tail -n 20 file
Follow a growing filetail -f file
Copy / move / renamecp -a src dst, mv old new
Delete a folder with contentrm -r folder (careful: no trash bin)
Create an empty file / update its timetouch file
Where am I?pwd
Disk usage of a folderdu -sh folder
§ home
~/knubertus/home/
1,1 Top