### Commands using awk (1,402) the last day the last week the last month all time sorted by date votes

• seq -s ' ' 1 9 | sed -n ':a;p;s/ *\w\$//;h;/^\$/t;b a;q' | tac | awk '{for(i=1;i 1x1=1 1x2=2 2x2=4 1x3=3 2x3=6 3x3=9 1x4=4 2x4=8 3x4=12 4x4=16 1x5=5 2x5=10 3x5=15 4x5=20 5x5=25 1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36 1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49 1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64 1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81

11
seq 9 | sed 'H;g' | awk -v RS='' '{for(i=1;i<=NF;i++)printf("%dx%d=%d%s", i, NR, i*NR, i==NR?"\n":"\t")}'
· 2011-10-22 18:41:09
• A more efficient way, with reversed order to put the focus in the big ones. Show Sample Output

11
du -x --max-depth=1|sort -rn|awk -F / -v c=\$COLUMNS 'NR==1{t=\$1} NR>1{r=int(\$1/t*c+.5); b="\033[1;31m"; for (i=0; i<r; i++) b=b"#"; printf " %5.2f%% %s\033[0m %s\n", \$1/t*100, b, \$2}'|tac
· 2015-09-12 10:36:49

• 10
while true; do X=\$Y; sleep 1; Y=\$(ifconfig eth0|grep RX\ bytes|awk '{ print \$2 }'|cut -d : -f 2); echo "\$(( Y-X )) bps"; done
· 2009-03-27 08:26:39
• This is useful if you have need to do port forwarding and your router doesn't assign static IPs, you can add it to a script in a cron job that checks if you IP as recently changed or with a trigger script. This was tested on Mac OSX.

10
ifconfig en1 | awk '/inet / {print \$2}' | mail -s "hello world" email@email.com
· 2009-04-28 06:01:52
• Show time and date when you installed your OS. Show Sample Output

10
ls -lct /etc | tail -1 | awk '{print \$6, \$7}'
· 2009-09-03 10:26:37
• Just refining last proposal for this check, showing awk power to make more complex math (instead /1024/1024, 2^20). We don't need declare variable before run lsof, because \$(command) returns his output. Also, awk can perform filtering by regexp instead to call grep. I changed the 0.0000xxxx messy output, with a more readable form purging all fractional numbers and files less than 1 MB. Show Sample Output

10
lsof -p \$(pidof firefox) | awk '/.mozilla/ { s = int(\$7/(2^20)); if(s>0) print (s)" MB -- "\$9 | "sort -rn" }'
· 2010-01-13 22:45:53
• man dmidecode [look for type] Show Sample Output

10
dmidecode -t 17 | awk -F":" '/Speed/ { print \$2 }'
· 2010-02-19 12:15:13
• poorman's ifstat using just sh and awk. You must change "eth0" with your interface's name. Show Sample Output

10
while true; do cat /proc/net/dev; sleep 1; done | awk -v dc="date \"+%T\"" '/eth0/{i = \$2 - oi; o = \$10 - oo; oi = \$2; oo = \$10; dc|getline d; close(dc); if (a++) printf "%s %8.2f KiB/s in %8.2f KiB/s out\n", d, i/1024, o/1024}'
· 2011-11-21 05:24:44

• 10
awk '{ \$1="";print}'
· 2015-05-26 20:55:36
• This command is useful when you want to know what process is responsible for a certain GUI application and what command you need to issue to launch it in terminal. Show Sample Output

9
xprop | awk '/PID/ {print \$3}' | xargs ps h -o pid,cmd
· 2009-02-16 07:55:19
• Very useful set of commands to know when your file system was created. Show Sample Output

9
df / | awk '{print \$1}' | grep dev | xargs tune2fs -l | grep create
· 2009-02-16 18:45:03
• You can use multiple field separators by separating them with | (=or). This may be helpful when you want to split a string by two separators for example. #echo "one=two three" | awk -F "=| " {'print \$1, \$3'} one three

9
awk -F "=| "
· 2009-03-02 21:09:51
• This command checks for the number of times when someone has tried to login to your server and failed. If there are a lot, then that user is being targeted on your system and you might want to make sure that user either has remote logins disabled, or has a strong password, or both. If your output has an "invalid" line, it is a summary of all logins from users that don't exist on your system. Show Sample Output

9
zgrep "Failed password" /var/log/auth.log* | awk '{print \$9}' | sort | uniq -c | sort -nr | less
· 2009-03-03 13:45:56
• This create an array 'a' with wole lines. only one occurrence of each line - Not Get lines ++ !

9
awk '!a[\$0]++' file
· 2009-08-23 15:28:43

• 9
tail -f file | awk '{now=strftime("%F %T%z\t");sub(/^/, now);print}'
· 2010-11-25 04:11:52
• Booting the VM headless via VBoxHeadless requires knowledge of the VM's network in order to connect. Using VBoxManage in this way and you can SSH to the VM without first looking up the current IP, which changes depending on how you have your VM configured. Show Sample Output

