2

I aim to make a script for mogrify. The mogrify command will resize images in a directory and put the resized images into a directory on the same directory level, with the same name as the work directory, but with a suffix (_a).

The new directory will be moved to another collection later on. Something like this,

#!/bin/bash  
mkdir ../n_a
for file in *{.JPG|.jpg}; do mogrify -path ../n_a -resize 1200x1200 -quality 96;done

I'm guessing ../ denotes the parent dir when working in a child directory, but I need help here.

Edit: "n" needs to be replaced with the syntax for the working directory name. Sorry there was a typo as well third script line, should have read n not x

Edit2: This script does exactly what I need and it's silent.

#!/bin/bash
DEST="../${PWD##*/}_a"
mkdir -p $DEST
mogrify -path $DEST -resize 1200x1200 -quality 96 *.jpg *.JPG

thanks to vgoff for the correct PWD syntax and cesareriva http://www.cesareriva.com/archives/722 for showing me the DEST function.

Something else:

${PWD##*/}_a   

is not caring for spaces in the directory name and the script fails. An empty dir is created in the same dir as the images.

Found it out now, it needs quotations on the $DEST too, presumably to help mkdir create the dir with a space in the name, and mogrify to write the files to the right place, like this

#!/bin/bash
DEST="../${PWD##*/}_a"
mkdir -p "$DEST"
mogrify -path "$DEST" -resize 1200x1200 -quality 96 *.jpg *.JPG

Edit3: Don't even need a script for this, ie

mkdir ../${PWD##*/}_a && mogrify -path ../${PWD##*/}_a -resize '1200x1200>' -quality 95 *{.jpg|.JPG}
1
  • Parameter Expansion is what I think you are looking for. It looks like this ${PWD##*/} You can echo this if you are using a bash shell to see that it indeed works.
    – vgoff
    Commented Nov 8, 2012 at 23:34

2 Answers 2

3

I'm guessing ../ denotes the parent dir when working in a child directory, but I need help here.

You have guessed correct.

"n" needs to be replaced with the syntax for the working directory name.

Aha. In that case, the pwd command will be your friend. Populate a variable with it or use the $PWD variable

7
  • yes .. is correct. but "n" needs to be the syntax to get the working directory name. Sorry I didn't make that clear.
    – mozerella
    Commented Nov 8, 2012 at 23:15
  • so how would the line look with mkdir, using pwd for the current directory?
    – mozerella
    Commented Nov 8, 2012 at 23:24
  • ${PWD##*/} should be right where your n would be, with the _a afterwards.
    – vgoff
    Commented Nov 8, 2012 at 23:36
  • ../${PWD##*/}_a yes that worked. I'm halfway there. The mogrify hasn't worked so far in putting resized images into that ../${PWD##*/}_a directory. @vgoff I would not have found that for a long time, thanks!
    – mozerella
    Commented Nov 8, 2012 at 23:42
  • Don't forget to vote up the answer if it is good. :)
    – vgoff
    Commented Nov 8, 2012 at 23:47
0

Yes. It is the parent directory.

One note is that this isn't specific to bash. It's not even shell. It's part of the way filesystems work.

The filesystem has an entry in each directory for .. and that is the parent directory. Each also has an entry for . representing the current directory.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .