Find files and execute commands on these files (advanced)
So, I need to find a bunch of files and delete them (this example, but sometimes I need it for something else) and my trusty go-to command has always been:
Works wonders... But:
and run the command above it will produce the following error:
Now... I cannot get the -0 option to work properly, can someone help?
Completely valid mr. Cragun. I agree, too often I forget that the find command has a built in exec possibility and in this example it is valid, however it's not always valid if you for instance need something like:
in this case I need the xargs (or in a more complicated case than this, it was the first fast idea I got). Anyways, I would love to find how I take care of ' and " and spaces in xargs and still get it to work.
Thank you for your solution in this example though
Last edited by Mr.Glaurung; 10-09-2014 at 10:18 AM..
Reason: find command was incorrect in my example.
you can eliminate the pipe to grep if
Concerning your first post
The -0 option to xargs and both -iname and -print0 primaries in find are extensions to the standards that are not available in several implementations. This is a little harder to write, but is both portable and more efficient:
This works even when filenames contain spaces, tabs, quotes (of any kind), and even newlines (which should never be included in a filename, except in examples explaining why newlines should never be used in filenames).
This User Gave Thanks to Don Cragun For This Post:
In the shell, and in UNIX and Linux in general, fork and exec are relatively expensive operations. So:
is more efficient than:
unless there is some reason to believe that grep is so much faster at processing a regular expression to match a filename than find that the overhead needed to write many more filenames into a pipe, fork and exec grep, and have grep read those filenames from the pipe, and perform the same regexp processing that find would have performed; doing it all in find will use fewer processes, pass less data between processes, and use less system memory than using find and grep to do the same work.
On systems that don't have find -print0 and xargs -0, piping a list of filenames that might contain whitespace and/or quote characters is not only less efficient, it is somewhere between harder and impossible to reconstruct the filenames that need to be processed while there is absolutely no problem figuring what filename should be used in find -exec command '{}' + and in find -exec command '{}' other operands \;.
That is why it is usually much more efficient to do all of the processing in find that you can do in find instead of firing up multiple processes in a pipeline to do the same work.
This User Gave Thanks to Don Cragun For This Post:
Hi All,
I need to write a script to find all "*.sh" files in /home file system and if any string find "*.sh" files with the name vijay@gmail.com need to replace with vijay.bhaskar@gmail.com. I just understood about the find the command to search .sh files. Please help me on this.
find / -name... (3 Replies)
Hi,
I am trying to removing the old files which were older than 10 days and same g zipping the files using the shell script.
script was return as follows.
find /jboss7_homes/JBOSS7/SKYLIV??/SKYLIV??_CRM/jboss-eap-7.0/standalone/log -mtime +10 -type f | xargs rm -f
find /cer_skyliv??/log... (6 Replies)
Hi,
I'm writing a post-upgrade script and I want to find which files don't have read and execute to everyone.
I can run a find . ! -perm, but then I have to use a list of the possible permissions (777,775, 755 etc). Is there a more elegant solution?
Thanks (2 Replies)
Hi All,
I need to find all files other than first two files dates & last file date for month and month/year wise list.
lets say there are following files in directory
Mar 19 2012 c.txt
Mar 19 2012 cc.txt
Mar 21 2012 d.txt
Mar 22 2012 f.txt
Mar 24 2012 h.txt
Mar 25 2012 w.txt
Feb 12... (16 Replies)
Find all files in the current directory only excluding hidden directories and files.
For the below command, though it's not deleting hidden files.. it is traversing through the hidden directories and listing normal which should be avoided.
`find . \( ! -name ".*" -prune \) -mtime +${n_days}... (7 Replies)
Hi
I have a requirement like below
I need to sort the files based on the timestamp in the file name and run them in sorted order and then archive all the files which are one day old to temp directory
My files looks like this
PGABOLTXML1D_201108121235.xml... (1 Reply)
Hi, all:
I've got two folders, say, "folder1" and "folder2".
Under each, there are thousands of files.
It's quite obvious that there are some files missing in each. I just would like to find them. I believe this can be done by "diff" command.
However, if I change the above question a... (1 Reply)
I am checking that a file is older than a reference file that I build with a touch command before processing it. If it is not old enough, I want to sleep for an hour and check again.
My problem is if it is old enough to process, I want to exit when I am done, but I cannot find a way to exit... (2 Replies)
I want to log into a remote server transfer over a new config and then backup the existing config, replace with the new config.
I am not sure if I can do this with BASH scripting.
I have set up password less login by adding my public key to authorized_keys file, it works.
I am a little... (1 Reply)
I need to compare 2 diff type of files and find out the duplicate after comparing each types of files:
Type 1 file name is like: file1.abc
(the extension abc could any 3 characters but I can narrow it down or hardcode for 10/15 combinations).
The other file is file1.bcd01abc (the extension... (2 Replies)