LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bulk File Renaming (https://www.linuxquestions.org/questions/linux-newbie-8/bulk-file-renaming-139879/)

nevereverend 01-29-2004 12:56 AM

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 ::

Tinkster 01-29-2004 01:03 AM

something like

Code:

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


should do the trick...


Cheers,
Tink

nevereverend 01-29-2004 01:31 AM

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.


acid_kewpie 01-29-2004 02:23 AM

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.

nevereverend 01-29-2004 02:31 AM

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.

benjithegreat98 01-29-2004 09:53 AM

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.

nevereverend 01-29-2004 10:31 AM

benjithegreat98,

Nope that didnt work either...

homey 01-29-2004 11:02 AM

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

jazernorth 01-29-2004 12:27 PM

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

nevereverend 01-29-2004 01:09 PM

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.

Shachaf 01-29-2004 06:02 PM

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".

linuxfond 07-03-2004 05:28 PM

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.

:cry:


All times are GMT -5. The time now is 06:40 AM.