LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 01-29-2004, 01:56 AM   #1
nevereverend
LQ Newbie
 
Registered: Jan 2004
Distribution: SuSE 9.0 Professional
Posts: 12

Rep: Reputation: 0
Bulk File Renaming


Can anyone tell me if there is a method for renaming several files at once, keeping their original file name, but adding a bit of text as a prefix to the file name (such as the date or month name)?

I have a buttload of old text files and logs I'd like to archive. So assuming i have files:

alphanotes.txt
betanotes.txt
zetanotes.txt

can i use a means to rename them all at once to (for example):

01_29_alphanotes.txt
01_29_betanotes.txt
01_29_zetanotes.txt

Any help would be appreciated.

:: nevereverend ::
 
Old 01-29-2004, 02:03 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
something like

Code:
#!/bin/bash
for i in `ls *.txt`
do
  mv $i "01_29"$i
done

should do the trick...


Cheers,
Tink
 
Old 01-29-2004, 02:31 AM   #3
nevereverend
LQ Newbie
 
Registered: Jan 2004
Distribution: SuSE 9.0 Professional
Posts: 12

Original Poster
Rep: Reputation: 0
Quote:
Originally posted by Tinkster
something like

Code:
#!/bin/bash
for i in `ls *.txt`
do
  mv $i "01_29"$i
done

should do the trick...


Cheers,
Tink
Hmmm... it tells me

/bin/bash: bad interpreter: Permission denied

I think it may have something to do with the fact that I'm doing this accessing files in a win98 FAT32 filesystem on another drive I have mounted.


Last edited by nevereverend; 01-29-2004 at 02:50 AM.
 
Old 01-29-2004, 03:23 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally posted by nevereverend
Hmmm... it tells me

/bin/bash: bad interpreter: Permission denied

I think it may have something to do with the fact that I'm doing this accessing files in a win98 FAT32 filesystem on another drive I have mounted.
nope not at all.... try changing it to "#!/bin/sh", more generic.
 
Old 01-29-2004, 03:31 AM   #5
nevereverend
LQ Newbie
 
Registered: Jan 2004
Distribution: SuSE 9.0 Professional
Posts: 12

Original Poster
Rep: Reputation: 0
Nope, still doesn't work... give me the same message:

/bin/sh: bad interpreter: Permission denied

I used the original script fine on a linux partition, which is what made me wonder if the problem is what i listed previously.
 
Old 01-29-2004, 10:53 AM   #6
benjithegreat98
Senior Member
 
Registered: Dec 2003
Location: Shelbyville, TN, USA
Distribution: Fedora Core, CentOS
Posts: 1,019

Rep: Reputation: 45
I've had this problem w/ the bad interpreter and I saw nothing wrong with the script/permissions. I think it was from when I cut and paste a script from here. I fixed it by deleting it and manually typing it out. Try that and see what happens.
 
Old 01-29-2004, 11:31 AM   #7
nevereverend
LQ Newbie
 
Registered: Jan 2004
Distribution: SuSE 9.0 Professional
Posts: 12

Original Poster
Rep: Reputation: 0
benjithegreat98,

Nope that didnt work either...
 
Old 01-29-2004, 12:02 PM   #8
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
Here are a couple of different methods to try. The first using awk and the second using sed.

for i in *.txt; do j=`echo $i | awk -F_ '{print $3}'`; mv "$i" "$j"; done

The second method....

for i in *.txt; do j=`echo $i | sed -e "s/^01_29_//g"`; mv $i "$j"; done
 
Old 01-29-2004, 01:27 PM   #9
jazernorth
Member
 
Registered: Jan 2004
Location: Green Bay
Distribution: RedHat 8.0, LFS-5.0
Posts: 100

Rep: Reputation: 15
rename

Rename works really well, only it changes the ending, and not the beginning. A little thought should make it so you can change the beginning.

See 'man rename'

It does take some getting used to though.

Example:

Rename .txt _01_09.txt *.txt

Enjoy.

JN
 
Old 01-29-2004, 02:09 PM   #10
nevereverend
LQ Newbie
 
Registered: Jan 2004
Distribution: SuSE 9.0 Professional
Posts: 12

Original Poster
Rep: Reputation: 0
jazernorth,

I tried using rename before I posted my question here. I could not figure out a way to get it to do what I needed, even after reading the man pages.

Tinkster's script works fine on my linux partition, just doesnt seem to work on the windows Fat32 partition. I just moved all the files in question to my home directory and used Tinkster's script to rename them. I'll try again another time with homey's awk and sed commands, but as i am not really familiar with either of them yet, I felt it easier to just move the files to where I knew the script would work fine.

Thank you all for the help.
 
Old 01-29-2004, 07:02 PM   #11
Shachaf
Member
 
Registered: Sep 2003
Location: Port Townsend, WA
Posts: 30

Rep: Reputation: 15
You can type (from a shell):

Code:
rename_txt_files() {
  for i in `ls *.txt`
   do
     mv "$i" "01_29_$i"
   done
}
to make the function rename_txt_files, which will work like a shell script. Also, make sure the script is executable (On a FAT(32) partition, all the files have the same permissions -- make sure they include execute permission for yourself) and that the file is writeable by you (again, all the permissions are the same).

Also, you can change the line mv "$i" "01_29_$i" to mv "$i" "`date +%m_%d_`$i".
 
Old 07-03-2004, 06:28 PM   #12
linuxfond
Member
 
Registered: Jan 2003
Location: Belgium
Distribution: Mandrake 9.2
Posts: 475

Rep: Reputation: 30
Guys, these scripts work like charm, but, how hell can I replace file names with, say, 0001.doc, 0002.doc etc... there are some hundreds files.
I need to rewrite the names which are all different, so there is no matching pattern.

 
  


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
Complex file renaming Imyrryr Linux - General 3 04-29-2005 09:27 AM
Batch File for format? Bulk Formatting Beebe Solaris / OpenSolaris 2 04-12-2005 06:07 AM
convertion from doc file to pdf in bulk sujith_deva General 6 12-10-2004 12:20 AM
renaming a file?? U-Toast Linux - Newbie 2 08-29-2004 02:07 AM
renaming or converting a text file to a dat file... tangaz Linux - Software 1 10-24-2003 07:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03: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