Hide

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.
Hide

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:

Hide

News

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!
Hide

Top Tags

Hide

Functions

Commands using sed from sorted by
Terminal - Commands using sed - 1,149 results
sed '/INSERT INTO `unwanted_table`/d' mydb.sql > reduced.sql
2016-06-24 20:13:47
User: sudopeople
Functions: sed
0

Starting with a large MySQL dump file (*.sql) remove any lines that have inserts for the specified table. Sometimes one or two tables are very large and uneeded, eg. log tables. To exclude multiple tables you can get fancy with sed, or just run the command again on subsequently generated files.

touch files.txt; cat reorder_files.sh | while read line; do x=`echo $line | sed 's/touch \([a-z0-9\.]\+.*.pdf\);.*/\1/'`; echo $x >> files.txt ; done;
pkg update -nv | sed -n '/entire/{N;p;}'
2016-04-15 20:48:49
User: apaton
Functions: sed
0

Above example is upgrading Solaris from 11.1SRU20.0.5 to 11.2SRU15.0.5. Patches are not applied options "-nv" are for dryrun and verbose output.

echo Which way up? | flip.pl | cowsay | tac | sed -e "s,/,+,g" -e "s,\\\,/,g" -e "s,+,\\\,g" -e "s,_,-,g" -e "s,\^,v,g"
2016-04-08 11:41:44
User: mpb
Functions: echo sed tac
1

It's quite fun to invert text using "flip.pl" (ref: http://ubuntuforums.org/showthread.php?t=2078323 ).

Slightly more challenging is to flip a whole "cowsay". :-)

ss -t -o state established '( dport = :443 || dport = :80 )'|grep tcp|awk '{ print $5 }'|sed s/:http[s]*//g|sort -u|netcat whois.cymru.com 43|grep -v "AS Name"|sort -t'|' -k3
transfer() { basefile=$(basename "$1" | sed -e 's/[^a-zA-Z0-9._-]/-/g');curl --progress-bar --upload-file "$1" "https://transfer.sh/$basefile"|xsel --clipboard;xsel --clipboard ; }
2016-03-20 19:38:48
User: leftyfb
Functions: basename sed
2

Requires:

curl

xsel

