-2

I would like to know how to rename the same log file which is in different path. I mean, the log file is in /main/rel/1.2, /main/rel/1.3, /main/rel/1.4, /main/rel/1.5 etc. I want just do it from /main/rel to all the path.

Could this be possible?

heemayl
  • 56,300
  • 1
    I'm sorry, but I couldn't understand your question. What do you mean by "change the same log file" . Would you like to rename it? Modify the contents? – rahul Apr 22 '15 at 13:33
  • You are right, i mean just rename. – FaMontyN2 Apr 22 '15 at 13:37
  • would you please elaborate ? If you could provide some examples in detail, it would be very helpful and much faster to figure out. – rahul Apr 22 '15 at 13:43
  • 1
    Is this what you're looking for? – Joseph R. Apr 22 '15 at 13:49
  • I have the same log file (e.g. file.log) saved in different path like /main/rel/1.2 /main/rel/1.3 /main/rel/1.4 /main/rel/1.5. I'd like rename this file in file.log.OLD in each path but just with one command run from /main/rel/ without having to go into each path. Clearer now? – FaMontyN2 Apr 22 '15 at 13:50
  • Then perhaps this will help. – Joseph R. Apr 22 '15 at 13:51

2 Answers2

1

using find to find all files you want to rename and running an exec script: `

 find /main/rel -name file.log -exec mv '{}' '{}.OLD' \;

this will run the mv cmd on all found files, the {} (escaped so the shell doesn't do weird things) will be replaced by the filename. this allows you to easily add extra characters to the original filename (e.g. append .OLD).

anyhow, checkout logrotate, which is a tool that was created for the job you want to do. it not only rotates logfiles for you (e.g. rename logfile to logfile.0; but before it moves logfiles.0 to logfile.1; compress older logfiles so they don't take up much space; delete very old logfiles) but also notifies the daemon that writes the logfile that it probably needs to reopen the new one (else you might rename your logfile to foobar.bak, only to discover that the server keeps appending to foobar.bak...)

umläute
  • 6,472
0

No. I don't think there's a single command to do so. you can do renaming these files one-by-one.

/main/rel$mv 1.2/somelog 1.2/some_newlog; mv 1.3/somelog 1.3/some_newlog; mv 1.4/somelog 1.4/some_newlog

and etc... However, you can write a script to do such job:

#!/bin/bash
for dir in 1.{2..4}
do
    `mv $dir/some.txt $dir/somenew.txt`
done

NOTE: Above Script Should be saved in "/main/rel" and you have to run this Bash script from there only.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
Shivam
  • 3
  • For the script I would recommend something more like:

    for file in `ls $1` ; do mv $1/$file $1/`echo $file | sed "s/some/somenew/g"`; done

    That way you can pass in a directory as an argument

    – Centimane Apr 22 '15 at 14:53
  • In your script we have to give the Directory/subDirectories as argument. Also If I run this script as: ./rename.sh 1.{2..6} then its only working for /main/rel/1.2 not for all i.e. 1.2,1.3,1.4, etc. – Shivam Apr 22 '15 at 18:09