What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. 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.

If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Universal configuration monitoring and system of record for IT.

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:



May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!

Top Tags





Commands tagged awk from sorted by
Terminal - Commands tagged awk - 297 results
tr -s ' ' | cut -d' ' -f2-
awk '{out="";for(i=2;i<=NF;i++){out=out" "$i};sub(/ /, "", out);print out}'
2015-05-06 22:26:28
User: endix
Functions: awk
Tags: awk

Increase "2" in "i=2" to drop more columns.

lsof -i -n -P | grep -e "$(ps aux | grep node | grep -v grep | awk -F' ' '{print $2}' | xargs | awk -F' ' '{str = $1; for(i = 2; i < NF; i++) {str = str "\\|" $i} print str}')"
2015-02-14 23:24:00
User: hochmeister
Functions: grep

us lsof, grep for any pid matching a given name such as "node".

tail -f access_log | awk '{print $1 , $12}'
2014-12-24 14:15:52
User: tyzbit
Functions: awk tail

Use this command to watch apache access logs in real time to see what pages are getting hit.

ip a s eth0 | awk -F'[/ ]+' '/inet[^6]/{print $3}'
ip addr show enp3s0 | awk '/inet[^6]/{print $2}' | awk -F'/' '{print $1}'
for i in `cat hosts_list`; do RES=`ssh myusername@${i} "ps -ef " |awk '/[p]rocessname/ {print $2}'`; test "x${RES}" = "x" && echo $i; done
2014-10-03 14:57:54
User: arlequin
Functions: awk echo test
Tags: ssh awk test ps

Given a hosts list, ssh one by one and echo its name only if 'processname' is not running.

tcpdump -tnn -c 2000 -i eth0 | awk -F "." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr | awk ' $1 > 10 '
2014-09-26 01:15:23
User: hochmeister
Functions: awk sort tcpdump uniq

capture 2000 packets and print the top 10 talkers

finger $(whoami) | egrep -o 'Name: [a-zA-Z0-9 ]{1,}' | cut -d ':' -f 2 | xargs echo
2014-09-24 01:22:07
User: swebber
Functions: cut egrep finger xargs

Its possible to user a simple regex to extract de username from the finger command.

The final echo its optional, just for remove the initial space

find . -name "*.pdf" -print0 | xargs -r0 stat -c %y\ %n | sort|awk '{print $4}'|gawk 'BEGIN{ a=1 }{ printf "mv %s %04d.pdf\n", $0, a++ }' | bash
2014-09-23 06:40:45
Functions: awk find gawk printf stat xargs
Tags: sort awk find xargs

Caution: distructive overwrite of filenames

Useful for concatenating pdfs in date order using pdftk

du -sm *| sort -nr | awk '{ size=4+5*int($1/5); a[size]++ }; END { print "size(from->to) number graph"; for(i in a){ printf("%d %d ",i,a[i]) ; hist=a[i]; while(hist>0){printf("#") ; hist=hist-5} ; printf("\n")}}'
2014-08-19 14:43:20
User: higuita
Functions: awk du sort
Tags: awk

This command makes a small graph with the histogram of size blocks (5MB in this example), not individual files. Fine tune the 4+5*int($1/5) block for your own size jumps : jump-1+jump*($1/jump)

Also in the hist=hist-5 part, tune for bigger or smaller graphs

YEAR=2015; echo Jul $(ncal 7 $YEAR | awk '/^Fr/{print $NF}')
2014-08-17 11:12:09
User: andreasS
Functions: awk echo
Tags: awk date

Calculate the date of Sysadmin day (last Friday of July) of any given year

awp () { awk '{print $'$1'}'; }
system_profiler SPHardwareDataType | awk '/UUID/ { print $3; }'
2014-07-25 06:54:40
Functions: awk

Gets the Hardware UUID of the current machine using system_profiler.

svn status | awk -F" " '{ for (i=2; i<=NF; i++) print "ls -ld \""$i"\""}' | sh
2014-07-09 09:41:24
User: kln0thing
Functions: awk
Tags: svn awk ls

The AWK part of the code will "collate" the fields from 2nd to Nth field (this is to handle any svn directories that may have spaces in them - typical when working with code that is interchangeably used with windows environment - for example, documentation teams) - the output is passed to "ls -ld" - the -d option to ls will tell ls to handle directories itself, rather than do ls on the directory. The '-p' option is just for pretty printing directories, links and executables (for added readability).

Finally, the entire "constructed" command will be passed onto sh for shell execution.

awk '/text to grep/{print $1}' "log" | sort -n | uniq -c | sort -rn | head -n 100
2014-07-09 08:48:06
User: kln0thing
Functions: awk head sort uniq

Original command: cat "log" | grep "text to grep" | awk '{print $1}' | sort -n | uniq -c | sort -rn | head -n 100

This is a waste of multiple cats and greps, esp when awk is being used

mco ping | head -n -4 | awk '{print $1}' | sort
ip route list 0/0
while true; do clear;awk '{a[$3]+=1};END{for(x in a){print x,a[x]}}' /proc/[0-9]*/stat; sleep 1; done
awk '$1=="Host"{$1="";H=substr($0,2)};$1=="HostName"{print H,"$",$2}' ~/.ssh/config | column -s '$' -t
2014-05-24 20:51:47
User: wejn
Functions: awk column
Tags: awk column

Spits out table that shows your Host->HostName aliases in ~/.ssh/config

grep URL ~/annex/.git/annex/webapp.html | tr -d '">' | awk -F= '{print $4 "=" $5}'
grep -Rl "pattern" files_or_dir
2014-04-06 18:18:07
User: N1nsun
Functions: grep
Tags: awk find grep

Grep can search files and directories recursively. Using the -Z option and xargs -0 you can get all results on one line with escaped spaces, suitable for other commands like rm.

find . | xargs grep -l "FOOBAR" | awk '{print "rm -f "$1}' > doit.sh
2014-04-06 15:48:41
User: sergeylukin
Functions: awk find grep xargs
Tags: awk find grep

After this command you can review doit.sh file before executing it.

If it looks good, execute: `. doit.sh`

lsof|gawk '$4~/txt/{next};/REG.*\(deleted\)$/{printf ">/proc/%s/fd/%d\n", $2,$4}'
2014-03-11 10:40:32
User: wejn
Functions: gawk
Tags: awk lsof gawk

While the posted solution works, I'm a bit uneasy about the "%d" part. This would be hyper-correct approach:

lsof|gawk '$4~/txt/{next};/REG.*\(deleted\)$/{sub(/.$/,"",$4);printf ">/proc/%s/fd/%s\n", $2,$4}'

Oh, and you gotta pipe the result to sh if you want it to actually trim the files. ;)

Btw, this approach also removes false negatives (OP's command skips any deleted files with "txt" in their name).