The power of xargs

Excerpted (stolen) from http://www.linux.com/print.pl?sid=04/04/13/211209

xargs is your friend. Using xargs, you can pull off feats of greatness
and not have to write a script to do it. xargs can take care of things
right on the command line. Though I focus mainly on files in this
article (it’s what I use it for almost exclusively), it’s important to
remember that xargs acts on standard input, which could mean lines
redirected from /var/log/messages or urls or whatever else you can
manage to point in its direction.

$ > rpm -qa | grep mozilla | xargs -n1 -p rpm -e –nodeps

What this says in English, is “Using RPM, query all (-qa) packages,
look for mozilla in the package name, and send the results one at a
time (-n1), to RPM’s uninstall command, and I don’t care about
dependencies, thank you very much (“rpm -e –nodeps”). Also, in case
there’s something that contains the word “mozilla” that I DON’T want
erased, prompt me (-p) before uninstalling.” The above command saves
you from having to manually list the packages containing the string
“mozilla,” then manually running separate “rpm -e” commands against
them one at a time.

$ > find / -name *.mp3 -type f -print | xargs tar -cvzf mp3z.tar.gz

This finds all the mp3z on my entire drive and puts ‘em all in a tar
file, and then I can untar them wherever I want :) I actually could’ve
piped that xargs “tar” line into a “tar xvzf” line to automatically
untar them. I also could’ve left out the “-type f” if I had grip set up
to use a custom directory structure that I wanted to preserve. You get
the idea :) PS – this works for other types of files, too, like finding
all the files that belong to you, tarring them and sending the tar to a
backup somewhere, so it does have legitimate use.

$> ls *.mp3 | xargs -n1 -i cp {} backup/.

This command takes all of the MP3 files in the current directory, and
feeds them one at a time (-n1) to the cp command, where the file
argument coming in from ls will replace the curly braces. Notice I
didn’t specify a string with “-i.” I don’t think I’ve ever had to. The
default string that xargs will look to replace when using the -i flag
is the curly braces. As your command lines get a little more complex,
or you start using xargs in scripts, there are a couple of useful
troubleshooting flags you may find helpful if you run into issues. One,
the -p flag, will prompt you for a yes or no before executing a command
on anything. The other, which is a real life saver, is “-t,” and it
does NOT prompt you for a yes or no (unless you use it with -p), but it
will output the command it’s trying to execute, so if something isn’t
quite right, you’ll be able to spot it right away. Comments:

$ > rpm -qa | grep mozilla | xargs -n1 -p rpm -e –nodeps

How about:

rpm -e –nodeps `rpm -qa|grep mozilla`

or if you want a prompt:

for pkg in `rpm -qa`
do
echo “Remove package $pkg? (y/n)”
read ans
if [ "$ans" == "y" ]; then
rpm -e –nodeps $pkg
fi
done

Far clearer. The use for xargs is cases where you want to use tools
(such as GNU Grep) which have limits on the amount of input they can
take. For example:

grep foo `find / -type f -print`

might be too much for grep to cope with;

find / -type f -print | xargs grep foo
> ls *.mp3 | xargs -n1 -i cp {} backup/.

Search remotely and send the results to local machine

If you want to grep (search) through a log file while you’re ssh’d in
to a server and then get that output to yourself on your workstation. I
usually do “grep … > ~/file.txt” and then scp it over.

But you can also do:

ssh remotehost -l remoteuser “grep regexpr logfile” > localfilename

Or if you are already on the remote but want the file to end up locally:

grep … | ssh localhost cat \>file.txt

~ from the gang at Milwaukee LUG

Mounting your old linux HD to recover files

A quickie lesson in mounting – If I have an old linux drive with stuff
on it that I want to use in another linux system (and I do), these are
the steps that I would follow. I would use the SLAVE, MASTER, CABLE
SELECT jumper on the harddrive to set the set the drive to be a slave,
and boot up my new linux install on another physical drive set to be
master. I closely watch the boot messages, or try going to a place like
/var/log/dmesg to see what hdX (e.g., hda, hdb, hdc) my old drive was
assigned at boot. Partitions on that physical drive get numbers. With
four partitions on my old drive, the root partition on my second drive
was at hdd4. So, I needed to create a mount point to mount my drive to.
For simplicity, I chose to mkdir /mnt/hdd4.

