command fuu


SUBMITTED BY: Guest

DATE: Dec. 4, 2013, 11:46 a.m.

FORMAT: Text only

SIZE: 85.0 kB

HITS: 870

  1. # commandlinefu.com by David Winterbottom
  2. # Run the last command as root
  3. sudo !!
  4. # Serve current directory tree at http://$HOSTNAME:8000/
  5. python -m SimpleHTTPServer
  6. # change to the previous working directory
  7. cd -
  8. # Runs previous command but replacing
  9. ^foo^bar
  10. # mtr, better than traceroute and ping combined
  11. mtr google.com
  12. # Rapidly invoke an editor to write a long, complex, or tricky command
  13. ctrl-x e
  14. # Execute a command without saving it in the history
  15. <space>command
  16. # Empty a file
  17. > file.txt
  18. # Salvage a borked terminal
  19. reset
  20. # Capture video of a linux desktop
  21. ffmpeg -f x11grab -s wxga -r 25 -i :0.0 -sameq /tmp/out.mpg
  22. # Place the argument of the most recent command on the shell
  23. 'ALT+.' or '<ESC> .'
  24. # currently mounted filesystems in nice layout
  25. mount | column -t
  26. # Query Wikipedia via console over DNS
  27. dig +short txt <keyword>.wp.dg.cx
  28. # Execute a command at a given time
  29. echo "ls -l" | at midnight
  30. # start a tunnel from some machine's port 80 to your local post 2001
  31. ssh -N -L2001:localhost:80 somemachine
  32. # Lists all listening ports together with the PID of the associated process
  33. netstat -tlnp
  34. # Quick access to the ascii table.
  35. man ascii
  36. # Runs previous command replacing foo by bar every time that foo appears
  37. !!:gs/foo/bar
  38. # output your microphone to a remote computer's speaker
  39. dd if=/dev/dsp | ssh -c arcfour -C username@host dd of=/dev/dsp
  40. # Get your external IP address
  41. curl ifconfig.me
  42. # Mount a temporary ram partition
  43. mount -t tmpfs tmpfs /mnt -o size=1024m
  44. # Mount folder/filesystem through SSH
  45. sshfs name@server:/path/to/folder /path/to/mount/point
  46. # Update twitter via curl
  47. curl -u user:pass -d status="Tweeting from the shell" http://twitter.com/statuses/update.xml
  48. # Compare a remote file with a local file
  49. ssh user@host cat /path/to/remotefile | diff /path/to/localfile -
  50. # Download an entire website
  51. wget --random-wait -r -p -e robots=off -U mozilla http://www.example.com
  52. # commandlinefu.com by David Winterbottom
  53. # Clear the terminal screen
  54. ctrl-l
  55. # Jump to a directory, execute a command and jump back to current dir
  56. (cd /tmp && ls)
  57. # List the size (in human readable form) of all sub folders from the current location
  58. du -h --max-depth=1
  59. # type partial command, kill this command, check something you forgot, yank the command, resume typing.
  60. <ctrl+u> [...] <ctrl+y>
  61. # A very simple and useful stopwatch
  62. time read (ctrl-d to stop)
  63. # SSH connection through host in the middle
  64. ssh -t reachable_host ssh unreachable_host
  65. # Shutdown a Windows machine from Linux
  66. net rpc shutdown -I ipAddressOfWindowsPC -U username%password
  67. # Make 'less' behave like 'tail -f'.
  68. less +F somelogfile
  69. # Watch Star Wars via telnet
  70. telnet towel.blinkenlights.nl
  71. # Check your unread Gmail from the command line
  72. curl -u username --silent "https://mail.google.com/mail/feed/atom" | perl -ne 'print "\t" if /<name>/; print "$2\n" if /<(title|name)>(.*)<\/\1>/;'
  73. # Simulate typing
  74. echo "You can simulate on-screen typing just like in the movies" | pv -qL 10
  75. # Set audible alarm when an IP address comes online
  76. ping -i 60 -a IP_address
  77. # Reboot machine when everything is hanging
  78. <alt> + <print screen/sys rq> + <R> - <S> - <E> - <I> - <U> - <B>
  79. # Close shell keeping all subprocess running
  80. disown -a && exit
  81. # List of commands you use most often
  82. history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
  83. # Display the top ten running processes - sorted by memory usage
  84. ps aux | sort -nk +4 | tail
  85. # 32 bits or 64 bits?
  86. getconf LONG_BIT
  87. # Display a block of text with AWK
  88. awk '/start_pattern/,/stop_pattern/' file.txt
  89. # Set CDPATH to ease navigation
  90. CDPATH=:..:~:~/projects
  91. # Push your present working directory to a stack that you can pop later
  92. pushd /tmp
  93. # Put a console clock in top right corner
  94. while sleep 1;do tput sc;tput cup 0 $(($(tput cols)-29));date;tput rc;done &
  95. # Watch Network Service Activity in Real-time
  96. lsof -i
  97. # Backticks are evil
  98. echo "The date is: $(date +%D)"
  99. # Create a script of the last executed command
  100. echo "!!" > foo.sh
  101. # diff two unsorted files without creating temporary files
  102. diff <(sort file1) <(sort file2)
  103. # commandlinefu.com by David Winterbottom
  104. # Sharing file through http 80 port
  105. nc -v -l 80 < file.ext
  106. # escape any command aliases
  107. \[command]
  108. # Reuse all parameter of the previous command line
  109. !*
  110. # Easy and fast access to often executed commands that are very long and complex.
  111. some_very_long_and_complex_command # label
  112. # Show apps that use internet connection at the moment. (Multi-Language)
  113. lsof -P -i -n
  114. # quickly rename a file
  115. mv filename.{old,new}
  116. # python smtp server
  117. python -m smtpd -n -c DebuggingServer localhost:1025
  118. # Delete all files in a folder that don't match a certain file extension
  119. rm !(*.foo|*.bar|*.baz)
  120. # Google Translate
  121. translate(){ wget -qO- "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$1&langpair=$2|${3:-en}" | sed 's/.*"translatedText":"\([^"]*\)".*}/\1\n/'; }
  122. # save command output to image
  123. ifconfig | convert label:@- ip.png
  124. # Remove duplicate entries in a file without sorting.
  125. awk '!x[$0]++' <file>
  126. # Extract tarball from internet without local saving
  127. wget -qO - "http://www.tarball.com/tarball.gz" | tar zxvf -
  128. # Kills a process that is locking a file.
  129. fuser -k filename
  130. # Matrix Style
  131. tr -c "[:digit:]" " " < /dev/urandom | dd cbs=$COLUMNS conv=unblock | GREP_COLOR="1;32" grep --color "[^ ]"
  132. # Rip audio from a video file.
  133. mplayer -ao pcm -vo null -vc dummy -dumpaudio -dumpfile <output-file> <input-file>
  134. # Display which distro is installed
  135. cat /etc/issue
  136. # Find the process you are looking for minus the grepped one
  137. ps aux | grep [p]rocess-name
  138. # Insert the last command without the last argument (bash)
  139. !:-
  140. # Inserts the results of an autocompletion in the command line
  141. ESC *
  142. # Add Password Protection to a file your editing in vim.
  143. vim -x <FILENAME>
  144. # Stream YouTube URL directly to mplayer.
  145. i="8uyxVmdaJ-w";mplayer -fs $(curl -s "http://www.youtube.com/get_video_info?&video_id=$i" | echo -e $(sed 's/%/\\x/g;s/.*\(v[0-9]\.lscache.*\)/http:\/\/\1/g') | grep -oP '^[^|,]*')
  146. # Copy your SSH public key on a remote machine for passwordless login - the easy way
  147. ssh-copy-id username@hostname
  148. # A fun thing to do with ram is actually open it up and take a peek. This command will show you all the string (plain text) values in ram
  149. sudo dd if=/dev/mem | cat | strings
  150. # Define a quick calculator function
  151. ? () { echo "$*" | bc -l; }
  152. # Show File System Hierarchy
  153. man hier
  154. # commandlinefu.com by David Winterbottom
  155. # Easily search running processes (alias).
  156. alias 'ps?'='ps ax | grep '
  157. # Create a CD/DVD ISO image from disk.
  158. readom dev=/dev/scd0 f=/path/to/image.iso
  159. # Graphical tree of sub-directories
  160. ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
  161. # Monitor progress of a command
  162. pv access.log | gzip > access.log.gz
  163. # Print all the lines between 10 and 20 of a file
  164. sed -n '10,20p' <filename>
  165. # Make directory including intermediate directories
  166. mkdir -p a/long/directory/path
  167. # Multiple variable assignments from command output in BASH
  168. read day month year <<< $(date +'%d %m %y')
  169. # intercept stdout/stderr of another process
  170. strace -ff -e trace=write -e write=1,2 -p SOME_PID
  171. # Job Control
  172. ^Z $bg $disown
  173. # Show apps that use internet connection at the moment. (Multi-Language)
  174. ss -p
  175. # Copy a file using pv and watch its progress
  176. pv sourcefile > destfile
  177. # Send pop-up notifications on Gnome
  178. notify-send ["<title>"] "<body>"
  179. # Edit a file on a remote host using vim
  180. vim scp://username@host//path/to/somefile
  181. # Create a pdf version of a manpage
  182. man -t manpage | ps2pdf - filename.pdf
  183. # replace spaces in filenames with underscores
  184. rename 'y/ /_/' *
  185. # Generate a random password 30 characters long
  186. strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 30 | tr -d '\n'; echo
  187. # Find Duplicate Files (based on size first, then MD5 hash)
  188. find -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1 find -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate
  189. # Check your unread Gmail from the command line
  190. curl -u username:password --silent "https://mail.google.com/mail/feed/atom" | tr -d '\n' | awk -F '<entry>' '{for (i=2; i<=NF; i++) {print $i}}' | sed -n "s/<title>\(.*\)<\/title.*name>\(.*\)<\/name>.*/\2 - \1/p"
  191. # Graph # of connections for each hosts.
  192. netstat -an | grep ESTABLISHED | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | awk '{ printf("%s\t%s\t",$2,$1) ; for (i = 0; i < $1; i++) {printf("*")}; print "" }'
  193. # Convert seconds to human-readable format
  194. date -d@1234567890
  195. # Remove all but one specific file
  196. rm -f !(survivior.txt)
  197. # return external ip
  198. curl icanhazip.com
  199. # Display a cool clock on your terminal
  200. watch -t -n1 "date +%T|figlet"
  201. # Monitor the queries being run by MySQL
  202. watch -n 1 mysqladmin --user=<user> --password=<password> processlist
  203. # Mount a .iso file in UNIX/Linux
  204. mount /path/to/file.iso /mnt/cdrom -oloop
  205. # commandlinefu.com by David Winterbottom
  206. # Get the 10 biggest files/folders for the current direcotry
  207. du -s * | sort -n | tail
  208. # Record a screencast and convert it to an mpeg
  209. ffmpeg -f x11grab -r 25 -s 800x600 -i :0.0 /tmp/outputFile.mpg
  210. # Search commandlinefu.com from the command line using the API
  211. cmdfu(){ curl "http://www.commandlinefu.com/commands/matching/$@/$(echo -n $@ | openssl base64)/plaintext"; }
  212. # Open Finder from the current Terminal location
  213. open .
  214. # Processor / memory bandwidthd? in GB/s
  215. dd if=/dev/zero of=/dev/null bs=1M count=32768
  216. # mkdir & cd into it as single command
  217. mkdir /home/foo/doc/bar && cd $_
  218. # Remove a line in a text file. Useful to fix
  219. ssh-keygen -R <the_offending_host>
  220. # Attach screen over ssh
  221. ssh -t remote_host screen -r
  222. # Share a terminal screen with others
  223. % screen -r someuser/
  224. # Create a persistent connection to a machine
  225. ssh -MNf <user>@<host>
  226. # Copy your ssh public key to a server from a machine that doesn't have ssh-copy-id
  227. cat ~/.ssh/id_rsa.pub | ssh user@machine "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys"
  228. # Makes the permissions of file2 the same as file1
  229. chmod --reference file1 file2
  230. # git remove files which have been deleted
  231. git add -u
  232. # directly ssh to host B that is only accessible through host A
  233. ssh -t hostA ssh hostB
  234. # (Debian/Ubuntu) Discover what package a file belongs to
  235. dpkg -S /usr/bin/ls
  236. # List all bash shortcuts
  237. bind -P
  238. # Draw kernel module dependancy graph.
  239. lsmod | perl -e 'print "digraph \"lsmod\" {";<>;while(<>){@_=split/\s+/; print "\"$_[0]\" -> \"$_\"\n" for split/,/,$_[3]}print "}"' | dot -Tpng | display -
  240. # RTFM function
  241. rtfm() { help $@ || man $@ || $BROWSER "http://www.google.com/search?q=$@"; }
  242. # Show numerical values for each of the 256 colors in bash
  243. for code in {0..255}; do echo -e "\e[38;05;${code}m $code: Test"; done
  244. # To print a specific line from a file
  245. sed -n 5p <file>
  246. # Run a command only when load average is below a certain threshold
  247. echo "rm -rf /unwanted-but-large/folder" | batch
  248. # Download Youtube video with wget!
  249. wget http://www.youtube.com/watch?v=dQw4w9WgXcQ -qO- | sed -n "/fmt_url_map/{s/[\'\"\|]/\n/g;p}" | sed -n '/^fmt_url_map/,/videoplayback/p' | sed -e :a -e '$q;N;5,$D;ba' | tr -d '\n' | sed -e 's/\(.*\),\(.\)\{1,3\}/\1/' | wget -i - -O surprise.flv
  250. # Broadcast your shell thru ports 5000, 5001, 5002 ...
  251. script -qf | tee >(nc -kl 5000) >(nc -kl 5001) >(nc -kl 5002)
  252. # Eavesdrop on your system
  253. diff <(lsof -p 1234) <(sleep 10; lsof -p 1234)
  254. # Remove security limitations from PDF documents using ghostscript
  255. gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=OUTPUT.pdf -c .setpdfwrite -f INPUT.pdf
  256. # commandlinefu.com by David Winterbottom
  257. # Run a file system check on your next boot.
  258. sudo touch /forcefsck
  259. # Release memory used by the Linux kernel on caches
  260. free && sync && echo 3 > /proc/sys/vm/drop_caches && free
  261. # Remove all files previously extracted from a tar(.gz) file.
  262. tar -tf <file.tar.gz> | xargs rm -r
  263. # using `!#$' to referance backward-word
  264. cp /work/host/phone/ui/main.cpp !#$:s/host/target
  265. # Binary Clock
  266. watch -n 1 'echo "obase=2;`date +%s`" | bc'
  267. # List only the directories
  268. ls -d */
  269. # Download all images from a site
  270. wget -r -l1 --no-parent -nH -nd -P/tmp -A".gif,.jpg" http://example.com/images
  271. # Duplicate installed packages from one machine to the other (RPM-based systems)
  272. ssh root@remote.host "rpm -qa" | xargs yum -y install
  273. # Start COMMAND, and kill it if still running after 5 seconds
  274. timeout 5s COMMAND
  275. # Use tee to process a pipe with two or more processes
  276. echo "tee can split a pipe in two"|tee >(rev) >(tr ' ' '_')
  277. # Edit a google doc with vim
  278. google docs edit --title "To-Do List" --editor vim
  279. # Backup all MySQL Databases to individual files
  280. for I in $(mysql -e 'show databases' -s --skip-column-names); do mysqldump $I | gzip > "$I.sql.gz"; done
  281. # Port Knocking!
  282. knock <host> 3000 4000 5000 && ssh -p <port> user@host && knock <host> 5000 4000 3000
  283. # Show a 4-way scrollable process tree with full details.
  284. ps awwfux | less -S
  285. # which program is this port belongs to ?
  286. lsof -i tcp:80
  287. # Sort the size usage of a directory tree by gigabytes, kilobytes, megabytes, then bytes.
  288. du -b --max-depth 1 | sort -nr | perl -pe 's{([0-9]+)}{sprintf "%.1f%s", $1>=2**30? ($1/2**30, "G"): $1>=2**20? ($1/2**20, "M"): $1>=2**10? ($1/2**10, "K"): ($1, "")}e'
  289. # Google text-to-speech in mp3 format
  290. wget -q -U Mozilla -O output.mp3 "http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=hello+world
  291. # What is my public IP-address?
  292. curl ifconfig.me
  293. # Search recursively to find a word or phrase in certain file types, such as C code
  294. find . -name "*.[ch]" -exec grep -i -H "search pharse" {} \;
  295. # Bring the word under the cursor on the :ex line in Vim
  296. :<C-R><C-W>
  297. # Block known dirty hosts from reaching your machine
  298. wget -qO - http://infiltrated.net/blacklisted|awk '!/#|[a-z]/&&/./{print "iptables -A INPUT -s "$1" -j DROP"}'
  299. # Synchronize date and time with a server over ssh
  300. date --set="$(ssh user@server date)"
  301. # Recursively remove all empty directories
  302. find . -type d -empty -delete
  303. # Add timestamp to history
  304. export HISTTIMEFORMAT="%F %T "
  305. # Rapidly invoke an editor to write a long, complex, or tricky command
  306. fc
  307. # commandlinefu.com by David Winterbottom
  308. # Find out how much data is waiting to be written to disk
  309. grep ^Dirty /proc/meminfo
  310. # Remove a line in a text file. Useful to fix "ssh host key change" warnings
  311. sed -i 8d ~/.ssh/known_hosts
  312. # Search for a <pattern> string inside all files in the current directory
  313. grep -RnisI <pattern> *
  314. # Colorized grep in less
  315. grep --color=always | less -R
  316. # check site ssl certificate dates
  317. echo | openssl s_client -connect www.google.com:443 2>/dev/null |openssl x509 -dates -noout
  318. # Create a quick back-up copy of a file
  319. cp file.txt{,.bak}
  320. # Convert Youtube videos to MP3
  321. youtube-dl -t --extract-audio --audio-format mp3 YOUTUBE_URL_HERE
  322. # Exclude multiple columns using AWK
  323. awk '{$1=$3=""}1' file
  324. # Get the IP of the host your coming from when logged in remotely
  325. echo ${SSH_CLIENT%% *}
  326. # ls not pattern
  327. ls !(*.gz)
  328. # Show apps that use internet connection at the moment.
  329. lsof -P -i -n | cut -f 1 -d " "| uniq | tail -n +2
  330. # Compare two directory trees.
  331. diff <(cd dir1 && find | sort) <(cd dir2 && find | sort)
  332. # exit without saving history
  333. kill -9 $$
  334. # Remind yourself to leave in 15 minutes
  335. leave +15
  336. # make directory tree
  337. mkdir -p work/{d1,d2}/{src,bin,bak}
  338. # Control ssh connection
  339. [enter]~?
  340. # pretend to be busy in office to enjoy a cup of coffee
  341. cat /dev/urandom | hexdump -C | grep "ca fe"
  342. # run complex remote shell cmds over ssh, without escaping quotes
  343. ssh host -l user $(<cmd.txt)
  344. # GREP a PDF file.
  345. pdftotext [file] - | grep 'YourPattern'
  346. # Exclude .svn, .git and other VCS junk for a pristine tarball
  347. tar --exclude-vcs -cf src.tar src/
  348. # delete a line from your shell history
  349. history -d
  350. # Recursively change permissions on files, leave directories alone.
  351. find ./ -type f -exec chmod 644 {} \;
  352. # Convert PDF to JPG
  353. for file in `ls *.pdf`; do convert -verbose -colorspace RGB -resize 800 -interlace none -density 300 -quality 80 $file `echo $file | sed 's/\.pdf$/\.jpg/'`; done
  354. # Prettify an XML file
  355. tidy -xml -i -m [file]
  356. # Quick access to ASCII code of a key
  357. showkey -a
  358. # commandlinefu.com by David Winterbottom
  359. # find files in a date range
  360. find . -type f -newermt "2010-01-01" ! -newermt "2010-06-01"
  361. # notify yourself when a long-running command which has ALREADY STARTED is finished
  362. <ctrl+z> fg; notify_me
  363. # easily find megabyte eating files or directories
  364. alias dush="du -sm *|sort -n|tail"
  365. # prints line numbers
  366. nl
  367. # A robust, modular log coloriser
  368. ccze
  369. # Remove blank lines from a file using grep and save output to new file
  370. grep . filename > newfilename
  371. # Create a directory and change into it at the same time
  372. md () { mkdir -p "$@" && cd "$@"; }
  373. # Save your sessions in vim to resume later
  374. :mksession! <filename>
  375. # How to establish a remote Gnu screen session that you can re-connect to
  376. ssh -t user@some.domain.com /usr/bin/screen -xRR
  377. # Take screenshot through SSH
  378. DISPLAY=:0.0 import -window root /tmp/shot.png
  379. # Lists all listening ports together with the PID of the associated process
  380. lsof -Pan -i tcp -i udp
  381. # Opens vi/vim at pattern in file
  382. vi +/pattern [file]
  383. # Alias HEAD for automatic smart output
  384. alias head='head -n $((${LINES:-`tput lines 2>/dev/null||echo -n 12`} - 2))'
  385. # Create colorized html file from Vim or Vimdiff
  386. :TOhtml
  387. # Colorful man
  388. apt-get install most && update-alternatives --set pager /usr/bin/most
  389. # Pipe stdout and stderr, etc., to separate commands
  390. some_command > >(/bin/cmd_for_stdout) 2> >(/bin/cmd_for_stderr)
  391. # Go to parent directory of filename edited in last command
  392. cd !$:h
  393. # Diff on two variables
  394. diff <(echo "$a") <(echo "$b")
  395. # Draw a Sierpinski triangle
  396. perl -e 'print "P1\n256 256\n", map {$_&($_>>8)?1:0} (0..0xffff)' | display
  397. # Create a nifty overview of the hardware in your computer
  398. lshw -html > hardware.html
  399. # Manually Pause/Unpause Firefox Process with POSIX-Signals
  400. killall -STOP -m firefox
  401. # Gets a random Futurama quote from /.
  402. curl -Is slashdot.org | egrep '^X-(F|B|L)' | cut -d \- -f 2
  403. # Use lynx to run repeating website actions
  404. lynx -accept_all_cookies -cmd_script=/your/keystroke-file
  405. # Intercept, monitor and manipulate a TCP connection.
  406. mkfifo /tmp/fifo; cat /tmp/fifo | nc -l -p 1234 | tee -a to.log | nc machine port | tee -a from.log > /tmp/fifo
  407. # Display a list of committers sorted by the frequency of commits
  408. svn log -q|grep "|"|awk "{print \$3}"|sort|uniq -c|sort -nr
  409. # commandlinefu.com by David Winterbottom
  410. # Copy a MySQL Database to a new Server via SSH with one command
  411. mysqldump --add-drop-table --extended-insert --force --log-error=error.log -uUSER -pPASS OLD_DB_NAME | ssh -C user@newhost "mysql -uUSER -pPASS NEW_DB_NAME"
  412. # Fast, built-in pipe-based data sink
  413. <COMMAND> |:
  414. # Use file(1) to view device information
  415. file -s /dev/sd*
  416. # Find usb device
  417. diff <(lsusb) <(sleep 3s && lsusb)
  418. # Bind a key with a command
  419. bind -x '"\C-l":ls -l'
  420. # copy working directory and compress it on-the-fly while showing progress
  421. tar -cf - . | pv -s $(du -sb . | awk '{print $1}') | gzip > out.tgz
  422. # analyze traffic remotely over ssh w/ wireshark
  423. ssh root@server.com 'tshark -f "port !22" -w -' | wireshark -k -i -
  424. # View the newest xkcd comic.
  425. xkcd(){ wget -qO- http://xkcd.com/|tee >(feh $(grep -Po '(?<=")http://imgs[^/]+/comics/[^"]+\.\w{3}'))|grep -Po '(?<=(\w{3})" title=").*(?=" alt)';}
  426. # convert unixtime to human-readable
  427. date -d @1234567890
  428. # Schedule a script or command in x num hours, silently run in the background even if logged out
  429. ( ( sleep 2h; your-command your-args ) & )
  430. # recursive search and replace old with new string, inside files
  431. $ grep -rl oldstring . |xargs sed -i -e 's/oldstring/newstring/'
  432. # Find files that have been modified on your system in the past 60 minutes
  433. sudo find / -mmin 60 -type f
  434. # find geographical location of an ip address
  435. lynx -dump http://www.ip-adress.com/ip_tracer/?QRY=$1|grep address|egrep 'city|state|country'|awk '{print $3,$4,$5,$6,$7,$8}'|sed 's\ip address flag \\'|sed 's\My\\'
  436. # read manpage of a unix command as pdf in preview (Os X)
  437. man -t UNIX_COMMAND | open -f -a preview
  438. # Speed up launch of firefox
  439. find ~ -name '*.sqlite' -exec sqlite3 '{}' 'VACUUM;' \;
  440. # List the number and type of active network connections
  441. netstat -ant | awk '{print $NF}' | grep -v '[a-z]' | sort | uniq -c
  442. # format txt as table not joining empty columns
  443. column -tns: /etc/passwd
  444. # Bind a key with a command
  445. bind '"\C-l":"ls -l\n"'
  446. # prevent accidents while using wildcards
  447. rm *.txt <TAB> <TAB>
  448. # Random Number Between 1 And X
  449. echo $[RANDOM%X+1]
  450. # live ssh network throughput test
  451. yes | pv | ssh $host "cat > /dev/null"
  452. # Press Any Key to Continue
  453. read -sn 1 -p "Press any key to continue..."
  454. # backup all your commandlinefu.com favourites to a plaintext file
  455. clfavs(){ URL="http://www.commandlinefu.com";wget -O - --save-cookies c --post-data "username=$1&password=$2&submit=Let+me+in" $URL/users/signin;for i in `seq 0 25 $3`;do wget -O - --load-cookies c $URL/commands/favourites/plaintext/$i >>$4;done;rm -f c;}
  456. # send echo to socket network
  457. echo "foo" > /dev/tcp/192.168.1.2/25
  458. # Perform a branching conditional
  459. true && { echo success;} || { echo failed; }
  460. # commandlinefu.com by David Winterbottom
  461. # Resume scp of a big file
  462. rsync --partial --progress --rsh=ssh $file_source $user@$host:$destination_file
  463. # shut of the screen.
  464. xset dpms force standby
  465. # Create a single-use TCP (or UDP) proxy
  466. nc -l -p 2000 -c "nc example.org 3000"
  467. # runs a bash script in debugging mode
  468. bash -x ./post_to_commandlinefu.sh
  469. # Instead of writing a multiline if/then/else/fi construct you can do that by one line
  470. [[ test_condition ]] && if_true_do_this || otherwise_do_that
  471. # Use tee + process substitution to split STDOUT to multiple commands
  472. some_command | tee >(command1) >(command2) >(command3) ... | command4
  473. # Retry the previous command until it exits successfully
  474. until !!; do :; done
  475. # Shell recorder with replay
  476. script -t /tmp/mylog.out 2>/tmp/mylog.time; <do your work>; <CTRL-D>; scriptreplay /tmp/mylog.time /tmp/mylog.out
  477. # A child process which survives the parent's death (for sure)
  478. ( command & )
  479. # find all file larger than 500M
  480. find / -type f -size +500M
  481. # exclude a column with cut
  482. cut -f5 --complement
  483. # Recover a deleted file
  484. grep -a -B 25 -A 100 'some string in the file' /dev/sda1 > results.txt
  485. # April Fools' Day Prank
  486. PROMPT_COMMAND='if [ $RANDOM -le 3200 ]; then printf "\0337\033[%d;%dH\033[4%dm \033[m\0338" $((RANDOM%LINES+1)) $((RANDOM%COLUMNS+1)) $((RANDOM%8)); fi'
  487. # Listen to BBC Radio from the command line.
  488. bbcradio() { local s PS3="Select a station: ";select s in 1 1x 2 3 4 5 6 7 "Asian Network an" "Nations & Local lcl";do break;done;s=($s);mplayer -playlist "http://www.bbc.co.uk/radio/listen/live/r"${s[@]: -1}".asx";}
  489. # Monitor bandwidth by pid
  490. nethogs -p eth0
  491. # throttle bandwidth with cstream
  492. tar -cj /backup | cstream -t 777k | ssh host 'tar -xj -C /backup'
  493. # Run a long job and notify me when it's finished
  494. ./my-really-long-job.sh && notify-send "Job finished"
  495. # List all files opened by a particular command
  496. lsof -c dhcpd
  497. # Tell local Debian machine to install packages used by remote Debian machine
  498. ssh remotehost 'dpkg --get-selections' | dpkg --set-selections && dselect install
  499. # GRUB2: set Super Mario as startup tune
  500. echo "GRUB_INIT_TUNE=\"1000 334 1 334 1 0 1 334 1 0 1 261 1 334 1 0 1 392 2 0 4 196 2\"" | sudo tee -a /etc/default/grub > /dev/null && sudo update-grub
  501. # Diff remote webpages using wget
  502. diff <(wget -q -O - URL1) <(wget -q -O - URL2)
  503. # Close a hanging ssh session
  504. ~.
  505. # List alive hosts in specific subnet
  506. nmap -sP 192.168.1.0/24
  507. # intersection between two files
  508. grep -Fx -f file1 file2
  509. # processes per user counter
  510. ps hax -o user | sort | uniq -c
  511. # commandlinefu.com by David Winterbottom
  512. # Create an audio test CD of sine waves from 1 to 99 Hz
  513. (echo CD_DA; for f in {01..99}; do echo "$f Hz">&2; sox -nt cdda -r44100 -c2 $f.cdda synth 30 sine $f; echo TRACK AUDIO; echo FILE \"$f.cdda\" 0; done) > cdrdao.toc && cdrdao write cdrdao.toc && rm ??.cdda cdrdao.toc
  514. # Show current working directory of a process
  515. pwdx pid
  516. # Quickly (soft-)reboot skipping hardware checks
  517. /sbin/kexec -l /boot/$KERNEL --append="$KERNELPARAMTERS" --initrd=/boot/$INITRD; sync; /sbin/kexec -e
  518. # Nicely display permissions in octal format with filename
  519. stat -c '%A %a %n' *
  520. # Brute force discover
  521. sudo zcat /var/log/auth.log.*.gz | awk '/Failed password/&&!/for invalid user/{a[$9]++}/Failed password for invalid user/{a["*" $11]++}END{for (i in a) printf "%6s\t%s\n", a[i], i|"sort -n"}'
  522. # convert uppercase files to lowercase files
  523. rename 'y/A-Z/a-z/' *
  524. # Check if system is 32bit or 64bit
  525. getconf LONG_BIT
  526. # Limit the cpu usage of a process
  527. sudo cpulimit -p pid -l 50
  528. # Convert seconds into minutes and seconds
  529. bc <<< 'obase=60;299'
  530. # Repoint an existing symlink to a new location
  531. ln -nsf <TARGET> <LINK>
  532. # send a circular
  533. wall <<< "Broadcast This"
  534. # The BOFH Excuse Server
  535. telnet towel.blinkenlights.nl 666
  536. # List files with quotes around each filename
  537. ls -Q
  538. # See udev at work
  539. udevadm monitor
  540. # Get your outgoing IP address
  541. dig +short myip.opendns.com @resolver1.opendns.com
  542. # Makes you look busy
  543. alias busy='my_file=$(find /usr/include -type f | sort -R | head -n 1); my_len=$(wc -l $my_file | awk "{print $1}"); let "r = $RANDOM % $my_len" 2>/dev/null; vim +$r $my_file'
  544. # Insert the last argument of the previous command
  545. <ESC> .
  546. # Duplicate several drives concurrently
  547. dd if=/dev/sda | tee >(dd of=/dev/sdb) | dd of=/dev/sdc
  548. # Make sure a script is run in a terminal.
  549. [ -t 0 ] || exit 1
  550. # Get your external IP address
  551. curl ip.appspot.com
  552. # use vim to get colorful diff output
  553. svn diff | view -
  554. # A fun thing to do with ram is actually open it up and take a peek. This command will show you all the string (plain text) values in ram
  555. sudo strings /dev/mem
  556. # find files containing text
  557. grep -lir "some text" *
  558. # Quickly graph a list of numbers
  559. gnuplot -persist <(echo "plot '<(sort -n listOfNumbers.txt)' with lines")
  560. # Analyse an Apache access log for the most common IP addresses
  561. tail -10000 access_log | awk '{print $1}' | sort | uniq -c | sort -n | tail
  562. # commandlinefu.com by David Winterbottom
  563. # prevent large files from being cached in memory (backups!)
  564. nocache <I/O-heavy-command>
  565. # Print diagram of user/groups
  566. awk 'BEGIN{FS=":"; print "digraph{"}{split($4, a, ","); for (i in a) printf "\"%s\" [shape=box]\n\"%s\" -> \"%s\"\n", $1, a[i], $1}END{print "}"}' /etc/group|display
  567. # Create strong, but easy to remember password
  568. read -s pass; echo $pass | md5sum | base64 | cut -c -16
  569. # Generate an XKCD #936 style 4 word password
  570. shuf -n4 /usr/share/dict/words | tr -d '\n'
  571. # Python version 3: Serve current directory tree at http://$HOSTNAME:8000/
  572. python -m http.server
  573. # VI config to save files with +x when a shebang is found on line 1
  574. au BufWritePost * if getline(1) =~ "^#!" | if getline(1) =~ "/bin/" | silent !chmod +x <afile> | endif | endif
  575. # I finally found out how to use notify-send with at or cron
  576. echo "export DISPLAY=:0; export XAUTHORITY=~/.Xauthority; notify-send test" | at now+1minute
  577. # perl one-liner to get the current week number
  578. date +%V
  579. # Execute a command with a timeout
  580. timeout 10 sleep 11
  581. # Find files that were modified by a given command
  582. touch /tmp/file ; $EXECUTECOMMAND ; find /path -newer /tmp/file
  583. # Have an ssh session open forever
  584. autossh -M50000 -t server.example.com 'screen -raAd mysession'
  585. # pipe output of a command to your clipboard
  586. some command|xsel --clipboard
  587. # Recursively compare two directories and output their differences on a readable format
  588. diff -urp /originaldirectory /modifieddirectory
  589. # Make anything more awesome
  590. command | figlet
  591. # When feeling down, this command helps
  592. sl
  593. # List recorded formular fields of Firefox
  594. cd ~/.mozilla/firefox/ && sqlite3 `cat profiles.ini | grep Path | awk -F= '{print $2}'`/formhistory.sqlite "select * from moz_formhistory" && cd - > /dev/null
  595. # Base conversions with bc
  596. echo "obase=2; 27" | bc -l
  597. # ls -hog --> a more compact ls -l
  598. ls -hog
  599. # Start a command on only one CPU core
  600. taskset -c 0 your_command
  601. # Get all the keyboard shortcuts in screen
  602. ^A ?
  603. # Switch 2 characters on a command line.
  604. ctrl-t
  605. # Notepad in a browser (type this in the URL bar)
  606. data:text/html, <html contenteditable>
  607. # convert single digit to double digits
  608. for i in ?.ogg; do mv $i 0$i; done
  609. # Annotate tail -f with timestamps
  610. tail -f file | while read; do echo "$(date +%T.%N) $REPLY"; done
  611. # Stamp a text line on top of the pdf pages.
  612. echo "This text gets stamped on the top of the pdf pages." | enscript -B -f Courier-Bold16 -o- | ps2pdf - | pdftk input.pdf stamp - output output.pdf
  613. # commandlinefu.com by David Winterbottom
  614. # Create a file server, listening in port 7000
  615. while true; do nc -l 7000 | tar -xvf -; done
  616. # bypass any aliases and functions for the command
  617. \foo
  618. # cat a bunch of small files with file indication
  619. grep . *
  620. # Find broken symlinks
  621. find -L . -type l
  622. # stderr in color
  623. mycommand 2> >(while read line; do echo -e "\e[01;31m$line\e[0m"; done)
  624. # Stop Flash from tracking everything you do.
  625. for i in ~/.adobe ~/.macromedia ; do ( rm $i/ -rf ; ln -s /dev/null $i ) ; done
  626. # Save a file you edited in vim without the needed permissions (no echo)
  627. :w !sudo tee > /dev/null %
  628. # Delete all empty lines from a file with vim
  629. :g/^$/d
  630. # Remote screenshot
  631. DISPLAY=":0.0" import -window root screenshot.png
  632. # your terminal sings
  633. echo {1..199}" bottles of beer on the wall, cold bottle of beer, take one down, pass it around, one less bottle of beer on the wall,, " | espeak -v english -s 140
  634. # Define words and phrases with google.
  635. define(){ local y="$@";curl -sA"Opera" "http://www.google.com/search?q=define:${y// /+}"|grep -Po '(?<=<li>)[^<]+'|nl|perl -MHTML::Entities -pe 'decode_entities($_)' 2>/dev/null;}
  636. # Harder, Faster, Stronger SSH clients
  637. ssh -4 -C -c blowfish-cbc
  638. # Clean up poorly named TV shows.
  639. rename -v 's/.*[s,S](\d{2}).*[e,E](\d{2}).*\.avi/SHOWNAME\ S$1E$2.avi/' poorly.named.file.s01e01.avi
  640. # Backup all MySQL Databases to individual files
  641. for db in $(mysql -e 'show databases' -s --skip-column-names); do mysqldump $db | gzip > "/backups/mysqldump-$(hostname)-$db-$(date +%Y-%m-%d-%H.%M.%S).gz"; done
  642. # check open ports
  643. lsof -Pni4 | grep LISTEN
  644. # Triple monitoring in screen
  645. tmpfile=$(mktemp) && echo -e 'startup_message off\nscreen -t top htop\nsplit\nfocus\nscreen -t nethogs nethogs wlan0\nsplit\nfocus\nscreen -t iotop iotop' > $tmpfile && sudo screen -c $tmpfile
  646. # Save an HTML page, and covert it to a .pdf file
  647. wget $URL | htmldoc --webpage -f "$URL".pdf - ; xpdf "$URL".pdf &
  648. # DELETE all those duplicate files but one based on md5 hash comparision in the current directory tree
  649. find . -type f -print0|xargs -0 md5sum|sort|perl -ne 'chomp;$ph=$h;($h,$f)=split(/\s+/,$_,2);print "$f"."\x00" if ($h eq $ph)'|xargs -0 rm -v --
  650. # create an emergency swapfile when the existing swap space is getting tight
  651. sudo dd if=/dev/zero of=/swapfile bs=1024 count=1024000;sudo mkswap /swapfile; sudo swapon /swapfile
  652. # Relocate a file or directory, but keep it accessible on the old location throug a simlink.
  653. mv $1 $2 && ln -s $2/$(basename $1) $(dirname $1)
  654. # a short counter
  655. yes '' | cat -n
  656. # Rsync remote data as root using sudo
  657. rsync --rsync-path 'sudo rsync' username@source:/folder/ /local/
  658. # Transfer SSH public key to another machine in one step
  659. ssh-keygen; ssh-copy-id user@host; ssh user@host
  660. # git remove files which have been deleted
  661. git rm $(git ls-files --deleted)
  662. # Copy stdin to your X11 buffer
  663. ssh user@host cat /path/to/some/file | xclip
  664. # commandlinefu.com by David Winterbottom
  665. # Get info about remote host ports and OS detection
  666. nmap -sS -P0 -sV -O <target>
  667. # List of commands you use most often
  668. history | awk '{print $2}' | sort | uniq -c | sort -rn | head
  669. # Copy a file structure without files
  670. find * -type d -exec mkdir /where/you/wantem/\{\} \;
  671. # Get list of servers with a specific port open
  672. nmap -sT -p 80 -oG - 192.168.1.* | grep open
  673. # Display current time in requested time zones.
  674. zdump Japan America/New_York
  675. # dmesg with colored human-readable dates
  676. dmesg -T|sed -e 's|\(^.*'`date +%Y`']\)\(.*\)|\x1b[0;34m\1\x1b[0m - \2|g'
  677. # Extract audio from Flash video (*.flv) as mp3 file
  678. ffmpeg -i video.flv -vn -ar 44100 -ac 2 -ab 192k -f mp3 audio.mp3
  679. # Share your terminal session real-time
  680. mkfifo foo; script -f foo
  681. # Short and elegant way to backup a single file before you change it.
  682. cp httpd.conf{,.bk}
  683. # clean up memory of unnecessary things (Kernerl 2.6.16 or newer)
  684. sync && echo 1 > /proc/sys/vm/drop_caches
  685. # Find all the links to a file
  686. find -L / -samefile /path/to/file -exec ls -ld {} +
  687. # Recover tmp flash videos (deleted immediately by the browser plugin)
  688. for h in `find /proc/*/fd -ilname "/tmp/Flash*" 2>/dev/null`; do ln -s "$h" `readlink "$h" | cut -d' ' -f1`; done
  689. # Single use vnc-over-ssh connection
  690. ssh -f -L 5900:localhost:5900 your.ssh.server "x11vnc -safer -localhost -nopw -once -display :0"; vinagre localhost:5900
  691. # send a circular
  692. echo "dear admin, please ban eribsskog" | wall
  693. # Visit wikileaks.com
  694. echo 213.251.145.96 wikileaks.com >>/etc/hosts
  695. # List all open ports and their owning executables
  696. lsof -i -P | grep -i "listen"
  697. # Create a single PDF from multiple images with ImageMagick
  698. convert *.jpg output.pdf
  699. # dd with progress bar and statistics
  700. sudo dd if=/dev/sdc bs=4096 | pv -s 2G | sudo dd bs=4096 of=~/USB_BLACK_BACKUP.IMG
  701. # View all date formats, Quick Reference Help Alias
  702. alias dateh='date --help|sed -n "/^ *%%/,/^ *%Z/p"|while read l;do F=${l/% */}; date +%$F:"|'"'"'${F//%n/ }'"'"'|${l#* }";done|sed "s/\ *|\ */|/g" |column -s "|" -t'
  703. # Monitor open connections for httpd including listen, count and sort it per IP
  704. watch "netstat -plan|grep :80|awk {'print \$5'} | cut -d: -f 1 | sort | uniq -c | sort -nk 1"
  705. # output your microphone to a remote computer's speaker
  706. arecord -f dat | ssh -C user@host aplay -f dat
  707. # download and unpack tarball without leaving it sitting on your hard drive
  708. wget -qO - http://example.com/path/to/blah.tar.gz | tar xzf -
  709. # Colored diff ( via vim ) on 2 remotes files on your local computer.
  710. vimdiff scp://root@server-foo.com//etc/snmp/snmpd.conf scp://root@server-bar.com//etc/snmp/snmpd.conf
  711. # Split a tarball into multiple parts
  712. tar cf - <dir>|split -b<max_size>M - <name>.tar.
  713. # Matrix Style
  714. echo -e "\e[32m"; while :; do for i in {1..16}; do r="$(($RANDOM % 2))"; if [[ $(($RANDOM % 5)) == 1 ]]; then if [[ $(($RANDOM % 4)) == 1 ]]; then v+="\e[1m $r "; else v+="\e[2m $r "; fi; else v+=" "; fi; done; echo -e "$v"; v=""; done
  715. # commandlinefu.com by David Winterbottom
  716. # Pretty Print a simple csv in the command line
  717. column -s, -t <tmp.csv
  718. # Ultimate current directory usage command
  719. ncdu
  720. # Cleanup firefox's database.
  721. find ~/.mozilla/firefox/ -type f -name "*.sqlite" -exec sqlite3 {} VACUUM \;
  722. # Terminal - Show directories in the PATH, one per line with sed and bash3.X `here string'
  723. tr : '\n' <<<$PATH
  724. # Find Duplicate Files (based on MD5 hash)
  725. find -type f -exec md5sum '{}' ';' | sort | uniq --all-repeated=separate -w 33 | cut -c 35-
  726. # Print a great grey scale demo !
  727. yes "$(seq 232 255;seq 254 -1 233)" | while read i; do printf "\x1b[48;5;${i}m\n"; sleep .01; done
  728. # Smiley Face Bash Prompt
  729. PS1="\`if [ \$? = 0 ]; then echo \e[33\;40m\\\^\\\_\\\^\e[0m; else echo \e[36\;40m\\\-\e[0m\\\_\e[36\;40m\\\-\e[0m; fi\` \u \w:\h)"
  730. # Create a system overview dashboard on F12 key
  731. bind '"\e[24~"':"\"ps -elF;df -h;free -mt;netstat -lnpt;who -a\C-m"""
  732. # coloured tail
  733. tail -f FILE | perl -pe 's/KEYWORD/\e[1;31;43m$&\e[0m/g'
  734. # Search for commands from the command line
  735. clfu-seach <search words>
  736. # Install a Firefox add-on/theme to all users
  737. sudo firefox -install-global-extension /path/to/add-on
  738. # clear current line
  739. CTRL+u
  740. # Convert all MySQL tables and fields to UTF8
  741. mysql --database=dbname -B -N -e "SHOW TABLES" | awk '{print "ALTER TABLE", $1, "CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;"}' | mysql --database=dbname &
  742. # Put readline into vi mode
  743. set -o vi
  744. # Delete all empty lines from a file with vim
  745. :g!/\S/d
  746. # Backup a remote database to your local filesystem
  747. ssh user@host 'mysqldump dbname | gzip' > /path/to/backups/db-backup-`date +%Y-%m-%d`.sql.gz
  748. # Recursively remove .svn directories from the current location
  749. find . -type d -name '.svn' -print0 | xargs -0 rm -rdf
  750. # Start a new command in a new screen window
  751. alias s='screen -X screen'; s top; s vi; s man ls;
  752. # Efficiently print a line deep in a huge log file
  753. sed '1000000!d;q' < massive-log-file.log
  754. # Encrypted archive with openssl and tar
  755. tar --create --file - --posix --gzip -- <dir> | openssl enc -e -aes256 -out <file>
  756. # Remove a range of lines from a file
  757. sed -i <file> -re '<start>,<end>d'
  758. # Hide the name of a process listed in the `ps` output
  759. exec -a "/sbin/getty 38400 tty7" your_cmd -erase_all_files
  760. # Create a local compressed tarball from remote host directory
  761. ssh user@host "tar -zcf - /path/to/dir" > dir.tar.gz
  762. # convert from hexidecimal or octal to decimal
  763. echo $((0x1fe)) $((033))
  764. # save man-page as pdf
  765. man -t awk | ps2pdf - awk.pdf
  766. # commandlinefu.com by David Winterbottom
  767. # Convert seconds into minutes and seconds
  768. echo 'obase=60;299' | bc
  769. # List by size all of the directories in a given tree.
  770. du -h /path | sort -h
  771. # All IP connected to my host
  772. netstat -lantp | grep ESTABLISHED |awk '{print $5}' | awk -F: '{print $1}' | sort -u
  773. # df without line wrap on long FS name
  774. df -P | column -t
  775. # rsync instead of scp
  776. rsync --progress --partial --rsh="ssh -p 8322" --bwlimit=100 --ipv4 user@domain.com:~/file.tgz .
  777. # Download a file and uncompress it while it downloads
  778. wget http://URL/FILE.tar.gz -O - | tar xfz -
  779. # Reuse last parameter
  780. !$
  781. # Make sudo forget password instantly
  782. sudo -K
  783. # View user activity per directory.
  784. sudo lsof -u someuser -a +D /etc
  785. # Print a row of characters across the terminal
  786. printf "%`tput cols`s"|tr ' ' '#'
  787. # Limit bandwidth usage by apt-get
  788. sudo apt-get -o Acquire::http::Dl-Limit=30 upgrade
  789. # Convert text to picture
  790. echo -e "Some Text Line1\nSome Text Line 2" | convert -background none -density 196 -resample 72 -unsharp 0x.5 -font "Courier" text:- -trim +repage -bordercolor white -border 3 text.gif
  791. # track flights from the command line
  792. flight_status() { if [[ $# -eq 3 ]];then offset=$3; else offset=0; fi; curl "http://mobile.flightview.com/TrackByRoute.aspx?view=detail&al="$1"&fn="$2"&dpdat=$(date +%Y%m%d -d ${offset}day)" 2>/dev/null |html2text |grep ":"; }
  793. # Tune your guitar from the command line.
  794. for n in E2 A2 D3 G3 B3 E4;do play -n synth 4 pluck $n repeat 2;done
  795. # Remove executable bit from all files in the current directory recursively, excluding other directories
  796. chmod -R -x+X *
  797. # convert filenames in current directory to lowercase
  798. rename 'y/A-Z/a-z/' *
  799. # More precise BASH debugging
  800. env PS4=' ${BASH_SOURCE}:${LINENO}(${FUNCNAME[0]}) ' sh -x /etc/profile
  801. # Remove color codes (special characters) with sed
  802. sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
  803. # redirect stdout and stderr each to separate files and print both to the screen
  804. (some_command 2>&1 1>&3 | tee /path/to/errorlog ) 3>&1 1>&2 | tee /path/to/stdoutlog
  805. # get all pdf and zips from a website using wget
  806. wget --reject html,htm --accept pdf,zip -rl1 url
  807. # bash: hotkey to put current commandline to text-editor
  808. bash-hotkey: <CTRL+x+e>
  809. # Use all the cores or CPUs when compiling
  810. make -j 4
  811. # Prints total line count contribution per user for an SVN repository
  812. svn ls -R | egrep -v -e "\/$" | xargs svn blame | awk '{print $2}' | sort | uniq -c | sort -r
  813. # Analyze awk fields
  814. awk '{print NR": "$0; for(i=1;i<=NF;++i)print "\t"i": "$i}'
  815. # Create a list of binary numbers
  816. echo {0..1}{0..1}{0..1}{0..1}
  817. # commandlinefu.com by David Winterbottom
  818. # Run a command, store the output in a pastebin on the internet and place the URL on the xclipboard
  819. ls | curl -F 'sprunge=<-' http://sprunge.us | xclip
  820. # Get your public ip using dyndns
  821. curl -s http://checkip.dyndns.org/ | grep -o "[[:digit:].]\+"
  822. # Start screen in detached mode
  823. screen -d -m [<command>]
  824. # How to run X without any graphics hardware
  825. startx -- `which Xvfb` :1 -screen 0 800x600x24 && DISPLAY=:1 x11vnc
  826. # Cut out a piece of film from a file. Choose an arbitrary length and starting time.
  827. ffmpeg -vcodec copy -acodec copy -i orginalfile -ss 00:01:30 -t 0:0:20 newfile
  828. # iso-8859-1 to utf-8 safe recursive rename
  829. detox -r -s utf_8 /path/to/old/win/files/dir
  830. # List the files any process is using
  831. lsof +p xxxx
  832. # Pipe STDOUT to vim
  833. tail -1000 /some/file | vim -
  834. # Show biggest files/directories, biggest first with 'k,m,g' eyecandy
  835. du --max-depth=1 | sort -r -n | awk '{split("k m g",v); s=1; while($1>1024){$1/=1024; s++} print int($1)" "v[s]"\t"$2}'
  836. # Terminate a frozen SSH-session
  837. RETURN~.
  838. # change directory to actual path instead of symlink path
  839. cd `pwd -P`
  840. # Download an entire static website to your local machine
  841. wget --recursive --page-requisites --convert-links www.moyagraphix.co.za
  842. # Batch convert files to utf-8
  843. find . -name "*.php" -exec iconv -f ISO-8859-1 -t UTF-8 {} -o ../newdir_utf8/{} \;
  844. # Get http headers for an url
  845. curl -I www.commandlinefu.com
  846. # vi a remote file
  847. vi scp://username@host//path/to/somefile
  848. # Show permissions of current directory and all directories upwards to /
  849. namei -m $(pwd)
  850. # Use top to monitor only all processes with the same name fragment 'foo'
  851. top -p $(pgrep -d , foo)
  852. # delete command line last word
  853. ctrl+w
  854. # move you up one directory quickly
  855. shopt -s autocd
  856. # Remove a line from a file using sed (useful for updating known SSH server keys when they change)
  857. ssh-keygen -R <thehost>
  858. # Show what PID is listening on port 80 on Linux
  859. fuser -v 80/tcp
  860. # Draw kernel module dependancy graph.
  861. lsmod | awk 'BEGIN{print "digraph{"}{split($4, a, ","); for (i in a) print $1, "->", a[i]}END{print "}"}'|display
  862. # Color man pages
  863. echo "export LESS_TERMCAP_mb=$'\E[01;31m'" >> ~/.bashrc
  864. # List files accessed by a command
  865. strace -ff -e trace=file my_command 2>&1 | perl -ne 's/^[^"]+"(([^\\"]|\\[\\"nt])*)".*/$1/ && print'
  866. # Protect directory from an overzealous rm -rf *
  867. cd <directory>; touch ./-i
  868. # commandlinefu.com by David Winterbottom
  869. # Watch RX/TX rate of an interface in kb/s
  870. while [ /bin/true ]; do OLD=$NEW; NEW=`cat /proc/net/dev | grep eth0 | tr -s ' ' | cut -d' ' -f "3 11"`; echo $NEW $OLD | awk '{printf("\rin: % 9.2g\t\tout: % 9.2g", ($1-$3)/1024, ($2-$4)/1024)}'; sleep 1; done
  871. # Figure out what shell you're running
  872. echo $0
  873. # Blink LED Port of NIC Card
  874. ethtool -p eth0
  875. # See where a shortened url takes you before click
  876. check(){ curl -sI $1 | sed -n 's/Location: *//p';}
  877. # Running scripts after a reboot for non-root users .
  878. @reboot <yourscript.sh>
  879. # run command on a group of nodes in parallel
  880. echo "uptime" | tee >(ssh host1) >(ssh host2) >(ssh host3)
  881. # run command on a group of nodes in parallel
  882. echo "uptime" | pee "ssh host1" "ssh host2" "ssh host3"
  883. # Remove Thumbs.db files from folders
  884. find ./ -name Thumbs.db -delete
  885. # Display BIOS Information
  886. # dd if=/dev/mem bs=1k skip=768 count=256 2>/dev/null | strings -n 8
  887. # open path with your default program (on Linux/*BSD)
  888. xdg-open [path]
  889. # Copy an element from the previous command
  890. !:1-3
  891. # use the previous commands params in the current command
  892. !!:[position]
  893. # Mirror a directory structure from websites with an Apache-generated file indexes
  894. lftp -e "mirror -c" http://example.com/foobar/
  895. # Choose from a nice graphical menu which DI.FM radio station to play
  896. zenity --list --width 500 --height 500 --column 'radio' --column 'url' --print-column 2 $(curl -s http://www.di.fm/ | awk -F '"' '/href="http:.*\.pls.*96k/ {print $2}' | sort | awk -F '/|\.' '{print $(NF-1) " " $0}') | xargs mplayer
  897. # Quickly share code or text from vim to others.
  898. :w !curl -F "sprunge=<-" http://sprunge.us | xclip
  899. # copy from host1 to host2, through your host
  900. ssh root@host1 "cd /somedir/tocopy/ && tar -cf - ." | ssh root@host2 "cd /samedir/tocopyto/ && tar -xf -"
  901. # Change prompt to MS-DOS one (joke)
  902. export PS1="C:\$( pwd | sed 's:/:\\\\\\:g' )\\> "
  903. # View network activity of any application or user in realtime
  904. lsof -r 2 -p PID -i -a
  905. # Print a list of standard error codes and descriptions.
  906. perl -le 'print $!+0, "\t", $!++ for 0..127'
  907. # Unbelievable Shell Colors, Shading, Backgrounds, Effects for Non-X
  908. for c in `seq 0 255`;do t=5;[[ $c -lt 108 ]]&&t=0;for i in `seq $t 5`;do echo -e "\e[0;48;$i;${c}m|| $i:$c `seq -s+0 $(($COLUMNS/2))|tr -d '[0-9]'`\e[0m";done;done
  909. # grep -v with multiple patterns.
  910. grep 'test' somefile | grep -vE '(error|critical|warning)'
  911. # Identify long lines in a file
  912. awk 'length>72' file
  913. # Show directories in the PATH, one per line
  914. echo "${PATH//:/$'\n'}"
  915. # Mount the first NTFS partition inside a VDI file (VirtualBox Disk Image)
  916. mount -t ntfs-3g -o ro,loop,uid=user,gid=group,umask=0007,fmask=0117,offset=0x$(hd -n 1000000 image.vdi | grep "eb 52 90 4e 54 46 53" | cut -c 1-8) image.vdi /mnt/vdi-ntfs
  917. # Show me a histogram of the busiest minutes in a log file:
  918. cat /var/log/secure.log | awk '{print substr($0,0,12)}' | uniq -c | sort -nr | awk '{printf("\n%s ",$0) ; for (i = 0; i<$1 ; i++) {printf("*")};}'
  919. # commandlinefu.com by David Winterbottom
  920. # Display current bandwidth statistics
  921. ifstat -nt
  922. # Given process ID print its environment variables
  923. sed 's/\o0/\n/g' /proc/INSERT_PID_HERE/environ
  924. # restoring some data from a corrupted text file
  925. ( cat badfile.log ; tac badfile.log | tac ) > goodfile.log
  926. # view the system console remotely
  927. sudo cat /dev/vcs1 | fold -w 80
  928. # Fix Ubuntu's Broken Sound Server
  929. sudo killall -9 pulseaudio; pulseaudio >/dev/null 2>&1 &
  930. # Look up the definition of a word
  931. curl dict://dict.org/d:something
  932. # Diff files on two remote hosts.
  933. diff <(ssh alice cat /etc/apt/sources.list) <(ssh bob cat /etc/apt/sources.list)
  934. # Ctrl+S Ctrl+Q terminal output lock and unlock
  935. Ctrl+S Ctrl+Q
  936. # Redirect STDIN
  937. < /path/to/file.txt grep foo
  938. # I hate `echo X | Y`
  939. base64 -d <<< aHR0cDovL3d3dy50d2l0dGVyc2hlZXAuY29tL3Jlc3VsdHMucGhwP3U9Y29tbWFuZGxpbmVmdQo=
  940. # Schedule a download at a later time
  941. echo 'wget url' | at 01:00
  942. # Send keypresses to an X application
  943. xvkbd -xsendevent -text "Hello world"
  944. # Add calendar to desktop wallpaper
  945. convert -font -misc-fixed-*-*-*-*-*-*-*-*-*-*-*-* -fill black -draw "text 270,260 \" `cal` \"" testpic.jpg newtestpic.jpg
  946. # Browse system RAM in a human readable form
  947. sudo cat /proc/kcore | strings | awk 'length > 20' | less
  948. # Get Cisco network information
  949. tcpdump -nn -v -i eth0 -s 1500 -c 1 'ether[20:2] == 0x2000'
  950. # Quick glance at who's been using your system recently
  951. last | grep -v "^$" | awk '{ print $1 }' | sort -nr | uniq -c
  952. # Generat a Random MAC address
  953. MAC=`(date; cat /proc/interrupts) | md5sum | sed -r 's/^(.{10}).*$/\1/; s/([0-9a-f]{2})/\1:/g; s/:$//;'`
  954. # ping a range of IP addresses
  955. nmap -sP 192.168.1.100-254
  956. # Check if system is 32bit or 64bit
  957. arch
  958. # Find the most recently changed files (recursively)
  959. find . -type f -printf '%TY-%Tm-%Td %TT %p\n' | sort
  960. # Ask for a password, the passwd-style
  961. read -s -p"Password: " USER_PASSWORD_VARIABLE; echo
  962. # monitor memory usage
  963. watch vmstat -sSM
  964. # Content search.
  965. ff() { local IFS='|'; grep -rinE "$*" . ; }
  966. # List 10 largest directories in current directory
  967. du -hs */ | sort -hr | head
  968. # Stream YouTube URL directly to MPlayer
  969. yt () mplayer -fs -quiet $(youtube-dl -g "$1")
  970. # commandlinefu.com by David Winterbottom
  971. # List open files that have no links to them on the filesystem
  972. lsof +L1
  973. # List of commands you use most often
  974. history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head > /tmp/cmds | gnuplot -persist <(echo 'plot "/tmp/cmds" using 1:xticlabels(2) with boxes')
  975. # find all active IP addresses in a network
  976. nmap -sP 192.168.1.0/24; arp -n | grep "192.168.1.[0-9]* *ether"
  977. # Convert all Flac in a directory to Mp3 using maximum quality variable bitrate
  978. for file in *.flac; do flac -cd "$file" | lame -q 0 --vbr-new -V 0 - "${file%.flac}.mp3"; done
  979. # Convert a Nero Image File to ISO
  980. dd bs=1k if=image.nrg of=image.iso skip=300
  981. # Copy with progress
  982. rsync --progress file1 file2
  983. # a shell function to print a ruler the width of the terminal window.
  984. ruler() { for s in '....^....|' '1234567890'; do w=${#s}; str=$( for (( i=1; $i<=$(( ($COLUMNS + $w) / $w )) ; i=$i+1 )); do echo -n $s; done ); str=$(echo $str | cut -c -$COLUMNS) ; echo $str; done; }
  985. # quickest (i blv) way to get the current program name minus the path (BASH)
  986. path_stripped_programname="${0##*/}"
  987. # A function to output a man page as a pdf file
  988. function man2pdf(){ man -t ${1:?Specify man as arg} | ps2pdf -dCompatibility=1.3 - - > ${1}.pdf; }
  989. # a trash function for bash
  990. trash <file>
  991. # Play music from youtube without download
  992. wget -q -O - `youtube-dl -b -g $url`| ffmpeg -i - -f mp3 -vn -acodec libmp3lame -| mpg123 -
  993. # generate a unique and secure password for every website that you login to
  994. sitepass() { echo -n "$@" | md5sum | sha1sum | sha224sum | sha256sum | sha384sum | sha512sum | gzip - | strings -n 1 | tr -d "[:space:]" | tr -s '[:print:]' | tr '!-~' 'P-~!-O' | rev | cut -b 2-11; history -d $(($HISTCMD-1)); }
  995. # Replace spaces in filenames with underscores
  996. rename -v 's/ /_/g' *
  997. # find and delete empty dirs, start in current working dir
  998. find . -empty -type d -exec rmdir {} +
  999. # Move all images in a directory into a directory hierarchy based on year, month and day based on exif information
  1000. exiftool '-Directory<DateTimeOriginal' -d %Y/%m/%d dir
  1001. # Follow tail by name (fix for rolling logs with tail -f)
  1002. tail -F file
  1003. # Colored SVN diff
  1004. svn diff <file> | vim -R -
  1005. # Find broken symlinks and delete them
  1006. find -L /path/to/check -type l -delete
  1007. # Find if the command has an alias
  1008. type -all command
  1009. # Show a config file without comments
  1010. egrep -v "^$|^[[:space:]]*#" /etc/some/file
  1011. # Download all Delicious bookmarks
  1012. curl -u username -o bookmarks.xml https://api.del.icio.us/v1/posts/all
  1013. # beep when a server goes offline
  1014. while true; do [ "$(ping -c1W1w1 server-or-ip.com | awk '/received/ {print $4}')" != 1 ] && beep; sleep 1; done
  1015. # Number of open connections per ip.
  1016. netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
  1017. # Create a favicon
  1018. convert -colors 256 -resize 16x16 face.jpg face.ppm && ppmtowinicon -output favicon.ico face.ppm
  1019. # Quickly generate an MD5 hash for a text string using OpenSSL
  1020. echo -n 'text to be encrypted' | openssl md5
  1021. # commandlinefu.com by David Winterbottom
  1022. # create dir tree
  1023. mkdir -p doc/{text/,img/{wallpaper/,photos/}}
  1024. # Check Ram Speed and Type in Linux
  1025. sudo dmidecode --type 17 | more
  1026. # Run any GUI program remotely
  1027. ssh -fX <user>@<host> <program>
  1028. # Run the Firefox Profile Manager
  1029. firefox -no-remote -P
  1030. # Delete the specified line
  1031. sed -i 8d ~/.ssh/known_hosts
  1032. # Backup your hard drive with dd
  1033. sudo dd if=/dev/sda of=/media/disk/backup/sda.backup
  1034. # Add forgotten changes to the last git commit
  1035. git commit --amend
  1036. # Sort dotted quads
  1037. sort -nt . -k 1,1 -k 2,2 -k 3,3 -k 4,4
  1038. # Press ctrl+r in a bash shell and type a few letters of a previous command
  1039. ^r in bash begins a reverse-search-history with command completion
  1040. # Extract audio from a video
  1041. ffmpeg -i video.avi -f mp3 audio.mp3
  1042. # Get Dell Service Tag Number from a Dell Machine
  1043. sudo dmidecode | grep Serial\ Number | head -n1
  1044. # Resume aborted scp file transfers
  1045. rsync --partial --progress --rsh=ssh SOURCE DESTINATION
  1046. # Use last argument of last command
  1047. file !$
  1048. # Commandline document conversion with Libreoffice
  1049. soffice --headless -convert-to odt:"writer8" somefile.docx
  1050. # HourGlass
  1051. hourglass(){ trap 'tput cnorm' 0 1 2 15 RETURN;local s=$(($SECONDS +$1));(tput civis;while (($SECONDS<$s));do for f in '|' '\' '-' '/';do echo -n "$f";sleep .2s;echo -n $'\b';done;done;);}
  1052. # Transfer a file to multiple hosts over ssh
  1053. pscp -h hosts.txt -l username /etc/hosts /tmp/hosts
  1054. # Verify/edit bash history command before executing it
  1055. shopt -s histverify
  1056. # Resize an image to at least a specific resolution
  1057. convert -resize '1024x600^' image.jpg small-image.jpg
  1058. # Print without executing the last command that starts with...
  1059. !ssh:p
  1060. # Query well known ports list
  1061. getent services <<service>>
  1062. # Create .pdf from .doc
  1063. oowriter -pt pdf your_word_file.doc
  1064. # Diff XML files
  1065. diffxml() { diff -wb <(xmllint --format "$1") <(xmllint --format "$2"); }
  1066. # Gets the english pronunciation of a phrase
  1067. say() { mplayer "http://translate.google.com/translate_tts?q=$1"; }
  1068. # What is the use of this switch ?
  1069. manswitch () { man $1 | less -p "^ +$2"; }
  1070. # Save the list of all available commands in your box to a file
  1071. compgen -c | sort -u > commands
  1072. # commandlinefu.com by David Winterbottom
  1073. # clear screen, keep prompt at eye-level (faster than clear(1), tput cl, etc.)
  1074. cls(){ printf "\33[2J";} or, if no printf, cat >cls;<ctrl-v><ctrl+[>[2J<enter><ctrl+d> cls(){ cat cls;}
  1075. # Down for everyone or just me?
  1076. down4me() { wget -qO - "http://www.downforeveryoneorjustme.com/$1" | sed '/just you/!d;s/<[^>]*>//g' ; }
  1077. # Run a program transparently, but print a stack trace if it fails
  1078. gdb -batch -ex "run" -ex "bt" ${my_program} 2>&1 | grep -v ^"No stack."$
  1079. # Compare copies of a file with md5
  1080. cmp file1 file2
  1081. # backup delicious bookmarks
  1082. curl --user login:password -o DeliciousBookmarks.xml -O 'https://api.del.icio.us/v1/posts/all'
  1083. # Rename all .jpeg and .JPG files to have .jpg extension
  1084. rename 's/\.jpe?g$/.jpg/i' *
  1085. # pretend to be busy in office to enjoy a cup of coffee
  1086. j=0;while true; do let j=$j+1; for i in $(seq 0 20 100); do echo $i;sleep 1; done | dialog --gauge "Install part $j : `sed $(perl -e "print int rand(99999)")"q;d" /usr/share/dict/words`" 6 40;done
  1087. # Generate a Random MAC address
  1088. openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'
  1089. # Purge configuration files of removed packages on debian based systems
  1090. aptitude purge '~c'
  1091. # Get all links of a website
  1092. lynx -dump http://www.domain.com | awk '/http/{print $2}'
  1093. # Find all active ip's in a subnet
  1094. sudo arp-scan -I eth0 192.168.1.0/24
  1095. # Display BIOS Information
  1096. dmidecode -t bios
  1097. # Disassemble some shell code
  1098. echo -ne "<shellcode>" | x86dis -e 0 -s intel
  1099. # make, or run a script, everytime a file in a directory is modified
  1100. while true; do inotifywait -r -e MODIFY dir/ && make; done;
  1101. # kill process by name
  1102. pkill -x firefox
  1103. # ignore the .svn directory in filename completion
  1104. export FIGNORE=.svn
  1105. # Ping scanning without nmap
  1106. for i in {1..254}; do ping -c 1 -W 1 10.1.1.$i | grep 'from'; done
  1107. # Working random fact generator
  1108. wget randomfunfacts.com -O - 2>/dev/null | grep \<strong\> | sed "s;^.*<i>\(.*\)</i>.*$;\1;"
  1109. # Remote backups with tar over ssh
  1110. tar jcpf - [sourceDirs] |ssh user@host "cat > /path/to/backup/backupfile.tar.bz2"
  1111. # Pronounce an English word using Dictionary.com
  1112. pronounce(){ wget -qO- $(wget -qO- "http://dictionary.reference.com/browse/$@" | grep 'soundUrl' | head -n 1 | sed 's|.*soundUrl=\([^&]*\)&.*|\1|' | sed 's/%3A/:/g;s/%2F/\//g') | mpg123 -; }
  1113. # Make ISO image of a folder
  1114. mkisofs -J -allow-lowercase -R -V "OpenCD8806" -iso-level 4 -o OpenCD.iso ~/OpenCD
  1115. # Change pidgin status
  1116. purple-remote "setstatus?status=away&message=AFK"
  1117. # Grep by paragraph instead of by line.
  1118. grepp() { [ $# -eq 1 ] && perl -00ne "print if /$1/i" || perl -00ne "print if /$1/i" < "$2";}
  1119. # Vim: Switch from Horizontal split to Vertical split
  1120. ^W-L
  1121. # Numbers guessing game
  1122. A=1;B=100;X=0;C=0;N=$[$RANDOM%$B+1];until [ $X -eq $N ];do read -p "N between $A and $B. Guess? " X;C=$(($C+1));A=$(($X<$N?$X:$A));B=$(($X>$N?$X:$B));done;echo "Took you $C tries, Einstein";
  1123. # commandlinefu.com by David Winterbottom
  1124. # generate random password
  1125. pwgen -Bs 10 1
  1126. # Change user, assume environment, stay in current dir
  1127. su -- user
  1128. # Testing hard disk reading speed
  1129. hdparm -t /dev/sda
  1130. # Identify differences between directories (possibly on different servers)
  1131. diff <(ssh server01 'cd config; find . -type f -exec md5sum {} \;| sort -k 2') <(ssh server02 'cd config;find . -type f -exec md5sum {} \;| sort -k 2')
  1132. # How fast is the connexion to a URL, some stats from curl
  1133. URL="http://www.google.com";curl -L --w "$URL\nDNS %{time_namelookup}s conn %{time_connect}s time %{time_total}s\nSpeed %{speed_download}bps Size %{size_download}bytes\n" -o/dev/null -s $URL
  1134. # move a lot of files over ssh
  1135. rsync -az /home/user/test user@sshServer:/tmp/
  1136. # List programs with open ports and connections
  1137. lsof -i
  1138. # Show git branches by date - useful for showing active branches
  1139. for k in `git branch|perl -pe s/^..//`;do echo -e `git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k|head -n 1`\\t$k;done|sort -r
  1140. # disable history for current shell session
  1141. unset HISTFILE
  1142. # Share a 'screen'-session
  1143. screen -x
  1144. # Show all detected mountable Drives/Partitions/BlockDevices
  1145. hwinfo --block --short
  1146. # Monitor TCP opened connections
  1147. watch -n 1 "netstat -tpanl | grep ESTABLISHED"
  1148. # Fibonacci numbers with awk
  1149. seq 50| awk 'BEGIN {a=1; b=1} {print a; c=a+b; a=b; b=c}'
  1150. # Read the output of a command into the buffer in vim
  1151. :r !command
  1152. # Calculates the date 2 weeks ago from Saturday the specified format.
  1153. date -d '2 weeks ago Saturday' +%Y-%m-%d
  1154. # Find broken symlinks
  1155. find . -type l ! -exec test -e {} \; -print
  1156. # Add your public SSH key to a server in one command
  1157. cat .ssh/id_rsa.pub | ssh hostname 'cat >> .ssh/authorized_keys'
  1158. # ssh tunnel with auto reconnect ability
  1159. while [ ! -f /tmp/stop ]; do ssh -o ExitOnForwardFailure=yes -R 2222:localhost:22 target "while nc -zv localhost 2222; do sleep 5; done"; sleep 5;done
  1160. # find process associated with a port
  1161. fuser [portnumber]/[proto]
  1162. # cycle through a 256 colour palette
  1163. yes "$(seq 232 255;seq 254 -1 233)" | while read i; do printf "\x1b[48;5;${i}m\n"; sleep .01; done
  1164. # scping files with streamlines compression (tar gzip)
  1165. tar czv file1 file2 folder1 | ssh user@server tar zxv -C /destination
  1166. # add all files not under version control to repository
  1167. svn status |grep '\?' |awk '{print $2}'| xargs svn add
  1168. # Count files beneath current directory (including subfolders)
  1169. find . -type f | wc -l
  1170. # Discover the process start time
  1171. ps -eo pid,lstart,cmd
  1172. # Run the built in PHP-server in current folder
  1173. php -S 127.0.0.1:8080
  1174. # commandlinefu.com by David Winterbottom
  1175. # Press enter and take a WebCam picture.
  1176. read && ffmpeg -y -r 1 -t 3 -f video4linux2 -vframes 1 -s sxga -i /dev/video0 ~/webcam-$(date +%m_%d_%Y_%H_%M).jpeg
  1177. # Automatically find and re-attach to a detached screen session
  1178. screen -D -R
  1179. # ping as traceroute
  1180. for i in {1..30}; do ping -t $i -c 1 google.com; done | grep "Time to live exceeded"
  1181. # what model of computer I'm using?
  1182. sudo dmidecode | grep Product
  1183. # tail a log over ssh
  1184. ssh -t remotebox "tail -f /var/log/remote.log"
  1185. # du with colored bar graph
  1186. t=$(df|awk 'NR!=1{sum+=$2}END{print sum}');sudo du / --max-depth=1|sed '$d'|sort -rn -k1 | awk -v t=$t 'OFMT="%d" {M=64; for (a=0;a<$1;a++){if (a>c){c=a}}br=a/c;b=M*br;for(x=0;x<b;x++){printf "\033[1;31m" "|" "\033[0m"}print " "$2" "(a/t*100)"% total"}'
  1187. # sort the output of the 'du' command by largest first, using human readable output.
  1188. du -h --max-depth=1 |sort -rh
  1189. # Target a specific column for pattern substitution
  1190. awk '{gsub("foo","bar",$5)}1' file
  1191. # Discovering all open files/dirs underneath a directory
  1192. lsof +D <dirname>
  1193. # Substrings a variable
  1194. var='123456789'; echo ${var:<start_pos>:<offset>}
  1195. # Check syntax for all PHP files in the current directory and all subdirectories
  1196. find . -name \*.php -exec php -l "{}" \;
  1197. # "Clone" a list of installed packages from one Debian/Ubuntu Server to another
  1198. apt-get install `ssh root@host_you_want_to_clone "dpkg -l | grep ii" | awk '{print $2}'`
  1199. # Timer with sound alarm
  1200. sleep 3s && espeak "wake up, you bastard" 2>/dev/null
  1201. # A formatting test for David Winterbottom: improving commandlinefu for submitters
  1202. echo "?????, these are the umlauted vowels I sing to you. Oh, and sometimes ?, but I don't sing that one cause it doesn't rhyme."
  1203. # Random unsigned integer
  1204. echo $(openssl rand 4 | od -DAn)
  1205. # kill all process that belongs to you
  1206. kill -9 -1
  1207. # Continue a current job in the background
  1208. <ctrl+z> bg
  1209. # Send email with curl and gmail
  1210. curl -n --ssl-reqd --mail-from "<user@gmail.com>" --mail-rcpt "<user@server.tld>" --url smtps://smtp.gmail.com:465 -T file.txt
  1211. # translates acronyms for you
  1212. wtf is <acronym>
  1213. # Create an animated gif from a Youtube video
  1214. url=http://www.youtube.com/watch?v=V5bYDhZBFLA; youtube-dl -b $url; mplayer $(ls ${url##*=}*| tail -n1) -ss 00:57 -endpos 10 -vo gif89a:fps=5:output=output.gif -vf scale=400:300 -nosound
  1215. # Print just line 4 from a textfile
  1216. sed -n '4{p;q}'
  1217. # Print just line 4 from a textfile
  1218. sed -n '4p'
  1219. # List bash functions defined in .bash_profile or .bashrc
  1220. compgen -A function
  1221. # Start a HTTP server which serves Python docs
  1222. pydoc -p 8888 & gnome-open http://localhost:8888
  1223. # Do some learning...
  1224. ls /usr/bin | xargs whatis | grep -v nothing | less
  1225. # commandlinefu.com by David Winterbottom
  1226. # Insert the last argument of the previous command
  1227. <ALT> .
  1228. # Typing the current date ( or any string ) via a shortcut as if the keys had been actually typed with the hardware keyboard in any application.
  1229. xvkbd -xsendevent -text $(date +%Y%m%d)
  1230. # recursive search and replace old with new string, inside files
  1231. find . -type f -exec sed -i s/oldstring/newstring/g {} +
  1232. # Clean your broken terminal
  1233. stty sane
  1234. # geoip information
  1235. curl -s "http://www.geody.com/geoip.php?ip=$(curl -s icanhazip.com)" | sed '/^IP:/!d;s/<[^>][^>]*>//g'
  1236. # Backup a local drive into a file on the remote host via ssh
  1237. dd if=/dev/sda | ssh user@server 'dd of=sda.img'
  1238. # Sort all running processes by their memory & CPU usage
  1239. ps aux --sort=%mem,%cpu
  1240. # Kill processes that have been running for more than a week
  1241. find /proc -user myuser -maxdepth 1 -type d -mtime +7 -exec basename {} \; | xargs kill -9
  1242. # Save current layout of top
  1243. <Shift + W>
  1244. # Show directories in the PATH, one per line
  1245. echo $PATH | tr \: \\n
  1246. # An easter egg built into python to give you the Zen of Python
  1247. python -c 'import this'
  1248. # Add prefix onto filenames
  1249. rename 's/^/prefix/' *
  1250. # Stream YouTube URL directly to mplayer
  1251. id="dMH0bHeiRNg";mplayer -fs http://youtube.com/get_video.php?video_id=$id\&t=$(curl -s http://www.youtube.com/watch?v=$id | sed -n 's/.*, "t": "\([^"]*\)", .*/\1/p')
  1252. # Get all IPs via ifconfig
  1253. ifconfig -a | perl -nle'/(\d+\.\d+\.\d+\.\d+)/ && print $1'
  1254. # Pick a random line from a file
  1255. shuf -n1 file.txt
  1256. # Get all these commands in a text file with description.
  1257. for x in `jot - 0 2400 25`; do curl "http://www.commandlinefu.com/commands/browse/sort-by-votes/plaintext/$x" ; done > commandlinefu.txt
  1258. # Stripping ^M at end of each line for files
  1259. dos2unix <filenames>
  1260. # Use Cygwin to talk to the Windows clipboard
  1261. cat /dev/clipboard; $(somecommand) > /dev/clipboard
  1262. # Backup files incremental with rsync to a NTFS-Partition
  1263. rsync -rtvu --modify-window=1 --progress /media/SOURCE/ /media/TARGET/
  1264. # Find removed files still in use via /proc
  1265. find -L /proc/*/fd -links 0 2>/dev/null
  1266. # Convert "man page" to text file
  1267. man ls | col -b > ~/Desktop/man_ls.txt
  1268. # Connect to google talk through ssh by setting your IM client to use the localhost 5432 port
  1269. ssh -f -N -L 5432:talk.google.com:5222 user@home.network.com
  1270. # Find last reboot time
  1271. who -b
  1272. # Get video information with ffmpeg
  1273. ffmpeg -i filename.flv
  1274. # for all flv files in a dir, grab the first frame and make a jpg.
  1275. for f in *.flv; do ffmpeg -y -i "$f" -f image2 -ss 10 -vframes 1 -an "${f%.flv}.jpg"; done
  1276. # commandlinefu.com by David Winterbottom
  1277. # Purge configuration files of removed packages on debian based systems
  1278. sudo aptitude purge `dpkg --get-selections | grep deinstall | awk '{print $1}'`
  1279. # from within vi, pipe a chunk of lines to a command line and replace the chunk with the result
  1280. !}sort
  1281. # Using awk to sum/count a column of numbers.
  1282. cat count.txt | awk '{ sum+=$1} END {print sum}'
  1283. # Append stdout and stderr to a file, and print stderr to the screen [bash]
  1284. somecommand 2>&1 >> logfile | tee -a logfile
  1285. # quickly change all .html extensions on files in folder to .htm
  1286. for i in *.html ; do mv $i ${i%.html}.htm ; done
  1287. # Grep for word in directory (recursive)
  1288. grep --color=auto -iRnH "$search_word" $directory
  1289. # Another Curl your IP command
  1290. curl -s http://checkip.dyndns.org | sed 's/[a-zA-Z<>/ :]//g'
  1291. # count IPv4 connections per IP
  1292. netstat -anp |grep 'tcp\|udp' | awk '{print $5}' | sed s/::ffff:// | cut -d: -f1 | sort | uniq -c | sort -n
  1293. # pattern match in awk - no grep
  1294. awk '/pattern1/ && /pattern2/ && !/pattern3/ {print}'
  1295. # Optimal way of deleting huge numbers of files
  1296. find /path/to/dir -type f -print0 | xargs -0 rm
  1297. # Convert Shell Text to Upper/Lower Case
  1298. ALT-U / ALT-L
  1299. # grep processes list avoiding the grep itself
  1300. ps axu | grep [a]pache2
  1301. # repeat a command every one second
  1302. watch -n 1 "do foo"
  1303. # Swap the two last arguments of the current command line
  1304. <ctrl+e> <esc+t>
  1305. # Daemonize nc - Transmit a file like a http server
  1306. while ( nc -l 80 < /file.htm > : ) ; do : ; done &
  1307. # Redirect tar extract to another directory
  1308. tar xfz filename.tar.gz -C PathToDirectory
  1309. # Run a command when a file is changed
  1310. while inotifywait -e modify /tmp/myfile; do firefox; done
  1311. # Find Malware in the current and sub directories by MD5 hashes
  1312. IFS=$'\n' && for f in `find . -type f -exec md5sum "{}" \;`; do echo $f | sed -r 's/^[^ ]+/Checking:/'; echo $f | cut -f1 -d' ' | netcat hash.cymru.com 43 ; done
  1313. # Execute a command, convert output to .png file, upload file to imgur.com, then returning the address of the .png.
  1314. imgur(){ $*|convert label:@- png:-|curl -F "image=@-" -F "key=1913b4ac473c692372d108209958fd15" http://api.imgur.com/2/upload.xml|grep -Eo "<original>(.)*</original>" | grep -Eo "http://i.imgur.com/[^<]*";}
  1315. # RDP through SSH tunnel
  1316. ssh -f -L3389:<RDP_HOST>:3389 <SSH_PROXY> "sleep 10" && rdesktop -T'<WINDOW_TITLE>' -uAdministrator -g800x600 -a8 -rsound:off -rclipboard:PRIMARYCLIPBOARD -5 localhost
  1317. # Rapidly invoke an editor to write a long, complex, or tricky command
  1318. <ESC> v or ctrl-x ctrl-e
  1319. # Remote screenshot
  1320. ssh user@remote-host "DISPLAY=:0.0 import -window root -format png -"|display -format png -
  1321. # List your MACs address
  1322. lsmac() { ifconfig -a | sed '/eth\|wl/!d;s/ Link.*HWaddr//' ; }
  1323. # ssh to machine behind shared NAT
  1324. ssh -NR 0.0.0.0:2222:127.0.0.1:22 user@jump.host.com
  1325. # Countdown Clock
  1326. MIN=10;for ((i=MIN*60;i>=0;i--));do echo -ne "\r$(date -d"0+$i sec" +%H:%M:%S)";sleep 1;done
  1327. # commandlinefu.com by David Winterbottom
  1328. # Look up a unicode character by name
  1329. egrep -i "^[0-9a-f]{4,} .*$*" $(locate CharName.pm) | while read h d; do /usr/bin/printf "\U$(printf "%08x" 0x$h)\tU+%s\t%s\n" $h "$d"; done
  1330. # Generate MD5 hash for a string
  1331. md5sum <<<"test"
  1332. # Ask user to confirm
  1333. Confirm() { read -sn 1 -p "$1 [Y/N]? "; [[ $REPLY = [Yy] ]]; }
  1334. # Make a file not writable / immutable by root
  1335. sudo chattr +i <file>
  1336. # prevent accidents and test your command with echo
  1337. echo rm *.txt
  1338. # Get My Public IP Address
  1339. curl ifconfig.me
  1340. # Empty a file
  1341. :> file
  1342. # Emptying a text file in one shot
  1343. :%d
  1344. # computes the most frequent used words of a text file
  1345. cat WAR_AND_PEACE_By_LeoTolstoi.txt | tr -cs "[:alnum:]" "\n"| tr "[:lower:]" "[:upper:]" | awk '{h[$1]++}END{for (i in h){print h[i]" "i}}'|sort -nr | cat -n | head -n 30
  1346. # Quick HTML image gallery from folder contents
  1347. find . -iname '*.jpg' -exec echo '<img src="{}">' \; > gallery.html
  1348. # check the status of 'dd' in progress (OS X)
  1349. killall -INFO dd
  1350. # Restrict the bandwidth for the SCP command
  1351. scp -l10 pippo@serverciccio:/home/zutaniddu/* .
  1352. # List all authors of a particular git project
  1353. git log --format='%aN' | sort -u
  1354. # live ssh network throughput test
  1355. pv /dev/zero|ssh $host 'cat > /dev/null'
  1356. # Carriage return for reprinting on the same line
  1357. while true; do echo -ne "$(date)\r"; sleep 1; done
  1358. # What is my ip?
  1359. curl http://www.whatismyip.org/
  1360. # Empty a file
  1361. truncate -s0 file
  1362. # List Network Tools in Linux
  1363. apropos network |more
  1364. # Function that outputs dots every second until command completes
  1365. sleeper(){ while `ps -p $1 &>/dev/null`; do echo -n "${2:-.}"; sleep ${3:-1}; done; }; export -f sleeper
  1366. # Search back through previous commands
  1367. Ctrl-R <search-text>
  1368. # Print text string vertically, one character per line.
  1369. echo "vertical text" | grep -o '.'
  1370. # Find running binary executables that were not installed using dpkg
  1371. cat /var/lib/dpkg/info/*.list > /tmp/listin ; ls /proc/*/exe |xargs -l readlink | grep -xvFf /tmp/listin; rm /tmp/listin
  1372. # Cleanup firefox's database.
  1373. pgrep -u `id -u` firefox-bin || find ~/.mozilla/firefox -name '*.sqlite'|(while read -e f; do echo 'vacuum;'|sqlite3 "$f" ; done)
  1374. # vim easter egg
  1375. $ vim ... :help 42
  1376. # Find the process you are looking for minus the grepped one
  1377. pgrep command_name
  1378. # commandlinefu.com by David Winterbottom
  1379. # VIM: Replace a string with an incrementing number between marks 'a and 'b (eg, convert string ZZZZ to 1, 2, 3, ...)
  1380. :let i=0 | 'a,'bg/ZZZZ/s/ZZZZ/\=i/ | let i=i+1
  1381. # Get the canonical, absolute path given a relative and/or noncanonical path
  1382. readlink -f ../super/symlink_bon/ahoy
  1383. # Create a Multi-Part Archive Without Proprietary Junkware
  1384. tar czv Pictures | split -d -a 3 -b 16M - pics.tar.gz.
  1385. # wrap long lines of a text
  1386. fold -s -w 90 file.txt
  1387. # Display last exit status of a command
  1388. echo $?
  1389. # Enable ** to expand files recursively (>=bash-4.0)
  1390. shopt -s globstar
  1391. # Command Line to Get the Stock Quote via Yahoo
  1392. curl -s 'http://download.finance.yahoo.com/d/quotes.csv?s=csco&f=l1'
  1393. # Convert camelCase to underscores (camel_case)
  1394. sed -r 's/([a-z]+)([A-Z][a-z]+)/\1_\l\2/g' file.txt
  1395. # Set your profile so that you resume or start a screen session on login
  1396. echo "screen -DR" >> ~/.bash_profile
  1397. # play high-res video files on a slow processor
  1398. mplayer -framedrop -vfm ffmpeg -lavdopts lowres=1:fast:skiploopfilter=all
  1399. # Grep colorized
  1400. grep -i --color=auto
  1401. # List your largest installed packages.
  1402. wajig large
  1403. # Monitor dynamic changes in the dmesg log.
  1404. watch "dmesg |tail -15"
  1405. # Generate a list of installed packages on Debian-based systems
  1406. dpkg --get-selections > LIST_FILE
  1407. # find the process that is using a certain port e.g. port 3000
  1408. lsof -P | grep ':3000'
  1409. # Pause Current Thread
  1410. ctrl-z
  1411. # Merge *.pdf files
  1412. gs -q -sPAPERSIZE=letter -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=out.pdf `ls *.pdf`
  1413. # Convert .wma files to .ogg with ffmpeg
  1414. find -name '*wma' -exec ffmpeg -i {} -acodec vorbis -ab 128k {}.ogg \;
  1415. # Generate Random Passwords
  1416. < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c6
  1417. # Grep without having it show its own process in the results
  1418. ps aux | grep "[s]ome_text"
  1419. # Find recursively, from current directory down, files and directories whose names contain single or multiple whitespaces and replace each such occurrence with a single underscore.
  1420. find ./ -name '*' -exec rename 's/\s+/_/g' {} \;
  1421. # Files extension change
  1422. rename .oldextension .newextension *.oldextension
  1423. # archive all files containing local changes (svn)
  1424. svn st | cut -c 8- | sed 's/^/\"/;s/$/\"/' | xargs tar -czvf ../backup.tgz
  1425. # Just run it ;)
  1426. echo SSBMb3ZlIFlvdQo= | base64 -d
  1427. # Show which programs are listening on TCP and UDP ports
  1428. netstat -plunt
  1429. # commandlinefu.com by David Winterbottom
  1430. # vim easter egg
  1431. $ vim ... :help 42
  1432. # copy with progress bar - rsync
  1433. rsync -rv <src> <dst> --progress
  1434. # find and replace tabs for spaces within files recursively
  1435. find ./ -type f -exec sed -i 's/\t/ /g' {} \;
  1436. # List your MACs address
  1437. cat /sys/class/net/eth0/address
  1438. # diff current vi buffer edits against original file
  1439. :w !diff -u % -
  1440. # Send data securly over the net.
  1441. cat /etc/passwd | openssl aes-256-cbc -a -e -pass pass:password | netcat -l -p 8080
  1442. # pretend to be busy in office to enjoy a cup of coffee
  1443. for i in `seq 0 100`;do timeout 6 dialog --gauge "Install..." 6 40 "$i";done
  1444. # JSON processing with Python
  1445. curl -s "http://feeds.delicious.com/v2/json?count=5" | python -m json.tool | less -R
  1446. # Get your external IP address without curl
  1447. wget -qO- icanhazip.com
  1448. # cycle through a 256 colour palette
  1449. yes "$(seq 1 255)" | while read i; do printf "\x1b[48;5;${i}m\n"; sleep .01; done
  1450. # Find all files larger than 500M and less than 1GB
  1451. find / -type f -size +500M -size -1G
  1452. # extract email adresses from some file (or any other pattern)
  1453. grep -Eio '([[:alnum:]_.-]+@[[:alnum:]_.-]+?\.[[:alpha:].]{2,6})'
  1454. # command line calculator
  1455. calc(){ awk "BEGIN{ print $* }" ;}
  1456. # Plays Music from SomaFM
  1457. read -p "Which station? "; mplayer --reallyquiet -vo none -ao sdl http://somafm.com/startstream=${REPLY}.pls
  1458. # infile search and replace on N files (including backup of the files)
  1459. perl -pi.bk -e's/foo/bar/g' file1 file2 fileN
  1460. # Create an SSH SOCKS proxy server on localhost:8000 that will re-start itself if something breaks the connection temporarily
  1461. autossh -f -M 20000 -D 8000 somehost -N
  1462. # Delete all files found in directory A from directory B
  1463. for file in <directory A>/*; do rm <directory B>/`basename $file`; done
  1464. # Block an IP address from connecting to a server
  1465. iptables -A INPUT -s 222.35.138.25/32 -j DROP
  1466. # Record microphone input and output to date stamped mp3 file
  1467. arecord -q -f cd -r 44100 -c2 -t raw | lame -S -x -h -b 128 - `date +%Y%m%d%H%M`.mp3
  1468. # loop over a set of items that contain spaces
  1469. ls | while read ITEM; do echo "$ITEM"; done
  1470. # Extract tar content without leading parent directory
  1471. tar -xaf archive.tar.gz --strip-components=1
  1472. # Function to split a string into an array
  1473. read -a ARR <<<'world domination now!'; echo ${ARR[2]};
  1474. # Update twitter via curl
  1475. curl -u user -d status="Tweeting from the shell" http://twitter.com/statuses/update.xml
  1476. # Check your spelling
  1477. aspell -a <<< '<WORDS>'
  1478. # View ~/.ssh/known_hosts key information
  1479. ssh-keygen -l -f ~/.ssh/known_hosts
  1480. # commandlinefu.com by David Winterbottom
  1481. # Binary digits Matrix effect
  1482. perl -e '$|++; while (1) { print " " x (rand(35) + 1), int(rand(2)) }'
  1483. # print multiplication formulas
  1484. seq 9 | sed 'H;g' | awk -v RS='' '{for(i=1;i<=NF;i++)printf("%dx%d=%d%s", i, NR, i*NR, i==NR?"\n":"\t")}'
  1485. # Rapidly invoke an editor to write a long, complex, or tricky command
  1486. <ESC> v
  1487. # add the result of a command into vi
  1488. !!command
  1489. # Super Speedy Hexadecimal or Octal Calculations and Conversions to Decimal.
  1490. echo "$(( 0x10 )) - $(( 010 )) = $(( 0x10 - 010 ))"
  1491. # bash shortcut: !$ !^ !* !:3 !:h and !:t
  1492. echo foo bar foobar barfoo && echo !$ !^ !:3 !* && echo /usr/bin/foobar&& echo !$:h !$:t
  1493. # Traceroute w/TCP to get through firewalls.
  1494. tcptraceroute www.google.com
  1495. # benchmark web server with apache benchmarking tool
  1496. ab -n 9000 -c 900 localhost:8080/index.php
  1497. # move a lot of files over ssh
  1498. tar -cf - /home/user/test | gzip -c | ssh user@sshServer 'cd /tmp; tar xfz -'
  1499. # sends a postscript file to a postscript printer using netcat
  1500. cat my.ps | nc -q 1 hp4550.mynet.xx 9100
  1501. # the same as [Esc] in vim
  1502. Ctrl + [
  1503. # open a screenshot of a remote desktop via ssh
  1504. xloadimage <(ssh USER@HOSTNAME DISPLAY=:0.0 import -window root png:-)
  1505. # Displays the attempted user name, ip address, and time of SSH failed logins on Debian machines
  1506. awk '/sshd/ && /Failed/ {gsub(/invalid user/,""); printf "%-12s %-16s %s-%s-%s\n", $9, $11, $1, $2, $3}' /var/log/auth.log
  1507. # Create a bunch of dummy files for testing
  1508. touch {1..10}.txt
  1509. # Quickly get summary of sizes for files and folders
  1510. du -sh *
  1511. # Temporarily ignore mismatched SSH host key
  1512. ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no username@host
  1513. # back up your commandlinefu contributed commands
  1514. curl http://www.commandlinefu.com/commands/by/<your username>/rss|gzip ->commandlinefu-contribs-backup-$(date +%Y-%m-%d-%H.%M.%S).rss.gz
  1515. # Find the package a command belongs to on Debian
  1516. dpkg -S $( which ls )
  1517. # Replace spaces in filenames with underscorees
  1518. ls | while read f; do mv "$f" "${f// /_}";done
  1519. # Google text-to-speech in mp3 format
  1520. say(){ mplayer -user-agent Mozilla "http://translate.google.com/translate_tts?tl=en&q=$(echo $* | sed 's#\ #\+#g')" > /dev/null 2>&1 ; }
  1521. # Terminal redirection
  1522. script /dev/null | tee /dev/pts/3
  1523. # Converts to PDF all the OpenOffice.org files in the directory
  1524. for i in $(ls *.od{tp}); do unoconv -f pdf $i; done
  1525. # Print info about your real user.
  1526. who loves mum
  1527. # Update twitter via curl (and also set the "from" bit)
  1528. curl -u twitter-username -d status="Hello World, Twitter!" -d source="cURL" http://twitter.com/statuses/update.xml
  1529. # Escape potential tarbombs
  1530. atb() { l=$(tar tf $1); if [ $(echo "$l" | wc -l) -eq $(echo "$l" | grep $(echo "$l" | head -n1) | wc -l) ]; then tar xf $1; else mkdir ${1%.tar.gz} && tar xf $1 -C ${1%.tar.gz}; fi ;}

comments powered by Disqus