9
ssh vm-user@`VBoxManage guestproperty get "vm-name" "/VirtualBox/GuestInfo/Net/0/V4/IP" | awk '{ print \$2 }'`
· 2011-05-04 18:01:36
• This command uses awk(1) to print all lines between two known line numbers in a file. Useful for seeing output in a log file, where the line numbers are known. The above command will print all lines between, and including, lines 3 and 6.

9
awk 'NR >= 3 && NR <= 6' /path/to/file
· 2011-12-14 14:28:56

• 9
sudo who | awk '!/root/{ cmd="/sbin/pkill -KILL -u " \$1; system(cmd)}'
· 2021-02-03 13:35:52

• 8
svn st | grep "^\?" | awk "{print \\$2}" | xargs svn add \$1
· 2009-02-05 17:28:53

• 8
gunzip -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
· 2009-03-03 04:26:57
• Searches the /var/log/secure log file for Failed and/or invalid user log in attempts. Show Sample Output

8
cat /var/log/secure | grep sshd | grep Failed | sed 's/invalid//' | sed 's/user//' | awk '{print \$11}' | sort | uniq -c | sort -n
· 2009-03-30 15:48:24
• Also useful with iostat, or pretty much anything else you want timestamped. Show Sample Output

8
vmstat 1 | awk '{now=strftime("%Y-%m-%d %T "); print now \$0}'
· 2009-07-17 07:41:57

• 8
awk -F ',' '{ x = x + \$4 } END { print x }' test.csv
· 2009-08-11 12:10:33
• Connect to a machine running ssh using mac address by using the "arp" command Show Sample Output

8
ssh 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/)//'`
· 2009-09-09 04:32:20
• This function uploads images to http://omploader.org and then prints out the links to the file. Some coloring can also be added to the command with: `ompload() { curl -F file1=@"\$1" http://omploader.org/upload|awk '/Info:|File:|Thumbnail:|BBCode:/{gsub(/<[^<]*?\/?>/,"");\$1=\$1;sub(/^/,"\033[0;34m");sub(/:/,"\033[0m:");print}';}` Show Sample Output

8
· 2009-11-07 20:56:52
•  < 1 2 3 4 5 >  Last ›

### What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

### Check These Out

Setting global redirection of STDERR to STDOUT in a script
You have a script where =ALL= STDERR should be redirected to STDIN and you don't want to add "2>&1" at the end of each command... E.G.: \$ ls -al /foo/bar 2>&1 Than just add this piece of code at the beginning of your script! I hope this can help someone. :)

Mac OS X: remove extra languages to save over 3 GB of space.
This will get the job done in the most efficient way - spawning only one `rm` process. "On-the-fly" find data is displayed through `tee` and you should have plenty of time to ctrl-c if needed before it's too late. You may need to re-run this after major Software Updates. To leave more languages in, add more ``-and \! -iname "lang*"'' statements: \$ sudo find / -iname "*.lproj" -and \! -iname "en*" -and \! -iname "spanish*" -print0 | tee /dev/stderr | sudo xargs -0 rm -rfv **Edit: note the 2nd sudo near the end of the pipeline - this is necessary.

Find usb device in realtime
Using this command you can track a moment when usb device was attached.

Search some text from all files inside a directory

Convert JSON to YAML
Requires installing json2yaml via npm: npm install -g json2yaml (can also pipe from stdin) Ref: https://www.npmjs.com/package/json2yaml

Drop all tables from a database, without deleting it

Easily decode unix-time (funtion)
A shell function using perl to easily convert Unix-time to text. Put in in your ~/.bashrc or equivalent. Tested on Linux / Solaris Bourne, bash and zsh. using perl 5.6 and higher. (Does not require GNU date like some other commands)

Set laptop display brightness
Run as root. Path may vary depending on laptop model and video card (this was tested on an Acer laptop with ATI HD3200 video). \$ cat /proc/acpi/video/VGA/LCD/brightness to discover the possible values for your display.

Backticks are evil
This is a simple example of using proper command nesting using \$() over ``. There are a number of advantages of \$() over backticks. First, they can be easily nested without escapes: \$ program1 \$(program2 \$(program3 \$(program4))) versus \$ program1 `program2 \`program3 \`program4\`\`` Second, they're easier to read, then trying to decipher the difference between the backtick and the singlequote: `'. The only drawback \$() suffers from is lack of total portability. If your script must be portable to the archaic Bourne shell, or old versions of the C-shell or Korn shell, then backticks are appropriate, otherwise, we should all get into the habit of \$(). Your future script maintainers will thank you for producing cleaner code.

a function to find the fastest DNS server
http://public-dns.info gives a list of online dns servers. you need to change the country in url (br in this url) with your country code. this command need some time to ping all IP in list.