LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   mass renaming script (https://www.linuxquestions.org/questions/programming-9/mass-renaming-script-604569/)

frieza 12-04-2007 09:25 PM

mass renaming script
 
can anyone help me with either a simple shell script, c, or c++ program to take files from a digital camera labeled (IMGXXXX.JPG) and reverse the order of how they are numbered?

chrism01 12-04-2007 10:49 PM

Well, one way is to create a list of your files in order eg
ls -1 |sort >sort.dat
then a list in reverse
ls -1|sort -r>rsort.dat
then a script eg in shell to open each file and read through both files, renaming fileN in sort.dat to fileN in rsort.dat.

ghostdog74 12-05-2007 02:51 AM

Code:

ls IMG*.jpg | awk '
{
  files[++c] = $0
}
{ d=c }
END {
  for (i=1;i<=c;i++) {
    revfile = files[d--]
    if ( revfile == files[i] ) exit
    # this part do the swapping
    cmdtemp = "cp " files[i] " temp "   
    mvcmd = "mv " revfile " "files[i]
    mvback = "mv temp " revfile
    system(cmdtemp)
    system(mvcmd)
    system(mvback)
  }
}'


bigearsbilly 12-05-2007 04:16 AM

perl?

notice, this only prints the command, doesn't execute it.
do it like this...
perl_script.pl > output

then you can review if it's what you want and then simply go...
sh output
to execute.

I use this technique a lot, as:
1. you can check it first.
2. you have a record of what has been done.

Code:

#!/usr/bin/perl

@ARGV = <IMG*>;

$count = "0000" . ""; # force to string, so we don't lose the zeroes
foreach $_ (reverse @ARGV) {

    $file = $_;
    $count++;
    $file =~ s/\d.*//g;  # remove from digits to the end

    print "mv $_ ",  $file . $count . ".jpg\n";
}



4265686f6c64212074686520476e6f6d652065617465720a

radoulov 12-05-2007 04:17 AM

Code:

bash <(paste <(printf "cp %s\n" IMG*.JPG|sort -nk4,7) <(printf "_%s\n" $(ls IMG*.JPG|sort -rnk4,7)))
for f in _IMG*.JPG;do mv "$f" "${f#_}";done


jschiwal 12-05-2007 04:23 AM

Code:

files=(IMG*)
for (( n=0; n<${#files[@]}; n++ )); do
    mv ${files[n]} ${files[${#files[@]}-$n-1]}.tmp;
done
rename .tmp '' *.tmp

files=(IMG*) puts the filenames in an array. The size of the array is ${#files[@]}. The last index is ${#files[@]} -1 because the array starts with 0.

radoulov 12-05-2007 05:09 AM

With zsh:

Code:

set -- IMG<->.JPG(On)                                     
max=${1//[^0-9]}                                         
autoload -U zmv
zmv -fQ 'IMG(<->).JPG(n)' '_IMG${(l:4::0:)$((max--))}.JPG'
zmv '_IMG(*).JPG' 'IMG$1.JPG'


radoulov 12-05-2007 05:26 AM

Quote:

Originally Posted by jschiwal (Post 2980777)
Code:

files=(IMG*)
for (( n=0; n<${#files[@]}; n++ )); do
    mv ${files[n]} ${files[${#files[@]}-$n-1]}.tmp;
done
rename .tmp '' *.tmp

files=(IMG*) puts the filenames in an array. The size of the array is ${#files[@]}. The last index is ${#files[@]} -1 because the array starts with 0.

Right,
given the filename format
the sort is superfluous :)

bigearsbilly 12-05-2007 07:11 AM

i think we all agree,

mine is the best solution ;)

ghostdog74 12-05-2007 07:30 AM

Quote:

Originally Posted by bigearsbilly (Post 2980887)
i think we all agree,

mine is the best solution ;)

might be, might be not :). The "best solution" ( note: double quotes ) is what the OP choose to be, to him. Not to us.

PAix 12-05-2007 08:14 AM

Yes, neat . . . and said with such confidence too.

You just know that hex string will bug me until I've found the perl to translate it don't you! Hopefully others too.

bigearsbilly 12-05-2007 09:41 AM

ho ho!
you started it,

here's a clue


hex_read.pl
Code:

#!/usr/bin/perl -n

tr/0-9a-fA-F//cd;
print pack "H*" , $_;

hex_write.pl
Code:

#!/usr/perl -ns
BEGIN {
    warn "-chomp to ignore new line characters\n" unless $chomp;
    warn "-neat for nice easier spacing\
 (hint: finish off with 'fmt' command)\n" unless $neat;
 warn "\n";

}
chomp if $chomp;
@L =  unpack("H2" x length($_), $_) ;
print $neat? "@L\n":@L;

binary_read.pl
Code:

#!/bin/perl -n

tr/01//cd;
print pack "B*" , $_;

binary_write.pl
Code:

#!/usr/bin/perl -ns
BEGIN {
    warn "-chomp to ignore new line characters\n" unless $chomp;
    warn "-neat for nice easier spacing\
 (hint: finish off with 'fmt' command)\n" unless $neat;
}
chomp if $chomp;
@L = unpack "B8" x length($_), $_ ;

print $neat? "@L\n":@L;


PAix 12-05-2007 11:27 AM

4b65657020656174696e672074686520666f6f640a666f72204920616d206265686f6c64656e2e0a0a

536c6f7720616e64207374656164792c20746f646179206973205065726c2064617920312e0a

More than enough Perl features in your scripts to get me started. Time to dig out my books and use the resources around me. Thanks. :)

frieza 12-05-2007 03:00 PM

thx.. i used the first one, tho i havnt tried to see how the other ones work, ;)

chrism01 12-05-2007 04:46 PM

Hey BB, I'm not sure
@ARGV = <IMG*>;
guarantees the list will be in order... Also, you need a separate output dir, otherwise when file eg A1 moves to eg A4, you've lost A4 before you can move it to A1 (assume basic list is A1, A2, A3, A4 etc)

jschiwal 12-06-2007 04:44 AM

One way to read those strings.

Code:

sed 's/\(..\)/\\x\1/g;s/.*/"&"/' | xargs echo -e

/bin/bash 12-06-2007 06:34 AM

If you like your code un-obfuscated and easy to understand: :P

Code:

#!/bin/bash
ARRAY=( $(ls -1 *.jpg) )
FIRST=0
LAST=$(( ${#ARRAY[@]} -1 ))
until [ "$FIRST" -ge "$LAST" ];do
  mv ${ARRAY[$FIRST]} ${ARRAY[$LAST]}.tmp
  mv ${ARRAY[$LAST]} ${ARRAY[$FIRST]}.tmp
  LAST=$(( --LAST ))
  FIRST=$(( ++FIRST ))
done
rename .tmp '' *.tmp


jschiwal 12-06-2007 06:59 AM

Quote:

Originally Posted by /bin/bash (Post 2981956)
If you like your code un-obfuscated and easy to understand: :P

Code:

#!/bin/bash
ARRAY=( $(ls -1 *.jpg) )
FIRST=0
LAST=$(( ${#ARRAY[@]} -1 ))
until [ "$FIRST" -ge "$LAST" ];do
  mv ${ARRAY[$FIRST]} ${ARRAY[$LAST]}.tmp
  mv ${ARRAY[$LAST]} ${ARRAY[$FIRST]}.tmp
  LAST=$(( --LAST ))
  FIRST=$(( ++FIRST ))
done
rename .tmp '' *.tmp


Code:

ARRAY=( $(ls -1 *.jpg) )
The ls isn't needed as fileglobbing will do it. ( and you are using file globbing anyway with *.jpg).

Also, he just wants to process files with the pattern IMG*.jpg. *.jpg will process all *.jpg files.

The "s" in "files" indicates plurality. But I probably should have used "pictures" instead of file. Using the word "file" has become a habit. I use it for oneliners in for loops working in the shell as well.

Using LAST in place of ${#ARRAY[@]} is a good idea for readability but the form of the for loop I used resembles C and is given if you enter "help for". I don't think using $LAST in the loop would precompute the value of LAST however. That would be the case in C. However, the name "LAST" indicates that it is the last element. Then you decrement it. That adds confusion it itself.
The letter "n" is commonly used to indicate an index. The ${#files[@]} was the only obfuscated part and should have been commented in the code.

Perhaps I should have added comments to the code instead of putting them at the end of the message.

If I have a saving grace, it's that I created a bunch of files matching the filepattern and tested the program before posting. Instead of images, IMG1000.jpg contained the text "IMG1000.jpg". So after the renaming, I cat'ed out the contents of the files to prove that the reordering took place.

/bin/bash 12-06-2007 01:20 PM

The ls isn't needed as fileglobbing will do it. ( and you are using file globbing anyway with *.jpg).
Just part of the un-obfuscating. Which is more easy to understand?
ARRAY=( $(ls -1 *.jpg) )
ARRAY=( *.jpg )
Maybe just a mater of preference. And I thought ls -1 IMG[0-9][0-9][0-9][0-9].jpg just looked ugly.

The "s" in "files" indicates plurality. But I probably should have used "pictures" instead of file. Using the word "file" has become a habit. I use it for oneliners in for loops working in the shell as well.

I used ARRAY because I was creating an ARRAY. I have no rhyme or reason for the things I do.

If I have a saving grace, it's that I created a bunch of files matching the filepattern and tested the program before posting. Instead of images, IMG1000.jpg contained the text "IMG1000.jpg". So after the renaming, I cat'ed out the contents of the files to prove that the reordering took place.
Yeah I did the exact same thing only mine were named like my camera names them (which explaines the pic000.img. Funny thing is the middle file never gets changed :(

BTW: I was joking and referring to the perl script, because as everyone knows nothing beats perl for obfuscating!

jschiwal 12-06-2007 08:15 PM

Using the ls is like preceding grep or sed with cat. It isn't un-obfuscating, it's a bad habit.

I'll compromise on the ARRAY, but it shouldn't be in capital letters because capital letters are used in bash to indicate constants. A name like picture_array or pic_array would be better. It communicated the type and what the elements are.

Ditto for FIRST and LAST. It looked strange when you started changing them.

/bin/bash 12-07-2007 05:20 AM

  1. You really need to chill.
  2. Please don't attempt to correct my scripting style (old dog new trick sort of thing.)
  3. Have a good day.
<mental note> Human race becoming more humorless and up tight.</mental note>

bigearsbilly 12-07-2007 07:40 AM

Quote:

BTW: I was joking and referring to the perl script, because as everyone knows nothing beats perl for obfuscating!
only for people who don't speak perl ;)

ghostdog74 12-07-2007 08:29 AM

Quote:

Originally Posted by jschiwal (Post 2982725)
Using the ls is like preceding grep or sed with cat. It isn't un-obfuscating, it's a bad habit.

I'll compromise on the ARRAY, but it shouldn't be in capital letters because capital letters are used in bash to indicate constants. A name like picture_array or pic_array would be better. It communicated the type and what the elements are.

Ditto for FIRST and LAST. It looked strange when you started changing them.


actually, it doesn't really matter. You can keep telling people not to use cat because its UUOC, or whatever, in the end, people will still UUOC. Even books that are published (yes , I saw one just now at the bookshop using UUOC ) You can tell people not to use capital letters for whatever reasons, but people will still use it. Its just a matter of choice, when that choice is presented to them. If bash could restrict the use of capital letters just for constants, then people won't be able to use capital letters as variables. but the bash people who created bash doesn't really think about that...you can tell people that Perl is the king of obfuscation, but people still use them, cause once again, the key to this => choice.

/bin/bash 12-08-2007 05:22 AM

only for people who don't speak perl
I once studied a perl script to see if I could learn from it. I learned that perl is a "self encrypting language!" :P

chrism01 12-08-2007 06:00 PM

People will ultimately do what they want (assuming it's syntactically legal), but it doesn't hurt to (gently) encourage them to do it better, especially if there's a danger of them doing it in prod where it will confuse/annoy everbody else.
There is a such a concept as 'best practice'.

bigearsbilly 12-10-2007 03:56 AM

Quote:

I learned that perl is a "self encrypting language!" :P
rubbish! you just need to be a retentive weirdo freak to enjoy it!

;)


All times are GMT -5. The time now is 02:10 AM.