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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-23-2010, 07:04 AM
|
#1
|
|
LQ Newbie
Registered: Mar 2010
Posts: 2
Rep:
|
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 ..
|
|
|
|
03-23-2010, 07:25 AM
|
#2
|
|
Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,644
|
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*
|
|
|
|
03-23-2010, 07:39 AM
|
#3
|
|
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: 
|
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. 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.
|
|
|
|
03-23-2010, 08:21 AM
|
#4
|
|
LQ Newbie
Registered: Mar 2010
Posts: 2
Original Poster
Rep:
|
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
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*
|
|
|
|
|
03-23-2010, 08:44 AM
|
#5
|
|
Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,644
|
Quote:
Originally Posted by ordinary
foo `bar`
|
back ticks are deprecated in favor of $(). e.g. foo $(bar)
|
|
|
|
03-23-2010, 10:45 AM
|
#6
|
|
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: 
|
Quote:
Originally Posted by jlightner
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
|
|
|
|
03-23-2010, 10:53 AM
|
#7
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,367
|
Quote:
Originally Posted by ordinary
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.
|
|
|
|
03-23-2010, 02:21 PM
|
#8
|
|
Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,644
|
Quote:
Originally Posted by ordinary
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. 
|
|
|
|
03-23-2010, 07:13 PM
|
#9
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 15,261
|
I know the feeling; I still use `...` for simple ones
|
|
|
|
03-24-2010, 07:16 AM
|
#10
|
|
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: 
|
Quote:
Originally Posted by chrism01
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.
|
|
|
|
03-24-2010, 08:52 AM
|
#11
|
|
Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,644
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 07:35 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|