Live data from Hacker News

Awesome but commonly unknown Linux Commands

anchor.com.au

21–30 of 88 posts

Re: Awesome but commonly unknown Linux Commands

#21
post #7
post #4

One I use all the time is this: $ pwd /Users/me/Sites/foo $ pushd . $ cd / $ pwd / $ popd $ pwd /Users/me/Sites/foo Or in English: If you're in a directory that you need to leave but you know you'll go back there in a minute, use "pushd ." to save that directory. Then go off and do whatever you need to, and when you need to return to the saved directory, use "popd" to take you straight back there.

It's really handy for scripts too. pushd/popd definitely live up to the word awesome. For interactive use zsh has an option called autopushd that automatically pushes directories you `cd` to. I never remember to use pushd so it's a nice convenience.

It's not just handy for scripts, I think it's imperative in scripts where you want to go in and out of a lot of directories.

  for i in foo bar baz; do
     cd $i;
     #... do something
     cd ..;
  done;
if "bar" doesn't exist, your cd .. will throw you off your original directory, whereas

  for i in foo bar baz; do
     pushd $i;
     #... do something
     popd;
  done;
you'll be guaranteed that after "popd" you're back in the right place to start a new iteration.

Re: Awesome but commonly unknown Linux Commands

#22

xargs. Too few people know xargs. Every time I see a "find" with "-exec" in it, my soul cries a little bit. (Yes, sometimes it might be necessary, but in the vast majority of cases not.)

find -exec is almost always faster and more secure than xargs.

Re: Awesome but commonly unknown Linux Commands

#23

Meh, I was hoping for at least semi obscure things like the 'moreutils' utils. Speaking of which, many people don't know about the 'moreutils' utils. Check them out ;)

http://blog.techfun.org/2009/08/moreutils-package-a-must/

vidir sounds very useful to me.

Re: Awesome but commonly unknown Linux Commands

#25
post #5

Earlier quoted context omitted.

You can also use `cd -` to return to the previous directory.

Yeah but only one directory in the history. With pushd/popd you can navigate as much as you want, an then popd to that directory you intended to bookmark.

yeah, I switched to cd - for a while but got bitten by this too many times.

$ pwd

/some/long/compli/cated/v1.0.7/path/to/_stuff

$ cd /simple/

$ do -stuff

$ cd ./foo

$ do -stuff

$ rem now let's get back to that complicated directory again

$ cd -

oh, wait, I cd'ed twice, shit

Re: Awesome but commonly unknown Linux Commands

#26
If you're running a modern Linux desktop, you should have a "notify-send" command which will cause a message to be displayed by your desktop-environment's pop-up notification system (on Ubuntu, this command is in the libnotify-bin package). I have two scripts I keep around which I call "notify-success" and "notify-failure":

notify-success:

    notify-send --icon=gtk-dialog-info Success! "$*"
notify-failure:

    notify-send --icon=gtk-dialog-warning Failure! "$*"
When I want to run a long-running command and don't want to have to keep checking on it, I'll do something like this:

    make -j3 && notify-success "Build complete" || notify-failure "Build failed"

Re: Awesome but commonly unknown Linux Commands

#27

Meh, I was hoping for at least semi obscure things like the 'moreutils' utils. Speaking of which, many people don't know about the 'moreutils' utils. Check them out ;)

I see sponge is somehow useless, how is `| sponge /etc/passwd` different from `> /etc/passwd`.

Re: Awesome but commonly unknown Linux Commands

#28

If you're running a modern Linux desktop, you should have a "notify-send" command which will cause a message to be displayed by your desktop-environment's pop-up notification system (on Ubuntu, this command is in the libnotify-bin package). I have two scripts I keep around which I call "notify-success" and "notify-failure": notify-success: notify-send --icon=gtk-dialog-info Success! "$*" notify-failure: notify-send -…

I do just that, but I also add audible notifications using "play" for when I am not looking at the screen.

Use "--expire-time" argument to "notify-send" to control how long the message is displayed.

Re: Awesome but commonly unknown Linux Commands

#29

xargs. Too few people know xargs. Every time I see a "find" with "-exec" in it, my soul cries a little bit. (Yes, sometimes it might be necessary, but in the vast majority of cases not.)

find -exec is almost always faster and more secure than xargs.

Whereas I almost always use for i in `find . -name foo`; do mv $i $i-new; done

That's very straightforward, and gets the job done without much weirdness.

Re: Awesome but commonly unknown Linux Commands

#30

xargs. Too few people know xargs. Every time I see a "find" with "-exec" in it, my soul cries a little bit. (Yes, sometimes it might be necessary, but in the vast majority of cases not.)

I thought find -exec was the preferred way of doing it (better with spaces, not limited in command line length, etc). Why would it be better to use xargs?
Post reply on HN