LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Reply
 
Search this Thread
Old 03-23-2010, 07:04 AM   #1
ozgur
LQ Newbie
 
Registered: Mar 2010
Posts: 2

Rep: Reputation: 0
Smile 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 ..
 
Old 03-23-2010, 07:25 AM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,002
Blog Entries: 5

Rep: Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782
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*
 
Old 03-23-2010, 07:39 AM   #3
ordinary
Member
 
Registered: Apr 2007
Location: the Rocket City
Distribution: Debian, Ubuntu, CentOS; in days past Fedora, Solaris, SunOS, 4.2BSD, 4.3BSD, SVR4, AIX, HP-UX
Posts: 101

Rep: Reputation: Disabled
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

Last edited by ordinary; 03-23-2010 at 07:49 AM.
 
Old 03-23-2010, 08:21 AM   #4
ozgur
LQ Newbie
 
Registered: Mar 2010
Posts: 2

Original Poster
Rep: Reputation: 0
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 View Post
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*
 
Old 03-23-2010, 08:44 AM   #5
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,002
Blog Entries: 5

Rep: Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782
Quote:
Originally Posted by ordinary View Post
foo `bar`
back ticks are deprecated in favor of $(). e.g. foo $(bar)
 
Old 03-23-2010, 10:45 AM   #6
ordinary
Member
 
Registered: Apr 2007
Location: the Rocket City
Distribution: Debian, Ubuntu, CentOS; in days past Fedora, Solaris, SunOS, 4.2BSD, 4.3BSD, SVR4, AIX, HP-UX
Posts: 101

Rep: Reputation: Disabled
Quote:
Originally Posted by jlightner View Post
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
 
Old 03-23-2010, 10:53 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by ordinary View Post
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.
 
Old 03-23-2010, 02:21 PM   #8
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,002
Blog Entries: 5

Rep: Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782
Quote:
Originally Posted by ordinary View Post
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.
 
Old 03-23-2010, 07:13 PM   #9
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,264

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
I know the feeling; I still use `...` for simple ones
 
Old 03-24-2010, 07:16 AM   #10
ordinary
Member
 
Registered: Apr 2007
Location: the Rocket City
Distribution: Debian, Ubuntu, CentOS; in days past Fedora, Solaris, SunOS, 4.2BSD, 4.3BSD, SVR4, AIX, HP-UX
Posts: 101

Rep: Reputation: Disabled
Quote:
Originally Posted by chrism01 View Post
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.

Last edited by ordinary; 03-24-2010 at 07:19 AM.
 
Old 03-24-2010, 08:52 AM   #11
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,002
Blog Entries: 5

Rep: Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782
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.
 
  


Reply

Tags
bash, exec, find, ls, rm


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
simple bash question. kalleanka Programming 2 11-16-2006 07:45 PM
simple bash question about cut perfect_circle Programming 2 05-17-2005 11:25 AM
Simple Bash Question sio Programming 6 11-22-2004 09:56 AM
Bash Scripting--Simple Question mooreted Linux - General 4 05-10-2004 01:44 PM
Simple bash question pk21 Programming 2 12-01-2002 05:06 PM


All times are GMT -5. The time now is 04:49 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration