2

If have the following problem: I have a series of files that come in pairs (but not always). There is 2400??????_001.jpg and 2400??????_002.jpg. I need to swap the _001 and _002. So I thought I could do this:

for f in $(find -type f -name "*_002.jpg"); do mv "${f}" "${f%_002.jpg}_003.jpg"; done
for g in $(find -type f -name "*_001.jpg"); do mv "${g}" "${g%_001.jpg}_002.jpg"; done
for h in $(find -type f -name "*_003.jpg"); do mv "${h}" "${h%_003.jpg}_001.jpg"; done

Strangely after step 2 I have *_003.jpg and *_002.jpg that are identical. What is going on here?

Then the problem gets a little more difficult: I only want to swap if both members of the pair exist. Sometimes only 2400??????_001.jpg exists and 2400??????_002.jpg is missing. If this is the case then I want to leave 2400??????_001.jpg alone.

dexter
  • 141
  • 4

1 Answers1

0

Try this in a bash shell:

find -type f -name "*_001.jpg" -print0 | while read -d $'\0' f; do
  t=$(mktemp --tmpdir=$(dirname "$f"))
  [ -f "${f%_001.jpg}_002.jpg" ] && \
  mv -v "$f" "$t" && mv -v "${f%_001.jpg}_002.jpg" "$f" && mv -v "$t" "${f%_001.jpg}_002.jpg"
done

As a script it whould look like this (a bit more readable):

#!/bin/bash
find -type f -name "*_001.jpg" -print0 | while read -d $'\0' f; do
  t=$(mktemp --tmpdir=$(dirname "$f"))
  [ -f "${f%_001.jpg}_002.jpg" ] && \
    mv -v "$f" "$t" && \
    mv -v "${f%_001.jpg}_002.jpg" "$f" && \
    mv -v "$t" "${f%_001.jpg}_002.jpg"
done

Explanation:

  • First we create a list of files with find. To process strange filenames use -print0 to delimit files with the nullbyte
  • while read -d $'\0' f: Read the output of find delimited by the nullbyte
  • Create a tempfile
  • Check if both files exist
  • Move the original (001) to the tempfile
  • Move 002 to 001
  • Move the tempfile to 002
chaos
  • 4,204
  • 2
  • 19
  • 28
  • When I put this script in a .sh so I can use in on the command line, I get 6: read: Illegal option -d. When I copy the script directly to the command line, it is executed, but strangely I do not see any changes. Except for the creation of temporaries. Then on the line [ -f "${f}" ] && [ -f "${f%_001.jpg}_002.jpg" ] I guess you are checking if both exist. But you would not need to check existence of the first would you? – dexter Jul 31 '15 at 09:42
  • @dexter What shell are you using? What is the version of bash? `bash --version` – chaos Jul 31 '15 at 09:43
  • @dexter In a script you have to use `#!/bin/bash` as first line – chaos Jul 31 '15 at 09:44
  • I am trying @chaos ... – dexter Jul 31 '15 at 09:47
  • GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu). So I first copied all in iswp.sh file and then: sh iswp.sh. – dexter Jul 31 '15 at 09:48
  • @dexter I edited my answer, and you're right; the first `[ -f "${f}" ]` is not needed of course. – chaos Jul 31 '15 at 09:54
  • I really think the script should work, but, for some reason on my system it doesn't. It does not change anything except for the creation of 0 bytes length temporaries... – dexter Jul 31 '15 at 10:43
  • No changes are visible, but it prints out this, which seems to be what is to be expected: ‘./2400A00/2400A00118_001.jpg’ -> ‘./2400A00/tmp.iSUZcuMKEM’ ‘./2400A00/2400A00118_002.jpg’ -> ‘./2400A00/2400A00118_001.jpg’ ‘./2400A00/tmp.iSUZcuMKEM’ -> ‘./2400A00/2400A00118_002.jpg’ BTW: at my system the find | while combination did not work. I had to change it to: for f in $(find -type f -name "*_001.jpg"); do – dexter Jul 31 '15 at 11:03
  • @dexter What os is it? – chaos Jul 31 '15 at 11:12
  • Is it Ubuntu 14.04. But I am starting to think too many strange things happen on my machine lately. Very slow access to simple text files as well (240 bytes) takes more than 20 s. to open.... Think the controllers maybe crap. – dexter Jul 31 '15 at 11:32
  • @dexter I tested the above in an ubuntu 12.04 and it worked. As you mentioned in the comment the `mv` command prints the paths correctly, so the file get moved. Maybe, if you work with the gui filebrowser, the directory is not refreshed. – chaos Jul 31 '15 at 11:37
  • I am sure it does work, it looks good and makes sense. Have to find out what's wrong here. Even reboot wont help. – dexter Jul 31 '15 at 13:06