Favorite One Liners

22 October 2017

Everyone who uses a linux terminal at home, sees themselves automating tasks every now and then. Especially if you have photos to convert, if you have a website with tons of files to upload, if you need to mass-update several files or even to backup files into a remote server. All you don't want is to do this work manually. Of course, there are apps you can use for each of the actions below. I prefer to do it on a command line.

Here I'm showing some useful linux one-liners in order to automate my most frequent tasks. Please note that the syntax among different variants of Linux may vary.

Upload changed files

When you have a website, you don't want to upload all of your files every time you make one change. So with this command, you can find the files with a certain extension, which have ben updated in the last X mins and upload them.

root@$ find . -cmin -60 -name "*php" -type f -exec curl -u "USERNAME":"PASSWORD" --ftp-create-dirs -T {} "YOURSERVER"/"YOURPATH"/{} \;

Compressing pictures

Of course you can't use your full JPGs on your website. In my case I had the original pictures in 20 different directories, each pictures with 20MB. The ideal size for the picture would be around 200KB. With this command you can compress your pictures to have a width of 1024 pixels and in the end the size should be ideal.

root@$ sips -Z 1024 *.jpg

Converting from RAW to DNG

If you use RAW Format you might run into trouble, if in a future time, your raw format is not supported anymore. Therefore I convert my photos to a standard format DNG.

root@$ find . -name "*CR2" -exec open -a /PATH/Adobe\ DNG\ Converter.app/Contents/MacOS/Adobe\ DNG\ Converter --args -u {} \;

Correcting Typos in all of your files

If you use a certain file as a template and this file has a typo, according to Murphy's law, you'll only find out after it's too late. With the command below, you can correct all your files in one go.

root@$ find . -name "*.php" -exec sed -i '' 's/herf/href/' {} \;

SED, AWK and GREP are very powerful tools for text processing. If you wish to see more examples on these tools, there are pages with thousands of examples online: SED Examples, AWK Examples, GREP Examples

Running a backup with Rsync

If you happen to have a remote storage and you have no idea what to do with it, you can use it for your backup. In order to synchronize your local folder on a remote server you can use the command below. This command will only upload or synchronize files up to 20 MB. It's handy to exclude large files if you have a lame connection.

root@$ rsync -rltD -v --stats --progress --max-size=20MB -e "ssh -i id_rsa" $SOURCE "USER"@"URL":/"DESTINATION"/

You can't simply cover everything you can do on a command line in an article. Hopefully, these commands will give you an idea of how to automate some of your daily tasks.