LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-04-2007, 09:25 PM   #1
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,101

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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?
 
Old 12-04-2007, 10:49 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
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.
 
Old 12-05-2007, 02:51 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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)
  }
}'
 
Old 12-05-2007, 04:16 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
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

Last edited by bigearsbilly; 12-05-2007 at 04:18 AM.
 
Old 12-05-2007, 04:17 AM   #5
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
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

Last edited by radoulov; 12-05-2007 at 04:19 AM.
 
Old 12-05-2007, 04:23 AM   #6
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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.

Last edited by jschiwal; 12-05-2007 at 04:30 AM.
 
Old 12-05-2007, 05:09 AM   #7
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
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'
 
Old 12-05-2007, 05:26 AM   #8
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
Quote:
Originally Posted by jschiwal View Post
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
 
Old 12-05-2007, 07:11 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
i think we all agree,

mine is the best solution
 
Old 12-05-2007, 07:30 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by bigearsbilly View Post
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.
 
Old 12-05-2007, 08:14 AM   #11
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
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.
 
Old 12-05-2007, 09:41 AM   #12
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
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;
 
Old 12-05-2007, 11:27 AM   #13
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
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.
 
Old 12-05-2007, 03:00 PM   #14
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,101

Original Poster
Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
thx.. i used the first one, tho i havnt tried to see how the other ones work,
 
Old 12-05-2007, 04:46 PM   #15
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
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)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mass renaming icons for use with XFE? Mr_Shameless Linux - Software 2 06-02-2007 05:55 AM
Bash - Mass file renaming problem smudge|lala Linux - Software 2 02-14-2007 06:02 PM
mass mp3 directory renaming schbond Linux - General 4 01-05-2007 11:18 AM
a question about renaming files in mass! zeltak Linux - Software 1 05-24-2006 06:12 AM
Simple Renaming Script Boffy Programming 5 08-12-2005 09:07 AM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration