Slashdot Mirror


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?"

13 of 272 comments (clear)

  1. lh and hog by newsblaze · · Score: 4, Informative

    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
  2. can't eat just one by yagu · · Score: 5, Informative

    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:

    • sed, or even ed. ed is great for scripting edit changes if you don't have the heart to master sed. I once used "sed" to create nroff output of document versions with delete markers (asterisks), change markers, and insertion markers from our technical documentation library.
    • awk. I couldn't have done half the work I've done in my career without this one. I watched entire teams scramble to solve some problem in Visual Studio, and would crank out a twenty line awk script that afternoon. Problem solved.
    • [ef]grep. If you're going to pipeline some shit, it's nice to filter it first (and sometimes second, then third, etc....). The grep family is second to none for this. (Funny story: I once worked at Microsoft. Was doing some stuff in DOS. Asked what command I'd use to find a string in a file or files. They showed me "FIND". Okay. I typed in:
      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?"

    • diff. A life saver. 'Nuff said.
    • find. Freak! When I first met this command I said "go away!". Talk about an obtuse syntax and paradigm. Learn it! It's worth it.
    • xargs. Nice way to get around the line length restrictions of some of the unix shells.
    • zsh, bash, (but not csh)... Any interactive shell with a good history and command line completion mechanism. I never have to remember command syntax anymore... usually I can find a usable and editable example somewhere in the last 2000 commands I've executed.

    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

  3. Here's mine by Henry+V+.009 · · Score: 5, Funny
    My most important tools as a system admin:
    alias rm 'rm -rf \!*'
    alias hose kill -9 '`ps -augxww | grep \!* | awk \'{print $2}\'`'
    alias kill 'kill -9 \!* ; kill -9 \!* ; kill -9 \!*'
    alias renice 'echo Renice\? You must mean kill -9.; kill -9 \!*'
  4. perl by Turmio · · Score: 5, Insightful

    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.

    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 /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.

    perl -pne's/oldip/newip/g' -i `find /etc -type f`

    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 :)

  5. Re:skill by Fweeky · · Score: 3, Informative
    Looks like killall to me:
    SYNOPSIS
        killall [-delmsvz] [-help] [-j jid] [-u user] [-t tty] [-c procname]
                [-SIGNAL] [procname ...]
     
    DESCRIPTION
        The killall utility kills processes selected by name, as opposed to the
        selection by pid as done by kill(1). By default, it will send a TERM
        signal to all processes with a real UID identical to the caller of
        killall that match the name procname. The super-user is allowed to kill
        any process.
    Though you need to be careful with that if you admin other systems like Solaris ;)

    pkill/grep are nice too, and are standard on a fair few systems now.
  6. Re:ls -d without the subcontents by pclminion · · Score: 5, Informative
    Dude...

    ls -d */

    The final slash is key.

  7. Junction for Windows by alan_dershowitz · · Score: 3, Informative

    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.

    1. Re:Junction for Windows by BrynM · · Score: 3, Informative
      without installing the entire Windows Resource Kit tools
      There are several RK tools in my MS toolbox, but the best thing is having real unix utils. Pop those in your %PATH% and enjoy some of the same fun that's being spoken of here. Of course there's always Cygwin, but these native ports are handy to keep on a USB drive and don't need any configuration/installation at all.
      --
      US Democracy:The best person for the job (among These pre-selected choices...)
  8. screen by pauljlucas · · Score: 5, Informative

    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.
  9. Give credit where credit is due by mister_jpeg · · Score: 5, Informative

    That's the Technical Fascist's .cshrc.

    http://www.gnu.org/fun/jokes/know.your.sysadmin.ht ml

    --
    -jpeg
  10. xargs and for loops by photon317 · · Score: 4, Informative

    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.

    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 .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:

    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 ...;rcp $fn remotehost:/tmp/;done

    --
    11*43+456^2
  11. ZTree and UnixTree by Glonoinha · · Score: 3, Interesting

    Yea, Though I compute through the user interface of the shadow of death, I shall fear no evil, for ZTree is with me.

    No joke - ZTree is a character for character re-write of an old utility called XTree Gold v2.0 or 2.5 - and it is by far the most effective and influential interface between me and my data. The entire file system is but an extension of my mental processes, and I can slice and dice through the multi-dimensional (time, space, attributes, multi-layered directory structure, multi-drive architecture,) in effect creating a virtual directory within which I control the parameters driving what is listed, in what order - then copy, view, move, delete, diff (file compare), view in hex mode (and edit it in hex mode), search for text in lists of files, compare directory trees for like or different files (binary, time stamp, etc.)

    It is totally CUI, about like Midnight commander but a ton better. Take time to get fluent in ZTree (UnixTree for Unix / Linux, a bit older with a few quirks, but still pretty damn good) and you will be like the guys in the Matrix sitting at their green screen terminals.

    ZTree Don't leave $HOME without it.

    --
    Glonoinha the MebiByte Slayer
  12. You are frickin' insane. by Just+Some+Guy · · Score: 4, Insightful
    find . -name "*.o" | xargs -n 50 rm -f

    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:

    rm -f ./ -rf /

    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?