{"id":22,"date":"2007-11-12T16:55:16","date_gmt":"2007-11-12T15:55:16","guid":{"rendered":"\/Blog\/?p=22"},"modified":"2011-03-15T13:54:38","modified_gmt":"2011-03-15T12:54:38","slug":"useful-shell-oneliners","status":"publish","type":"post","link":"https:\/\/seegras.discordia.ch\/Blog\/useful-shell-oneliners\/","title":{"rendered":"Useful Shell Oneliners"},"content":{"rendered":"<p>From time to time you come upon some useful oneliner in the shell. Either because you see it somewhere, see someone doing it, or because you need it and produce one yourself. And most of the time, it&#8217;s not enough to put into a shell-script, so you find yourself hunting for it in your .bash_history. Well, here are some: <\/p>\n<p><strong>Do something to a lot of files<\/strong><br \/>\n<code>for i in *; do command $i; done<\/code><\/p>\n<p>You&#8217;ll need this a lot. Does not work with files containing spaces <\/p>\n<p><strong>Convert a load of images<\/strong><br \/>\n<code>for i in *.tif; do convert -quality 75 $i `basename $i .tif`.jpeg;<\/code><\/p>\n<p><strong>Make pdf out of a several images<\/strong><br \/>\n<code>convert -limit memory 32 -limit map 32 *.png target.pdf<\/code><\/p>\n<p>You really want to set the limits, unless you have more RAM than the whole images converted to pbm.<\/p>\n<p>With Graphicsmagick, you need to set a compression for jpg, otherwise the resulting PDF will be huge.<\/p>\n<p><code>gm convert -compress JPEG *.jpg target.pdf<\/code><\/p>\n<p>The resources you want to give those programs can be set using environment-variables: <\/p>\n<p><code>export MAGICK_MEMORY_LIMIT=\"640mb\"<br \/>\nexport MAGICK_MAP_LIMIT=\"320mb\"<br \/>\nexport MAGICK_AREA_LIMIT=\"640mb\"<\/code><\/p>\n<p><strong>Make pdf out of <em>a lot<\/em> of images<\/strong><br \/>\nBut the whole idea of using ImageMagick or GraphicsMagick to create pdf-files is very much flawed, since both first convert the jpeg-files into an raw, uncompressed intermediate format which tends to eat up all available memory, and then crash. And which is completely nonsensical too, since jpeg-files can be embedded into pdf as they are. Luckily, pdfjoin will help us, tough it accepts only files with the extension &#8216;.pdf&#8217; as input as of yet: <\/p>\n<p><code>mmv '*.jpg' '#1.pdf'; pdfjoin --outfile target.pdf *.pdf<\/code><\/p>\n<p><strong>Change resolution (DPI) of an image<\/strong><br \/>\n<code>convert -density 600 -units PixelsPerInch source.png target.png<\/code><\/p>\n<p><strong>split\/crop pictures<\/strong><br \/>\n<code>convert -crop 1230x880 source.png target<\/code><\/p>\n<p>Note that this does not really crop the image, but splits it into as many parts as necessary, using &#8220;target&#8221; as prefix, adding a number as suffix. <\/p>\n<p><strong>fix offset<\/strong><br \/>\n<code>convert +repage source.png target<\/code><\/p>\n<p>Lately, &#8220;crop&#8221; seems to behave differently, it leaves all but the first part with an offset. This makes the picture disappear in gimp, and stretched  if you make a pdf out of it. That&#8217;s why you may need to repage. <\/p>\n<p><strong>rotate<\/strong><br \/>\n<code>convert -rotate 90 source.png target<\/code><\/p>\n<p>rotates 90 degrees clockwise. <\/p>\n<p><strong>Attach pictures to each other, one below the other<\/strong><br \/>\n<code>montage +frame +shadow +label -tile 1x2 -geometry \"widthxheight+0+0\" source1.png source2.png target.png<\/code><\/p>\n<p>Important is that width and height are the dimensions of each of the sources. For putting them next to each other, just use 2&#215;1 as tile. <\/p>\n<p><strong>Rename files according a textfile-list<\/strong><br \/>\n<code>for i in `cat list` ; do mv `echo $i | awk -F\"-\" '{print $1\".ext\"}'` $i.ext ; done<\/code><\/p>\n<p><strong>Delete empty directories<\/strong><br \/>\n<code>find . -type d -empty -depth -exec rmdir {} \\;<\/code><\/p>\n<p><strong>Remove first page of a PDF<\/strong><br \/>\n<code>pdftk A=source.pdf  cat A2-end output target.pdf<\/code><\/p>\n<p>Yes, pdftks syntax is a bit alien. <\/p>\n<p><strong>Merge PDFs<\/strong><br \/>\n<code>pdftk sources*.pdf cat output target.pdf<\/code><\/p>\n<p><strong>Unpack lots of packed files into different directories<\/strong><br \/>\n<code>for i in *.rar; do mkdir `basename $i .rar`; mv $i  `basename $i .rar`; cd `basename $i .rar`; rar x $i; cd ..; done <\/code><\/p>\n<p>If you have a load of packed files, in this case rar, and you want to unpack each into a different subdirectory, according to the name of the rar-file. <\/p>\n<p><strong>Get rid of magnatune-advertisements in mp3s<\/strong><\/p>\n<p><code>for i in *.mp3; do mp3splt -s -p min=2 -d `basename $i .mp3` $i ; done<\/code><\/p>\n<p><strong>Merge AVI-movies<\/strong><br \/>\n<code>mencoder -forceidx -oac copy -ovc copy -o outputfile.avi parts*.avi<\/code><\/p>\n<p>That&#8217;s the basic way. If you want to process a whole directory with avi-files whose filenames have a distinct part (like &#8220;cd&lg;num&gt;&#8221; or &#8220;-Part&lg;num&gt;&#8221; as in my example) you can do something this:<\/p>\n<p><code>for i in *-Part1.avi; do mencoder -forceidx -ovc copy -oac copy -o `basename $i -Part1.avi`.avi `basename $i -Part1.avi`-Part*.avi; done<\/code><\/p>\n<p>To be continued. You might also want to check out my <a href=\"http:\/\/seegras.discordia.ch\/Programs\/\">Program-section<\/a> for small useful scripts. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>From time to time you come upon some useful oneliner in the shell. Either because you see it somewhere, see someone doing it, or because you need it and produce one yourself. And most of the time, it&#8217;s not enough to put into a shell-script, so you find yourself hunting for it in your .bash_history. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,93,3],"tags":[16,21,19,18,15,17,22,20,14,13],"class_list":["post-22","post","type-post","status-publish","format-standard","hentry","category-computers","category-terminal","category-webdesign","tag-bash","tag-convert","tag-graphicsmagick","tag-imagemagick","tag-oneliners","tag-pdf","tag-pdftk","tag-rename","tag-shell","tag-unix"],"_links":{"self":[{"href":"https:\/\/seegras.discordia.ch\/Blog\/wp-json\/wp\/v2\/posts\/22","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/seegras.discordia.ch\/Blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/seegras.discordia.ch\/Blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/seegras.discordia.ch\/Blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/seegras.discordia.ch\/Blog\/wp-json\/wp\/v2\/comments?post=22"}],"version-history":[{"count":13,"href":"https:\/\/seegras.discordia.ch\/Blog\/wp-json\/wp\/v2\/posts\/22\/revisions"}],"predecessor-version":[{"id":361,"href":"https:\/\/seegras.discordia.ch\/Blog\/wp-json\/wp\/v2\/posts\/22\/revisions\/361"}],"wp:attachment":[{"href":"https:\/\/seegras.discordia.ch\/Blog\/wp-json\/wp\/v2\/media?parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seegras.discordia.ch\/Blog\/wp-json\/wp\/v2\/categories?post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seegras.discordia.ch\/Blog\/wp-json\/wp\/v2\/tags?post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}