Find files and execute commands on these files (advanced)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find files and execute commands on these files (advanced)
# 1  
Old 10-09-2014
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:

Code:
find . -type f -name '*file*' | xargs -I## rm '##'

Works wonders... But:

Code:
touch file\ file\'.txt
touch file.txt
touch file\ file.txt
touch file\".txt
touch file\'\ \".txt

and run the command above it will produce the following error:

Code:
xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option

Now... I cannot get the -0 option to work properly, can someone help?
# 2  
Old 10-09-2014
Why use xargs? Why not just do it in find:
Code:
find . -type f -name '*file*' -exec rm {} +

# 3  
Old 10-09-2014
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:

Code:
find . -type f -name "*.jpg" | grep -i summer | xargs -I## mv '##' ./SummerPictures/

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 Smilie

Last edited by Mr.Glaurung; 10-09-2014 at 10:18 AM.. Reason: find command was incorrect in my example.
# 4  
Old 10-09-2014
Quote:
Originally Posted by Mr.Glaurung
Code:
find . -type f -name "*.jpg" | grep -i summer | xargs -I## mv '##' ./SummerPictures/

you can eliminate the pipe to grep if
Code:
find . -iname \*summer\*.jpg -type f

Concerning your first post

Code:
find . -type f -name '*file*' -print0 | xargs  -0 -I file rm file

# 5  
Old 10-09-2014
Quote:
Originally Posted by Aia
you can eliminate the pipe to grep if
Code:
find . -iname \*summer\*.jpg -type f

Concerning your first post

Code:
find . -type f -name '*file*' -print0 | xargs  -0 -I file rm file

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:
Code:
find . -type f -name '*[sS][uU][mM][mM][eE][rR]*[.][jJ][pP][gG]' -exec mv '{}' ./SummerPictures/ \;

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:
# 6  
Old 10-09-2014
@Don Cragun

Out of the nth times you have written the same about find and the standards, you have never said why it is more efficient. Next time, maybe?
# 7  
Old 10-09-2014
In the shell, and in UNIX and Linux in general, fork and exec are relatively expensive operations. So:
Code:
find . -type f -name name

is more efficient than:
Code:
find . -type f | grep name

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:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find all .sh files in file system and need to replace the string inside .sh files

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)
Discussion started by: bhas85
3 Replies

2. UNIX for Beginners Questions & Answers

Find and removing the old files and zipping the files using shell script

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)
Discussion started by: venkat918
6 Replies

3. Shell Programming and Scripting

Find list of files missing read & execute permission

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)
Discussion started by: Catullus
2 Replies

4. UNIX for Advanced & Expert Users

Find all files other than first two files dates & last file date for month

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)
Discussion started by: Makarand Dodmis
16 Replies

5. UNIX for Advanced & Expert Users

Find all files in the current directory excluding hidden files and directories

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)
Discussion started by: ksailesh1
7 Replies

6. Shell Programming and Scripting

sort the files based on timestamp and execute sorted files in order

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)
Discussion started by: saidutta123
1 Replies

7. Shell Programming and Scripting

Compare 2 folders to find several missing files among huge amounts of files.

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)
Discussion started by: jiapei100
1 Replies

8. Shell Programming and Scripting

Execute multiple commands in a find

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)
Discussion started by: prismtx
2 Replies

9. Shell Programming and Scripting

Can BASH execute commands on a remote server when the commands are embedded in shell

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)
Discussion started by: bash_in_my_head
1 Replies

10. Shell Programming and Scripting

Find duplicates from multuple files with 2 diff types of files

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)
Discussion started by: ricky007
2 Replies
Login or Register to Ask a Question