Re: Linux Magazine for one liners

From: Brian E. Lavender (brian@brie.com)
Date: Thu Aug 12 1999 - 11:50:48 PDT


It looks like I owe Mike a Linux Magazine which of course I forgot to
bring to last night's meeting. Dooh! Anyway, I have a one liner
that I believe just might top Mike's command. It will travel down a
directory tree, open files that match a specified pattern, get rid of
those annoying ^M's you get when you transfer text files from dos in
binary mode ftp and it will back up the files it processed to the same
name, but with the added extension .bak. Then, I have a one liner to
remove all the backed up files.

The first one liner

$ find . -type f -name \*.html -exec perl -pi.bak -e 's/\r//' {} \;

The second one liner

$ find . -type f -name \*.bak -exec rm {} \;

Here's what is happening in the first one liner. The find will search down
a directory tree, starting from a specified directory. So, in this case it
is the current working directory specified by the '.'. The find is also
given a couple extra options. The first is the -type. This option tells
find to find files that are ordinary files. Sym links, and directories
are skipped. The -name option gives us the pattern. In this case we
are looking for files that end in .html. The -exec option tells it to
execute a command. In this case it is executing perl which also has two
options specified for it. The -pi.bak tells it to back up the original
file to the same name with the .bak added to the name. The -e says the
perl code to be executed will be after this. Inside the single quotes is
the search and replace that removes the ^M's. The curly brackets supply
the file names to the executed perl command found by the find command.

The second command does pretty much the same, but instead of executing a
perl command, we execute an rm command which deletes the file. It is also
looking for .bak files instead of the .html files.

How is that for a one liner? I have to give credit to Ed Avis
<epa@datcon.co.uk> for this because he is the one who responded to a
question I posted to comp.lang.perl.misc about a year and a half ago.

brian

On Wed, Aug 11, 1999 at 10:30:32AM -0700, Mike Machado wrote:
> I got one I did not yet get a chance to share.
>
> tr -d '\r' < msfile > unixfile
>
> This will stript those annoying ^M's you get when you transfer a MS file
> to linux.
>
> Brian E. Lavender wrote:
>
> > Free copy for one liner at tonights meeting
> >
> > I have some copies of Linux Magazine. If you have a
> > cool one liner to give at the beginning of the meeting,
> > I have a free copy of Linux Magazine for you. Here's an
> > example:
> >
> > tar cf - .| (cd /var/named/; tar xfp -)
> >
> > This command will copy files from one directory to another
> > while maintaining permissions and owner/group ownership. It
> > works across NFS mounts too.
> >
> > cp -a
> >
> > works similar to this. The tar command is more bullet proof (IMHO)
> >
> > brian
> > --
> > Brian Lavender
> > http://www.brie.com/brian/
>
> --
> Mike Machado
> mike@innercite.com
> InnerCite
> Network Specialist
>

-- 
Brian Lavender
http://www.brie.com/brian/



This archive was generated by hypermail 2b29 : Fri Feb 25 2000 - 14:29:06 PST