Useful CLI Snippets
Here are some ad hoc snippets that you might find useful or tweak them to fit your needs or take inspiration to write some for your own needs.
scp a folder
For eg. To deploy your local build artifact on your VM.
scp some/path/to/local/file remotehostname:/destination/on/remote/box
Here’s a helpful post from a quick search - Linuxize Post on scp with more explanation and flags. You can even copy from one remote host to another remote one it seems.
I wrote
remotehostnameas you can leverage your alias for the host if you’ve set up your~/.ssh/configcorrectly.
Do a series of subcommands
You can use this to do any commands actually but I group them with related subcommands for a command.
Suppose you have a command / script to manage your deployed apps, let’s say deploy.sh, then the following structure is useful.
for sub in <commands list separated by spaces>; do task sub; done
An example - You need to do a clean start of your local build of the service.
for subcommand in stop clean remove; do deploy.sh $subcommand <servicename>; done
This will run
deploy.sh stop
deploy.sh clean
deploy.sh remove
letting you have a clean slate for your next build.
Wait for a service to start up by monitoring the logs automatically
After scp’ing over your new build, you will likely start the service and wait for some indication of when the service is up.
Let’s say you have a log file that the service writes to when it starts up and has the word APP-READY in the log when it has started.
We can monitor this continuously and only let go with the output APP-READY when it has started.
while ! grep "APP-READY" startup.log; do echo "starting..."; sleep 1; done
Instead if you have some command that changes its output when the service becomes ready (for eg. some status command), monitor that instead with grep.
rsync
Some useful flags to rsync.
rsync --archive --human-readable --progress --partial <srcfile> <dest>;
Some quick look at the man page and a prompt to Claude later, here’s an explanation of the above flags –
--archivemakes sure that the permissions, timestamps, symbolic links and other metadata are preserved as it is giving an exact copy.--human-readablehelps displaying file sizes in KB, MB, GB instead of bytes.--progressshows a progress bar for the operation.--partialkeeps partially backed up files if its interrupted mid transfer so thatrsynccan pick up from that point onwards later.
I used the long form so that later the full meaning of the command is clearer. Lot of people use the flags as -ahP which are the shorthands for the above 4 options.