Here are some time-saving UNIX tips that every UNIX user should know, including some from Joshua Levy via Quora:
Everyday use
- In bash, use Ctrl-R to search through command history.
- In bash, use Ctrl-W to kill the last word, and Ctrl-U to kill the line.
- To go back to the previous working directory: cd -
- Use xargs. It’s very powerful. Note you can control how many items execute per line (-L) as well as parallelism (-P). If you’re not sure if it’ll do the right thing, use xargs echo first. Also, -I{} is handy. Examples:
find . -name \*.py | xargs grep some_function
cat hosts | xargs -I{} ssh root@{} hostname
- pstree -p is a helpful display of the process tree.
- Use pgrep and pkill to find or signal processes by name (-f is helpful).
- Know the various signals you can send processes. For example, to suspend a process, use kill -STOP [pid]. For the full list, see man 7 signal
- Use nohup or disown if you want a background process to keep running forever.
- Check what processes are listening via netstat -lntp. See also lsof.
- Use man ascii for a good ASCII table, with hex and decimal values.
- For web debugging, curl and curl -I are handy, and/or their wget equivalents.
- To convert HTML to text: lynx -dump -stdin
- In ssh, knowing how to port tunnel with -L or -D (and occasionally -R) is useful, e.g. to access web sites from a remote server.
- If you are halfway through typing a command but change your mind, hit Alt-# to add a # at the beginning and enter it as a comment (or use Ctrl-A, #, enter). You can then return to it later via command history.
- To know disk/cpu/network status, use iostat, netstat, top (or the better htop), and (especially) dstat. Good for getting a quick idea of what’s happening on a system.
- To know memory status, run and understand the output of free. In particular, be aware the “cached” value is memory held by the Linux kernel as file cache, so effectively counts toward the “free” value.
- Use mtr as a better traceroute, to identify network issues. (not by default installed on Mac OS X)
- To find which socket or process is using bandwidth, try iftop or nethogs.
- Use /proc. It’s amazingly helpful sometimes when debugging live problems. Examples: /proc/cpuinfo, /proc/xxx/cwd, /proc/xxx/exe, /proc/xxx/fd/, /proc/xxx/smaps.
- When debugging why something went wrong in the past, sar can be very helpful. It shows historic statistics on CPU, memory, network, etc.
- Use dmesg whenever something’s acting really funny (it could be hardware or driver issues).
There are lots more here.
A challenge? Understand everything that’s mentioned above. Not learn it, or memorize, but grok the context.