-1

Suppose some logs is being rotated by size 50MB each . I did 'grep' some string and my string is present in log file, log_3 and I want to copy content of log_3 before it gets rotated(renamed) to log_4 . Please suggest how to take backup of content of log_3 before it is rotated . I just need the content of log_3 . I don't want like I copied log_3 (by cp -p log_3 log_3_backup) but by that time logs got rotated and now log_3_backup contains content of log_2 .Is there any way we can do to avoid this . Working on an automation project I need a solution to this . Thank you very much for your suggestions in advanced . You May share python or shell script .

LMC
  • 10,453
  • 2
  • 27
  • 52
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww May 17 '18 at 00:52
  • Please [edit] your question to show [the code you have so far](http://whathaveyoutried.com). Most of us here are happy to help you improve your craft, but are less happy acting as short-order unpaid programming staff. Show us your work so far as a [mcve], the result you were expecting and the results you got, and we'll help you figure it out. It may help to re-read [ask]. – Toby Speight May 17 '18 at 11:47

1 Answers1

1

You can get the file inode number that won't change if renamed, then reference the file by that name

for f in *.log; do
    # get inode of file
    iname=$(ls -i $f)
    # test file contents for pattern presence 
    if grep -q 'some pattern' $f; then
        # the file contains the searched pattern, let's do something
        # find by inode number and move it
        find -inum $iname -exec mv {} {}.bak ';'
    fi
done

Perhaps getting a backup of the file is not necessary anymore, let's grep it again

find -inum $iname -print0 | xargs -r0 grep 'some pattern'
LMC
  • 10,453
  • 2
  • 27
  • 52