What's Your Command Line Judo?
lousyd asks: "We all have our CLI amor. That two- or three-letter command that fiddles our heartstrings. 'mv' is pedestrian, but 'mmv' is so cool. 'cp' can lug bits around all day to no applause, but 'cpio' will excite even the most jaded of command line linguists. 'How perfect!' you exclaim. So what's your poison? What turns you on? What little known command performs just the right function for you?"
My favourites are
lh ls -lt !* | head -15
which shows me the newest 15 stories in the current or specified directory
hog "ps -eo pcpu,vsz,args,time | sort -rn | head -11"
which shown the 10 most cpu-intensive processes
Daily News http://newsblaze.com
Command line judo? Sheesh! Where to start?
Okay, the tool I'm using now: vim, derivative of Bill Joy's vi, with color syntax and a bounty of enhancements. (Yes, I prepare my comments in vi, then cut and paste, don't even try to make me use some GUI text widget editor and claim it can be productive.)
And then there's:
find mystring *.*
and literally got back:
mystring found in *.*, and then a listing of all the lines found. No reference to the files they were in... Shit, after all, I asked to find the lines in '*.*'.
I complained. They showed me their (Microsoft's purchased from IBM) unix, "xenix", and their "grep" command. Ahhhhh, better. I typed: grep -i mystring *.*
and it replied "unknown option -i". I complained about not having an "ignore case" option. They looked at me like I was crazy... "Why would you ever want to ignore case?"
I could go on, and probably will in some subsequent posts. When you have so many well written, well evolved, well crafted, and well behaved tools all flying in
First, I must comment the article. Question goes What little known command performs just the right function for you? I hope all sane people here (haha) would answer "None". There's no command that does just the right function since there's no one The Function. It depends on the situation what the function is. And in that case, per Unix philosophy, where one tool does one simple job, but does it well, you should choose the tool accordingly.
/etc to this new IP address. First you might think searching all the files under /etc with find(1), then passing the list of files to grep(1) and then manually editing the places where the old IP address was found with your $EDITOR. That's fine and will get the job done and all but what if you could just edit the files in place? With perl, you can.
/etc -type f`
:)
Enough of that. If you really must name something, then, in my opinion, there's one gizmo above others. And that is
perl
Perl one-liners is a damn powerful concept when you get it. Say one of your boxen switches IP address. You want to replace all references in files under
perl -pne's/oldip/newip/g' -i `find
and you're done (better be extra careful with commands like that for obvious reasons!). Of course you're able to do the same thing with other tools too, but I don't think it could be much easier than that. And naturally you're not just limited to simple search and replace of text, you have the full power of Perl (and CPAN!) at your disposal.
Besides being my number one choice for creating complex scripts and small applications, Perl has very special place in my command line toolbox just next to the old friends such as grep(1), cut(1), wc(1), etc. and a huge pile of pipes
ls -d */
The final slash is key.
Multiple multiplexed ttys that stay running even after disconnect and you can reattach to them later.
If you reply, do so only to what I explicitly wrote. If I didn't write it, don't assume or infer it.
That's the Technical Fascist's .cshrc.
t ml
http://www.gnu.org/fun/jokes/know.your.sysadmin.h
-jpeg
There's millions of tricks, but if I had to a couple simple powerful techniques that anyone should learn that doesn't know them already, it would be xargs and commandline "for" loops.
.o files anywhere underneath the current directory. However, if there are too many .o files to fit on a single commandline, it will barf with "argument list too long" or some such sounding error. The xargs way to do this would be:
...
...;rcp $fn remotehost:/tmp/;done
xargs takes whatever is piped into it, and executes a command with those things as arguments. It can do it all at once, or it can break them up in chunks, or it can execute your command once per input. Consider:
rm -f `find . -name "*.o"`
This normally works fine, and will forcibly remove all
find . -name "*.o" | xargs -n 50 rm -f
Which will execute a seperate "rm -f" for each chunk of 50 filenames. Take a look at the "-i" mode as well, read the whole man page. It's a great little peice of glue.
On to for loops. You've seen them in sh/ksh/bash shellscripts like so:
for fn in *.c
do
echo Sending $fn
rcp $fn remotehost:/tmp/
done
You can of course do this straight from the commandline, which is indispensable for complex looped operations. To do it all in one line, you just have to get the semicolons in the right place. Just remember there's a semicolon before the "do", but not immediately after it:
for fn in *.c; do echo Sending $fn
11*43+456^2
That's great. Now, create a directory called " " (empty space). Inside that, create a directory called " -rf " ("-rf" with an empty space on either side). Inside that, create a file named " " (yet another empty space). Now, watch in horror as find prints "./ -rf /", which it passes dutifully to "rm -f". Since xargs by default passes each word as an individual argument, that expands to:
Hope you weren't running as root! The moral of this story is to never, never! use find/xargs without the "-print0" option whenever the command you're executing is destructive. "ls" is probably OK. "rm" definitely isn't.
Dewey, what part of this looks like authorities should be involved?