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

Hide

Credits

Commands using grep from sorted by
Terminal - Commands using grep - 1,628 results
find /proc/*/fd -xtype f -printf "%l\n" | grep -P '^/(?!dev|proc|sys)' | sort | uniq -c | sort -n
2015-08-18 17:58:21
User: flatcap
Functions: find grep sort uniq
Tags: sort uniq find grep
1

List all open files of all processes.

.

find /proc/*/fd

Look through the /proc file descriptors

.

-xtype f

list only symlinks to file

.

-printf "%l\n"

print the symlink target

.

grep -P '^/(?!dev|proc|sys)'

ignore files from /dev /proc or /sys

.

sort | uniq -c | sort -n

count the results

.

Many processes will create and immediately delete temporary files.

These can the filtered out by adding:

... | grep -v " (deleted)$" | ...
lsof -a -d 1-99 -Fn / | grep ^n | cut -b2- | sort | uniq -c | sort -n
echo "quit" | openssl s_client -connect facebook.com:443 | openssl x509 -noout -text | grep "DNS:" | perl -pe "s/(, )?DNS:/\n/g"
grep -nrHi generate.*sh .i3/
2015-07-28 16:22:55
User: deterenkelt
Functions: grep
-3

Performs a case-insensitive (-i) recursive search (-r) and prints not only the match, but also the filename (-H) in which the match was found and corresponding line number (-n). All this is highlighted with different colours.

Requires GNU grep (v.2.21 was used for this example)

