Need help with shell script - renaming multiple files
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
For good measure, I thought I'd throw this one in too. It's a perl script, but it still requires the find command format earlier.
Code:
#!/usr/bin/perl
if ($#ARGV > -1 )
{
$filename = shift;
$new_filename = $filename;
if ( $new_filename =~ /\.dd-/ )
{
$new_filename =~ s/\.dd-//g;
}
if ( $new_filename =~ /-dead-[0-9]*>/ )
{
$new_filename =~ s/-dead-[0-9]*>//g;
}
if( $filename ne $new_filename )
{
system mv, $filename, $new_filename;
}
}
It will rename the file in one action. It'll take care of the ".dd-" and "-dead-" portions. Create the script (I named it file_rename.pl), make it executable, and then (assuming it's in the same dir as the files to rename) type this at your bash prompt (you're creating a mini-wrapper script):
Code:
ls | while read filename; do ./file_rename.pl $filename; done
Please, help!
I have a problem: plenty of Japanese files, with disguised names, such as: V^[N[X܂̓X@�5 .doc (invalid Unicode)
In some instances they have no extention, or the period before:
V^[N[X܂̓X@�5?doc
The lenght of the names is always different.
All these files were imported from Windzoo 97.
OpenOffice opens them beautifully (btw, the latest OpenOffice works like charm, and loads really fast! I also solved the problem of printing margins.), if the names look like something more civilized, say, 1.doc, 2.doc etc.
I need a script wich will batch rename all these files, replacing the names with a number and *.doc extention.
I read mv and rename man, but this task is beyond my skills.
Can anyone post here a ready to use script, or something I could start with? Thanks in advance.
P.S. rename ".doc (invalid Unicode)" ".doc" *
works, but I don't know what to do with the mess before the extension
#!/bin/bash
document_number=1
old_ifs=${IFS}
IFS=$'\n'
for filename in $( ls -1 *.doc ) ; do
mv -v "${filename}" ${document_number}.doc
((document_number=document_number+1))
done
IFS=${old_ifs}
exit 0
Save it, and then make it executable (chmod u+x filename)
Cd to the directory with the documents to be renamed, and execute the script.
As I mentioned earlier, always try new scripts that move or delete files on a test set of data first. Verify that it does what you want it to do before running it on your originals.
Last edited by Dark_Helmet; 07-04-2004 at 11:25 AM.
This looks nice. It should work and I will give it a try now.
BTW, Today while I was messing up with these scripts, I renamed all folders in the /root directory (incidentally I run the script as root -- luckily I have not donemore damage!)
Anyway, all folders in /root were renamed in 1.doc 2.doc 3.doc
I figured out that one of them was tmp folder, another was Desktop, the third folder contains log files, auto-install.pl script etc. I don't remember what was the name of this folder. Do you have any idea?
As for the problem with renaming the directories in root, well, it all depends on your installation. There are some standard ones:
/bin
/boot
/dev
/sbin
/home
/usr
/tmp
/var
/etc
/lib
/opt
/proc
/root
/mnt
The one that contains log files is typically /var.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.