I have been compiling noteworthy command line tools in a Note on my Mac, and thought it would be nice to share them. Example usage is provided for each. I continuously update this page as I find more useful commands.
Basic Movement / Text Entry
- Move cursor to beginning of line —
Ctrl + a - Move cursor to end of line —
Ctrl + e - Delete the line —
Ctrl + u
Scheduling Tasks (Linux)
Scheduled tasks (cron jobs) run at a certain recurring time. To edit them:
$ crontab -e
The format is space-delimited: minute hour day month day-of-week command. A * means every value of the field.
# Every day at 3am
0 3 * * * [command]
# April 9 at noon
0 12 9 4 * [command]
# 17:00 on the 1st and 15th of February and September
0 17 1,15 2,9 * [command]
# Every other hour
0 */2 * * * [command]
Tip: crontab.guru translates cron schedule expressions into plain English.
Symlinks
Symlinks create an alias to a local file or directory from another location.
$ ln -s /destination /link-name
To rename a symlink, use the -f (force) flag to remove the existing link before creating the new one.
Aliases
Add frequently-used commands as aliases in ~/.bashrc:
alias [alias name]="[command]"
Then reload:
$ source ~/.bashrc
Some useful aliases:
alias ins="sudo apt-get install"
alias h="history"
alias c="clear"
alias grep="grep --color -n"
alias df="df -h"
alias du="du -sh"
alias sc="source $HOME/.bashrc"
To run a command without its alias, prepend \:
$ \df
Install .deb Packages
$ sudo dpkg -i [package.deb]
Git — Rewrite Commit Author
To fix commits made with the wrong email address across history:
$ git filter-branch --env-filter 'if [ "$GIT_AUTHOR_EMAIL" = "old@email.com" ]; then
GIT_AUTHOR_EMAIL=new@email.com;
GIT_AUTHOR_NAME="Your Name";
GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL;
GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"; fi' -- --all
Then force-push:
$ git push --force
Transfer Files via SCP
# Download
$ scp user@host:/remote/file /local/file
# Upload a file
$ scp /local/file user@host:/remote/file
# Upload a directory
$ scp -r /local/directory user@host:/remote/directory
SSH Tunneling
Access an application on a remote port through an SSH tunnel:
$ ssh -N [user]@[IP] -L [local port]:[local address]:[remote port]
Example — access port 1234 on my server via localhost:4321:
$ ssh -N clrung@christopherrung.com -L 4321:localhost:1234
Show 10 Largest Files in a Directory
$ du -h * | sort -hr | head
Batch Rename Files
$ rename s/"IMG_"/"Ski Trip "/ *
Delete Files Older Than N Days
$ find /path/to/directory/* -mtime +N -exec rm {} \;
Re-run Previous Command as Root
!! is an alias for the last command you ran:
$ sudo !!
List Disk Names and Mount Points
$ lsblk
...
sdf 8:80 0 698.7G 0 disk
└─sdf1 8:81 0 698.7G 0 part /media/timemachine
...
Long-Running Commands with screen
The screen command lets you detach from a long-running process and reattach later.
# Start a screen session
$ screen
# Detach: Ctrl-A then d
# Reattach
$ screen -r
# List open screens
$ screen -ls
# Reattach to a specific screen
$ screen -r 5037
Check if Directory Contains Files
#!/bin/sh
if find /path/to/directory -mindepth 1 -print -quit | grep -q .; then
echo "not empty"
else
echo "empty"
fi
Determine Which Process is Using a Port
$ lsof -i tcp:[port]
# Example
$ lsof -i tcp:8000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 2965 chris 5u IPv4 ... 0t0 TCP *:irdmi (LISTEN)
Get WAN IP via Terminal
$ curl -s http://whatismyip.akamai.com/
That's it for now! Please let me know if you have any questions or corrections.