pyt() { id=$(curl -s 'https://www.youtube.com/results?search_query='$(tr \ + <<<"$1") | grep -om3 '"[[:alnum:]]\{11\}"' | awk NR==3 | tr -d \"); youtube-dl -q 'https://www.youtube.com/watch?v='"$id" -o - | mplayer -vo null /dev/fd/3 3<&0 </dev/tty; }
2015-07-20 05:30:27
User: snipertyler
Functions: awk grep tr
4

pyt 'Stairway to heaven - Led Zeppelin'

pyt 'brain damage - Pink Floyd'

No web browser or even X needed. Just a cli and internet connection!

mplayer is pauseable and can skip ahead

This may break if youtube changes their search html.

grep -v -e '^$' -e '^[#\[]' -e '\/' some_file
2015-07-17 21:28:14
User: abhikeny
Functions: grep
Tags: grep no-match
0

Grep for:

>> non-empty lines : denoted by -v -e '^$'

>> that do not start with # (comments) or : denoted by -v -e '^[#\]'

>> and does not include : denoted by -v -e '\'

(Note that we use -v only once)

diff a.txt b.txt | grep -E '^(<|>)' | sed 's:^< \(.*\):<del style="color\:red; text-decoration\: none">- \1</del><br>:' | sed 's:^> \(.*\):<ins style="color\:green; text-decoration\: none">+ \1</ins><br>:'
find <path> |xargs grep <pattern>
grep MemTotal: /proc/meminfo # display how much memory installed
2015-05-15 09:19:02
User: mpb
Functions: grep
0

This will display the system memory size in kb. If you want to see the value in mb, you can type:

grep MemTotal: /proc/meminfo | awk '{printf("MemTotal: %d MB\n",$2/1024)}'

showip() { nmcli connection show $1|grep ipv4.addresses|awk '{print $2}' ; }
2015-05-13 16:24:28
User: nnsense
Functions: awk grep
1

Sometimes it's useful to output just the ip address. Or some other information, changing the "ipv4.addresses" in command. The power of awk! Show all possible "greps" with

nmcli connection show [yourInterfaceNameHere]
grep page.php /var/log/httpd/access_log|awk '{print $1}'|sort|uniq|perl -e 'while (<STDIN>){chomp; $cmd=`ipset add banned -! -q $_`; }'
mosth() { history | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n10; }
2015-05-11 17:41:55
User: nnsense
Functions: awk column grep head nl sort
0

I copied this (let's be honest) somewhere on internet and I just made it as a function ready to be used as alias. It shows the 10 most used commands from history. This seems to be just another "most used commands from history", but hey.. this is a function!!! :D

fileinfo() { RPMQF=$(rpm -qf $1); RPMQL=$(rpm -ql $RPMQF);echo "man page:";whatis $(basename $1); echo "Services:"; echo -e "$RPMQL\n"|grep -P "\.service";echo "Config files:";rpm -qc $RPMQF;echo "Provided by:" $RPMQF; }
2015-05-11 16:46:01
User: nnsense
Functions: basename echo grep rpm whatis
2

Many times I give the same commands in loop to find informations about a file. I use this as an alias to summarize that informations in a single command. Now with variables! :D

qf2s() { rpm -ql $(rpm -qf $1)|grep -P "\.service"; }
2015-05-11 16:32:16
User: nnsense
Functions: grep rpm
1

I use this as an alias to get all .service files related a single installed file/conf (if it has services, of course).

For rpm based systems ;)

curl -s https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/ | grep -o '[^"]*Linux/7/pdf[^"]*' | xargs -I{} wget https://access.redhat.com{}
2015-05-11 11:57:20
User: SuperFly
Functions: grep wget xargs
0

Let's give Flatcap credit for this elegant solution, instead of leaving it hidden as a comment.

Tested on RHEL6 and it works. Nice and clean.

wget -q -O- https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/ | grep Linux/7/pdf | cut -d \" -f 2 | awk '{print "https://access.redhat.com"$1}' | xargs wget
find . -path "*/any_depth/*" -exec grep "needle" {} +
for a in $(ls /usr/sbin /usr/bin); do ps -fC $a;done|grep -v PPID
2015-04-27 18:15:56
User: knoppix5
Functions: grep ls ps
-2

Thanks to pooderbill for the idea :-)

input=a.pdf ; pages=`pdftk $input dump_data | grep -i numberofpages | cut -d" " -f 2`; pdftk A=$input shuffle A1-$[$pages/2] A$pages-$[$pages/2+1] output "${input%.*}.rearranged.${input##*.}"
2015-04-26 20:05:20
User: kobayashison
Functions: cut grep
0

Rearrange pdf document coming from a simplex document feed scanner, feeded first with odd pages, then with even pages from the end. Needs pdftk >1.44 w/ shuffle.

Similar to http://www.commandlinefu.com/commands/view/7965/pdf-simplex-to-duplex-merge where there are 2 separate documents, odd and even

timeDNS() { parallel -j0 --tag dig @{} "$*" ::: 208.67.222.222 208.67.220.220 198.153.192.1 198.153.194.1 156.154.70.1 156.154.71.1 8.8.8.8 8.8.4.4 | grep Query | sort -nk5; }
du -sc .[!.]* * |grep '^[0-9]{4}'
2015-04-24 10:51:13
User: gander
Functions: du grep
0

thanks to GREP_COLOR the output will highlite the first 4 digits. if all files are few MB only, this gives a quick overview of how many powers of 10 bigger than 1MB they really are, a logarithmic scale. same works if files are more than 1GB when you replace the "4" by a "7", I usually use "5" in order to manually decide what files to delete...

netstat -anp | grep :80 | grep ESTABLISHED | wc -l
2015-04-10 19:32:31
User: krizzo
Functions: grep netstat wc
Tags: session
-2

This counts all established sessions on port 80. You can change :80 to any port number you want to check.

sudo lsof -i -n | grep sshd | grep sshuser | grep :[PORT-RANGE] | grep -v IPv6 | awk -F\: '{print $2}' | grep -v http | awk -F" " '{print $1}'
2015-04-09 15:41:11
User: das_shark
Functions: awk grep sshd sudo
-2

gets network ports

only ones for the sshd service

only logged in a specific user (changed for public posting)

only in a specific localhost:port range

not IPv6

Only the part of the response after the ":" character

Only the part of the response before the 1st space

Output is just the rssh port

debugfs -R "stat <$(stat --printf=%i filename)>" /dev/sdaX | grep crtime
2015-04-09 01:23:56
User: pggx999
Functions: debugfs grep
3

Return the creation date of a file on ext2, 3, 4 filesystems, because stat command won't show it.

Useful on ubuntu, debian, and else

xset -display :0 q | grep ' Monitor is On' > /dev/null && xset -display :0 dpms force off || xset -display :0 dpms force on
2015-04-06 19:04:04
User: electrotux
Functions: grep
0

Queries whether the monitor is on according to DPMS. If true then turns the monitor off, if false turns it on. The -display option on xset means the command will work from sessions other than the console, such as ssh or a cron'd script. Command should display any errors if there are any problems (eg no X available), otherwise no output if successful.