Bash One-Liners
This is a collection of bash one-liners (or, small multi-liners) that I have found useful at one time or another. I will use this as my one-stop reference and update it as others occur to me.
Bash process control
Keep downloading a file
I used this recently to download a large file that would abruptly halt over an unstable connection. curl -C
continues the download, where the server supports this. The exit code of curl
when my download failed was consistently 18, so I just kept continuing the download until it wouldn’t return with 18. As you can see, the real trick is to continually export the exit code for evaluation in the loop.
export ec=18;
while [ $ec -eq 18 ];
do curl -O -C - "https://ftp.fau.de/kiwix/zim/wikipedia/wikipedia_en_all_maxi_2018-10.zim";
export ec=$?;
done
The watch
command
For when you want to watch the results to a mysql
command. I use this to watch the temperate downstairs, which gets logged every 1 minute or so into a database.
$ watch mysql -t -h <hostname> -u <username> -p<password> <dbname> -e \"select \* from temperatures where location = \'downstairs\' order by timestamp desc limit 20\;\"
Docker
Finding where the big log files are
Sometimes I forget the requirement to configure logging in Docker’s daemon.json
file before I create a docker container on a new host. This means that the log file will be written to endlessly, until I realise the mistake.
To see the log file sizes of running containers, a nice one-liner is:
$ sudo du -h $(docker inspect --format='' $(docker ps -qa))