simple question about bash .. ls and rm combination
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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 ?
Distribution: Debian, Ubuntu, CentOS; in days past Fedora, Solaris, SunOS, 4.2BSD, 4.3BSD, SVR4, AIX, HP-UX
Posts: 95
Rep:
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.
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
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*
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.