Search This Blog

Friday, August 14, 2020

Why do experts always recommend using the command line and not the GUI (Win/Linux)?

Let me show you a shell command that earned me $500 in less than 5 minutes:

    • $ find pictures/ -type f -iname '*.jpg' | xargs -I '{}' convert '{}' -resize 150x150 -quality 100 '{}'.png 

This single command line created thumbnails of the more that 20,000 images a photographer had in his portfolio. He already had exported them manually to JPG, only to find out that he actually needed PNGs of 150px max in each dimension.

Oh, and I earned a beer for

    • $ find pictures/ -type f -iname '*.jpg' -exec rm {} \; 

Eat this, GUI! ;)

EDIT:

As per request of Abdel Nabut, let's break down these commands:

  1. find[1] is a command that searches for files and directories, optionally matching certain criteria.
  2. pictures/ tells find where to start its search.
  3. -type f is the first search criterion: it tells find to only look for files
  4. -iname '*.jpg' tells find the second search criterion: do a case insensitive search for all files ending with`.jpg`. The case insensitivity causes this criterion to also match `.JPG` or `.jPg`
  5. The |, called the pipe symbol, causes the shell to pipe the out put of the command on its left hand side to be used as the input of the command of the right hand side.
  6. xargs [2] executes the command given as it's arguments to be executed for each line of input.
  7. -I '{}' instructs xargs to replace every occurrence of '{}' in the arguments that follow with the content of the current line of the input.
  8. convert[3] is part of the ImageMagick text manipulation package.
  9. '{}' will be replaced by the content of the current line of the input. Say, the first file `find` found is `pictures/aaa_first.jpg` the effective command for this iteration of xargs would read convert pictures/aaa_first.jpg ...
  10. -resize 150x150 instructs convert to change the size of the image at hand to a maximum size pf 150x150, while maintaining the aspect ratio. So, for example if the original picture was 300 x 200, the resulting picture would be 150x100. If the original size was 150x300, the dimensions of the converted image would be 75x150.
  11. -quality 100 instructs convert to generate with the best quality it can achieve. Sometimes, you do not need (or even want) full quality images, for example for previews. In this case, you could set -quality 80.
  12. '{}'.png tells convert where to save the output to. In this example, to pictures/aaa_first.jpg.png As convert is rather smart and detects the image type of the original JPG, it knows that it is supposed to write the output file in PNG format.

Footnotes


Regards, LongNX

No comments:

Post a Comment

PHÂN BIỆT QUẢN TRỊ VÀ QUẢN LÝ

PHÂN BIỆT QUẢN TRỊ VÀ QUẢN LÝ Hội đồng quản trị, tiếng Anh là BOD (Board Of Directors). Còn Ban giám đốc hay Ban quản lý tiếng Anh là BOM (B...