Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Chaining together exec within find

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 1 Week Ago
jeffs42885 jeffs42885 is offline
Registered User
 
Join Date: Nov 2010
Last Activity: 23 June 2017, 2:29 PM EDT
Location: PA
Posts: 89
Thanks: 14
Thanked 0 Times in 0 Posts
Chaining together exec within find

I need to do the following with a find command on my AIX box

Find all files that are -type f

Then do the following steps:-
  • Take a listing of them, and write them to a log in /directory/backup/log
  • Tar them up in /directory/backup/tar
  • and remove the files.

Here is what I have so far:


Code:
find /directory/files -type f -exec ls -latr >> /directory/backup/log/2.log \; -exec tar -cvf * /directory/backup/tar/testtar.tar * \; -exec rm -f {} \;

When I have three files-


Code:
 
 Testfile1.text
 Testfile2.text
 Testfile3.text

It does the following-

Removes the first file
Tars up the second two
Writes the second two to a log in the directory the find statement is being executed in

What could I be doing wrong?

Thanks in advance

Last edited by rbatte1; 1 Week Ago at 10:42 AM.. Reason: Changed fir st CODE tags to ICODE tags for inline-code and added a bullet list.
Sponsored Links
    #2  
Old Unix and Linux 1 Week Ago
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 27 June 2017, 4:29 PM EDT
Location: Saskatchewan
Posts: 22,256
Thanks: 1,105
Thanked 4,200 Times in 3,885 Posts
What -exec tar -cvf * /something/ * does only depends on your current directory, because the *'s are evaluated by the shell before it is run. exec is not a shell and would not handle the *'s if you escaped them, either.

What is that one in particular meant to do?
Sponsored Links
    #3  
Old Unix and Linux 1 Week Ago
jeffs42885 jeffs42885 is offline
Registered User
 
Join Date: Nov 2010
Last Activity: 23 June 2017, 2:29 PM EDT
Location: PA
Posts: 89
Thanks: 14
Thanked 0 Times in 0 Posts
Thanks for the input.

I want it to tar up the results of the find command. There will be other parameters, such as size, age, etc.. But for this example I only included -type f

Last edited by rbatte1; 1 Week Ago at 10:43 AM.. Reason: Changed CODE tags to ICODE tags
    #4  
Old Unix and Linux 1 Week Ago
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 27 June 2017, 4:29 PM EDT
Location: Saskatchewan
Posts: 22,256
Thanks: 1,105
Thanked 4,200 Times in 3,885 Posts
>> /directory/backup/log/2.log is similarly handled by shell, not exec, but in this case probably does close to what you want, except it will capture all stdout, not just ls. This may not matter if nothing else prints to stdout.
Sponsored Links
    #5  
Old Unix and Linux 1 Week Ago
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 27 June 2017, 4:29 PM EDT
Location: Saskatchewan
Posts: 22,256
Thanks: 1,105
Thanked 4,200 Times in 3,885 Posts
I still don't understand what that tar command is intended to do.

What filename is it supposed to create, and where?
Sponsored Links
    #6  
Old Unix and Linux 1 Week Ago
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 27 June 2017, 4:29 PM EDT
Location: Saskatchewan
Posts: 22,256
Thanks: 1,105
Thanked 4,200 Times in 3,885 Posts
Since tar may be run multiple times, you need to use the append option, not the create option.

How about:


Code:
$ tar -rf archive.tar # Create empty tar file to append to

$ find testout -type f -exec echo ls -latrd '{}' ';' -exec echo tar -rvf archive.tar '{}' ';' -exec echo rm '{}' ';'

ls -latrd testout/testfile1
tar -rvf archive.tar testout/testfile1
rm testout/testfile1
ls -latrd testout/testfile2
tar -rvf archive.tar testout/testfile2
rm testout/testfile2
ls -latrd testout/testfile3
tar -rvf archive.tar testout/testfile3
rm testout/testfile3

# remove echos to actually run these commands instead of printing them

If you have GNU find, you can use + instead of ; for increased efficiency as it will bundle several files into each call:


Code:
$ find testout -type f -exec echo ls -latrd '{}' '+' -exec echo tar -rvf /absolute/path/to/archive.tar '{}' '+' -exec echo rm '{}' '+'

ls -latrd testout/testfile1 testout/testfile2 testout/testfile3
tar -rvf /absolute/path/to/archive.tar testout/testfile1 testout/testfile2 testout/testfile3
rm testout/testfile1 testout/testfile2 testout/testfile3

$


Last edited by Corona688; 1 Week Ago at 02:45 PM..
Sponsored Links
    #7  
Old Unix and Linux 1 Week Ago
jeffs42885 jeffs42885 is offline
Registered User
 
Join Date: Nov 2010
Last Activity: 23 June 2017, 2:29 PM EDT
Location: PA
Posts: 89
Thanks: 14
Thanked 0 Times in 0 Posts
Here is what I am trying to do, and I could be taking the wrong approach.

I have a bunch of directories that need to have their files purged, based on certain criteria. For example.

/folder/one <- Delete files that are 30 days old, and more than 2MB
/folder/two <- Delete files that are 7 days old, and have the extension PDF
/folder/three <- Delete files that are 7 days old, extension PDF, more than 2MB

We have a script that runs that does a basic find, and exec -rm -f, but we want to add logging, and take a compressed backup of the files, and throw them into a preserve directory for X days until we need them.

Any suggestions? Am I taking the right approach.

Thanks
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
find: missing argument to `-exec' while redirecting using find in perl ramkumarselvam Shell Programming and Scripting 2 12-16-2011 06:48 PM
Find and exec disconnectus Ubuntu 1 10-12-2011 07:29 AM
Find Exec prasanna1157 UNIX for Dummies Questions & Answers 1 09-27-2010 07:33 AM
Using MV FIND and -EXEC degraff63 Shell Programming and Scripting 0 05-05-2009 03:54 AM
find and exec big123456 UNIX for Advanced & Expert Users 2 01-02-2006 11:10 AM



All times are GMT -4. The time now is 04:50 PM.