2

Why is this command not copying the 17 .png files into a sub-directory within my current directory?

$ find . -maxdepth 1 -mtime -3 | wc -l
17
$ find . -maxdepth 1 -mtime -3 -print0 | grep -z loader | xargs -r0 cp -t ./user_name_tutorial/
$ find . -maxdepth 1 -mtime -3 -print0
../Plugins.png./Screenshot from 2015-08-06 22:30:56.png./Screenshot from 2015-08-06 22:28:21.png./Plugins_ex-in-post.png./Screenshot from 2015-08-06 22:31:14.png./Pages_visit_site.png./user_name_tutorial./Page_edit.png./Screenshot from 2015-08-06 22:28:52.png./Page_edit-visual.png./Plugins-google-maps_v2-example.png./Screenshot from 2015-08-06 22:29:27.png./Plugin_google_apps.png./Screenshot from 2015-08-06 22:30:44.png./Media_uploads.png./Plugins_add_new.pnguser_name@COMP_NAME-X:~/Pictures$ 
2
  • So how many .png files did it actually copy? What is grep -z loader intended for?
    – yaegashi
    Aug 10, 2015 at 3:25
  • please disregard the grep loader command that was a former file name
    – phillipsk
    Aug 10, 2015 at 17:21

1 Answer 1

4

You're showing us the output of

find . -maxdepth 1 -mtime -3 -print0

What's the output of

find . -maxdepth 1 -mtime -3 -print0 | grep -z loader

?  I don't see any file names containing the word loader in the output that you did show.  If the output from the grep (which is the input to xargs) is nothing, then, naturally, nothing will be copied.


Also,

  • Rather than find … | grep foo, you can say find … -name "*foo*" and save a process.
    Except:

    I notice that you used -maxdepth 1, which (as you know) restricts the search to the current directory.  Without that, find would look into subdirectories, and find … | grep foo would give you all files in any subdirectory named food_fight, football, or pound_foolish, regardless of the file's name, since the grep would be looking at the entire (.-relative) pathname, rather than just the filename.

  • -maxdepth 1 doesn't prevent find from reporting the names of subdirectories in the current directory — so, if you had a subdirectory named loader (or other_loader_files), its name would be passed to cp — which would just get you an error message, since you didn't say cp -r, but there might be similar situations where that could catch you by surprise and do something nasty.
  • You can avoid the above (potential) problem by saying -type f.
  • I realize that you used -mtime -3, which (as you know) restricts the search based on modification date.  Note that, if you weren't using a search-restricting test such as -mtime -3 or -type f, using find … -maxdepth 1 is pretty much equivalent to using a wildcard (commonly known as a "glob" or a filename expansion pattern), as in

    cp -t ./user_name_tutorial/ *loader*
    

    or

    cp *loader* ./user_name_tutorial/
    
1
  • The other guy makes a valid point — if doesn't make sense to use -print0 when you're just writing find's output to the terminal for debugging purposes. Aug 10, 2015 at 2:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .