slacka

Favorite Unix Commands

Jan 8th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.00 KB | None | 0 0
  1. python -m SimpleHTTPServer
  2. Serve current directory tree at http://$HOSTNAME:8000/
  3.  
  4. cd -
  5. change to the previous working directory
  6.  
  7. mtr google.com
  8. mtr, better than traceroute and ping combined
  9. mtr combines the functionality of the traceroute and ping programs in a single network diagnostic tool.
  10. As mtr starts, it investigates the network connection between the host mtr runs on and HOSTNAME. by sending packets with purposly low TTLs. It continues to send packets with low TTL, noting the response time of the intervening routers. This allows mtr to print the response percentage and response times of the internet route to HOSTNAME. A sudden increase in packetloss or response time is often an indication of a bad (or simply over?loaded) link.
  11.  
  12. > file.txt
  13. Empty a file
  14.  
  15. $ssh-copy-id user@host
  16. Copy ssh keys to user@host to enable password-less ssh logins.
  17.  
  18. reset
  19. Salvage a borked terminal
  20. If you bork your terminal by sending binary data to STDOUT or similar, you can get your terminal back using this command rather than killing and restarting the session. Note that you often won't be able to see the characters as you type them.
  21.  
  22. ffmpeg -f x11grab -s wxga -r 25 -i :0.0 -sameq /tmp/out.mpg
  23. Capture video of a linux desktop
  24.  
  25. 'ALT+.' or '<ESC> .'
  26. Place the argument of the most recent command on the shell
  27. When typing out long arguments, such as:
  28. cp file.txt /var/www/wp-content/uploads/2009/03/
  29. You can put that argument on your command line by holding down the ALT key and pressing the period '.' or by pressing <ESC> then the period '.'. For example:
  30. cd 'ALT+.'
  31. 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.
  32.  
  33. mount | column -t
  34. currently mounted filesystems in nice layout
  35. 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.
  36.  
  37. ssh -N -L2001:localhost:80 somemachine
  38. start a tunnel from some machine's port 80 to your local post 2001
  39. now you can access the website by going to http://localhost:2001/
  40.  
  41. echo "ls -l" | at midnight
  42. Execute a command at a given time
  43. This is an alternative to cron which allows a one-off task to be scheduled for a certain time.
  44. dig +short txt <keyword>.wp.dg.cx
  45. Functions: dig
  46. Query Wikipedia via console over DNS
  47. Query Wikipedia by issuing a DNS query for a TXT record. The TXT record will also include a short URL to the complete corresponding Wikipedia entry.You can also write a little shell script like:
  48. $ cat wikisole.sh
  49. #!/bin/sh
  50. dig +short txt ${1}.wp.dg.cx
  51. and run it like
  52. ./wikisole.sh unix
  53. were your first option ($1) will be used as search term.
  54.  
  55. netstat -tlnp
  56. Lists all listening ports together with the PID of the associated process
  57. The PID will only be printed if you're holding a root equivalent ID.
  58. Comments (8) | Add to favourites | Report as malicious | Submit alternative | Report as a duplicate
  59.  
  60. dd if=/dev/dsp | ssh -c arcfour -C username@host dd of=/dev/dsp
  61. output your microphone to a remote computer's speaker
  62. This will output the sound from your microphone port to the ssh target computer's speaker port. The sound quality is very bad, so you will hear a lot of hissing.
  63.  
  64. curl -u user:pass -d status="Tweeting from the shell" http://twitter.com/statuses/update.xml
  65. Update twitter via curl
  66.  
  67. !!:gs/foo/bar
  68. Runs previous command replacing foo by bar every time that foo appears
  69. Very useful for rerunning a long command changing some arguments globally.
  70. As opposed to ^foo^bar, which only replaces the first occurrence of foo, this one changes every occurrence.
  71.  
  72. mount -t tmpfs tmpfs /mnt -o size=1024m
  73. Mount a temporary ram partition
  74. Makes a partition in ram which is useful if you need a temporary working space as read/write access is fast.
  75. Be aware that anything saved in this partition will be gone after your computer is turned off.
  76.  
  77. man ascii
  78. Quick access to the ascii table.
  79.  
  80. sshfs name@server:/path/to/folder /path/to/mount/point
  81. Mount folder/filesystem through SSH
  82. Install SSHFS from http://fuse.sourceforge.net/sshfs.html
  83. Will allow you to mount a folder security over a network.
  84.  
  85. curl ifconfig.me
  86. Get your external IP address
  87. curl ifconfig.me/ip -> IP Adress
  88. curl ifconfig.me/host -> Remote Host
  89. curl ifconfig.me/ua ->User Agent
  90. curl ifconfig.me/port -> Port
  91. thonks to http://ifconfig.me/
  92.  
  93.  
  94.  
  95. use iperf. It's a client server arrangement in that you run it in server mode at one end and connect to its from another computer on the other side of the network.
  96.  
  97. One both machines run:
  98.  
  99. sudo apt-get install iperf
  100. Then on Computer A:
  101.  
  102. iperf -s
  103. And on Computer B:
  104.  
  105. iperf -c <address of Computer A>
  106. On the client machine, you'll see something like this:
  107.  
  108. oli@bert:~$ iperf -c tim
  109. ------------------------------------------------------------
  110. Client connecting to tim, TCP port 5001
  111. TCP window size: 16.0 KByte (default)
  112. ------------------------------------------------------------
  113. [ 3] local 192.168.0.4 port 37248 connected with 192.168.0.5 port 5001
  114. [ ID] Interval Transfer Bandwidth
  115. [ 3] 0.0-10.0 sec 1.04 GBytes 893 Mbits/sec
  116. Of course, if you're running a firewall on the server machine, you'll need to allow connections on port 5001 or change the port with the -p flag.
  117.  
  118.  
  119. * In bash, 'ctrl-r' searches your command history as you type
  120.  
  121. SHELL - APPEND ONE FILE TO ANOTHER IN LINUX - STACK OVERFLOW
  122. cat file2 >> file1
  123.  
  124.  
  125. Using `find` is nice to get just directories:
  126. $ find . -type d -maxdepth 1
  127.  
  128.  
  129. Here's my list, a very compact plaintext file: http://mmb.pcb.ub.es/~carlesfe/unix/tricks.txt
  130.  
  131. I have marked with a * those which I think are absolutely essential
  132. Items for each section are sorted by oldest to newest. Come back soon for more!
  133.  
  134. BASH
  135. * In bash, 'ctrl-r' searches your command history as you type
  136. - Add "set -o vi" in your ~/.bashrc to make use the vi keybindings instead
  137. of the Emacs ones. Takes some time to get used to, but it's fantastic!
  138. - Input from the commandline as if it were a file by replacing
  139. 'command < file.in' with 'command <<< "some input text"'
  140. - '^' is a sed-like operator to replace chars from last command
  141. 'ls docs; ^docs^web^' is equal to 'ls web'. The second argument can be empty.
  142. * '!!:n' selects the nth argument of the last command
  143. 'ls file1 file2 file3; cat !!:1-2' shows all files and cats only 1 and 2
  144. - More in-line substitutions: http://tiny.cc/ecv0cw
  145. - 'nohup ./long_script &' to leave stuff in background even if you logout
  146. - 'cd -' change to the previous directory you were working on
  147. - 'ctrl-x ctrl-e' opens an editor to work with long or complex command lines
  148. * Use traps for cleaning up bash scripts on exit http://tiny.cc/traps
  149.  
  150. VIM
  151. - ':set spell' activates vim spellchecker. Use ']s' and '[s' to move between
  152. mistakes, 'zg' adds to the dictionary, 'z=' suggests correctly spelled words
  153.  
  154. TOOLS
  155. * 'htop' instead of 'top'
  156. - 'ranger' is a nice console file manager for vi fans
  157. - Use 'apt-file' to see which package provides that file you're missing
  158. - 'dict' is a commandline dictionary
  159. - Learn to use 'find' and 'locate' to look for files
  160. - 'find . -type d -exec chmod g+x {} \;' let people in your group access
  161. folders without messing up file permissions (never do 'chmod g+x * -R'!)
  162. - Compile your own version of 'screen' from the git sources. Most versions
  163. have a slow scrolling on a vertical split or even no vertical split at all
  164. * 'trash-cli' sends files to the trash instead of deleting them forever.
  165. Be very careful with 'rm' or maybe make a wrapper to avoid deleting '*' by
  166. accident (e.g. you want to type 'rm tmp*' but type 'rm tmp *')
  167. - 'file' gives information about a file, as image dimensions or text encoding
  168. - 'sort | uniq' to check for duplicate lines
  169. - 'echo start_backup.sh | at midnight' starts a command at the specified time
  170. - Pipe any command over 'column -t' to nicely align the columns
  171. * Google 'magic sysrq' and learn how to bring you machine back from the dead
  172. - 'diff --side-by-side fileA.txt fileB.txt | pager' to see a nice diff
  173. * 'j.py' http://tiny.cc/62qjow remembers your most used folders and is an
  174. incredible substitute to browse directories by name instead of 'cd'
  175. - 'dropbox_uploader.sh' http://tiny.cc/o2qjow is a fantastic solution to
  176. upload by commandline via Dropbox's API if you can't use the official client
  177.  
  178. NETWORKING
  179. - SMB is better than NFS. Use 'sshfs_mount' as a last resort,
  180. any network failure will be trouble
  181. - 'python -m SimpleHTTPServer 8080' shares all the files in the current
  182. folder over HTTP, port 8080
  183. - 'ssh -R 12345:localhost:22 server.com "sleep 1000; exit"' forwards
  184. server.com's port 12345 to your local ssh port, even if you machine
  185. is not externally visible on the net.
  186. Now you can 'ssh localhost -p 12345' from server.com and you will
  187. log into your machine.
  188. 'sleep' avoids getting kicked out from server.com for inactivity
  189. * Read on 'ssh-keygen' to avoid typing passwords every time you ssh
  190. - 'socat TCP4-LISTEN:1234,fork TCP4:192.168.1.1:22' forwards your port
  191. 1234 to another machine's port 22. Very useful for quick NAT redirection.
  192. * Configure postfix to use your personal Gmail account as SMTP:
  193. http://tiny.cc/n5k0cw. Now you can send emails from the command line.
  194. 'echo "Hello, User!" | mail [email protected]'
  195. - Some tools to monitor network connections and bandwith:
  196. 'lsof -i' monitors network connections in real time
  197. 'iftop' shows bandwith usage per *connection*
  198. 'nethogs' shows the bandwith usage per *process*
  199. * Use this trick on .ssh/config to directly access 'host2' which is on a private
  200. network, and must be accessed by ssh-ing into 'host1' first
  201. Host host2
  202. ProxyCommand ssh -T host1 'nc %h %p'
  203. HostName host2
  204.  
  205. -~-
  206.  
  207. (CC) by-nc, Carles Fenollosa <[email protected]>
  208. Retrieved from http://mmb.pcb.ub.es/~carlesfe/unix/tricks.txt
  209. Last modified: Mon 07 Jan 2013 09:48:46 PM CET
Advertisement
Add Comment
Please, Sign In to add comment