Quick Links
- copy a file
- duplicate a file
- copy a directory
- duplicate a directory
- move a file
- rename a file
- move a directory
- rename a directory
- merge directories
- create a new file
- create a new directory
- show file/directory size
- show file/directory info
- open a file with the default program
- open a file in any application
- zip a directory
- unzip a directory
- peek files in a zip file
- remove a file
- remove a directory
- remove all files of certain criteria
- list directory contents
- tree view a directory and its subdirectories
- find a stale file
- show a calendar
- find a future date
- use a calculator
- force quit a program
- check server response
- view content of a file
- search for a text in a file
- search in all files in current working directory, quickly (entire disk in less than 15 minutes)
- view an image
- show disk size
- check cpu usage, processes and RAM
- know whether your computer is under load, and whether it's due to memory or CPU
- poweroff or reboot your computer
- locate USB drives
- unmount USB drives
- format USB drives
- check USB format
- run command on all files of a directory
- check network connectivity to a remote address and port
- check DNS config of a domain
- check the ownership and registration of a domain
- Hotkeys
- I can't remember these cryptic commands
copy a file
Copy readme.txt to the documents directory
$ cp readme.txt documents/
Go to table of contents πΌ
duplicate a file
$ cp readme.txt readme.bak.txt
More advanced:
$ cp readme{,.bak}.txt
# Note: learn how the {} works with touch foo{1,2,3}.txt and see what happens.
Go to table of contents πΌ
copy a directory
Copy myMusic directory to the myMedia directory
$ cp -a myMusic myMedia/
# or
$ cp -a myMusic/ myMedia/myMusic/
Go to table of contents πΌ
duplicate a directory
$ cp -a myMusic/ myMedia/
# or if `myMedia` folder doesn't exist
$ cp -a myMusic myMedia/
Go to table of contents πΌ
move a file
$ mv readme.txt documents/
Always use a trailing slash when moving files, for this reason.
Go to table of contents πΌrename a file
$ mv readme.txt README.md
Go to table of contents πΌ
move a directory
$ mv myMedia myMusic/
# or
$ mv myMedia/ myMusic/myMedia
Go to table of contents πΌ
rename a directory
$ mv myMedia/ myMusic/
Go to table of contents πΌ
merge directories
$ rsync -a /images/ /images2/ # note: may over-write files with the same name, so be careful!
Go to table of contents πΌ
create a new file
$ touch 'new file' # updates the file's access and modification timestamp if it already exists
# or
$ > 'new file' # note: erases the content if it already exists
Go to table of contents πΌ
create a new directory
$ mkdir 'untitled folder'
# or
$ mkdir -p 'path/may/not/exist/untitled folder'
Go to table of contents πΌ
show file/directory size
$ du -sh node_modules/
Go to table of contents πΌ
show file/directory info
$ stat -x readme.md # on macOS
$ stat readme.md # on Linux
Go to table of contents πΌ
open a file with the default program
$ xdg-open file # on Linux
$ open file # on MacOS
$ start file # on Windows
Go to table of contents πΌ
open a file in any application
$ open -a appName file
Go to table of contents πΌ
zip a directory
$ zip -r archive_name.zip folder_to_compress
Go to table of contents πΌ
unzip a directory
$ unzip archive_name.zip
Go to table of contents πΌ
peek files in a zip file
$ zipinfo archive_name.zip
# or
$ unzip -l archive_name.zip
Go to table of contents πΌ
remove a file
$ rm my_useless_file
IMPORTANT: The rm command deletes my_useless_file permanently, which is equivalent to move my_useless_file to Recycle Bin and hit Empty Recycle Bin.
remove a directory
$ rm -r my_useless_folder
Go to table of contents πΌ
remove all files of certain criteria
$ find . -name "*.bak" -type f -delete
IMPORTANT: run find . -name "*.bak" -type f first to see exactly which files you will remove.
list directory contents
$ ls my_folder # Simple
$ ls -la my_folder # -l: show in list format. -a: show all files, including hidden. -la combines those options.
$ ls -ltrha my_folder # -r: reverse output. -t: sort by time (modified). -h: output human-readable sizes.
Go to table of contents πΌ
tree view a directory and its subdirectories
$ tree # on Linux
$ find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g' # on MacOS
# Note: install homebrew (https://brew.sh) to be able to use (some) Linux utilities such as tree.
# brew install tree
Go to table of contents πΌ
find a stale file
Find all files modified more than 5 days ago
$ find my_folder -mtime +5
Go to table of contents πΌ
show a calendar
Display a text calendar
$ cal
Display selected month and year calendar
$ cal 11 2018
Go to table of contents πΌ
find a future date
What is today's date?
$ date +%m/%d/%Y
What about a week from now?
$ date -d "+7 days" # on Linux
$ date -j -v+7d # on MacOS
Go to table of contents πΌ
use a calculator
$ bc -l
Go to table of contents πΌ
force quit a program
$ killall -9 program_name
Go to table of contents πΌ
check server response
$ curl -i rbtreevi.web.app
# curl's -i (--include) option includes HTTP response headers in its output.
Go to table of contents πΌ
view content of a file
$ cat apps/settings.py
# if the file is too big to fit on one page, you can use a 'pager' (less) which shows you one page at a time.
$ less apps/settings.py
Go to table of contents πΌ
search for a text in a file
$ grep -i "Query" file.txt
Go to table of contents πΌ
search in all files in current working directory, quickly (entire disk in less than 15 minutes)
$ ripgrep -i "Query"
# brew install ripgrep
Go to table of contents πΌ
view an image
$ imgcat image.png
# Note: requires iTerm2 terminal.
Go to table of contents πΌ
show disk size
$ df -h
Go to table of contents πΌ
check cpu usage, processes and RAM
$ top
if you want some more details:
$ htop
Go to table of contents πΌ
know whether your computer is under load, and whether it's due to memory or CPU
$ glances
# brew install glances
# pip install --user 'glances[all]'
Go to table of contents πΌ
poweroff or reboot your computer
This can be useful when you're patching a server that is accessed via SSH and you don't have a GUI.
# poweroff
$ sudo shutdown -h now
# reboot
$ sudo shutdown -r now
Go to table of contents πΌ
locate USB drives
$ df
Go to table of contents πΌ
unmount USB drives
$ sudo umount /dev/sdb1
Go to table of contents πΌ
format USB drives
# FAT32
$ sudo mkfs.vfat /dev/sdb1
# NTFS
$ sudo mkfs.ntfs /dev/sdb1
# exFAT
$ sudo mkfs.exfat /dev/sdb1
Go to table of contents πΌ
check USB format
$ sudo fsck /dev/sdb1
Go to table of contents πΌ
run command on all files of a directory
$ for FILE in *; do echo $FILE; done
Go to table of contents πΌ
check network connectivity to a remote address and port
$ nc -vz www.google.com 443
$ nc -vz 1.1.1.1 53
Go to table of contents πΌ
check DNS config of a domain
$ dig www.google.com
Go to table of contents πΌ
check the ownership and registration of a domain
$ whois www.google.com
Go to table of contents πΌ
Hotkeys
| Hotkey | Description |
|---|---|
| Ctrl+A | Go to the beginning of the line you are currently typing on |
| Ctrl+E | Go to the end of the line you are currently typing on |
| Ctrl+L | Clears the Screen, similar to the clear command |
| Ctrl+U | Clears the line before the cursor position. If you are at the end of the line, clears the entire line. |
| Ctrl+H | Same as backspace |
| Ctrl+R | Lets you search through previously used commands |
| Ctrl+C | Kill whatever you are running |
| Ctrl+D | Exit the current shell |
| Ctrl+Z | Puts whatever you are running into a suspended background process. fg restores it. |
| Ctrl+W | Delete the word before the cursor |
| Ctrl+K | Clear the line after the cursor |
| Ctrl+T | Swap the last two characters before the cursor |
| Ctrl+F | Move cursor forward one character |
| Ctrl+B | Move cursor backward one character |
| Esc+T | Swap the last two words before the cursor |
| Alt+T | Same as Esc + T |
| Alt+F | Move cursor forward one word on the current line |
| Alt+B | Move cursor backward one word on the current line |
| Esc+F | Same as Alt + F |
| Esc+B | Same as Alt + B |
| Alt+. | Paste the last word of the most recently command |
| Tab | Auto-complete files and directory names |
I can't remember these cryptic commands
You can always google or man the commands you are not familiar with. Or, checkout tldr, a collection of simplified and community-driven man pages.