Commands tagged grep (408) the last day the last week the last month all time sorted by date votes

• 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
• 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
• Tail is much faster than sed, awk because it doesn't check for regular expressions. Show Sample Output

-5
tail -n +<N> <file> | head -n 1
· 2011-09-30 08:30:30

• -6
grep -A 3 -i "example" demo_text
· 2013-05-09 08:20:31
• I know how hard it is to find an old command running through all the files because you couldn't remember for your life what it was. Heres the solution!! Grep the history for it. depending on how old the command you can head or tail or if you wanted to search all because you cannot think how long ago it was then miss out the middle part of the command. This is a very easy and effective way to find that command you are looking for.

-9
cat .bash_history | tail -100 | grep {command}
· 2013-04-10 10:40:52
• Normally, if you just want to see directories you'd use brianmuckian's command 'ls -d *\', but I ran into problems trying to use that command in my script because there are often multiple directories per line. If you need to script something with directories and want to guarantee that there is only one entry per line, this is the fastest way i know Show Sample Output

-10
ls -l | grep ^d | sed 's:.*\ ::g'
· 2011-08-06 23:52:46

• -10
grep -r <searchterm> *
· 2012-02-08 11:48:08
• David thanks for that grep inside! here is mine version: psgrep() { case \${1} in ( -E | -e ) local EXTENDED_REGEXP=1 shift 1 ;; *) local EXTENDED_REGEXP=0 ;; esac if [[ -z \${*} ]] then echo "psgrep - grep for process(es) by keyword" >&2 echo "Usage: psgrep [-E|-e] ... " >&2 echo "" >&2 echo "option [-E|-e] enables full extended regexp support" >&2 echo "without [-E|-e] plain strings are looked for" >&2 return 1 fi \ps -eo 'user,pid,pcpu,command' w | head -n1 local ARG='' if (( \${EXTENDED_REGEXP} == 0 )) then while (( \${#} > 0 )) do ARG="\${1}" shift 1 local STRING=\${ARG} local LENGTH=\$(expr length \${STRING}) local FIRSCHAR=\$(echo \$(expr substr \${STRING} 1 1)) local REST=\$(echo \$(expr substr \${STRING} 2 \${LENGTH})) \ps -eo 'user,pid,pcpu,command' w | grep "[\${FIRSCHAR}]\${REST}" done else \ps -eo 'user,pid,pcpu,command' w | grep -iE "(\${*})" fi }

-10
psgrep() ... func to long, please look under "description"
· 2015-01-01 02:58:48
• ‹ First  < 15 16 17

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

Take a screenshot every 2 seconds
Take a screenshot every 2 seconds and save it as a png file

encode image to base64 and copy to clipboard
I use it for embedding images in CSS for Stylish, the Firefox addon. Thought it might be useful to others.

Testing php configuration
Run the function phpinfo() on the shell

Displays the attempted user name, ip address, and time of SSH failed logins on Debian machines
A variation of a script I found on this site and then slimmed down to just use awk. It displays all users who have attempted to login to the box and failed using SSH. Pipe it to the sort command to see which usernames have the most failed logins.

Using mplayer to play the audio only but suppress the video
Sometimes you only want to linsten the audio while output the video will be a waste of CPU resource and an annoying window. With option -vo null, you will enjoy the audio!

check open ports without netstat or lsof

get a process list by listen port

Make sure your script runs with a minimum Bash version
If you use new features of a certain Bash version in your shell script, make sure that it actually runs with the required version.

Get AWS temporary credentials ready to export based on a MFA virtual appliance
You might want to secure your AWS operations requiring to use a MFA token. But then to use API or tools, you need to pass credentials generated with a MFA token. This commands asks you for the MFA code and retrieves these credentials using AWS Cli. To print the exports, you can use: `awk '{ print "export AWS_ACCESS_KEY_ID=\"" \$1 "\"\n" "export AWS_SECRET_ACCESS_KEY=\"" \$2 "\"\n" "export AWS_SESSION_TOKEN=\"" \$3 "\"" }'` You must adapt the command line to include: * \$MFA_IDis ARN of the virtual MFA or serial number of the physical one * TTL for the credentials

Trace python statement execution and syscalls invoked during that simultaneously