LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 10-02-2006, 10:09 PM   #1
auwolf
LQ Newbie
 
Registered: Oct 2006
Location: Gladstone, Queensland
Posts: 4

Rep: Reputation: 0
Rename a bunch of files


Hi,

New on site and Linux.

Looking for a script to rename files.
Have a spreadsheet with existing name to new name.
Ext is the same for all.

Thanx
 
Old 10-03-2006, 02:02 AM   #2
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 11,762

Rep: Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587Reputation: 1587
Welcome to LQ.
You can use "rename" for your task.
Code:
man rename
for details on how to use it.

Regards
 
Old 10-03-2006, 02:39 AM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975
new to lq? new to internet forums? please try to post threads in suitable forums, and post them ONCE.

moved to Linux - Newbie.
 
Old 10-03-2006, 09:02 AM   #4
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
If you have KDE then you can use KRename. It's great if you have a lot of files that need the same change to their name. I use it mostly to change upper case to lower case or to change file name extensions such as jpeg to jpg. It's only worth using if you have a lot of files to rename since it takes several steps to enter the information about how to change the file names.
 
Old 10-05-2006, 05:53 PM   #5
auwolf
LQ Newbie
 
Registered: Oct 2006
Location: Gladstone, Queensland
Posts: 4

Original Poster
Rep: Reputation: 0
rename

The files do not follow, I have a document to what the new name should be.
The extensions for all are the same.
 
Old 10-05-2006, 08:17 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,362

Rep: Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377
I think we need more info eg example
 
Old 10-05-2006, 09:11 PM   #7
ckoniecny
Member
 
Registered: Oct 2005
Posts: 162

Rep: Reputation: 30
You could do something like:

Code:
#!/bin/sh

PATH="/home/user/files"

cd $PATH

for files in *
do
     mv $files "whatever you want your file named to"
done
You need a unique name for "whatever you want your file named to" or else you'll overwrite the same file.

Last edited by ckoniecny; 10-06-2006 at 12:57 AM.
 
Old 10-06-2006, 12:51 AM   #8
auwolf
LQ Newbie
 
Registered: Oct 2006
Location: Gladstone, Queensland
Posts: 4

Original Poster
Rep: Reputation: 0
Lets try again,

existing name
decw-2rch-0011.pdf
deew-2rch-0011.pdf
deel-1sga-0001.pdf
deew-5rhg-0001.pdf

new name
qsr-xr2-plan-500110.pdf
qsr-xr2-plan-500011.pdf
qsr-ewp-plan-500001.pdf
qsr-cwp-civl-500001.pdf

Something in the line of this. The files names are unique and will be renamed
to a unique new name. I have a spreadsheet to tell me what the new name should be.
Can put it in a word doc as per above.

Thanx for the help so far.

Last edited by auwolf; 10-06-2006 at 12:54 AM.
 
Old 10-06-2006, 01:50 AM   #9
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Using Rename or mv to rename your files is one part of the solution. The other part would involve
reading the data from your spread sheet/document.
There are many ways to accomplish that second part, but one of the easiest would be:
1. Copy the contents of your spread sheet/word doc in a plain text file, preferrably specifying the original
name & it's corresponding new name on the same line. The delimiter between both columns is rather
irrelevant.
Alternatively, if the spreadsheet has 2 columns (with old and new names), try saving your spreadsheet in
csv (comma separated values) format. This would accomplish putting the old/new name pairs, each on a
separate line, using ',' as delimiter.
2. Write a simple script to iterate over each line in the plain text file. Use 'cut' to extract both file names
and 'mv' to do your renaming.
Basically, this script would look somthing like (if the delimiter isn't a space or tab):
Code:
#!/bin/bash
DELIM='your_delimiter';
for i in `your_plain_file`; do
   old_name=`cut -d"${DELIM}" -f1`; #assuming your old file names are in first column
   new_name=`cut -d"${DELIM}" -f2`; #and new names are in second (if the other way around, switch the -f 
                                    #options)
   mv ${old_name} ${new_name}
   if (( $? != 0 )); then
       echo "mv ${old_name} ${new_name} failed.";
   fi;
done;
You may want to replace "mv" by "echo", just to see if the script would rename exactly the way you'd like it to, before actually renaming anything.
 
Old 10-06-2006, 01:55 AM   #10
Ynot Irucrem
Member
 
Registered: Apr 2005
Location: Perth, Western Australia
Distribution: Debian
Posts: 233

Rep: Reputation: 30
EDIT: Sniped... Well there you go, you should probably go for the other one. Looks nicer.

EDIT 2: wow, that is so much simpler.
*hits head against wall*

WARNING: I'm a complete n00b, and I'm not even on a *nix machine right now to test this. Maybe you should wait 'til someone replies and corrects me or at least make a copy of the directory to try this in.


Assuming your spreadsheet is like:

oldname1 newname1
oldname2 newname2
oldname3 newname3


If there are no inconsistencies between the files and the spreadsheet (no files in the list that aren't in the folder, or vice versa):

1. sort alphabetically by the oldname column
2. delete the oldname column
3. export to .csv
Code:
#!/bin/bash

dir = "/path/to/files/"
csv = "/path/to/spreadsheet.csv"

cd $dir

textArray[0] = ""
i = 0

while read line; do
    textArray[i] = $line
    i = $(expr $i + 1)
done < $csv

i = 0

for files in *; do
    mv $files "$textArray[i]".pdf
    i = $(expr $i + 1)
done
If there are inconsistencies:

This method will run slower, which may or may not be noticeable depending on how many files we're talking about.

1. export to .csv

Code:
#!/bin/bash

dir = "/path/to/files/"
csv = "/path/to/spreadsheet.csv"

cd $dir

textArray[0] = ""
i = 0

while read line; do
    textArray[i] = $line
    i = $(expr $i + 1)
done < $csv

for files in *; do
    i = 0
    while [ "${textArray[i]:0:$(expr index \"$textArray[i]\" ,)}.pdf" != "$files" ]; do
         i = $(expr $i + 1)
    done
    mv $files ${line:$(expr index "$textArray[i]" ,)}.pdf
done
If the spreadsheet values contain the ".pdf", remove all the ".pdf"s from the script.

Last edited by Ynot Irucrem; 10-06-2006 at 02:00 AM.
 
  


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
un-TARring a bunch of files at once grapnell Linux - General 2 02-12-2006 09:18 AM
un-TARring a bunch of files at once grapnell Linux - Software 4 02-12-2006 09:07 AM
Need to chown a bunch of remote files. Kbear? $ftp? asktoby Linux - Software 0 12-06-2004 10:27 AM
Rename a bunch of directories cessburn Linux - Software 1 06-07-2004 08:52 PM
Need helpThis script erased a bunch of my files HELP VisionZ Linux - Newbie 12 03-25-2004 10:02 AM


All times are GMT -5. The time now is 12:30 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration