一些好用的命令

摘录自http://clippy.in/b/YJLM9W

$ sudo !!
Run the last command as root
Useful when you forget to use sudo for a command. “!!” grabs the last run command.

$ python -m SimpleHTTPServer
Serve current directory tree at http://$HOSTNAME:8000/

$ :w !sudo tee %
Save a file you edited in vim without the needed permissions
I often forget to sudo before editing a file I don’t have write permissions on. When you come to save that file and get the infamous “E212: Can’t open file for writing”, just issue that vim command in order to save the file without the need to save it to a temp file and then copy it back again.

$ cd -
change to the previous working directory

$ ^foo^bar
Runs previous command but replacing
Really useful for when you have a typo in a previous command. Also, arguments default to empty so if you accidentally run:echo "no typozs"
you can correct it with^z

$ ctrl-x e
Rapidly invoke an editor to write a long, complex, or tricky command
Next time you are using your shell, try typing ctrl-x e (that is holding control key press x and then e). The shell will take what you’ve written on the command line thus far and paste it into the editor specified by $EDITOR. Then you can edit at leisure using all the powerful macros and commands of vi, emacs, nano, or whatever.

$ <space>command
Execute a command without saving it in the history
Prepending one or more spaces to your command won’t be saved in history.
Useful for pr0n or passwords on the commandline.
Tested on BASH.

$ > file.txt
Empty a file
For when you want to flush all content from a file without removing it (hat-tip to Marc Kilgus).

$ $ssh-copy-id user@host
Copy ssh keys to user@host to enable password-less ssh logins.
To generate the keys use the command ssh-keygen

$ 'ALT+.' or '<ESC> .'
Place the argument of the most recent command on the shell
When typing out long arguments, such as:
cp file.txt /var/www/wp-content/uploads/2009/03/
You can put that argument on your command line by holding down the ALT key and pressing the period ‘.’ or by pressing then the period ‘.’. For example: cd 'ALT+.'
would put ‘/var/www/wp-content/uploads/2009/03/ as my argument. Keeping pressing ‘ALT+.’ to cycle through arguments of your commands starting from most recent to oldest. This can save a ton of typing.

$ mount | column -t
currently mounted filesystems in nice layout
Particularly useful if you’re mounting different drives, using the following command will allow you to see all the filesystems currently mounted on your computer and their respective specs with the added benefit of nice formatting.

$ ssh -N -L2001:localhost:80 somemachine
start a tunnel from some machine’s port 80 to your local post 2001
now you can acces the website by going to http://localhost:2001/

$ netstat -tlnp
Lists all listening ports together with the PID of the associated process
The PID will only be printed if you’re holding a root equivalent ID.

$ man ascii
Quick access to the ascii table.

$ sshfs name@server:/path/to/folder /path/to/mount/point
Mount folder/filesystem through SSH
Install SSHFS from http://fuse.sourceforge.net/sshfs.html
Will allow you to mount a folder security over a network.

ref:

  1. http://mmb.pcb.ub.es/~carlesfe/unix/tricks.txt
  2. http://clippy.in/b/YJLM9W



– EOF –

Categories: linux
Tags: shell