From: James Harris on
On 17 May, 19:57, "Bill Cunningham" <nos...(a)nspam.invalid> wrote:
> "James Harris" <james.harri...(a)googlemail.com> wrote in message
>
> news:fb2f79c9-77b4-4d75-ba94-5fc01dbae464(a)o12g2000vba.googlegroups.com...
>
> Since you mention bash I assume you are on a Unix-like OS. Check out
> xargs. You can use it with rm but remember to use it carefully and
> consider the -0 (zero) option if your files have spaces in their
> names.
>
> I have text like this,
>
> /usr
> /usr/lib
> /usr/lib/gcc
> /usr/lib/gcc/   and then this goes one until all the files have been placed.

You could delete these all with rm -r /usr as they are all in the /usr
folder but I'll assume your requirements are more complex.

> Can I include all the text into a variable and then just rm -fr ...var
> and get rid of that? xargs is a little complicated to the first time viewer.

True, like most Unix commands xargs can be complex. And if you are
using commands such as rm -rf you don't want to get something wrong or
you'll delete things you didn't mean to. Try the command with "echo"
first rather than "rm -rf" then you get to confirm it is working. For
example, if your text is in a file called /tmp/my_file_list

cat /tmp/my_file_list | sort -r | xargs -I {} echo I will delete
'"{}"'

This will tell you what files it will delete in which order. If you
are happy with the list you can then change to use rm -rf. A tip:
change it to echo the exact command first and only if you are sure
that's what you want to do remove the echo part so it really does
delete the files. A second tip: start with /tmp/my_file_list having
just a small subset of the files and only add to it as you gain
confidence you are doing the right thing.

BTW, the sort -r sorts the file list into reverse order. This is to
delete any files before the directories that contain them, if you have
both in your list.

Bear in mind that if you rm -rf a directory and you have access to
everything in it then everything in it will be deleted.

James
From: Bill Cunningham on

"James Harris" <james.harris.1(a)googlemail.com> wrote in message
news:29098d3e-1fca-403c-8bbf-66e09c816ce7(a)s41g2000vba.googlegroups.com...

[snip]

cat /tmp/my_file_list | sort -r | xargs -I {} echo I will delete
'"{}"'

This will tell you what files it will delete in which order. If you
are happy with the list you can then change to use rm -rf.

What I get with the line above is an error. Apparently according to bash
there is no -I switch to xargs and I didn't see any such thing in the man
page about xargs.

Bill


From: James Harris on
On 19 May, 21:19, "Bill Cunningham" <nos...(a)nspam.invalid> wrote:
> "James Harris" <james.harri...(a)googlemail.com> wrote in message
>
> news:29098d3e-1fca-403c-8bbf-66e09c816ce7(a)s41g2000vba.googlegroups.com...
>
> [snip]
>
>>   cat /tmp/my_file_list | sort -r | xargs -I {} echo I will delete
>> '"{}"'
>>
>> This will tell you what files it will delete in which order. If you
>> are happy with the list you can then change to use rm -rf.
>
>     What I get with the line above is an error. Apparently according to bash
> there is no -I switch to xargs and I didn't see any such thing in the man
> page about xargs.

That's odd. In the above command chain "-I {}" tells xargs to replace
any occurence of {} with the line from the file. Maybe your version of
xargs uses a different argument to do the same thing. Take a look at
your man page for a replace option. Also, there are some alternatives
documented at

http://www.gnu.org/software/findutils/manual/html_node/find_html/xargs-options.html#xargs-options

James