Change extension of mutiple files in RedHat Linux (AS4)

I admit, I'm relatively new to Linux, but why does this (simple) O/S task seem so difficult to accomplish in RedHat Linux?  I just want to find all of the *.log files in a directory, and rename them to *.bak (so a process can create new log files without overwriting the old ones and no, this process cannot easily create more-unique log file names).  Yes, I realize that I first have to delete the existing *.bak files, but that's no problem: "rm -f *.bak".

I've worked in VMS and DOS/Windows for years, but never in UNIX.  In Windows, for example the command is simply:
ren *.log *.bak

The bash shell in RedHat Linux must have a way to rename a group of files with a relatively-simple command, right?
LVL 36
Mark GeerlingsDatabase AdministratorAsked:
Who is Participating?
 
MikeOM_DBACommented:

Try this:

#!/bin/bash
crdt=`date +%Y%m%d`
for fil in  /The/logs/path/*.log
do
  fn=${fil%%.*}
  echo " mv $fil ${fn}-${crdt}.bak"
done
0
 
Mark GeerlingsDatabase AdministratorAuthor Commented:
Thanks Mike, I'm out of time for today, but will give that a try in the morning.
0
 
achillecarstenCommented:
also check to see if you have zsh (type zsh)
from there you can do:
zmv (*).log ${1}.bak
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
achillecarstenCommented:
(oh and type: autoload -U zmv  before you run the update )
0
 
TraskAdamCommented:
RedHat does have a 'rename' command.

[root@localhost /tmp/ee]# which rename
/usr/bin/rename
[root@localhost /tmp/ee]# rpm -q -f `which rename`
util-linux-2.13-0.20.4
[root@localhost /tmp/ee]# rename
call: rename from to files...
[root@localhost /tmp/ee]# ls
1294.log   15299.log  1899.log   29556.log  6520.log
13837.log  15603.log  21024.log  4316.log   7004.log
[root@localhost /tmp/ee]# rename .log .bak *.log
[root@localhost /tmp/ee]# ls
1294.bak   15299.bak  1899.bak   29556.bak  6520.bak
13837.bak  15603.bak  21024.bak  4316.bak   7004.bak
[root@localhost /tmp/ee]#

But a loop like MikeOM_DBA's is a better, more general solution, except...

  fn=${fil%%.*}

.... should only have one % sign...

  fn=${fil%.*}

... or get rid of the glob...

 fn=${fil%%.log}

... (in which case %% and % work the same) otherwise files with more than dot arn't handled correclty.

[root@localhost /tmp/ee]# fil=one.two.three.log
[root@localhost /tmp/ee]# echo $fil
one.two.three.log
[root@localhost /tmp/ee]# echo ${fil%%.*}
one
[root@localhost /tmp/ee]# echo ${fil%.*}
one.two.three
[root@localhost /tmp/ee]# echo ${fil%%.log}
one.two.three
[root@localhost /tmp/ee]# echo ${fil%.log}
one.two.three
[root@localhost /tmp/ee]#


0
 
fridomCEO/ProgrammerCommented:
You probably want mmv, FAIKT there is also a RedHat .rpm package available.

Regards
Friedrich
0
 
ahoffmannCommented:
man mmv
 (as already suggested)

or independent of any OS or shell
  ls *.log|perl -ne 'm/((.*).log)/;rename$1,"$1.bak"'
or
  ls *.log|awk '/log$/{print "mv "$0" "$0".bak"}'|sh
   (works only if there're no white spaces in the dilename)
0
 
Mark GeerlingsDatabase AdministratorAuthor Commented:
OK, I do have the RedHat 'rename' command, and I remember trying to use that some months ago, but it has the very nasty "feature" of deleting one (or more) of the files if a syntax error is made!  I tried that today too, and got the same effect: not the changed extension that I wanted on multiple files, but the deletion of the first file in the set.

I don't have "mmv" or "zmv".   I do have: "zsh", but this command: "autoload -U zmv" didn't seem to do anything: no errors, but nothing useful that I could tell either.

I did a little more testing with "rename" and I can get that to work now, but I have to be so careful with it because the number and the order of the parameters is so different from the DOS "ren" (rename) command that I am used to.

To TraskAdam:
I'm curious, why do you say that: "a loop like MikeOM_DBA's is a better, more general solution"?  Sure, I like what it does, but I don't like having to type that much to do what seems to me should be a simple O/S task: just change the extension on a group of files.

Maybe I'm biased or prejudiced, but I'm not a Linux or UNIX sys admin, and don't plan to become one.  I'm an Oracle DBA, but we use Linux now as the O/S for Oracle, and we don't have a sys admin, so I have to do the O/S admin tasks.  I just want simple ways to do them.
0
 
fridomCEO/ProgrammerCommented:
mmv is an extra package you have to install.

Regards
Friedrich
0
 
TraskAdamCommented:
Okay, perhaps just a more general solution rather than necessarily a better one.  The for loop is mighty handy tool to have in ones' mental tool kit.  A few examples from my command line history...

Covert gzip's to bunzip2's...

for i in *.gz; do gunzip $i && bzip2 ${i%*.gz}; done

Copy my files to a remote host with a new name that shows where they came from...

for i in *.cap; do scp $i remotehost:$(hostname).$i; done

Move files from <dir>/<file>.gz to <file>.<dir>.gz ...

for i in *; do for j in $(ls $i); do mv -v $i/$j $(echo $j | sed s/gz$//)$i.gz; done; done

Indeed, 'rename' or 'zmv' or 'mmv' if they're available is the "simple way" you're looking for in this particular case but it can be handy to be able to take it a step further.
0
 
ahoffmannCommented:
> .. but I don't like having to type that much to do what seems to me should be a simple O/S task

first test this command if it does what you want
   ls *.log|awk '/log$/{print "mv "$0" "$0".bak"}'|sh

then make a simple alias in your shell, like
# csh, tcsh syntax:
  alias renamelog 'ls *.log|awk '"'"'/log$/{print "mv "$0" "$0".bak"}'"'"'|sh'

# sh and friends syntax:
  alias renamelog 'ls *.log|awk '"'"'/log$/{print "mv "$0" "$0".bak"}'"'"'|sh'

then you can simply use:

   renamelog
0
 
Mark GeerlingsDatabase AdministratorAuthor Commented:
The script file I ended up with is quite simple:

#!/bin/bash
cd /[the log directory]
rm -f *.bak
rename .log .bak *.log


I appreciate the other ideas and the more-complex options, examples and syntax that you gave me.  I'll file them away for possible future use someday, but for now I like the simple approach.  And, for me "regular expressions" are not simple!  They may be powerful, but they are another foreign language.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.