[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] how to understand the xargs

From: Greg Wooledge
Subject: Re: [Help-bash] how to understand the xargs
Date: Tue, 27 Dec 2011 10:02:44 -0500
User-agent: Mutt/

On Tue, Dec 27, 2011 at 12:11:59AM +0800, lina wrote:
> I don't understand the xargs well,

There's really only one thing you need to know about xargs: never use it.

(Well, OK, I exaggerate very slightly.  It's OK to use it if you supply
an option that suppresses its native uselessness, such as GNU's -0.)

> can someone help me understand the below sentence,
> ls . | xargs -i -t cp ./{} {}.bak

Never use the output of ls in a script.  Ever.

If your goal here is to make a backup of each file in the current directory,
then do this instead:

for f in ./*; do cp "$f" "$f.bak"; done

Using a glob (like ./*) instead of the output of ls is always better.
Globs are not mangled by ls (replacing non-printable characters with
question marks, failing completely on files with newlines in their names,
etc.), and because you are not using xargs, you will also not have any
of the problems that xargs has with filenames (spaces, quotes).

> How does xargs work,


imadev:~$ echo 'hi"mom' | xargs echo
xargs: missing quote?: himom

imadev:~$ echo "it doesn't work" | xargs echo 
xargs: missing quote?: doesnt work

imadev:~$ echo "this is one filename.txt" | xargs ./bin/args rm
5 args: <rm> <this> <is> <one> <filename.txt>

> ls | xargs -i cp ./{} {basename {}.bak}
> definitely not work,

I don't know what that example is supposed to do.  You realize that the
basename command is used to *strip out* directory paths from path names,
or suffixes from file names?  (Also, that is not how you do a command
substitution... but since I can't tell what you were trying to do, I can't
write a working example.)

On Tue, Dec 27, 2011 at 12:25:52AM +0800, lina wrote:
> is the help-bash an active list?
> seems no reply.

I just got your messages a few minutes ago.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]