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

• -4
aptitude purge linux-image | grep ^i | grep -v \$(uname -r)
· 2010-06-11 22:20:42
• Of course, the httpd can be replaced with any other process name Show Sample Output

-4
top -b -n 1 |grep httpd|wc -l
· 2010-07-25 08:40:03

• -4
a=`curl http://xkcd.com 2>/dev/null | grep -iE 'src=.*imgs.xkcd.com/comics/'`; b=`echo \${a#*src=\"}`; eog \${b%%\"*}
· 2010-08-27 11:06:14
• Suppose you have 11 marbles, 4 of which are red, the rest being blue. The marbles are indistinguishable, apart from colour. How many different ways are there to arrange the marbles in a line? And how many ways are there to arrange them so that no two red marbles are adjacent? There are simple mathematical solutions to these questions, but it's also possible to generate and count all possibilities directly on the command line, using little more than brace expansion, grep and wc! The answer to the question posed above is that there are 330 ways of arranging the marbles in a line, 70 of which have no two red marbles adjacent. See the sample output. To follow the call to marbles 11 4: after c=''; for i in \$(seq \$1); do c+='{b,r}'; done;, \$c equals {b,r}{b,r}{b,r}{b,r}{b,r}{b,r}{b,r}{b,r}{b,r}{b,r}{b,r} After x=\$(eval echo \$c), and brace expansion, \$x equals bbbbbbbbbbb bbbbbbbbbbr ... rrrrrrrrrrb rrrrrrrrrrr, which is all 2^11 = 2048 strings of 11 b's and r's. After p=''; for i in \$(seq \$2); do p+='b*r'; done;, \$p equals b*rb*rb*rb*r Next, after y=\$(grep -wo "\${p}b*" Finally, grep -vc 'rr' Show Sample Output

-4
marbles () { c=''; for i in \$(seq \$1); do c+='{b,r}'; done; x=\$(eval echo \$c); p=''; for i in \$(seq \$2); do p+='b*r'; done; y=\$(grep -wo "\${p}b*" <<< \$x); wc -l <<< "\$y"; grep -vc 'rr' <<< "\$y"; }
· 2010-08-27 23:04:33

• -4
grep --exclude-dir=.svn --exclude=*~ -i "TODO" -rl .
· 2011-04-06 20:25:41
• if Argument list too long

-4
echo **/* | xargs grep 'hoge'
· 2011-04-12 13:18:41

• -4
grep -r hoge .
· 2011-04-12 17:39:52

• -4
while sleep 1; do date; (netstat -a -n | grep 80) ; done
· 2011-05-16 07:56:56

• -4
grep query -r . --exclude-dir=.svn
· 2011-09-24 23:20:06
• Every 20 minutes test if host with IP 192.168.0.14 is 'dead' or not reachable. The line should be put in your crontab file.

-4
10,30,50 * * * * ping -q -c1 -w3 192.168.0.14 | grep '1 received' - || mail -ne -s'Host 192.168.0.14 not reachable' admin@example.com
· 2012-02-06 10:42:46
• # define user pid to kill PID=httpd ; # kill all pids ps aux | grep \$PID | grep -v grep | awk '{print \$2}' | xargs kill -9 Show Sample Output

-4
PID=httpd ; ps aux | grep \$PID | grep -v grep | awk '{print \$2}' | xargs kill -9
· 2012-02-21 23:27:47
• Get the first IPv4 address of an interface Show Sample Output

-4
ip a s eth0 | grep "inet " | head -n 1 | awk '{print \$2}' | cut -f1 -d'/'
· 2012-07-16 07:54:43
• Oneliner to get domain names list of all existing domain names (from wikipedia) Show Sample Output

-4
curl http://en.m.wikipedia.org/wiki/List_of_Internet_top-level_domains | grep "<tr valign=\"top\">" | awk -F">" '{ print \$5 }' | awk -F"<" '{ print \$1 }'
· 2012-12-24 21:00:36
• Did some research and found the previous command wrong, we don't kill a zombie but its parent. Just made some modifcation to khashmeshab's command.

-4
ps -xaw -o state,ppid | grep Z | grep -v PID | awk '{ print \$2 }' | xargs kill -9
· 2013-01-09 04:21:54
• CPU flags: rm --> 16-bit processor (real mode) tm --> 32-bit processor (? mode) lm --> 64-bit processor (long mode)

-4
if [[ lm = \$(cat /proc/cpuinfo | grep " lm ") ]] ; then echo "64 bits" ; else echo "32 bits" ; fi
· 2013-02-11 22:40:46
• I use these command to validate twitter accounts, we can use a "for a in \$(cat list.txt)" to validate a complete list of twitter accounts.

-4
if lynx --dump http://twitter.com/xmuda | grep -q "Sorry, that page does"; then echo "Dont Exist"; else echo "Exist"; fi
· 2013-03-23 16:12:24

• -4
find /etc -exec grep '[0-9][0-9]*[.][0-9][0-9]*[.][0-9][0-9]*[.][0-9][0-9]*' {} \;
· 2013-04-29 10:10:11

• -5
alias pfind='ps aux | grep '
· 2009-02-09 04:13:52

• -5
dd bs=1 count=32 if=/dev/random 2> /dev/null | md5 | grep -o '\w*'
· 2009-02-15 23:22:46
• This command will grep the entire directory looking for any files containing the list of files. This is useful for cleaning out your project of old static files that are no longer in use. Also ignores .svn directories for accurate counts. Replace 'static/images/' with the directory containing the files you want to search for. Show Sample Output

-5
ls -1 static/images/ | while read line; do echo -n \$line' '[; grep -rc \$line *|grep -v ".svn"|cut -d":" -f2|grep -vc 0| tr "\n" -d; echo -n ]; echo ; done
· 2009-03-20 20:33:36

• -5
ps aux | grep 'httpd ' | awk {'print \$2'} | xargs kill -9
· 2009-03-23 16:15:06

• -5
ps ax | grep <processname> | grep -v grep | awk '{print \$1}' | sudo xargs kill -9
· 2009-03-24 19:52:47
• You can use this to loop any command to periodically see the output. `while true; do [YOUR COMMAND HERE]; sleep [# of seconds]; done;` Alternatively, you can put it in a simple shell script to save typing! #/!bin/bash while true; do \$1; sleep \$2; done; `/path/to/script.sh "ifconfig eth0 | grep 'inet addr:'" 60` Show Sample Output

-5
while true; do ifconfig eth0 | grep "inet addr:"; sleep 60; done;
· 2009-04-01 19:29:28
• Ok so it's rellay useless line and I sorry for that, furthermore that's nothing optimized at all... At the beginning I didn't managed by using netstat -p to print out which process was handling that open port 4444, I realize at the end I was not root and security restrictions applied ;p It's nevertheless a (good ?) way to see how ps(tree) works, as it acts exactly the same way by reading in /proc So for a specific port, this line returns the calling command line of every thread that handle the associated socket

-5
p=\$(netstat -nate 2>/dev/null | awk '/LISTEN/ {gsub (/.*:/, "", \$4); if (\$4 == "4444") {print \$8}}'); for i in \$(ls /proc/|grep "^[1-9]"); do [[ \$(ls -l /proc/\$i/fd/|grep socket|sed -e 's|.*\[\(.*\)\]|\1|'|grep \$p) ]] && cat /proc/\$i/cmdline && echo; done
· 2009-04-30 12:39:48
• Just find out the daemon with \$ netstat -atulpe. Then type in his name and he gets the SIGTERM.

-5
kill_daemon() { echo "Daemon?"; read dm; kill -15 \$(netstat -atulpe | grep \$dm | cut -d '/' -f1 | awk '{print \$9}') }; alias kd='kill_daemon
· 2009-05-26 20:39:56
• ‹ First  < 72 73 74 75 76 >  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

Measure, explain and minimize a computer's electrical power consumption
Run this command as root to get enough stats. It works on AMD and Intel machines, including desktops. If ran on a laptop it'll give you suggestions on extending your battery life. You'll need to install PowerTOP if you don't have, via 'apt-get install powertop', etc. To grep the output use: sudo powertop -d | grep ... The many command suggestions PowerTOP gives you alone will increase your command-line fu!

How To Get the Apache Document Root
Grabs the Apache config file (yielded from httpd) and returns the path specified as DocumentRoot.

Check if a domain is available and get the answer in just one line
Returns nothing if the domain exists and 'No match for domain.com' otherwise.

Convert seconds to [DD:][HH:]MM:SS
Converts any number of seconds into days, hours, minutes and seconds. sec2dhms() { declare -i SS="\$1" D=\$(( SS / 86400 )) H=\$(( SS % 86400 / 3600 )) M=\$(( SS % 3600 / 60 )) S=\$(( SS % 60 )) [ "\$D" -gt 0 ] && echo -n "\${D}:" [ "\$H" -gt 0 ] && printf "%02g:" "\$H" printf "%02g:%02g\n" "\$M" "\$S" }

Calculate days on which Friday the 13th occurs (inspired from the work of the user justsomeguy)
Friday is the 5th day of the week, monday is the 1st. Output may be affected by locale.

Which processes are listening on a specific port (e.g. port 80)
swap out "80" for your port of interest. Can use port number or named ports e.g. "http"

lists files and folders in a folder
lists files and folders in a folder with summary.

is today the end of the month?

Find usb device
I often use it to find recently added ou removed device, or using find in /dev, or anything similar. Just run the command, plug the device, and wait to see him and only him

Check if a domain is available and get the answer in just one line
Returns nothing if the domain exists and 'No match for domain.com' otherwise.