Re: Weird behavior with find command when tarring files

From: Dave Herring <gdherri_at_gmail.com>
Date: Mon, 17 Dec 2018 16:44:52 -0600
Message-ID: <CAFN=diByvdhWSAJewy_v80PshaMZuvJQ4Dw=hL5iZmTOCYoX7g_at_mail.gmail.com>



Thanks for the tips with "xargs", Jared, although I don't quite understand what "xargs" is doing when "-0E" are involved. The following was done on RHEL6.6: Create 5 randomly named files with "x_" prefix.

% for ((i=1; i<=10; i++)); do touch x_`date +%N`.txt; done

% find . -maxdepth 1 -type f -print0

./x_287598645.txt./x_295644322.txt./x_303214626.txt./x_310464579.txt./x_317687226.txt./x_325071789.txt./x_332189678.txt./x_339226164.txt./x_346212508.txt./x_353403282.txt

Show that a "find" on "y_" names piped to "xargs -0 ls" still shows all files. This is because "ls" doesn't require [FILE] to be listed.

% find . -maxdepth 1 -type f -name 'y_*' -print0 | xargs -0 ls

x_287598645.txt x_303214626.txt x_317687226.txt x_332189678.txt x_346212508.txt

x_295644322.txt x_310464579.txt x_325071789.txt x_339226164.txt x_353403282.txt

Same test using "rm -v". This fails because "rm" requires some value for it's parameter [FILE].

% find . -maxdepth 1 -type f -name 'y_*' -print0 | xargs -0 rm -v

rm: missing operand

Try `rm --help' for more information.

This is where I'm not clear. I'd rather be safe and make sure "rm" is given a value but:

find . -maxdepth 1 -type f -name 'y_*' -print0 | xargs -0E rm -v

xargs: invalid option -- 'v'

Usage: xargs [-0prtx] [--interactive] [--null] [-d|--delimiter=delim]

       [-E eof-str] [-e[eof-str]] [--eof[=eof-str]]

       [-L max-lines] [-l[max-lines]] [--max-lines[=max-lines]]

       [-I replace-str] [-i[replace-str]] [--replace[=replace-str]]

       [-n max-args] [--max-args=max-args]

       [-s max-chars] [--max-chars=max-chars]

       [-P max-procs] [--max-procs=max-procs] [--show-limits]

       [--verbose] [--exit] [--no-run-if-empty] [--arg-file=file]

       [--version] [--help] [command [initial-arguments]]

It looks like "xargs" is assuming arguments for "rm" apply to itself and that only happens when the "-E" argument is used. See the following 2 examples that run against files that ARE found:

% find . -maxdepth 1 -type f -name 'x_*5*' -print0 | xargs -0E rm -v

xargs: invalid option -- 'v'

Usage: xargs [-0prtx] [--interactive] [--null] [-d|--delimiter=delim]

       [-E eof-str] [-e[eof-str]] [--eof[=eof-str]]

       [-L max-lines] [-l[max-lines]] [--max-lines[=max-lines]]

       [-I replace-str] [-i[replace-str]] [--replace[=replace-str]]

       [-n max-args] [--max-args=max-args]

       [-s max-chars] [--max-chars=max-chars]

       [-P max-procs] [--max-procs=max-procs] [--show-limits]

       [--verbose] [--exit] [--no-run-if-empty] [--arg-file=file]

       [--version] [--help] [command [initial-arguments]]

% find . -maxdepth 1 -type f -name 'x_*5*' -print0 | xargs -0 rm -v

removed `./x_287598645.txt'

removed `./x_295644322.txt'

removed `./x_310464579.txt'

removed `./x_325071789.txt'

removed `./x_346212508.txt'

removed `./x_353403282.txt'

That last example works as I expected.

Dave

On Wed, Dec 5, 2018 at 9:39 PM Jared Still <jkstill_at_gmail.com> wrote:

> yes, the empty input can be dangerous, use -E to deal with that
>
> > find tmp -name notexist | xargs ls
> 12c-connect.txt blog git-ignore-template
> nohup.out snow.sh
> DBI so github-config-template nwt
> snow2.sh
> Desktop build ht-detect.sh ogg
> sounds
> Documents c html old
> sqlnet.log
> Downloads cdrom imagejpeg_0.jpg
> ora112304a.jks.com sqltext.txt
> FlameGraph checking iostat
> oracle ssh-find-agent
> ...
> jkstill_at_poirot ~ $
>
>
>
> jkstill_at_poirot ~ $
> > find tmp -name notexist | xargs -E ls -l
>
> jkstill_at_poirot ~ $
> > find tmp -name j | xargs -E ls -l
> tmp/run_stats_load/j
> tmp/performance/th/j
> tmp/sqlstats/perl/j
> tmp/sqlstats/ddl/j
> tmp/j
> tmp/cursor/j
> jkstill_at_poirot ~ $
> >
>
> now imagine the ls command was instead rm...
>
>
> On Fri, Oct 26, 2018 at 06:42 <post.ethan_at_gmail.com> wrote:
>
>> Thanks for the tip, I didn’t know about -print0. Is this option generally
>> available on older Unix (hpux, aix, solaris)? I have been using double
>> quotes in most of my code for a couple years around file variables in
>> anticipation of being able to run on Windows environments. Have not dare
>> tested that yet however.
>>
>>
>>
>> One other thing to note here. Be really careful with xargs. I believe it
>> executes on null input too. I don’t recall the exact issue but I have been
>> burned at least twice and good enough to recall you need to be very careful
>> with it if you are deleting or modifying things. Also stay away from “find
>> .” and make sure you try to use a full path there. This is all obvious
>> maybe and only used here for the sake of examples but throwing it out there
>> for the newbies if any are reading.
>>
>>
>>
>> Ethan Post
>>
>> https://www.linkedin.com/in/ethanraypost/
>>
>> https://arclogicsoftware.com
>>
>> https://twitter.com/poststop
>>
>>
>>
>>
>>
>> *From:* oracle-l-bounce_at_freelists.org <oracle-l-bounce_at_freelists.org> *On
>> Behalf Of *Jared Still
>> *Sent:* Friday, October 26, 2018 1:49 AM
>> *To:* Hameed, Amir <Amir.Hameed_at_xerox.com>
>> *Cc:* oracle-l_at_freelists.org
>> *Subject:* Re: Weird behavior with find command when tarring files
>>
>>
>>
>> In addition to the other fine comments, you should get in the habit of
>> dealing with filenames that have spaces.
>>
>>
>>
>> It seems impossible to anymore to avoid this, as you don't always have
>> control over it.
>>
>>
>>
>> Here's how: use the '-print0' option for find, and the '-0' option for
>> xargs.
>>
>>
>>
>> find . type f -mmin +239 -print0 | xargs -0 tar -cvf /tmp/test.tar
>>
>>
>>
>>
>>
>>
>> Jared Still
>> Certifiable Oracle DBA and Part Time Perl Evangelist
>>
>> Principal Consultant at Pythian
>>
>> Pythian Blog http://www.pythian.com/blog/author/still/
>>
>> Github: https://github.com/jkstill
>>
>>
>>
>>
>>
>> --
> Jared Still
> Certifiable Oracle DBA and Part Time Perl Evangelist
> Principal Consultant at Pythian
> Pythian Blog http://www.pythian.com/blog/author/still/
> Github: https://github.com/jkstill
>
>
>

-- 
Dave

--
http://www.freelists.org/webpage/oracle-l
Received on Mon Dec 17 2018 - 23:44:52 CET

Original text of this message