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
I don't know how much information your script gives, but cp -g gives you a progress bar, transfer rate, completion percentage and other stuff when a transfer is going to take more than a few seconds (similar to what scp gives you). It's sometimes a handy feature to have, sounds like what your script is doing...
-ReK
md5sum -c reality.md5
reality: FAILED
md5sum: WARNING: 1 of 1 computed checksum did NOT match
I never figured out a way to use 'ls' to show only directories (and not their subcontents), so I created an alias called 'lsd':
.bash_profile
alias lsd='\ls -l | grep "drwx"'
and placed it in my
It's quite useful, but it doesn't work well with shell scripts.
Don't think that a small group of dedicated individuals can't change the world. It's the only thing that ever has.
pkill/grep are nice too, and are standard on a fair few systems now.
Junction lets you make symlinks in Windows without installing the entire Windows Resource Kit tools. Also, CACLS.EXE for changing ACLs in Windows via the command line, since I have no fucking clue where you do this in the GUI. Some of the more usefule CLI commands in Windows, IMO. I hope this discussion wasn't limited to Unix or anything.
in bash:
;
/home/
function dusort ()
{
du -s "$@" | sort -r -n |
awk '{sum+=$1;printf("%9d %9d %s\n",sum,$1,$2)}'
}
in tcsh:
alias dusort 'du -s \!* | sort -r -n | awk '"'"'{sum+=$1;printf("%9d %9d %s\n",sum,$1,$2)}'"'"
The most common way to use those commands would be:
cd
dusort *
It's useful for tracking down what's using up your space, for example finding a sub directory deep in a source tree that isn't cleaned by make clean.
perl -pi -e "s/x/y"
ever had to make a change to every line in a test vector (up to several million lines long), but didn't have the half hour it would take to retranslate the whole thing? - has saved my ass more than anything I can think of
also fun to do something like
perl -pi -e "s/(alias \w) \'.+\'/$1 \'echo \"DFU DFU DFU\"/g ~user/.aliases
Here's something that I run via cron on a nightly basis. See if you can decode its function :-)
for F in $(find $SRCDIR); do echo $(basename $F) $F; done | sort | rev | uniq -c -f 1 | grep -E ^[[:space:]]*1[[:space:]] | awk '{print $2 " " $3}' | rev > $CACHEFILE
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
If you're using gnu grep, you can use the -H option (or --with-filename) instead of the dummy /dev/null argument.
I mod down all the "free iPod"-sig losers.
Need to find that file named somethingfoosomeotherthing?
g foo2
/etc/
alias ff='find . |grep -i '
>ff foo
./somedir/somethingfoo
./somedir/somethin
./somedir/somethingfoo3
Need to find that pesky configuration file for printer?
~/bin/gr:
grep -i -r $1
~/bin/fgre:
grep -i -r -l $1
>cd
>fgr laserjet
./cups/ppd/hp.ppd:*ShortNickName: "HP LaserJet Series"
Just interested which files to check?
>fgre laserjet
./cups/ppd/hp.ppd
strace/truss/ktrace for Linux, Solaris and BSD/MacOS respectively. If a program fails to start, or terminates abnormally, this will usually give me the heads up on why (it's usually a missing file, or bad permissions) without having to break out gdb.
lsof. Useful in so many ways; for debugging situations similar to the above, as well as hardening systems and building chroot environments for specific programs.
tcpdump/snoop/tethereal/ethereal. If you can see what's really on the wire between two network applications, you can probably figure out what's going wrong. Ethereal is particularly nice.
hexdump/khexedit. If you can see what's really in the file used by an application, you can probably figure out what's going wrong. :-) khexedit also has a bonus feature of being able to perform statistical logical operations across the file; useful if you have a file which you suspect has been encrypted with some lame substitution cipher.
After those, the usual - sed, awk, grep, find. It's rare that I can't turn any problem into an awk-shaped nail. :-)
scp -C -r * user@system:target
and
scp -C -r 'user@system:/target/*' .
There are places where the networks are not touching,and there are places where they are-Boeing's Lori Gunter
Not to mention, it's really useful for temporary (ie. until you can reinstall) cleanups of compromised systems, since (a) it uses its own internal code for things like ls, chmod, etc, and (b) rootkits don't generally replace it :)
Can't find examples of evolution? No matter, neither could Dawkins