Linux Commands You've Never Used


# lsof | grep TCP
portmap    2587   rpc  4u   IPv4     5544  TCP *:sunrpc (LISTEN)
rpc.statd  2606  root  6u   IPv4     5585  TCP *:668 (LISTEN)
sshd       2788  root  3u   IPv6     5991  TCP *:ssh (LISTEN)
sendmail   2843  root  4u   IPv4     6160  TCP badhd:smtp (LISTEN)
vsftpd     9337  root  3u   IPv4    34949  TCP *:ftp (LISTEN)
cupsd     16459  root  0u   IPv4    41061  TCP badhd:ipp (LISTEN)


Here's a short list of useful Linux shell programs, many of which you may have never known about. I've got a new favorite shell command, lsof (shown above), which displays information on every open file handle. - Link.

If I could add one to the list, it'd have to be the short and sweet command line search and replace using perl:
perl -pi -e 's/searchpattern/replacewith/g' *.html

Do you have a favorite command line secret? Please share it in the comments.

Recent Entries

Comments

Oldest comments listed first.

Posted by: A.Nonny.Mouse on February 25, 2007 at 8:26 AM

This is probably a well known trick, but I like xargs.
It takes standard in and treats it as an argument to a command give in the args of xargs... So, for example, say you have a web directory tree with a mixed bag of php, html, css and xml files. If you only want to search for something in the php files you can do this:

find . -name "*.php" | xargs grep -i "special"

That'll find only the php files and grep them for special.

You could also pipe ps through awk and use xargs to kill all of a certain process...

But everybody probably already knew that.


Posted by: jher on February 26, 2007 at 11:15 AM

can't get something to unmount so you can restart autofs?

try this:

ps -ef |grep rpciod |awk '{print $2}' |xargs kill -9

This doesn't actually cause rpciod to die, it just forces it to release any stuck nfs mount points (in linux).


Posted by: illovich on February 26, 2007 at 12:10 PM

For the record (or for the mac users, I guess) I thought I would point out that the following from the page 10 linux commands also work in the Mac OS X terminal:

#3 bc
#4 split
#5 nl
#6 mkfifo
#8 col
and
#10 lsof

Additionally xargs is available as well (from the first tip).


Posted by: LBonanomi on March 4, 2007 at 6:23 PM

I'm begging you, please don't pipe grep out to awk. Awk can match patterns all by it's lonesome. In fact, if you find awk in a pipeline with cat, grep, tr, wc or sed you need to stop and re-think whats happening.

Awk handles files without a cat pipeline. Awk can make its own text substitutions with sub and gsub. Awk can count lines with 'END { print NR }'. If your version of awk doesn't accept environment variables (my old rationale for using grep | awk...) use:
eval awk '/$1/ { print }'

Awk may not have the horsepower of perl, but it can do a lot more than act as a heavier version of cut.


Posted by: ctb on March 9, 2007 at 11:54 AM

I always enjoy passing multiple files or directories with curly brackets. This is nice for changing permissions on a handful of items really quickly, i.e...


chmod 755 {file1,file2,dir1,file3}


Or for creating a handful of directories in one swipe...


mkdir {music,images,text,archive}


Posted by: MCasillo on March 30, 2007 at 10:11 PM

A.Nonny.Mouse - this will also work for your example :)
grep -i "special" *.html


Another neat shell trick for commands without an 'ignore case' command is to use bracketed 'sets' :

chmod all .Zip or .zip files
chmod 755 help.[Zz]ip

chmod all files named help.zip (or HelP.zIP, etc)
chmod 755 [Hh][Ee][Ll][Pp].[Zz][Ii][Pp]

chmod all files that start with the letter 's' but do not end in any form of zip:

chmod 755 [Ss]*.[!Zz][!Ii][!Pp]


Leave a comment


Subscribe to MAKE!Subscribe to MAKE Magazine!

Subscribe today, save 42% and get web access to MAKE free. MAKE Digital Edition is available only to subscribers.

$34.95 / 1 year
(4 Quarterly Issues)

Subscribe now


Void your warranty, violate a user agreement, fry a circuit, blow a fuse, poke an eye out. Make: The risk-takers, the doers, the makers of things... Welcome to Make: Online!


CRAFT Maker Shed Maker Faire MAKE television
MAKE: en EspaƱol MAKE: Japan


Check out all of the episodes of Make: television

Make: Science Room

Connect with MAKE

Be a MAKE fan on Facebook MAKE on Facebook
Visit our Facebook page and become a fan of MAKE!
MAKE on Twitter MAKE on Twitter
Follow our MAKE tweets!
MAKE Flickr Pool MAKE on Flickr
Join our MAKE Flickr Pool!
    make_tips on Twitter

    MAKE's RSS feed is here.
    Add MAKE to iGoogle - GoogleGoogle.
    How to add MAKE to your RSS reader - Real simple.
    Add MAKE on FriendFeed




    Maker SHED

    Advertise here with FM.

    Why advertise on MAKE?
    Read what folks are saying about us!

    Click here to advertise on MAKE!



    Subscribe to MAKE Magazine!

    Make: Online authors!

    Gareth BranwynGareth Branwyn
    Senior Editor


    Phillip TorronePhillip Torrone
    Senior Editor
    | AIM | Twitter


    Becky SternBecky Stern
    Associate Editor
    | AIM | Twitter


    Marc de VinckMarc de Vinck
    Contributing Writer
    | AIM | Twitter


    John ParkJohn Park
    Contributing Writer
    | Twitter


    Sean RaganSean Ragan
    Contributing Writer
    | Twitter


    Matt MetsMatt Mets
    Contributing Writer
    | AIM | Twitter


    Dale DoughertyDale Dougherty
    Editor & Publisher
    | Twitter


    Shawn ConnallyShawn Connally
    Managing Editor
    | Twitter


    Goli MohammadiGoli Mohammadi
    Associate Managing Editor

    Kip KayKip Kay
    Weekend Projects
    | AIM | Twitter


    Collin CunninghamCollin Cunningham
    Contributing Writer
    | AIM | Twitter

    Adam FlahertyAdam Flaherty
    Contributing Writer
    | AIM | Twitter



    More contributors: Mark Frauenfelder (Editor-in-Chief, MAKE magazine), Kipp Bradford (Technical Consultant/Writer), Chris Connors (Education), Diana Eng (Guest Author), Peter Horvath (Intern), Brian Jepson (O'Reilly Media), Robert Bruce Thompson (Science Room)

    Suggest a Site!

    Current Podcast

    itunesdl.gif Weekend Project: Making Char Cloth Learn how to make a cheap and effective fire starter made from an old t-shirt. To download The Char Cloth video click here and subscribe in iTunes. See Char Cloth in action with the Fire Piston from William Gurstelle.... More...

    Get the Make: Online sent via email
    Enter your email to receive Make: Online each day:



    MAKE Fascination video series brought to you by Dow

    Make: Education

    Important please read


    Subscribe to MAKE Magazine!

    Recent Posts from the Craft: Blog