Ask HN: Best Unix Tricks?
41–50 of 107 posts
Re: Ask HN: Best Unix Tricks?
#42Actually, I rather hope there's something better than strace for the latter. But it works.
Re: Ask HN: Best Unix Tricks?
#43I love the '-exec' flag for find
It is much, much faster to pipe commands to "xargs". For instance:
find . -name core -print0 | xargs -0 rm -f
Since "find" doesn't know when your command is multi-file, it has to run the command once per file; whereas, "xargs" can run it as few times as necessary. It works well for any command that accepts "a list of files".In the example above, the -print0/-0 are a safety net to null-delimit filenames so that it doesn't matter if any of the paths contains a space.
Re: Ask HN: Best Unix Tricks?
#44I use this to synch a directory with another: tar cf - * | (cd ; tar xf - )
Re: Ask HN: Best Unix Tricks?
#45ctrl + r Starts a search through your history file.
# search history on key instead of paging previous commands:
bindkey "\e[A" history-search-backward
bindkey "\e[B" history-search-forwardRe: Ask HN: Best Unix Tricks?
#46Pipes, grep and such come in handy in many applications. For example: ps -e | grep processname will grab the pid of any process so that you can subsequently kill it. sudo !! (run last command as sudo) is great too when you forget to type sudo on a regular basis like me.
I like to use:
killall processname
Re: Ask HN: Best Unix Tricks?
#47 # a visual recursive list of all files and directories
function tree()
{
find . | sed -e 's/[^\/]*\//|--/g' -e 's/-- |/ |/g' | $PAGER
}
# recursive text search (ack is also nifty)
function f()
{
find . -name '*' | xargs grep -l $1
}
# recursive search and replace (ignoring hidden files and dirs), example: replace foo bar
function replace()
{
find . \( ! -regex '.*/\..*' \) -type f | xargs perl -pi -e "s/$1/$2/g"
}Re: Ask HN: Best Unix Tricks?
#48This is more a general hint than a single tip. Years ago I tried to improve my Unix skills by doing everything at home from the command line -- burning CDs, playing music, copying trees etc. I learnt a lot but this exercise was much less convenient and more mentally demanding than using a gui. Recently I have become much more lazy.
I see my 'window manager' as a neccesary evil to run browser and email clients, everything else runs in terminal windows.
Of course you could use 'pine' and 'lynx' but that gets old pretty quickly.
Re: Ask HN: Best Unix Tricks?
#49screen is infinitely useful.
Re: Ask HN: Best Unix Tricks?
#50My tip: pushd without an arg pushes the current directory onto the stack, and then switches to what popd would have. Hence, repeated invocations of pushd let you cycle between two directories without thinking. $CDPATH is also amusing: $ mkdir /foo/bar/baz $ cd baz bash: cd: baz: no such file or directory $ export CDPATH="/foo/bar" $ cd baz $ pwd /foo/bar/baz
could you give an example of the pushd thing you mentioned?
$ pwd
/home/jrockway
$ pushd foo
$ pwd
/home/jrockway/foo
$ popd
$ pwd
/home/jrockway
$ popd
error: directory stack empty
$ pushd
error: directory stack empty
$ pushd foo
$ pwd
/home/jrockway/foo
$ pushd
/home/jrockway
$ pushd
/home/jrockway/foo
$ pushd
/home/jrockway
...