Note that there are some explanatory texts on larger screens.

plurals
  1. POFilter Hidden Files with Bash (for Batch Image Resize Script)
    text
    copied!<p>I'm writing a script to batch resize images. Originally I was applying an operation <code>for file in $(ls $1)</code>, but I would like to be able to use globbing, so I'm looking at something more like <code>for file in $(echo $1)</code>. The problem is that dotglob may or may not be enabled, so <code>echo *</code> could return hidden files (notably, .DS_Store), which cause <code>convert</code> to throw an error and stop the script. <strong>I would like the default behavior of the command to be</strong> that if I <code>cd</code> into a directory full of images and execute <code>resize * 400x400 jpg</code>, all of the images will be resized <i>excluding</i> hidden files, regardless of whether dotglob is enabled.</p> <p>So, in pseudo code, <strong>I'm looking for</strong>:</p> <pre><code>for file in $(echo $1 | [filter-hidden-files]) </code></pre> <p>Here is my script with the <strong>older behavior</strong>. Will update with new behavior when I find a solution:</p> <pre><code># !/bin/bash # resize [folder] [sizeXxsizeY] [outputformat] # if [outputformat] is omitted, the input file format is assumed for file in $(ls $1) do IMGNAME=$(echo "$file" | cut -d'.' -f1) if test -z $3 then EXTENSION=$(echo "$file" | cut -d'.' -f2) convert $1/$file -resize $2 -quality 100 $1/$IMGNAME-$2.$EXTENSION echo "$file =&gt; $IMGNAME-$2.$EXTENSION" else convert $1/$file -resize $2 -quality 100 $1/$IMGNAME-$2.$3 echo "$file =&gt; $IMGNAME-$2.$3" fi done </code></pre> <p>Here is the <strong>current</strong> script:</p> <pre><code># !/bin/bash # resize [pattern] [sizeXxsizeY] [outputformat] # if [outputformat] is omitted, the input file format is assumed for file in $(echo $1) do IMGNAME=$(echo "$file" | cut -d'.' -f1) if test -z $3 &amp;&amp; if test -f $3 then EXTENSION=$(echo "$file" | cut -d'.' -f2) convert $file -resize $2 -quality 100 $IMGNAME-$2.$EXTENSION echo "$file =&gt; $IMGNAME-$2.$EXTENSION" else convert $file -resize $2 -quality 100 $IMGNAME-$2.$3 echo "$file =&gt; $IMGNAME-$2.$3" fi done </code></pre> <p>Given the command <code>resize * 400x400</code>, convert throws an error as it cannot process .DS_Store (a hidden file residing in every file on an OSX system). As I will never be processing hidden images, I would like to automatically filter them. I've been trying to do this with grep or find, but I haven't figured it out yet. </p> <p>New script goes here:</p> <pre><code>for file in $(echo $1) do </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload