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 :: |
something like
Code:
#!/bin/bash should do the trick... Cheers, Tink |
Quote:
/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. |
Quote:
|
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. |
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.
|
benjithegreat98,
Nope that didnt work either... |
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 |
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 |
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. |
You can type (from a shell):
Code:
rename_txt_files() { Also, you can change the line mv "$i" "01_29_$i" to mv "$i" "`date +%m_%d_`$i". |
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. |