How to add filepath to filename when moving it?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to add filepath to filename when moving it?
# 1  
Old 05-11-2011
How to add filepath to filename when moving it?

Hi

I have a cron job setup that searches for 'core dump' files in a fixed set of directories and moves the core files to a separate file system.
I am able to add date and time info to the file name.
How can I add the source directory path to the file name?
In case anyone is wondering why I need to know this, it's because the support personnel demand to know the location where the core was collected from.

Here's the command in my cron job:
find /var/mqsi/ -name core -type f -exec mv '{}' /corefile/core_$(date +%Y%m%d-%H%M%S) \;

Thanks in advance.
dexter126
# 2  
Old 05-11-2011
You'll need to incorporate a processing block.
For example (untested):

Code:
find /var/mqsi/ -type f -name core -print | while read filename
do
      # Converted solidus in filename to underscore
      location=`echo "${filename}"|sed -e "sX/X_Xg"`
      mv "${filename}" "/corefile/core_$(date +%Y%m%d-%H%M%S)_${location}"
done

Before removing a file called "core" it is important to know whether it contains a core dump. Use the unix "file" command to determine the file type. When you type "man core" just imagine whether there are any vulnerable files. Some unix systems have a critical package called "core" - be careful.

To be more specific we would need to know precisely what Operating System and Shell you have. The output from "file core" on a typical core file is very helpful.
# 3  
Old 05-11-2011
Code:
find /var/mqsi/ -name core 2>/dev/null | while read x; do d=$(echo ${x%/*} | sed 's/\//_/g'); echo "$x" "/corefile/core$d"$(date +%Y%m%d-%H%M%S);done

Replace echo with mv if you're OK with this solution.
This User Gave Thanks to tukuyomi For This Post:
# 4  
Old 05-11-2011
Quote:
Originally Posted by dexter126
Hi

I have a cron job setup that searches for 'core dump' files in a fixed set of directories and moves the core files to a separate file system.
I am able to add date and time info to the file name.
How can I add the source directory path to the file name?
In case anyone is wondering why I need to know this, it's because the support personnel demand to know the location where the core was collected from.

Here's the command in my cron job:
find /var/mqsi/ -name core -type f -exec mv '{}' /corefile/core_$(date +%Y%m%d-%H%M%S) \;

Thanks in advance.
For Solaris:
Code:
man coreadm

For Linux, this might work:

Capturing core files in Red Hat Enterprise Linux | Golden Apple Enterprises Ltd.
# 5  
Old 05-12-2011
Quote:
Originally Posted by methyl
To be more specific we would need to know precisely what Operating System and Shell you have. The output from "file core" on a typical core file is very helpful.
My OS is SLES v11.
dexter126
# 6  
Old 05-17-2011
Quote:
Originally Posted by tukuyomi
Code:
find /var/mqsi/ -name core 2>/dev/null | while read x; do d=$(echo ${x%/*} | sed 's/\//_/g'); echo "$x" "/corefile/core$d"$(date +%Y%m%d-%H%M%S);done

Replace echo with mv if you're OK with this solution.
Worked flawlessly. All I added was an underscore before the date variable.
Thanks a lot Tukuyomi.
dexter126
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract filepath names between two strings

OS : Fedora Linux 26 Shell : bash I have a file with around 5000 lines like below. file /usr/share/icons/Papirus/16x16/actions/papirus-icon-theme-20180501-1.noarch conflicts with file ... file /usr/share/icons/Papirus/16x16/actions/align-horizontal-left-to-anchor.svg conflicts between... (7 Replies)
Discussion started by: John K
7 Replies

2. UNIX for Beginners Questions & Answers

Copy filepath along with filename at end of another file

Hi , Please help me out for the below problem - I have 2 files in a directory - $ ls -ltr total 4 -rwx------+ 1 abc Domain Users 615 May 31 17:33 abc.txt -rwx------+ 1 abc Domain Users 0 May 31 17:33 ll.sh I want to write the filename of abc.txt along with the directory to the... (2 Replies)
Discussion started by: Pratik4891
2 Replies

3. UNIX for Dummies Questions & Answers

finding and moving files based on the last three numerical characters in the filename

Hi, I have a series of files (upwards of 500) the filename format is as follows CC10-1234P1999.WGS84.p190, all in one directory. Now the last three numeric characters, in this case 999, can be anything from 001 to 999. I need to move some of them to a seperate directory, the ones I need to... (5 Replies)
Discussion started by: roche.j.mike
5 Replies

4. Solaris

How to get the filepath and filenames in pfiles PID command?

Hi, When I use pfiles PID, it displays the below output and i just see the inode, can i able to get the full path and file name? pl help me on this. Current rlimit: 8192 file descriptors 0: S_IFCHR mode:0620 dev:308,0 ino:12582968 uid:1001378434 gid:7 rdev:24,26 O_RDWR ... (3 Replies)
Discussion started by: balamv
3 Replies

5. Shell Programming and Scripting

Problem while using grep (multi-level) with the space-separated filepath.

Hi, I've been trying to use grep to find out all the files which have two particular patterns in it (both pattern1 AND pattern2). I have a script to do the same, in which I'm getting the output of the first grep (with -l option) which contains the list of file paths and I'm trying to search for... (3 Replies)
Discussion started by: NanJ
3 Replies

6. Shell Programming and Scripting

moving files with spaces in filename from one directory to another

Hello, When I run following script #!/bin/bash cd ~/directory1 mv `ls -trF | grep -v / | tail -10 ` ~/directory2 works fine with filenames not having any space but runs into issues with filenames that have spaces tried with $file variable still doesnot work. Can someone help me (4 Replies)
Discussion started by: asakhare
4 Replies

7. Shell Programming and Scripting

Grab a number at end of filepath

Hello, I have a file path such as: /path/to/whatever/30 and I want to get the number off the end. My problem is that there might be other numbers in the path and the last number can be 1 or 2 digits. I tried something like: sed 's/.*\(\/\{1,2\}\).*/\1/' which seems to work fine for single digit... (7 Replies)
Discussion started by: phreezr
7 Replies

8. Shell Programming and Scripting

tar: extract single file to different filepath

Hi, This is my first post here - I'm hoping I can get some help! I have searched these forums and othersand not getting anything that works. I am trying to extract a single file from a tar archive to a diffierent location than it will default to. For example my tar log shows me ... a... (3 Replies)
Discussion started by: littleIdiot
3 Replies

9. Shell Programming and Scripting

splitting up of absolute filepath

Hi, I am using the bash shell on suse.I need to split the absolute file path into filename and directory path. eg: /cas/43fg/temp/sample.txt i want to split the above into sample.txt(file) and /cas/43fg/temp(directory path) I want to do this by... (5 Replies)
Discussion started by: DILEEP410
5 Replies

10. UNIX for Dummies Questions & Answers

Moving files by splitting the path embedded in the filename

Hello All. I am having a directory /tmp/rahul which contains many files in the format @#home@#rahul@#programs@#script.pl where /home/rahul/programs is the directory where the script.pl file is to be placed. I have many files in this format. What i want is a script which read these... (7 Replies)
Discussion started by: rahulrathod
7 Replies
Login or Register to Ask a Question