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

Commands by b_t from sorted by
Terminal - Commands by b_t - 21 results
pacman -Qdt -q | xargs pacman --noconfirm -R
2014-02-27 05:17:57
User: b_t
Functions: xargs
1

-Qdt Lists dependencies/packages which are no longer required by any packages

-q Output only package name (not the version number)

-R Remove package(s)

Rest is self-explanatory.

I just started out with Arch - so if there is any better/standard method to achieve the same - please suggest.

netstat -tup -W | column -t
2014-01-08 22:39:01
User: b_t
Functions: column netstat
3

The -W switch of netstat makes it print complete URL of the connections, which otherwise by default

is truncated to fit its default column size.

Now to compensate for irregular column sizes, pipe the output to column (-t switch of column prints in tabular form). The only downside to this part is that the very first row, the header, goes pear shape.

sort -M filename
2011-12-10 12:50:30
User: b_t
Functions: sort
Tags: sort
2

sort command can sort month-wise (first three letters of each month).

See the sample output for clarification.

Sorting Stable ? NO. Take note if that matters to you.

Sample output suggests that sort performs unstable

sorting (see the relative order of two 'feb' entries).

$ history -a #in one shell , and $ history -r #in another running shell
2011-11-05 01:19:30
User: b_t
Tags: history bash
10

By default bash history of a shell is appended (appended on Ubuntu by default: Look for 'shopt -s histappend' in ~/.bashrc) to history file only after that shell exits.

Although after having written to the history file, other running shells do *not* inherit

that history - only newly launched shells do.

This pair of commands alleviate that.

function whichpkg() { readlink -f "$(which $1)" | xargs --no-run-if-empty dpkg -S; }
2011-10-28 02:53:19
User: b_t
Functions: readlink xargs
6

This revision to my command (command #8851) was called for when it failed to find the parent

package of 'rlogin', which is really a deep symbolic link to /usr/bin/ssh.

This revision fixes this newfound issue, while ensuring fixes of other older issues work too.

shopt -s histverify
2011-10-27 00:33:34
User: b_t
Tags: history bash
13

Bash history commands are those that begin with the character !

(eg. the most popular 'sudo !!' Explained here => http://www.commandlinefu.com/commands/view/13).

By default bash immediately executes the history command.

Setting this shell option will make bash first allow you to verify/edit an

history command before executing it.

To set this option permanently, put this command in ~/.profile or ~/.bashrc file.

To unset this option issue following command.

shopt -u histverify
whichpkg () { dpkg -S $1 | egrep -w $(readlink -f "$(which $1)")$; }
2011-07-17 13:39:56
User: b_t
Functions: egrep readlink
Tags: dpkg readlink
1

Advanced revision to the command 8776 . This revision follows symbolic links.

The quotation-marks surrounding $(which $1) allows for graceful handling of errors ( ie. readlink does not complain incase 'which' command generates (null) output)

function summpkg { dpkg -s $(dpkg -S $1 | egrep -w $(which $1)$ | awk -F: '{print $1}') ; }
2011-07-05 23:06:37
User: b_t
Functions: awk egrep which
Tags: which dpkg
0

This version builds on my command 8776 (Find the package a command belongs to on debian-based distros). So if you use that command to find package name then you could alternatively use following for

package summary:

function summpkg { dpkg -s $(whichpkg $1 | awk -F: '{print $1}'); }
function whichpkg { dpkg -S $1 | egrep -w $(which $1)$; }
curl -s http://example.com | grep -o -P "<a.*href.*>" | grep -o "http.*.pdf" | xargs -d"\n" -n1 wget -c
2011-06-09 14:42:46
User: b_t
Functions: grep wget xargs
0

This example command fetches 'example.com' webpage and then fetches+saves all PDF files listed (linked to) on that webpage.

[*Note: of course there are no PDFs on example.com. This is just an example]

make -d | egrep --color -i '(considering|older|newer|remake)'
2011-06-03 01:55:08
User: b_t
Functions: egrep make
Tags: make
1

Say your dependencies specified in your Makefile (or dates on your source files) is causing 'make' to

skip some source-files (that it should not) or on the other other end, if it is causing make to always build some source-files regardless of dates of target, then above command is handy to find out what 'make' thinks of your date v/s target date-wise or what dependencies are in make's view-point.

The egrep part removes the extra noise, that you might want to avoid.

git push origin :featureless
2011-06-02 14:53:31
User: b_t
Tags: git
-1

This will delete the branch 'featureless' on the origin remote.

Do not forget to delete the branch locally using:

git branch -d featureless

'I got it here'-credit:

http://gitready.com/beginner/2009/02/02/push-and-delete-branches.html

I duplicated here incase you stumbled here first.

ls | grep '^[A-Z0-9]*$'
2010-12-19 21:45:53
User: b_t
Functions: grep ls
0

Some source package have many 'README' kind of files, among many other regular files/directories. This command could be useful when one wants to list only 'README' kind of files among jungle of other files. (e.g. I came across this situation after downloading source for module-init-tools)

Warning: This command would miss a file like => README.1 (or one with spaces in-between)

Corrections welcome.

kill -l
2010-11-17 23:48:51
User: b_t
Functions: kill
6

This command seems to achieve the similar/same goal.

history | awk '{print $2,$3}' | sed s/sudo// | awk '{print $1}' | awk 'BEGIN {FS="|"}{print $1}' | sort | uniq -c | sort -n | tail | sort -nr
2010-11-17 12:15:04
User: b_t
Functions: awk sed sort tail uniq
0

Your version works fine except for someone who's interested in commands 'sudo' was prefixed to

i.e. in your command, use of sudo appears as number of times sudo was used.

Slight variation in my command peeks into what commands sudo was used for and counts the command

(ignores 'sudo')

lst=`dpkg -L iptables` ; for f in $lst; do if [ -x $f ] && [ ! -d $f ] ; then echo $f; fi; done;
2010-10-30 14:47:45
User: b_t
Functions: echo
1

I wanted to view only executables installed by a package. This seemed to work.

There's got to be easier way, please share.

Note:

(1) Replace iptables with the package name of your interest

(2) The command will trash any existing environment variable named 'lst'

(3) Instead if you are interested in viewing just .ko or .so files installed by this package, then

that would be easy:

$ dpkg -L iptables | grep "\.[sk]o$"

wn wonder -over
2010-10-05 13:56:06
User: b_t
Tags: dictionary
2

Note:

1) Replace 'wonder' with any word you looking the meaning for in the above example

2) Need to install these packages:

