LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   simple question about bash .. ls and rm combination (http://www.linuxquestions.org/questions/linux-newbie-8/simple-question-about-bash-ls-and-rm-combination-797269/)

ozgur 03-23-2010 08:04 AM

simple question about bash .. ls and rm combination
 
hi guys < and ladies of course, if there is :) > <please don't, i'm just kidding >

my name is Özgür from Turkey (Türkiye) ..

here is my information <if necessary>

Code:

[root@redwin test2]# cat /etc/*-release
LSB_VERSION="1.3"
Red Hat Enterprise Linux AS release 4 (Nahant Update 1)

Code:

[root@redwin test2]# bash --version
GNU bash, version 3.00.15(1)-release (i386-redhat-linux-gnu)
Copyright (C) 2004 Free Software Foundation, Inc.

my question is quite simple ..

why can't i use "ls" with "rm" ..

please check out this work .. (i've just prepared easy example)

Code:

[root@redwin test2]# touch file{1,2,3,4,5,6,7,8,9}
[root@redwin test2]# ll
total 36
-rw-r--r--  1 root root 0 Mar 23 13:56 file1
-rw-r--r--  1 root root 0 Mar 23 13:56 file2
-rw-r--r--  1 root root 0 Mar 23 13:56 file3
-rw-r--r--  1 root root 0 Mar 23 13:56 file4
-rw-r--r--  1 root root 0 Mar 23 13:56 file5
-rw-r--r--  1 root root 0 Mar 23 13:56 file6
-rw-r--r--  1 root root 0 Mar 23 13:56 file7
-rw-r--r--  1 root root 0 Mar 23 13:56 file8
-rw-r--r--  1 root root 0 Mar 23 13:56 file9
[root@redwin test2]# ls -1 file*
file1
file2
file3
file4
file5
file6
file7
file8
file9
[root@redwin test2]# ls -1 file* | rm -rf
[root@redwin test2]# echo $?
0
[root@redwin test2]# ls -l
total 36
-rw-r--r--  1 root root 0 Mar 23 13:56 file1
-rw-r--r--  1 root root 0 Mar 23 13:56 file2
-rw-r--r--  1 root root 0 Mar 23 13:56 file3
-rw-r--r--  1 root root 0 Mar 23 13:56 file4
-rw-r--r--  1 root root 0 Mar 23 13:56 file5
-rw-r--r--  1 root root 0 Mar 23 13:56 file6
-rw-r--r--  1 root root 0 Mar 23 13:56 file7
-rw-r--r--  1 root root 0 Mar 23 13:56 file8
-rw-r--r--  1 root root 0 Mar 23 13:56 file9
[root@redwin test2]#
[root@redwin test2]#
[root@redwin test2]# find . -name 'file*' -exec rm -rf {} \;
[root@redwin test2]# ls -l
total 0
[root@redwin test2]#

i know i know, it's easy to use find and exec rm style :) as you see, i can use it .. but i'm really wondering why doesn't this (ls and rm) work ? or what is that i'm doing wrong ?

thanks ..

MensaWater 03-23-2010 08:25 AM

For a pipe to work the item you're piping into has to accept stdin as input but it doesn't - it requires a definite target.

However you can use the xargs command for this purpose - for each input from a pipe it repeats the command using the input as the target:

ls -1 file* |xargs rm -rf

It isn't clear why you'd do this though since you could more easily simply type: rm -rf file*

ordinary 03-23-2010 08:39 AM

The pipe sends the output of one command to the input of the other. To use the output of one command as the command line arguments for another, use backquotes.

Rather than ls and rm, lets use foo and bar so it doesn't look silly.
Quote:

foo `bar`
That gives foo the output of bar as command line arguments. This can be a very useful and powerful idiom.

Hope this helps,
Phil

ozgur 03-23-2010 09:21 AM

thanks
 
thank you for your explanation :) i think, that's the why xargs command's existance :) for first look, it seems really silly or non logical to me .. thank you again ..


by the way, of course i could (rm -rf file*) :) i was just trying different pipe methods :) is it clear now ? :)


Quote:

Originally Posted by jlightner (Post 3908896)
For a pipe to work the item you're piping into has to accept stdin as input but it doesn't - it requires a definite target.

However you can use the xargs command for this purpose - for each input from a pipe it repeats the command using the input as the target:

ls -1 file* |xargs rm -rf

It isn't clear why you'd do this though since you could more easily simply type: rm -rf file*


MensaWater 03-23-2010 09:44 AM

Quote:

Originally Posted by ordinary (Post 3908914)
foo `bar`

back ticks are deprecated in favor of $(). e.g. foo $(bar)

ordinary 03-23-2010 11:45 AM

Quote:

Originally Posted by jlightner (Post 3908994)
back ticks are deprecated in favor of $(). e.g. foo $(bar)

Didn't know that. I guess I'll stop recommending backquotes in public, but it will be hard to break a 25 year habit, especially on the command line.

Phil

catkin 03-23-2010 11:53 AM

Quote:

Originally Posted by ordinary (Post 3909117)
Didn't know that. I guess I'll stop recommending backquotes in public, but it will be hard to break a 25 year habit, especially on the command line.

For reasons explained here which don't hold good for simple usage on the command line.

MensaWater 03-23-2010 03:21 PM

Quote:

Originally Posted by ordinary (Post 3909117)
Didn't know that. I guess I'll stop recommending backquotes in public, but it will be hard to break a 25 year habit, especially on the command line.

Phil

You and me both. :D

chrism01 03-23-2010 08:13 PM

I know the feeling; I still use `...` for simple ones

ordinary 03-24-2010 08:16 AM

Quote:

Originally Posted by chrism01 (Post 3909740)
I know the feeling; I still use `...` for simple ones

Yeah, I can't imagine changing a developed habit because someone else "deprecates" it. I doubt the feature actually disappears.

I deprecate Fortran, but that ain't slowing the M&S crowd down.

MensaWater 03-24-2010 09:52 AM

I used the word "deprecated" rather than "forbidden".

As for me I like to prove that you CAN teach old dogs new tricks. Having seen two separate shell scripting presentation in recent months where the presenters made cogent arguments for use of $() (including that it works in ksh which was important to me) was good enough for me to try to start changing the habit.

The cut command still exists and though I used it in several of my early scripts years ago for field separation I almost always use awk instead these days. Nowadays I typically use it mainly for character splits rather than field splits.


All times are GMT -5. The time now is 08:48 PM.