Solved

Change extension of mutiple files in RedHat Linux (AS4)

Posted on 2006-11-28
12
1,581 Views
Last Modified: 2012-06-27
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?
0
Comment
Question by:Mark Geerlings
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
12 Comments
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 100 total points
ID: 18032285

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
 
LVL 35

Author Comment

by:Mark Geerlings
ID: 18032338
Thanks Mike, I'm out of time for today, but will give that a try in the morning.
0
 
LVL 1

Expert Comment

by:achillecarsten
ID: 18033238
also check to see if you have zsh (type zsh)
from there you can do:
zmv (*).log ${1}.bak
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:achillecarsten
ID: 18033245
(oh and type: autoload -U zmv  before you run the update )
0
 
LVL 1

Assisted Solution

by:TraskAdam
TraskAdam earned 100 total points
ID: 18034012
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
 
LVL 24

Expert Comment

by:fridom
ID: 18034883
You probably want mmv, FAIKT there is also a RedHat .rpm package available.

Regards
Friedrich
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 18036173
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
 
LVL 35

Author Comment

by:Mark Geerlings
ID: 18036740
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
 
LVL 24

Expert Comment

by:fridom
ID: 18037241
mmv is an extra package you have to install.

Regards
Friedrich
0
 
LVL 1

Expert Comment

by:TraskAdam
ID: 18037268
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
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 100 total points
ID: 18037448
> .. 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
 
LVL 35

Author Comment

by:Mark Geerlings
ID: 18039823
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question