Linux commands
Files and folders#
Where am I, what's here#
| Command | Does |
|---|---|
pwd |
Show the folder you're in |
ls |
List files |
ls -la |
List everything, including hidden files (names starting with .), with details |
ls -lh |
Details with readable sizes (K, M, G) |
cd /srv/files |
Go to a folder |
cd ~ or just cd |
Go home |
cd .. |
Go up one folder |
cd - |
Go back to the previous folder |
tree -L 2 |
Show folders as a tree, 2 levels deep (sudo apt install tree) |
Read a file#
| Command | Does |
|---|---|
cat FILE |
Print the whole file |
less FILE |
Scroll through it (arrows, Space for next page, /word to search, Q quits) |
head -20 FILE |
First 20 lines |
tail -20 FILE |
Last 20 lines |
tail -f FILE |
Keep showing new lines as they're added (Ctrl+C stops) |
wc -l FILE |
Count the lines |
Edit a file#
nano FILE # your own files
sudo nano /etc/FILE # system settings
| In nano | Does |
|---|---|
| Ctrl+O Enter | Save |
| Ctrl+X | Exit |
| Ctrl+W | Search |
| Ctrl+K / Ctrl+U | Cut line / paste |
| Alt+U | Undo |
Before editing a system file, keep a copy
sudo cp -a /etc/ssh/sshd_config /etc/ssh/sshd_config.bak. -a keeps the owner and permissions.
Create, copy, move, delete#
| Command | Does |
|---|---|
mkdir NAME |
New folder |
mkdir -p a/b/c |
New folder with all the parent folders |
touch FILE |
New empty file |
cp FILE COPY |
Copy a file |
cp -r FOLDER COPY |
Copy a folder with everything inside |
cp -a SOURCE DEST |
Copy and keep owner, permissions and dates (best for backups) |
mv OLD NEW |
Rename, or move to another folder |
mv -n OLD NEW |
Same, but never overwrite an existing file |
rm FILE |
Delete a file (no recycle bin) |
rm -r FOLDER |
Delete a folder and everything inside |
rm -i FILE |
Ask before each delete |
rm has no undo
Check the path twice. Never run rm -rf with sudo on a path you copied from somewhere else.
Find things#
| Command | Does |
|---|---|
find /srv/files -name "*.pdf" |
All PDFs under a folder |
find . -iname "*report*" |
Name contains "report" (-iname ignores upper/lower case) |
find /srv/files -size +1G |
Files bigger than 1 GB |
find ~ -mtime -1 |
Changed in the last day |
grep "word" FILE |
Lines containing a word |
grep -rn "word" /etc/ssh/ |
Search all files in a folder, with line numbers |
grep -i "error" FILE |
Ignore upper/lower case |
Pack and unpack#
| Command | Does |
|---|---|
tar -czf backup.tar.gz FOLDER |
Pack a folder into one compressed file |
tar -tzf backup.tar.gz |
List what's inside, without unpacking |
tar -xzf backup.tar.gz -C /tmp/test |
Unpack into a folder |
gpg --symmetric FILE |
Encrypt with a passphrase (makes FILE.gpg) |
gpg -d FILE.gpg > FILE |
Decrypt |
sha256sum FILE |
Fingerprint of a file (to check copies are identical) |