Register
It is currently Thu Oct 23, 2014 11:10 am

File renaming script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Sun Jan 24, 2010 9:48 pm   

Joined: Sun Jan 24, 2010 9:43 pm
Posts: 1
I am a scripting newb and I get by ok on my own but I have a problem that I can't seem to find an answer to. I saved a damaged hard disk with testdisk and copied over 9,000,000 files to a recovery drive but for some reason the lost+found directory added a # sign to the begining of each file. The filenames are just numbers like this 98765 they represent media files and a database actually knows what kind of file each number is. Anyway the files are supposed to look like this 98765 but instead they look like this #98765 I need a script to remove the # sign from all of them but retain the rest of the file name. Is this possible on this number of files? I would assume sed is the way to go and I am pretty good with manipulating text within a file with sed but not filenames.

Anyhelp is appreciated.

John


Top
 Profile  
 PostPosted: Mon Jan 25, 2010 1:57 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
Hi,

this simple function should do the trick
Code:
ren() {
for file in $1/*
do [ -d $file ] && ren $file
[ -f $file ] && [[ $file == *#* ]] && mv $file ${file//\/#/\/}
done
}
then type
Code:
dir2rename=path/to/directory_where_files_have_to_be_renamed
ren $dir2rename

I tried with
Code:
abc/#123
abc/def/#456
abc/def/#789
abc/def/ghi/#789
ren .
and I've got
Code:
abc/123
abc/def/456
abc/def/789
abc/def/ghi/789


Top
 Profile  
 PostPosted: Mon Jan 25, 2010 7:45 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Wouldn't this be easier done with find?

Code:
find -iname \#* -exec basc -c "mv \"$a\" \"${1:1}\"" {} {} \;


This should remove the first character in any filename/directory starting with a #. This is also insensitive to the famous "command-line too long" error when working with över 3000~ files.

ps. As per usual I haven't properly tested this solution, so I recommend putting an "echo" before the commands and make sure it's properly outputting. ds.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Mon Jan 25, 2010 3:18 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
Hi frederick,

the problem this time is to delete the first character of the filename
Code:
find . -type f -iname "#*" -exec bash -c 'mv "$1" "$(echo ${1:1})"' '{}' '{}' \;
mv: cannot move `./repertoire2/#456' to `/repertoire2/#456': No such file or directory
mv: cannot move `./repertoire2/repertoire2/#456' to `/repertoire2/repertoire2/#456': No such file or directory
and so on,
because the parameter expansion rule deletes the first character of the whole path/file, which here is a dot, and mv tries to move files to directories at the root of the filesystem, where they don't exist.


Top
 Profile  
 PostPosted: Thu Jan 28, 2010 3:18 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
True! Human error I guess :) but there's a solution for that too :p

Code:
find . -type f -iname "#*" -exec bash -c 'a=$(basename $1); mv "$1" "$(dirname $1)/${a:1}"' '{}' '{}' \;


It just chops out the filename with basename, removes the first character, and adds it onto the dirname of the file.

This untested thou, since I only have solaris find(1) at hand right now. Which doesn't support pretty much anything :P

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Thu Jan 28, 2010 3:31 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
I tested it on 120 files in 120 directories. It works fine.

I would not guess, but it's about three times more time consuming than "my" function:
Code:
time find testRep -type f -iname "#*" -exec bash -c 'a=$(basename $1); mv "$1" "$(dirname $1)/${a:1}"' '{}' '{}' \;

real   0m2.030s
user   0m0.620s
sys   0m1.304s

dir2rename=testRep
time ren $dir2rename

real   0m0.676s
user   0m0.228s
sys   0m0.368s

Now, I'd like to have feedback from feloniousj about using each on 9,000,000 files


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP