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

23 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

    1. Re:can't eat just one by ATomkins · · Score: 2, Informative
      I complained. They [Microsoft] showed me their (Microsoft's purchased from IBM) unix, "xenix"...

      Sorry to be picky, but Microsoft bought a license for UNIX from AT&T, developed Xenix, then sold it to SCO who would transform it into SCO UNIX.

  3. cp -g by ReKleSS · · Score: 2, Informative

    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
  4. ls -d without the subcontents by bergeron76 · · Score: 2, Informative

    I never figured out a way to use 'ls' to show only directories (and not their subcontents), so I created an alias called 'lsd':

    alias lsd='\ls -l | grep "drwx"'

    and placed it in my .bash_profile

    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.
    1. Re:ls -d without the subcontents by pclminion · · Score: 5, Informative
      Dude...

      ls -d */

      The final slash is key.

    2. Re:ls -d without the subcontents by Nutria · · Score: 2, Informative
      alias lsd='\ls -l | grep "drwx"'

      What if a file has the string "drwx" in it?

      And you forgot about dot directories.

      alias lsd='ls -al | grep ^d'
      --
      "I don't know, therefore Aliens" Wafflebox1
  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. 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 ConceptJunkie · · Score: 2, Informative

      I hope this discussion wasn't limited to Unix or anything.

      It shouldn't be. Another source of good command line tools is sysinternals.com.

      The pstools let you do all kinds of nifty things that are sometimes covered by RK utils, but the sysinternals stuff is usually way better... ...with one exception that drives me crazy. pslist is their version of ps, which I just alias using 4NT, however they don't echo the banner to stderr like most utils, so if you sort the list looking to see if you need to kill and errant process of Firefox 1.5 beta 1, you get the banner sorted with the list of processes.

      Other than that, they are great tools.

      You can easily control services on other machines ("net" might let you do this but I don't know how), you can even defrag with "contig". Heck there's even a blue screen screen saver. At one place, years ago I used to run this to encourage people not to mess with my machine.

      Of course no Windows command line would be complete without cygwin in the path.

      --
      You are in a maze of twisty little passages, all alike.
    2. 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...)
  7. dusort by RGRistroph · · Score: 2, Informative

    in bash:

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

  8. hmm... by bbrack · · Score: 2, Informative

    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

  9. We're not talking about cool commands, here! by pclminion · · Score: 2, Informative
    I don't think the point of the article is to discuss which commands are coolest, rather, what ways can you combine those commands to achieve powerful results?

    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

  10. 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.
    1. Re:screen by vrai · · Score: 2, Informative
      I'd add disown to that list - under bash it detaches background jobs from the tty and allows them to continue running after session is closed (like nohup).

      Example:

      vrai@lenin $ complex_task
      ^Z
      [1]+ Stopped complex_task
      vrai@lenin $ bg %1
      [1]+ complex_task &
      vrai@lenin $ disown %1
      vrai@lenin $ exit
      The complex task continues to run in the background; though the output will be lost to /dev/null unless redirected.
  11. 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
  12. 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
  13. Re:the one that impresses most people by _randy_64 · · Score: 2, Informative

    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.
  14. make yourself a few aliases by frn123 · · Score: 2, Informative

    Need to find that file named somethingfoosomeotherthing?
    alias ff='find . |grep -i '
    >ff foo

    ./somedir/somethingfoo
    ./somedir/something foo2
    ./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 /etc/
    >fgr laserjet

    ./cups/ppd/hp.ppd:*ShortNickName: "HP LaserJet Series"

    Just interested which files to check?
    >fgre laserjet

    ./cups/ppd/hp.ppd

  15. My favourites by cowbutt · · Score: 2, Informative

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

  16. scp is your friend by DrSkwid · · Score: 2, Informative


    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
  17. Re:Midnight Commander. by fuzza · · Score: 2, Informative

    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