# 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.
12 thoughts on “Linux Commands You’ve Never Used”
Comments are closed.
ADVERTISEMENT
Join Make: Community Today
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.
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).
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).
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.
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}
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]