Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In Linux, preferably using bash, what would be the best way to achieve the goal below?

Say I have a series of numbered files

001.png, 002.png, 003.png

What is the best way to move/rename them to be in the reverse order?

Bonus points: I have a series of 1500 files, I would actually like to reverse the order of ever second block of 40. Note the letters are not part of the name. I use them to indicate the uniqueness of the content.

001a, 002b, ..., 040c <- leave as is
041a, 042b, ..., 080c <- reverse order of these files
081a, 082b, ..., 120c <- leave as is
121a, 122b, ..., 160a <- reverse order of these files


001a, 002b, ..., 040c
041c, 042b, ..., 080a
081a, 082b, ..., 120c
121c, 122b, ..., 160a
share|improve this question

The "bonus" portion (acting on just the even numbered lines) is a duplicate, with one set of answers here:

share|improve this answer
This is not a full answer to the question. It just explains how to act on every even or odd line, but not how to perform the renaming. Also, it'd be preferred if you put the actual answer here, and not just link to another SE site. Thank you. – slhck Aug 12 '13 at 8:58
Sorry ... I'd assumed the OP had his files listed out in such a way that any output from the solutions posted at that SE question could be fed into the "reverse" procedure, such as you wrote below. (Instead of images=(*.png), something such as while read ; do images=$REPLY; .... done would operate on the "every other line"). Closer inspection shows that the filelist probably doesn't exist in a text file, so rather than building the file solely for the purpose of skipping lines, it's probably better to build a list of 1-40, 81-120, etc for this specific case. – Kent Aug 12 '13 at 23:51

Assuming the files are named sequentially without any missing pieces, and don't use anything else except the numbers before the extension, this is in pure Bash (since 3.0).

Run this from the directory the PNG images are in.

# create a temporary directory
mkdir -p ./tmp

# create an array of images and find maximum number

# loop through array keys and subtract the key from maximum number to reverse
for i in "${!images[@]}"; do 
  # rename to the temporary directory, with three-digit zero padding
  mv -- "${images[$i]}" ./tmp/$(printf "%03d.png" $(($max - i)))

# move files back and remove temporary directory
mv ./tmp/*.png .
rmdir ./tmp
share|improve this answer

You must log in to answer this question.

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