Display find results, and pipe to xargs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Display find results, and pipe to xargs
# 1  
Old 06-19-2011
Display find results, and pipe to xargs

I have an overnight script which runs across a large directory to repair permissions and ownership. I also have this command output the list of files affected so that cron can email these as a log file. Previously I had the command in the form:
Code:
find /path/to/files -not -user myname -print -exec chown myname "{}" \;

As it is a long command sometimes I never receive a log email even though the ownership and permissions have changed, so I want to make the script more efficient. As such I have changed the command to:
Code:
find /path/to/files -not -user myname -print0 | xargs -0 chown myname

Is there anything I can do to that command so that it sill lists the files on standard output so that they will be emailed by cron in addition to piping them to xargs?

I cannot include the -print action in the find command as this will also be piped to xargs. Likewise if I include an -exec action with an echo command. Also I do not want to use the -t option with xargs as I just want the list of files and not the chown commands it generates.

The best solution I have been able to come up with is to use tee to ssave output in a temporary file to display afterwards, but I am hoping there is better solution.

Thanks,

Michael.

Last edited by Franklin52; 06-19-2011 at 11:34 AM.. Reason: Please use code tags, when posting data and code samples, thank you
# 2  
Old 06-19-2011
Try:
Code:
find /path/to/files -not -user myname -print0 | xargs -i -0 sh -c 'chown myname {}; echo {}'

# 3  
Old 06-19-2011
I have just tried that, and although it works, it is executing the commands individually for each file. Presumably this means it is not offering any performance advantage over that just using find's -exec action, if not less because of having to spawn the shell as well? The only reason for switching to use xargs was so that it can pass multiple files to chown reducing the number of times it would be called.

Thanks for the help though.

Michael.
# 4  
Old 06-19-2011
Code:
find /path/to/files -not -user myname -print -exec chown myname "{}" +

This User Gave Thanks to jlliagre For This Post:
# 5  
Old 06-19-2011
That is exactly what I wanted. Everything tested okay and I have been able to update my script.

I feel so stupid now. I have read the find man page so often I should have known that.

Many thanks,

Michael.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pipe output a command to another using xargs

xargs work great when a command gives multiple line output which can be input to another. In my case it is not working coz the second command uses two words in it. $ scr.sh gives output like 193740 638102 375449 .. .. another command takes these number as inputs. it works great... (1 Reply)
Discussion started by: mahesh113
1 Replies

2. UNIX for Dummies Questions & Answers

xargs vs. pipe

I have been using unix on and off for a number of years. I am not a sys admin. I use what I need. I have googled this, but I really can't figure out what is the difference between using xarg and just using a regular pipe? Why do I need to include xarg sometimes and how do I know when I need it? (2 Replies)
Discussion started by: guessingo
2 Replies

3. Shell Programming and Scripting

How to launch results of a pipe?

Good afternoon, I have just messed up and deleted some directories on my UNIX machine. I would now want to know which packages are impacted by this. Therefore I have a look in the file "/var/sadm/install/contents" (which contains the filenames/directory names for each installation package). After... (2 Replies)
Discussion started by: scampsd
2 Replies

4. Programming

difference bewteen pipe, xargs, and exec

I have read several docs on these on the web and looked at examples. I can't figure out the difference. In some cases you use one or the other or you combine them. can someone help me understand this? (1 Reply)
Discussion started by: guessingo
1 Replies

5. Shell Programming and Scripting

How can I execute the results of a pipe?

Hello, I'm using csh. I have a file named "setup" that I normally source, e.g. source setupI'd like a one-liner that sources this file, but excluding lines that contain "DEBUG", e.g. cat setup | grep -v DEBUG | sourceOf course, the above does not work. How can I execute the results of a... (5 Replies)
Discussion started by: acheong87
5 Replies

6. Solaris

Piping results of 'ls' to 'find' using 'xargs'

I'm trying to get a count of all the files in a series of directories on a per directory basis. Directory structure is like (but with many more files): /dir1/subdir1/file1.txt /dir1/subdir1/file2.txt /dir1/subdir2/file1.txt /dir1/subdir2/file2.txt /dir2/subdir1/file1.txt... (4 Replies)
Discussion started by: MartynAbbott
4 Replies

7. UNIX for Dummies Questions & Answers

Pipe results of Grep Command to LS Comand

I'm using the command grep -l XYZ to get a list of files containing the string XYZ. Then I using the comand ls -l ABC to get the create date timestamp of the each file. I've tried combining the comands using the pipe command, grep -l XYZ | ls -l, but its not working. What am I doing wrong? (3 Replies)
Discussion started by: jhtate
3 Replies

8. AIX

Removing a filename which has special characters passed from a pipe with xargs

Hi, On AIX 5200-07-00 I have a find command as following to delete files from a certain location that are more than 7 days old. I am being told that I cannot use -exec option to delete files from these directories. Having said that I am more curious to know how this can be done. an sample... (3 Replies)
Discussion started by: jerardfjay
3 Replies

9. Shell Programming and Scripting

String substitution on find results inside exec/xargs

What I'm trying to do is perform a copy, well a ditto actually, on the results of a find command, but some inline string substitution needs to happen. So if I run this code find ./ -name "*.tif" I get back these results. .//1234567.tif .//abcdefg.tif Now the action from exec or xargs I... (2 Replies)
Discussion started by: myndcraft
2 Replies

10. UNIX for Dummies Questions & Answers

Filter results through pipe with grep

ls -ltr | grep string How can I use regular expressions to filter the results provided even more. I am using the above command as a reference. (1 Reply)
Discussion started by: ckandreou
1 Replies
Login or Register to Ask a Question