Live data from Hacker News

Unix Tricks

cfenollosa.com

121–130 of 166 posts

Re: Unix Tricks

#121

A really handy one a colleague showed me yesterday was the /dev/fd/0|1|2 files, which are stdin/out/err respectively. Means you can use that file for utils that expect a file only. E.g echo "This would be contents of file" | someCommand /dev/fd/0

These are also available as /dev/std{in,out,err}; the names of which are a little more self documenting :)

    $ ls -l /dev/fd /dev/std* | column -t
    lrwxrwxrwx  1  root  root  13  Jun  27  16:32  /dev/fd      ->  /proc/self/fd
    lrwxrwxrwx  1  root  root  15  Jun  27  16:32  /dev/stderr  ->  /proc/self/fd/2
    lrwxrwxrwx  1  root  root  15  Jun  27  16:32  /dev/stdin   ->  /proc/self/fd/0
    lrwxrwxrwx  1  root  root  15  Jun  27  16:32  /dev/stdout  ->  /proc/self/fd/1
You can even access the file descriptors of other processes with /proc/${pid}/fd/; which is handy for (say) un-deleting a file that a process still has open.

Another handy one is "process substitution" which lets you do the same thing with multiple streams by creating new file descriptors, and automatically turning them into /dev/fd/* paths:

    someCommand 
which is somewhat explained by:

    $ echo someCommand 

Re: Unix Tricks

#122
zsh has a simple builtin `r` (which is just an alias for `fc -e -`). It lets you edit the previous command with search and replace. I use it for brew and git a lot.

  $ brew info rust
  $ r fo=stal

Re: Unix Tricks

#123

Its implied on the list but not mentioned specifically. !! will be substituted with the last command you typed. So if your somebody like me who frequently types $ apt-get update Could not lock yada yada are you root? $ sudo apt-get update instead you can type $ apt-get update $ sudo !!

I named my cat sudo bangbang

Re: Unix Tricks

#124
post #64

Missing trick: In bash, "ESC" then "." fetches the last parameter of the previous command. It's invaluable (same as typing "!$" but you get to see and edit it)

I use `!:p` to the same effect. Which has the benefit that you can insert a history number between the bang! and the colon: ( set PS1 to include the command number if you do this a lot ).

Re: Unix Tricks

#125
post #80

Earlier quoted context omitted.

> Typing in long commands in the Mac OS X terminal and then them wrapping weirdly. For all people who dont use Mac OS X: This happens because your PS1 is wrongly set and bash cant calculate correctly the length left of your line. Try it out, by going back to default with no colors and crap and see how long it goes. For mac os x users. The above wont help, dont even try it.

I found the solution to bash problems was to move to zsh. I did that almost 10 years ago and haven't regretted the decision.

Can you suggest an intro guide to zsh for former bashers?

Re: Unix Tricks

#126
post #87
post #17

Earlier quoted context omitted.

Here you go, just throw it in your .bashrc: shopt -s histappend export HISTSIZE=100000 export HISTFILESIZE=100000 export HISTCONTROL=ignoredups:erasedups export PROMPT_COMMAND="history -a;history -c;history -r;$PROMPT_COMMAND" I wish I could give credit to where I originally found this but it was ages ago.

I've been using `history -a`, but what do `history -c` and `history -r` do? It looks like it clears your shell's history, and then re-populates it from the .bash_history file?

   history -a  # append history lines from this session to the history file.
   
   #History file may contain history from other terminals not in this one so:

   history -c # clear [in-memory] history list deleting all of the entries.
   history -r # read the history file and append the contents to the history list instead.
I've heard that -n can be problematic which is why -c then -r is used.

Re: Unix Tricks

#127

Earlier quoted context omitted.

> I think [DOS] is easier to understand in it's entirety and find your way around than Unix Yikes, I recently had to do a bit of development on a windows box and I found the command-line tools (not to mention CMD itself, which seems to have stopped development in 1993) to be absolutely awful. I could barely survive without Cygwin, git bash, etc giving me some semblance of a functional shell setup. I guess it's differ…

I agree: awful tools and batch language is horrible (and largely non-portable between different versions). Getting complicated stuff to work can be tricky due to the lack of essential commands. For instance, there's no way to reliably get an ISO formatted datestamp without a 3rd party utility (although I think PowerShell supports this). Lots of odd little things like that simply don't work. But the filesystem layout…

> But the filesystem layout and set of built-in commands is pretty easy to understand fully. It's generally pretty easy to find things.

On the face of it, the basic directories are straightforward (Programs -> \Progra~1, User files -> \Docume~1, System files -> \Window~1, Very System files -> \Window~1\System32), but I don't recall having an easy time finding things (speaking from memories of my XP days).

Where would you look for a config file of some application? Maybe it'll be in \Progra~1. Or $user\LocalSettings. Or ApplicationData. Maybe in the Registry? (Don't get me started about the registry!)

Where would you look for log files? Does the Event Viewer show everything nowadays, or do you still have to hunt for logfiles in the same fashion as config files?

On *nix, I know that my configs are under /etc/ (global) and ~/.{program name}/ (user-specific), and my logs are under /var/log/. I don't know what every single directory is, but I know where to look for things when I need to.

Re: Unix Tricks

#128
post #81
post #64

Missing trick: In bash, "ESC" then "." fetches the last parameter of the previous command. It's invaluable (same as typing "!$" but you get to see and edit it)

It's actually Meta , emulated by an ESC character by the terminal. OSX tip: check "use Option as Meta" in Terminal's preferences, and you can keep Option pressed while you mash ".".

Which is fine if you never need to generate characters using the Option key. For example on a German keyboard you need the Option key to write { or }.

Re: Unix Tricks

#129
post #29

man hier is pretty cool. It explains the root directory structure of the system.

Which was updated just yesterday[1]. [1]: https://plus.google.com/+LennartPoetteringTheOneAndOnly/post...

international pun, hier being french for yesterday.

Re: Unix Tricks

#130
post #21
post #3

It's amazing how many years I've been using bash and I still have things to discover. Ctrl-x Ctrl-e nice tip!

I still prefer "sh" on BSD (particularly OpenBSD). It doesn't have as many knobs to turn nor as many surprises.

sh, the Bourne shell mode of pdksh? Isn't that a little too limited? pdksh (/bin/ksh) though is great, yes.

I'm in general a big fan of the OpenBSD userland: straightforward and very, very well-documented. Real manpages, no religious "The full documentation for xyz is maintained as a TeXinfo manual" crap.

A well-conceived featureset and good docs are key to properly learning a platform. OpenBSD taught me a lot about Unix. If you later realize that Bash has some feature that you actually need, you can still install it.

Post reply on HN