Hey folks! I have a bunch of files on a server that are in the format "filename_x.dat" and I need to rename them "filename.dat", i.e. strip to last two characters off the filename while maintaining the extension. If it wasn't for the fact that I have about 400 files in multiple directories, I'd do it by hand. However, it's of a somewhat urgent nature - hence this slightly off-topic cry for help! =) I'm pretty sure there's a way to do it via a shell script (I use bash) - the only problem being that I've never written one before. Can anyone point me to a faq/tutorial/website that might explain the process? If it's a trivial task, anyone care to take a stab? Google gave me a few leads, but most aren't helping a lot. Any help is appreciated! Thanks! --Tim
for i in * ; do mv $i `echo $i | sed 's/\(.*\)..\.\(.*\)/\1.\2/'` done Should do the trick... It moves each file to the original filename, minus the two characters before the last . As far as a book to get, _Learning the bash Shell, 2nd Edition_, published by o'reilly. Also likely useful _sed & awk, 2nd Edition_, also published by o'reilly. The sed book will translate that example of leaning toothpick syndrome. Scott On Wed, 17 Jul 2002, Tim Trachimowicz wrote:
Hey folks!
I have a bunch of files on a server that are in the format "filename_x.dat" and I need to rename them "filename.dat", i.e. strip to last two characters off the filename while maintaining the extension.
If it wasn't for the fact that I have about 400 files in multiple directories, I'd do it by hand. However, it's of a somewhat urgent nature - hence this slightly off-topic cry for help! =)
I'm pretty sure there's a way to do it via a shell script (I use bash) - the only problem being that I've never written one before. Can anyone point me to a faq/tutorial/website that might explain the process? If it's a trivial task, anyone care to take a stab? Google gave me a few leads, but most aren't helping a lot.
Any help is appreciated! Thanks!
--Tim _______________________________________________ Wlug mailing list Wlug@mail.wlug.org http://mail.wlug.org/mailman/listinfo/wlug
Scott Venier said:
for i in * ; do mv $i `echo $i | sed 's/\(.*\)..\.\(.*\)/\1.\2/'` done
I realise it's probably of little concern when dealing with files, but some 1000 calls to sed vs. using a bash-builtin makes for a huge difference in speed: [orion@tribble(~)] TEXT="stuff to work with" [orion@tribble(~)] time for i in $(seq 1 1000); do OTHER=$(echo $TEXT | sed "s/work/goosnargh/g"); done; echo $OTHER real 0m19.405s user 0m5.010s sys 0m12.310s stuff to goosnargh with [orion@tribble(~)] time for i in $(seq 1 1000); do OTHER=${TEXT/work/goosnargh};done; echo $OTHER real 0m0.204s user 0m0.180s sys 0m0.000s stuff to goosnargh with there's quite a few other builtins that can almost replace sed and cut in most situations. look in bash's manpage under "Parameter Expansion" :) -- Aaron Haviland orion [at] tribble [dot] dyndns [dot] org orion [at] parsed [dot] net Can I get 1 bpm by flashing a mirror?
On Wed, Jul 17, 2002 at 08:47:35PM -0700, Tim Trachimowicz wrote:
I have a bunch of files on a server that are in the format "filename_x.dat" and I need to rename them "filename.dat", i.e. strip to last two characters off the filename while maintaining the extension.
Are they all "_x"? You could do something like: for i in *_x.*; do mv -f $i `echo $i | sed -e 's/_x././'`; done it would be more flexible to do in perl of course: opendir(T,".") || die "can't open dir:$!"; while($_=readdir(T)){ next unless ( -f $_ ); ($a,$b) = /^(.+)\.(.+)$/; next unless ( $a && $b ); $a =~ s/..$//; rename($_,"$a.$b"); } closedir(T); Warning: none of these have been tested, I just wrote them out so YMMV. -- Randomly Generated Tagline: "I'm at the age where food has taken the place of sex in my life. In fact, I've just had a mirror placed over my kitchen table." - Rodney Dangerfield
On Wednesday 17 July 2002 23:47, Tim Trachimowicz wrote:
Hey folks!
I have a bunch of files on a server that are in the format "filename_x.dat" and I need to rename them "filename.dat", i.e. strip to last two characters off the filename while maintaining the extension.
Here's yet another way of doing it, by using bash builtins: for _file in $(ls *_x.*); do _front=${_file%\_*} _back=${_file#*\.} mv $_file $_front.$_back done; The code may need to be tweaked slightly to deal with multiple underscores or multiple dots in the filename, and if they all end in .dat, then the logic is even simpler. --Skip
Skip Gaede <sgaede@attbi.com> writes:
Here's yet another way of doing it, by using bash builtins:
And, here's yet another way of doing it, if you have mmv installed. (mmv is cool!) $ mmv "*_x.*" "#1.#2" Also, mmv is pretty good about checking for stupid things, like trying to move all the files in a directory into 1 file. -- Josh Huber
On Thu, Jul 18, 2002 at 10:04:40AM -0400, Josh Huber wrote: Bad Josh! you didn't post from the address you're subscribed with! no cookie for you... -- Frank Sweetser fs at wpi.edu, fs at suave.net | $ x 18 Full-time WPI Network Engineer, Part time Linux/Perl guy |
I was wondering why the ext2fs filesystems was developed for Linux rather than using the BSD4.4 Berkeley Fast Filesystem. Because it was a real fun to design and program it... -- Remy Card
On Thu, Jul 18, 2002 at 10:04:40AM -0400, Josh Huber wrote: huber> And, here's yet another way of doing it, if you have mmv installed. huber> (mmv is cool!) huber> huber> $ mmv "*_x.*" "#1.#2" util-linux comes with "rename", which wil work IF the extension is always the same and there is only the one dot in the filename: rename '_x.' '.' *_x.* -- Charles R. Anderson <cra@wpi.edu> / http://angus.ind.wpi.edu/~cra/ PGP Key ID: 49BB5886 Fingerprint: EBA3 A106 7C93 FA07 8E15 3AC2 C367 A0F9 49BB 5886
participants (8)
-
Aaron Haviland
-
Charles R. Anderson
-
Frank Sweetser
-
Josh Huber
-
Scott Venier
-
Skip Gaede
-
Theo Van Dinter
-
Tim Trachimowicz