LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-05-2002, 02:56 PM   #1
l_9_l
Member
 
Registered: Dec 2001
Location: New Zealand,Auckland
Distribution: debian
Posts: 86

Rep: Reputation: 15
mv


q:suppose you have a huge collection of files with random names,and you want to rename them in a sorted way;how will you accomplish that?
 
Old 05-05-2002, 05:59 PM   #2
jasonfurtney
LQ Newbie
 
Registered: May 2002
Distribution: redhat
Posts: 2

Rep: Reputation: 0
use Perl;

#!/usr/bin/perl -w

@files = `ls`;

foreach $file (@files) {
chomp $file;
$c++;
if ($c < 10) {$nf = "file_00$c"}
elsif ($c < 100) {$nf = "file_0$c"} #ascii order will break
else {$nf = "file_$c"} #after 999 !

system ("mv $file $nf");
print ("mv $file $nf ... \n");
}

print "Changed $c files to file_XXX\n";


If I understand your question correctly you have a directory with a
bunch of files that have random names and you want to give them
systematic names. This script will homogenise the filenames of each
file in the directory it is run in, in the order that they appear in the ls command.

If you want to sort them in a different manner (based on size, type,
permissions, etc) Perl has powerful regular expressions and easy
ways to sort associative arrays. Or as something to do for fun one could
write a bash script to do the same thing !

This is a rather quick and dirty way to solve this problem so use this
script with care. The best way to use it is probably to put it in the
directory below where you want to use it and run it as

bash# perl ../<name_of_script>

Hope that helps
Jason

---

snowfall11:38pm~/lq/t# touch a 423wer 3sdwer eer64 linux sdfas00sd mu fslwd32 lwfnwbs923 perl asasd
snowfall11:39pm~/lq/t# ll
total 0
-rw-rw-r-- 1 jkf jkf 0 May 5 23:39 3sdwer
-rw-rw-r-- 1 jkf jkf 0 May 5 23:39 423wer
-rw-rw-r-- 1 jkf jkf 0 May 5 23:39 a
-rw-rw-r-- 1 jkf jkf 0 May 5 23:39 asasd
-rw-rw-r-- 1 jkf jkf 0 May 5 23:39 eer64
-rw-rw-r-- 1 jkf jkf 0 May 5 23:39 fslwd32
-rw-rw-r-- 1 jkf jkf 0 May 5 23:39 linux
-rw-rw-r-- 1 jkf jkf 0 May 5 23:39 lwfnwbs923
-rw-rw-r-- 1 jkf jkf 0 May 5 23:39 mu
-rw-rw-r-- 1 jkf jkf 0 May 5 23:39 perl
-rw-rw-r-- 1 jkf jkf 0 May 5 23:39 sdfas00sd
snowfall11:39pm~/lq/t# perl ../tst.pl
mv 3sdwer file_001 ...
mv 423wer file_002 ...
mv a file_003 ...
mv asasd file_004 ...
mv eer64 file_005 ...
mv fslwd32 file_006 ...
mv linux file_007 ...
mv lwfnwbs923 file_008 ...
mv mu file_009 ...
mv perl file_010 ...
mv sdfas00sd file_011 ...
Changed 11 files to file_XXX
snowfall11:39pm~/lq/t# ls
file_001 file_002 file_003 file_004 file_005 file_006 file_007 file_008 file_009 file_010 file_011
snowfall11:39pm~/lq/t#
 
Old 05-06-2002, 01:29 AM   #3
l_9_l
Member
 
Registered: Dec 2001
Location: New Zealand,Auckland
Distribution: debian
Posts: 86

Original Poster
Rep: Reputation: 15
cool script jason,thanks.
the script works properly if the names of the files were read-able,unfortunately in my case they aren't,
look like that:
?? ?????.mp3
?? ???.mp3

I did use "tr" to replace the ?s with another letter,but I don't know,if I couldn't 95% or "tr" couldn't accomplish it!

any solution out there?
 
Old 05-06-2002, 02:16 AM   #4
l_9_l
Member
 
Registered: Dec 2001
Location: New Zealand,Auckland
Distribution: debian
Posts: 86

Original Poster
Rep: Reputation: 15
someone his name/nickname "rpete" in
irc.openprojects.net/#perl has solved it.
this is his solution:
opendir DIR, '.';
$i=0; while ($old = readdir DIR)
{
-f $old or next; $new = sprintf "file_%03d", $i++;
rename($old, $new) or die "$old: $!";
}

-copyright reserved for Rpete,published under GPL

Thanks for both Jason and Rpete--cool perlers.
 
Old 05-06-2002, 03:40 AM   #5
l_9_l
Member
 
Registered: Dec 2001
Location: New Zealand,Auckland
Distribution: debian
Posts: 86

Original Poster
Rep: Reputation: 15
btw i think we can solve it too via the "find" utility.
find -exec mv {} --new-names-- \;

does anyone know what the --new-names-- should be?
 
Old 05-06-2002, 09:01 AM   #6
rpete
LQ Newbie
 
Registered: May 2002
Posts: 1

Rep: Reputation: 0
If you only wanted a shell command, this should do the trick:

i=0; for f in *; do test -f "$f" || continue; mv "$f" file_$i; i=$(($i+1)); done
 
  


Reply



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 On
HTML code is Off



LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:01 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration