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

Re: which is the best command to use



On Tue, Dec 27, 2011 at 12:57:55PM GMT, lina wrote:
> Hi,
> 
> I have two questions, wish to get advice,
> 
> Question 1:
> 
> For a series of files:
> 
> cat  a_*.o* | grep "WARNING"

It's one of the useless uses of cat[0].

Use:

% grep WARNING a_*.o*

instead.

> some file like a_1.o12 has WARNING, but others does not have WARNING
> 
> How can I let grep returns the filename?

% man grep

-l, --files-with-matches
	Suppress normal output; instead print the name of each input file
	from which output would normally have been printed. The scanning
	will stop on the first match. (-l is specified by POSIX.)

> Question 2:
> 
> ls . | xargs -i -t cp ./{} {}.bak
> 
> How does xargs work,
> 
> I checked google, :

Maybe it's time to start checking man instead ;^)

% man xargs

> -i : replace the string

--replace[=replace-str]
-i[replace-str]
	This option is a synonym for -Ireplace-str if replace-str is
	specified, and for -I{} otherwise.
	This option is deprecated; use -I instead.

> -t print

--verbose
-t
	Print the command line on the standard error output before
	executing it.
	    
> but still lack well understanding,

it's quite self explanatory ;^)

> how those xargs organise things,
> 
> and
> $ ls .
> a.txt b.txt
> 
> above will give a.txt.bak b.txt.bak
> ls | xargs -i cp ./{} {basename {}.bak}
> definitely not work,

How about:

% ls | xargs -I '{}' cp '{}' '{}'.bak

BTW, if you're only after copying the files with .bak appended to the
copied file name, this should suffice.

for i in `ls` ; do cp $i $i.bak ; done

There are other ways to do it as well.

> Sorry I send it to debian list, I asked on another bash list, for
> two days the second question, no one replied. I have much more faith
> in this list.

Good to hear that, but maybe you should start to reading more and
test a bit yourself before you write an email. It'll definitely be less
than two days to find a solution and sometimes it'll take less time to
find it in the manual than to write an email.

I appreciate the fact that you're a beginner but your "problems" are
NOT Debian-specific and ARE trivial to "resolve" so maybe it's time
to learn how to look for solutions yourself rather than others holding
your hand all the time, eh? ;^)

[0] http://partmaps.org/era/unix/award.html

All the best.
-- 
Raf


Reply to: