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.

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

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

All commands from sorted by
Terminal - All commands - 11,951 results
awk '{ total += gsub(/yourstring/,"") } END { print total }' yourfile
2014-12-16 21:00:45
User: bugmenot
Functions: awk
0

Count how many times a pattern is present into a file. It can be one or more lines. No overlapping. It means searching for aa on aaa will output 1 not 2.

echo something | awk '{ total += gsub(/yourstring/,"") } END { print total }'
2014-12-16 20:58:42
User: bugmenot
Functions: awk echo
0

Count how many times a pattern is present into a stream. It can be one or more lines. No overlapping. It means searching for aa on aaa will output 1 not 2.

cd -P .
curl -s "http://smacie.com/randomizer/borat.txt" | shuf -n 1 -
2014-12-16 04:18:48
User: benjabean1
2

Turns out smacie.com has a text file containing every single one of the borat quotes, each one on a newline. This makes it very convenient, as this can be done without any sed-parsing, and uses less bandwitdth!

Note that borate quotes are quite offensive, much more so than "fortunes-off"!

ls -l | head -n 65535 | awk '{if (NR > 1) total += $5} END {print total/(1024*1024*1024)}'
rsync -v --ignore-existing `ls | head -n 40` root@localhost:/location
scp -r `ls | grep -vE "(Pattern1|Pattern2)"` user@remote_host:/location
apt-cache pkgnames linux-
2014-12-14 06:48:57
User: benjabean1
Functions: apt
1

In this case, linux- is the prefix; simply running

apt-cache pkgnames

would list every package APT knows about.

The default APT config assumes -g, --generate; to use the cache as/is, you could similarly run:

apt-cache --no-generate pkgnames [prefix]

Adding --all-names, like so:

apt-cache --no-generate --all-names pkgnames [prefix]

would print all the packages APT knows about, using the cache as/is, including virtual packages and missing dependencies.

This command was shamelessly stolen from the apt-cache(8) man-page.

for USER in /var/spool/cron/*; do echo "--- crontab for $USER ---"; cat "$USER"; done
2014-12-11 19:48:46
User: tyzbit
Functions: cat echo
0

This is not exhaustive but after checking /etc/cron* is a good way to see if there are any other jobs any users may have set.

Note: this is a repost from a comment "flatcap" made on http://www.commandlinefu.com/commands/view/3726/print-crontab-entries-for-all-the-users-that-actually-have-a-crontab#comment, for which I am grateful and I take no credit.

strings /dev/urandom | tr -cd '[:alnum:]' | fold -w 30 | head -n 1
2014-12-11 06:21:51
User: atoponce
Functions: fold head strings tr
1

This command is similar to the alternate, except with head(1), you can pick as many passwords as you wish to generate by changing the number of lines you wish to preview.

ps axo pcpu,args | awk '/[p]hp.*pool/ { sums[$4] += $1 } END { for (pool in sums) { print sums[pool], pool } }' | sort -rn | column -t
find . -printf '%.5m %10M %#9u %-9g %TY-%Tm-%Td+%Tr [%Y] %s %p\n'|sort -nrk8|head
rpm -e --allmatches gpg-pubkey-1aa043b8-53b2e946
2014-12-09 21:27:08
User: krizzo
Functions: rpm
Tags: GPG rpm yum pubkey
0

This will remove the gpg-pubkey-1aa043b8-53b2e946 from rpm/yum and you'll be prompted to add it back from the given repo.

rpm -qa gpg-pubkey --qf "%{version}-%{release} %{summary}\n"
2014-12-09 21:23:28
User: krizzo
Functions: rpm
Tags: GPG rpm yum pubkey
0

This will list all the gpg keys that were accepted and installed for yum.

du -hs * |egrep -i "^(\s?\d+\.?\d+G)"
2014-12-09 15:23:21
User: krizzo
Functions: du egrep
Tags: size sort du
0

This will list all the files that are a gigabyte or larger in the current working directory. Change the G in the regex to be a M and you'll find all files that are a megabyte up to but not including a gigabyte.

x="() { :; }; echo x" bash -c :
2014-12-08 22:21:18
User: malathion
Functions: bash
Tags: Security bash
1

If this command prints 'x' then your shell is vulnerable. Null output confirms that you are protected. Further reading: http://allanmcrae.com/2014/09/shellshock-and-arch-linux/

while sleep 1; do if [ $(echo "$(cat /proc/loadavg | cut -d' ' -f1) > .8 " | bc) -gt 0 ]; then echo -e "\n\a"$(date)" \e[5m"$(cat /proc/loadavg)"\e[0m"; ps aux --sort=-%cpu|head -n 5; fi; done
2014-12-08 15:44:40
User: tyzbit
Functions: cat echo head ps sleep
0

This checks the system load every second and if it's over a certain threshold (.8 in this example), it spits out the date, system loads and top 4 processes sorted by CPU.

Additionally, the \a in the first echo creates an audible bell.

(echo n; echo p; echo 3; echo 1; echo; echo; echo w; echo q) | /sbin/fdisk /dev/sda
sudo netstat -tulpn | grep :8080
mv -iv $FILENAME{,.$(stat -c %Z $FILENAME)}
2014-12-02 13:47:52
User: bunam
Functions: mv stat
Tags: mv
3

FILENAME=nohup.out

mv -iv $FILENAME{,.$(stat -c %Y $FILENAME)}

does it help ?

mv -iv $FILENAME{,.$(stat -c %y $FILENAME | awk '{print $1}')}
2014-12-01 22:41:38
User: pdxdoughnut
Functions: awk mv stat
Tags: mv
3

Note that the -i will not help in a script. Proper error checking is required.

find /srv/code -maxdepth 4 -type f -regex ".*\(\(package\|composer|npm\\|bower\)\.json\|Gemfile\|requirements\.txt\\|\.gitmodules\)"
2014-11-28 16:34:35
User: renoirb
Functions: find
Tags: bash git PHP ruby
0

List all dependencies manifests so you can install them.

In a scenario where you want to deploy a number of web applications and run their dependency managers, how could you run all of them in a systematic order.

One of the complexity is to ensure you get only your own top level dependencies. That way, you don recursively call development dependencies of your own dependencies.

Otherwise you might end up discovering dependency management manifests that are already been pulled by your own projects.

# Using this command

This command helps me find them and I can then run what?s required to pull them from their respective sources.

This command assumes the following:

1. Your code checkouts are in a flat repository layout (i.e. not nested).

2. Finds manifests for:

- NPM (nodejs),

- Composer (php),

- bower,

- requirements.txt (Python), and

- git submodules

find . -type f -name "*\?*" | while read f;do mv "$f" "${f//[^0-9A-Za-z.\/\(\)\ ]/_}";done
2014-11-28 14:55:27
User: miccaman
Functions: find mv read
Tags: bash find mv
1

replace all "?" characters in filename to underscore

find . -type d -name "*\?*" | while read f;do mv "$f" "${f//[^0-9A-Za-z.\/\(\)\ ]/_}";done
2014-11-28 14:52:46
User: miccaman
Functions: find mv read
Tags: bash find mv
0

rename all dirs with "?" char in name, leave spaces and () in place

( ps -U nms -o pid,nlwp,cmd:500 | sort -n -k2) && (ps h -U nms -o nlwp | paste -sd+ | bc)