Operating System - HP-UX
1837971 Members
3536 Online
110124 Solutions
New Discussion

Script to rename core files and put them in an archive

 
SOLVED
Go to solution
Ronald Cogen
Frequent Advisor

Script to rename core files and put them in an archive

Hello out there,
I put a simple script together that should collect the the core files on my system, rename them by putting an '_' instead of a '/', so that the files could be individually placed in an archive directory. It dosen't work. Can someone tell me why?:

ARCHIVE_DIR="/archive/core_files"

for file in 'find / -name core'
do
target_name='echo $file ?? awk '{print sub(".", "/")}
mv $file $ARCHIVE_DIR/$target_name
done
I've been down so long it looks like up to me
10 REPLIES 10
Bill McNAMARA_1
Honored Contributor

Re: Script to rename core files and put them in an archive

so this is a quick analysis:

target_name='echo $file ?? awk '{print sub(".", "/")}

is missing a '

Instead of using 's use $() to enclose command substitutions.

ie:
target_name=$(echo $file ?? awk '{print sub(".", "/")})

I'd recommend changing the find too.. -local at least.

watch out for files really called core - to the best of my knowledge there's one in /opt/netscape that should be called core.
You also don't want to move dirs and possibly want to redirect stderr to /dev/null

Later,
Bill

It works for me (tm)
Dietmar Konermann
Honored Contributor

Re: Script to rename core files and put them in an archive

What about:

ARCHIVE_DIR="/archive/core_files"

for file in $(find / -name core)
do
target_name=$(echo $file | sed 's%/%_%g')
mv $file $ARCHIVE_DIR/$target_name
done
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Leif Halvarsson_2
Honored Contributor

Re: Script to rename core files and put them in an archive

hi

ARCHIVE_DIR="/archive/core_files/"

for file in `find . -name core`
do
mv $file ${ARCHIVE_DIR}`echo $file |tr "/" "_"`
done

The filenames can be rather long.
Rita C Workman
Honored Contributor

Re: Script to rename core files and put them in an archive

you could do this is one simple find command....

find / -file core -exec mv /archive/core_files {} \;


Just a thought,
Rita
Ronald Cogen
Frequent Advisor

Re: Script to rename core files and put them in an archive

Thank you all for the quick reply.

Rita, that was the first thing that came to my mind. But, if you try it out and think about it carefully you'll also see why it won't work.

Leif, I tried it but got an error message. I'll have to look into it. The idea is great . I hadn't thought about piping it using 'tr'.

Dieter, it looks like it will work. Il'll try it out tomorrow.

Bill, you noticed my typing errors. Thanks for your good suggestions.

This is what it looks like now and it works:

DATETIME=`date +%y%m%d%H%M`
ARCHIVE_DIR="/archive/core_files/"
for file in `find / -name core`
do
name=`echo $file | awk '{print substr( $0,length-3 )}'`
target_path=`echo $file | awk '{print substr( $0,1,length-5 )}'`
target_name_path=`echo $target_path | awk 'gsub ( "/","_") { print $0} '`
target_own_grp=`ls -al $file | awk '{ print $3"_"$4 }'`
mv $file $ARCHIVE_DIR$name$target_name_path"_"$target_own_grp"_"$DATETIME
done

Ron
I've been down so long it looks like up to me
Shannon Petry
Honored Contributor
Solution

Re: Script to rename core files and put them in an archive

Just a quick note for getting name dir info...

name=`basename $FOUND_CORE_FILE`
dir=`dirname $FOUND_CORE_FILE`

Seems a bit easier than stripping with awk anyway.

Regards,
Shannon
Microsoft. When do you want a virus today?
Jordan Bean
Honored Contributor

Re: Script to rename core files and put them in an archive


Will this work?

find / -type f -name core | while read core; do mv $core /archive/core_files/$(echo $core | sed -e 's%/%_%g'); done

Geoff Wild
Honored Contributor

Re: Script to rename core files and put them in an archive

I would add a "type" to your find command:

find / -name core -type f

Just in case some user or application has a directory called "core".
And yes, we have one - on our oracle/sap servers there is a core dir that contains what they refer to as "core" components.

After all, you wouldn't want to move a directory to your archive...

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Leif Halvarsson_2
Honored Contributor

Re: Script to rename core files and put them in an archive

Hi

A small modification (delete the "." in the beginning of the filenames).

ARCHIVE_DIR="/tmp/core_files/"

for file in `find . -name core`
do
mv $file ${ARCHIVE_DIR}`echo $file |tr "/" "_" | tr -d "."`
done

I created a small directory tree with a "core" file in each directory and run the script. In the "ARCHIVE_DIR" I got:

core_files:
_aax_bbx_core _aax_core _core
Rick Beldin
HPE Pro

Re: Script to rename core files and put them in an archive

Just an idea... 11.x has a kernel param that will append the pid to the end of the core name, so instead of just 'core' you would end up with core.nnnn. This is helpful when cores would overwrite each other.

To turn it on:

echo "core_addpid /w 1" | adb -w /stand/vmunix /dev/kmem
Necessary questions: Why? What? How? When?