wordnet & wordnet-base (latter should be automatically installed because of dependency)

3) Combined size of packages is about 30MB on my old ubuntu system (I find it worth it)

gksudo gedit /etc/passwd &
2010-10-05 13:11:04
User: b_t
1

Need package: gksu

Note:

Launching gui app in background that needs sudo, won't work great with our old

friendly style of launching:

sudo gedit /etc/passwd &

because this would put sudo in background !

Using gksudo as demonstrated, would popup a gui sudo window.

May be this is a common knowledge, but not knowing this

frustrated me during my newbie year.

sudo -K
2010-10-05 12:44:26
User: b_t
Functions: sudo
16

By default sudo 'remembers' password for a few minutes, so that you do not need to re-enter password for a series of sudo commands that might follow within a short time duration.

However, sometime you might want sudo to instantly 'forget' the password.

(Next sudo command will need you to reenter the password)

Credit: I first learned this while listening to one of the 'tuxradar' podcast.

watch -n 900 "notify-send -t 10000 'Look away. Rest your eyes'"
2010-10-05 09:39:31
User: b_t
Functions: watch
0

Note:

1) -n option of watch accepts seconds

2) -t option of notify-send accepts milliseconds

3) All quotes stated in the given example are required if notification

message is more than a word.

4) I couldn't get this to run in background (use of & at the end fails). Any

suggestions/improvements welcome.

file /bin/* | msort -j -l -n-1 -n2 2> /dev/null
2010-10-05 00:37:33
User: b_t
Functions: file
Tags: sort msort
4

Notes:

1) -n-1 means sort key is the last field

2) -l is important if each separate record is on a new line (usually so for text files)

3) -j tells msort not to create log file (msort.log) in the working directory

4) may need to install msort package.

5) msort does lot more. Check man msort