Solved

Possible to Error Check 'Find -exec cp' command?

Posted on 2013-02-05
7
399 Views
Last Modified: 2013-02-07
Is it possible to error check the results of the -exec option when used with the find command?

for example, running:
find -mmin -10 -exec cp {} /somefolder \;-

Open in new window

in a folder that has multiple files that are less than 10 min and the '/somefolder' does not exist errors out for each file found with "no such file or directory exists"

if I error check with echo $? I receive 0 which is due to the find command being succesfull.  The -exec command however is not and this is what I would like to error check for.

Anyway of doing this with out offloading to another script?
0
Comment
Question by:jwmcf1
  • 4
  • 3
7 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 38857506
Why don't you do something like

[[ -d /somefolder ]] && find -mmin -10 -exec cp {} /somefolder \;

Open in new window


This would make sure that find is only executed when the folder exists
0
 

Author Comment

by:jwmcf1
ID: 38857567
Thank you for the recomendation.  However, this line of code is just a small part of a script.

Really what I am attempting to do is prevent the deletion of a file if the copy of a file errors out for any reason.

basically the script is something like:

#first find the newest files and then copy them
find -mtime -2 -exec cp {} /somefolder \;
if [ "$?" = "0" ]
then 
#delete the current oldest file which in this case is +30 days
find -mtime +30 -exec rm {} \;
else
echo "error during copy of file.  stop script and prevent any deletions until fixed blah bl.."
fi

Open in new window


ultimate goal is to maintain the most recent 30 days worth of files and then copy these files to another location for backup and after backup delete the oldest file.  But before I delete anything I want to make sure the copy has actually happend.
0
 
LVL 31

Accepted Solution

by:
farzanj earned 500 total points
ID: 38857905
From your script, first your find command looks wrong, because you need to mention a path as well.  So I suppose the path is current directory, so I will put dot. You can put any path instead of dot
Try this:

files="$(find . -mtime -2)"
for file in "$files"
do
    cp $file /somefolder
    if [[ $? == 0 ]]
    then
           ... other find command
    fi
done

Open in new window

0
Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

 
LVL 31

Expert Comment

by:farzanj
ID: 38857909
Your logic is still a little fuzzy.  Which file to check and which one to delete?  Any thing in the name or how would I tell?
0
 

Author Comment

by:jwmcf1
ID: 38860448
I will try testing using a for loop as you have shown.

As far as logic, I copy the most recent file and the cp command is being checked for error and if no error from the copy;  file older than 30 days is being deleted.

One file copied, one file deleted.  Idea is to always have the last 30 days worth of files in the local path and while also having an archive (the daily copy) at another location (remote server).
0
 
LVL 31

Expert Comment

by:farzanj
ID: 38860756
I understand that, but is the criteria only about timestamp?  Are there categories of files?  Like abc_old and abc_new_date and xyz_old and xyz_new_date, are you have to get the latest in the each category.  This would have been more realistic.  

If this is the only criteria, then you can have
ls -lsr | tail -1 | awk '{print $NF}'  gets you the lastest file and if you remove option r from ls, you get the oldest.
0
 

Author Comment

by:jwmcf1
ID: 38864818
The for loop works to accomplish this task.  Thank you for the help.

this code does what i am looking for
#!/bin/bash

logfile=/usr/local/bin/exportlog.txt
addresses=somebody@email.com
todaysdate=$(date +"%Y-%m")

echo $todaydate
date > $logfile
echo " " >> $logfile 2>&1

umount /mnt/backup >> $logfile 2>&1
mount -t cifs -o username=someuser,password=somepass //backupserver/E /mnt/backup >> $logfile 2>&1
mkdir /mnt/backup/exporttest/$(hostname)/$todaysdate >> $logfile 2>&1

echo $(date)
echo "Starting Export Backup" >> $logfile 2>&1
echo " " >> $logfile 2>&1

echo "Copying..." >> $logfile 2>&1
cd /data/
files="$(find -mtime -1)"
for file in "$files"
do
   cp --parents --preserve -a $file /mnt/backup/exporttest/$(hostname)/$todaysdate >> $logfile 2>&1
   if [[ $? == 0 ]]
   then
    find . -mtime +30 -exec rm {} \; >> $logfile 2>&1
   else
    cat /usr/local/bin/exportlog.txt | mail -s "EXPORT COPY ERROR" $addresses
   fi
done

cd /
sleep 1m
umount /mnt/backup

echo "Finished Exports Backup."
echo $(date)

Open in new window

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
busybox bash/ash - bash -x | grep 19 187
PowerShell Get-Help 10 84
powershell code to list items in dropdown 6 102
How to make a Bash alias that takes parameters 5 84
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 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