1

I have a bash script set that executes in a folder containing a number of photos and sequentially stitches them into panoramas based on the user input. Once the generation is completed, the output file is renamed to $x-pano.jpg and moved one folder higher.

My issue is the number prefix is based on the sequential execution of the script, meaning all files get renamed 1-pano.jpg to n-pano.jpg based on the number of panoramas generated during the script execution.

How can I modify the renaming process to look at the storage folder and get the largest $x? I want to increment that number by 1 and use as the file's numerical prefix. My current code is

//get the list of files in directory and sort in increasing order
$filelist=$(find ../ -maxdepth 1 -type f | sort -n)

//get number of files
$length=${filelist[@]}

//get the last file
$lastFile=${fileList[$((length-1))]}

will get a list of the files, sort in increasing order and get the last file from the list. This is where I get stuck. Using - as a delimiter, how can I capture the current value?

1
  • No dollar signs on the left side of an assignment. Add a set of parentheses to create an array: filelist=($(find ../ -maxdepth 1 -type f | sort -n)). To get the count: length=${#filelist[@]}. To get the last file: lastFile=${fileList[@]: -1} (note that an array subscript is already an arithmetic context so no $(()) is necessary, but it's not until Bash 4.2 that you can use negative subscripts so you have to use a negative slice in earlier versions). Commented Jun 21, 2012 at 4:28

2 Answers 2

1

You're probably looking for the ${var%%foo} construct, which strips the longest foo from the end of a variable:

$ F=1-pano.jpg
$ echo ${F%%-pano.jpg}
1
$ F=1222-pano.jpg
$ echo ${F%%-pano.jpg}
1222

Try lastNum=${lastFile%%-pano.jpg}.

Sign up to request clarification or add additional context in comments.

1 Comment

I ended up going in a different direction, but your answer helped a great dea.
1

Assuming this is just a renaming task:

$ find jason-pics/ -type f
jason-pics/other.jpg
jason-pics/somefile.jpg
jason-pics/mountain.jpg
jason-pics/car.jpg

Renaming:

i=0 
for jfile in $(find jason-pics/ -type f)
    do 
        echo "Renaming $jfile to jason-pics-rename/$i-pano-$(basename $jfile)" 
        ((i+=1)) 
        cp -v $jfile jason-pics-rename/$i-pano-$(basename $jfile)
done

Content of jason-pics-rename directory:

$ find jason-pics-rename/ -type f 
jason-pics-rename/4-pano-car.jpg
jason-pics-rename/1-pano-other.jpg
jason-pics-rename/3-pano-mountain.jpg
jason-pics-rename/2-pano-somefile.jpg

Removing basename to rename files to 1-pano.jpg to n-pano.jpg

$ i=0; for jfile in $(find jason-pics/ -type f); do echo "Renaming $jfile to jason-pics-rename/$i-pano-$(basename $jfile)" ;((i+=1)); cp -v $jfile jason-pics-rename/$i-pano.jpg; done

Content is:

$ find jason-pics-rename/ -type f 
jason-pics-rename/3-pano.jpg
jason-pics-rename/2-pano.jpg
jason-pics-rename/1-pano.jpg
jason-pics-rename/4-pano.jpg

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.