Sunday, July 31, 2011

Finding information about commands you use

Q: How to find out where the binary of a command I'm running?
A: You can use type command (available on both zsh and bash):
% type ls
ls is an alias for ls -h --color=auto
% type cat
cat is /bin/cat
% type alias
alias is a shell builtin

Q: Sometimes I want to know if a command is a shell script or a compiled binary.  How do I do that?
A: If you use zsh, you can use =command to get to command's full path.
% file =backup
/home/manki/d/bin/backup: a /bin/rbash script text executable
% # To show you what =backup actually translates to
% echo =backup
If you use bash, you can use type command within backticks or the equivalent $(...).
$ file `type -p backup`
/home/manki/d/bin/backup: a /bin/rbash script text executable
When you have aliases, this can get tricky.  On bash I don't know how to do this, but zsh is smart enough to find the executable even when you have aliases set up.  For instance, I have aliased ls to ls -h --color=auto, but zsh gives me the right binary for =ls:
% file =ls    
/bin/ls: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, stripped

Thursday, July 14, 2011

Saving the input to `less' command

less is good for viewing long outputs from other programs.  There may be times when you pipe the output of a program to less and realise only later that you want the output in a file... maybe because you need to send the output to someone via email.

You can quit less and change the command line to output to a file.  But there are better/faster options.  less supports an s command to save its input to a file.  Just press s, enter a file path to save the text to, and you're done.