As my old drive was of the ext3 filesystem type, so all mounting
operations need to be told what type (-t for mount) the drive is. To
mount this partitiion, as root I typed mount -t ext3 /dev/hdd4
/mnt/hdd4. I could then cd to /mnt/hdd4 and copy off my files. If I
wanted to reboot with the partition mounted, simply I would just add
something like /dev/hdd4 /mnt/hdd4 etx3 to my /etc/fstab file.

Handy shell file lister for cygwin or *NIX

This tip is useful for any system with a useful implementaion of ls, wc, and awk. However, some options may need to be modified. For example, the ls options work best on linux, though they suffice on my cygwin install on Windows when my username does not have a space in it

The commands for running this trick usefully on cygwin/windows is:

ls -ghGR --full-time | awk '{ print $1"\t" $3 "\t" $4 "\t" $7 $8 $9 $10 $11 $12 }' | unix2dos > filelist.txt && wc -l filelist.txt >> filelist.txt

to produce the following listing:

[snip]
total
-rwx——+ 16K 2008-06-09 Export_Output.shp.xml
drwx——+ 0 2008-06-24 java
drwx——+ 0 2008-06-24 licenses
-rwx——+ 42K 2008-05-29 openoffice.org-activex.cab
-rwx——+ 1.8M 2008-05-29 openoffice.org-base.cab
-rwx——+ 18M 2008-05-29 openoffice.org-core05.cab
-rwx——+ 28M 2008-05-29 openoffice.org-core06.cab
-rwx——+ 3.7M 2008-05-29 openoffice.org-core07.cab
-rwx——+ 2.4M 2008-05-29 openoffice.org-writer.cab
-rwx——+ 37K 2008-05-29 openoffice.org-xsltfilter.cab
-rwx——+ 4.2M 2008-05-29 openofficeorg24.msi
drwx——+ 0 2008-06-24 readmes
-rwx——+ 217 2008-05-29 setup.ini
-rwx——+ 500K 2008-06-19 stormwater.mdb

./java:
total
-rwx——+ 16M 2008-01-15 jre-6u4-windows-i586-p.exe

./licenses:
total

158 filelist.txt

My thoughts exactly

Frankly, I think this is bull and it represents a growing frustration
of mine over corporate greed and their ability to hijack our laws for
the benefit their own interests through sludgehammer oppression of the
free expression and creativity of the Amerian people. Everyone just
grow up AND THINK ABOUT THE FUTURE PAST THE NEXT FISCAL QUARTER.


From the article on slashdot.org http://yro.slashdot.org/article.pl?sid=03/10/09/2211259&mode=nested&tid=123&tid=126&tid=141&tid=172&tid=188&tid=93&tid=99

SunnComm Says Pointing to Shift Key ‘Possible Felony’
Posted by CowboyNeal on Thursday October 09, @05:56PM

from the same-shift-not-different-dmca dept.
The Importance of writes “A couple of weeks ago BMG released an audio CD with a new type of DRM.
Earlier this week, a computer science graduate student at Princeton
wrote a report showing the DRM was ineffective – it could easily be
defeated by use of the ‘shift’ key.
The stock of the DRM company (SunnComm) has since fallen by 20%. Now,
SunnComm plans to sue the student under the DMCA and claim that
SunnComm’s reputation has been falsely damaged. According to SunnComm’s
CEO, ‘No matter what their credentials or rationale, it is wrong to use
one’s knowledge and the cover of academia to facilitate piracy and
theft of digital property.’”

” this is retarded (Score:1)
by Anonymous Coward on Thursday October 09, @10:59PM (#7180029)

This is bullshit, just like the parents sueing a school for installing
a wireless network. The people in this country (USA) need to get a
*censored*ing life or a job or something. I swear to God, all anyone
knows how to do here is call their frickin lawyers. People need to stop
sueing everyone for every little thing, and stop trying to act like
their precious intellectual property is more important than the people
paying them to use it. Boycott is not the answer either, I normally am
not for government regulation but we need to make these things a real
issue for the American public otherwise they will keep electing idiots
that don’t tell the RIAA/MPAA to *censored* off. Most people over 40
have no clue what the DMCA is or what an MP3 is or even that the RIAA
and MPAA are pulling this bullshit. Every time I turn on CNN all I want
to do it walk up to someone new and say, no, you can’t *censored*ing
sue for that, and for being so damn dumb I’m going to punch you in the
head until you pass out. GRRR!”