access to the internet(http://transfer.sh)

This is an alias utilizing the transfer.sh service to make sharing files easier from the command line. I have modified the alias provided by transfer.sh to use xsel to copy the resulting URL to the clipboard.

The full modified alias is as follows since commandlinefu only allows 255 characters:

transfer() { if [ $# -eq 0 ]; then echo "No arguments specified. Usage:\necho transfer /tmp/test.md\ncat /tmp/test.md | transfer test.md"; return 1; fi if tty -s; then basefile=$(basename "$1" | sed -e 's/[^a-zA-Z0-9._-]/-/g'); curl --progress-bar --upload-file "$1" "https://transfer.sh/$basefile" |xsel --clipboard; else curl --progress-bar --upload-file "-" "https://transfer.sh/$1" |xsel --clipboard ; fi; xsel --clipboard; }

adb shell "screencap -p | base64" | sed 's/\r$//' | base64 -d | display
2016-02-07 18:46:35
User: T4b
Functions: sed
4

Dependencies on phone: adb access, screencap command, base64 command.

Dependencies on computer: adb, sed, base64, display (from imagemagick, but can substitute other image viewer which reads from stdin).

This should work around adb stupidies (i.e. that it replaces \n with \r\n) with base64.

sudo strace -e trace=network -p `pidof nginx | sed -e 's/ /,/g'`
2016-01-28 18:48:16
Functions: sed strace sudo
1

Nginx (and other webservers like Apache) can be awkward to trace. They run as root, then switch to another user once they're ready to serve web pages. They also have a "master" process and multiple worker processes.

The given command finds the process IDs of all Nginx processes, joins them together with a comma, then traces all of them at once with "sudo strace." System trace output can be overwhelming, so we only capture "networking" output.

TIP: to kill this complex strace, do "sudo killall strace".

Compare with a similar command: http://www.commandlinefu.com/commands/view/11918/easily-strace-all-your-apache-processes

get_iplayer --type=radio --channel "Radio 4 Extra" | grep : | awk '{ if ( NR > 1 ) { print } }'|sed 's/:.*//' |sed '$ d' > pidlist && while read p; do get_iplayer --get --fields=pid $p; done <pidlist && rm pidlist
2016-01-16 17:20:54
User: dunryc
Functions: awk grep read rm sed
0

use get_iplay to download all listed content from http://www.bbc.co.uk/radio4extra run every night to make sure no episodes are missed

git log --oneline | nl -v0 | sed 's/^ \+/&HEAD~/'
2015-11-23 21:35:57
User: flatcap
Functions: nl sed
Tags: git sed nl git-log
1

Print a git log (in reverse order) giving a reference relative to HEAD.

HEAD (the current revision) can also be referred to as HEAD~0

The previous revision is HEAD~1 then HEAD~2 etc.

.

Add line numbers to the git output, starting at zero:

... | nl -v0 | ...

.

Insert the string 'HEAD~' before the number using sed:

... | sed 's/^ \+/&HEAD~/'

.

Thanks to bartonski for the idea :-)

find <mydir> -type f -exec sed -i 's/<string1>/<string2>/g' {} \;
ls -1 | while read a; do mv "$a" `echo $a | sed -e 's/\ //g'`; done
echo ${IP} | sed "s/[0-9\.]//g"
2015-10-19 18:20:03
User: andregyn62
Functions: echo sed
0

This command validates if exist any character different in 0-255 and dot.

If any characters different is typed the error menssage is showing.

adb shell screencap -p | sed 's/\r$//' > FILENAME.PNG
curl -q -s http://www.hasthelhcdestroyedtheearth.com/ | sed -En '/span/s/.*>(.*)<.*/\1/p'
2015-09-23 23:26:30
User: Tatsh
Functions: sed
0

Use -q as first argument (as described in `man curl`) to ignore curlrc to ensure the output is always the same regardless of user's configuration.

curl -s http://www.hasthelhcdestroyedtheearth.com/ | sed -En '/span/s/.*>(.*)<.*/\1/p'
2015-09-23 12:30:31
User: BeniBela
Functions: sed
1

This says if the LHC has destroyed the world. Run it in a loop to monitor the state of Earth. Might not work reliable, if the world has actually been destroyed.

weather() { curl -s "http://www.wunderground.com/q/zmw:$1.1.99999" | grep "og:title" | cut -d\" -f4 | sed 's/&deg;/ degrees F/'; }
pidstat -t | sed 's/,/./4' | awk -v seuil='10.0' '{if (NR>3 && $8>seuil) print }'
last|grep `whoami`|grep -v logged|cut -c61-71|sed -e 's/[()]//g'|awk '{ sub("\\+", ":");split($1,a,":");if(a[3]){print a[1]*60*60+a[2]*60+a[3]} else {print a[1]*60+a[2] }; }'|paste -s -d+ -|bc|awk '{printf "%dh:%dm:%ds\n",$1/(60*60),$1%(60*60)/60,$1%60}'
2015-09-19 03:02:43
User: donjuanica
Functions: awk cut grep last paste sed
-1

Add -n to last command to restrict to last num logins, otherwise it will pull all available history.

sudo apt-get remove --purge $(dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d')
dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d'
curl $1 | grep -E "http.*\.mp3" | sed "s/.*\(http.*\.mp3\).*/\1/" | xargs wget
2015-09-17 13:19:53
User: theodric
Functions: grep sed xargs
3

The difference between the original version provided and this one is that this one works rather than outputting a wget error

sed -e "s/[^/]*\/\/\([^@]*@\)\?\([^:/]*\).*/\2/"
sed 's/,/\n/g;q' file.csv | nl
2015-08-26 11:38:56
User: flatcap
Functions: sed
Tags: sed nl
0

Take the header line from a comma-delimited CSV file and enumerate the fields.

.

First sed replaces all commas with newlines

s/,/\n/g

Then sed quits (q) after the first line.

Finally, nl numbers all the lines

sed -i.$(date +%F@%T) 's/^LogLevel warn/LogLevel debug/g' httpd.conf
2015-07-22 14:47:26
User: zlemini
Functions: date sed
1
ls

httpd.conf [email protected]:43:20