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 alperyilmaz from sorted by
Terminal - Commands by alperyilmaz - 22 results
(ulimit -v 1000000; scriptname)
2011-01-27 21:30:59
User: alperyilmaz
Functions: ulimit
Tags: ulimit
2

When I'm testing some scripts or programs, they end up using more memory than anticipated. In that case, computer nearly halts due to swap space usage, and sometimes I have to press Magic SysRq+REISUB to reboot.

So, I was looking for a way to limit memory usage per script and found out that ulimit can limit memory. If you run it this way:

$ ulimit -v 1000000

.

$ scriptname

Then the new memory limit will be valid for that shell. I think changing the limit within a subshell is much more flexible and it won't interfere with your current shell ulimit settings.

note: -v 1000000 corresponds to approximately 1GB of RAM

echo <percentage> | sudo dd of=/proc/acpi/video/VGA/LCD/brightness
2011-01-05 03:57:58
User: alperyilmaz
Functions: dd echo sudo
Tags: dd echo
2

An alternative which does not require to be root

tar --transform 's#.*/\([^/]*\)$#\1#' -xzvf test-archive.tar.gz
2010-11-29 23:16:57
User: alperyilmaz
Functions: tar
Tags: tar
1

If you want to decompress the files from an archive to current directory by stripping all directory paths, use --transform option to strip path information. Unfortunately, --strip-components option is good if the target files have same and constant depth of folders.

The idea was taken from http://www.unix.com/solaris/145941-how-extract-files-tar-file-without-creating-directories.html

paste --serial file1 file2 file3
style TEXT-FILE
2010-10-27 08:07:04
User: alperyilmaz
Tags: style diction
2

Style analyses the surface characteristics of the writing style of a document. It prints various readability grades, length of words, sentences and paragraphs.

It can further locate sentences with certain characteristics. If no files are given, the document is read from standard input.

style is part of "diction" package

echo rm *.txt
2010-10-27 07:26:26
User: alperyilmaz
Functions: echo rm
11

if you're using wildcards * or ? in your command, and if you're deleting, moving multiple files, it's always safe to see how those wildcards will expand. if you put "echo" in front of your command, the expanded form of your command will be printed. It's better safe than sorry.

cut -f5 --complement
2010-10-21 20:21:07
User: alperyilmaz
Functions: cut
Tags: cut
22

Show all columns except 5th. This might help you save some typing if you are trying to exclude some columns from the output.

history | perl -F"\||<\(|;|\`|\\$\(" -alne 'foreach (@F) { print $1 if /\b((?!do)[a-z]+)\b/i }' | sort | uniq -c | sort -nr | head
2010-04-08 13:46:09
User: alperyilmaz
Functions: perl sort uniq
4

Most of the "most used commands" approaches does not consider pipes and other complexities.

This approach considers pipes, process substitution by backticks or $() and multiple commands separated by ;

Perl regular expression breaks up each line using | or < ( or ; or ` or $( and picks the first word (excluding "do" in case of for loops)

note: if you are using lots of perl one-liners, the perl commands will be counted as well in this approach, since semicolon is used as a separator

perl -MStatistics::Descriptive -alne 'my $stat = Statistics::Descriptive::Full->new; $stat->add_data(@F[1..4]); print $stat->variance' filename
2010-04-02 21:16:12
User: alperyilmaz
Functions: perl
1

In this example, file contains five columns where first column is text. Variance is calculated for columns 2 - 5 by using perl module Statistics::Descriptive. There are many more statistical functions available in the module.

speaker-test -D plug:surround51 -c 6 -l 1 -t wav
2009-11-05 02:57:46
User: alperyilmaz
5

Useful tool to test if all speaker channels are working properly. speaker-test is part of alsa-utils package

for f in *.html; do sed '$d' -i "$f"; done
2009-10-12 14:46:43
User: alperyilmaz
Functions: sed
1

sed can be used deleting the last line and with -i option, there's no need to for temp files, the change is made on the actual file

wget -q -O- http://www.gutenberg.org/dirs/etext96/cprfd10.txt | sed '1,419d' | tr "\n" " " | tr " " "\n" | perl -lpe 's/\W//g;$_=lc($_)' | grep "^[a-z]" | awk 'length > 1' | sort | uniq -c | awk '{print $2"\t"$1}'
2009-05-04 16:00:39
User: alperyilmaz
Functions: awk grep perl sed sort tr uniq wget
-4

This command might not be useful for most of us, I just wanted to share it to show power of command line.

Download simple text version of novel David Copperfield from Poject Gutenberg and then generate a single column of words after which occurences of each word is counted by sort | uniq -c combination.

This command removes numbers and single characters from count. I'm sure you can write a shorter version.

perror NUMBER
2009-03-31 19:19:44
User: alperyilmaz
1

perror should be installed if mysql-server package is installed

awk '{print > $3".txt"}' FILENAME
2009-03-31 15:14:13
User: alperyilmaz
Functions: awk
2

This command will sort the contents of FILENAME by redirecting the output to individual .txt files in which 3rd column will be used for sorting. If FILENAME contents are as follows:

foo foo A foo

bar bar B bar

lorem ipsum A lorem

Then two files called A.txt and B.txt will be created and their contents will be:

A.txt

foo foo A foo

lorem ipsum A lorem

and B.txt will be

bar bar B bar

find ~/.thumbnails/ -type f -atime +30 -print0 | xargs -0 rm
2009-03-30 04:23:07
User: alperyilmaz
Functions: find xargs
1

By time thumbnail images in ~/thumbnails take up too much space, this command will help deleting old ones.

Find options explained:

-type f : find files only, not directories

-atime +30 : last accessed more than 30 days ago

xclip -o > /tmp/spell.tmp; aspell check /tmp/spell.tmp ; cat /tmp/spell.tmp | xclip
2009-03-26 00:49:59
User: alperyilmaz
Functions: cat
2

xclip -o > /tmp/spell.tmp # Copy clipboard contents to a temp file

aspell check /tmp/spell.tmp # Run aspell on that file

cat /tmp/spell.tmp | xclip # Copy the results back to the clipboard, so that you can paste the corrected text

I'm not sure xclip is installed in most distributions. If not, you can install x11-apps package

swapoff -a ; swapon -a
2009-03-25 03:30:41
User: alperyilmaz
Functions: swapoff swapon
Tags: swapoff swapon
10

When you run a memory intensive application (VirtualBox, large java application, etc) swap area is used as soon as memory becomes insufficient. After you close the program, the data in swap is not put back on memory and that decreases the responsiveness. Swapoff disables the swap area and forces system to put swap data be placed in memory. Since running without a swap area might be detrimental, swapon should be used to activate swap again.

Both swapoff and swapon require root privileges.

sed 's/\b\(0*\)//g' filename
2009-03-24 20:19:42
User: alperyilmaz
Functions: sed
Tags: sed
1

Leading zeros might help correct sorting and they can be removed by sed after sorting

sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta' filename
2009-03-24 20:06:02
User: alperyilmaz
Functions: sed
Tags: sed
3

Does not necessarily require a file to process, it can be used in a pipe as well:

cat filename | sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'

I don't remember where I copy/pasted this from, I wish I credited the original author

mysql DATABASE -N -s -r -e 'SQL COMMAND'
2009-03-24 19:53:46
User: alperyilmaz
Tags: mysql
5

-N removes header

-s removes separator chars

-r raw output

After using these options, the MySQL ouptut can be used with pipes very easily

cat -v -t -e
2009-03-24 19:29:03
User: alperyilmaz
Functions: cat
Tags: cat
4

Useful to detect number of tabs in an empty line, DOS newline (carriage return + newline).

A tool that can help you understand why your parsing is not working.

echo -e "[client]\nuser = YOURUSERNAME\npassword = YOURPASSWORD" > ~/.my.cnf
2009-03-24 19:05:39
User: alperyilmaz
Functions: echo
Tags: mysql bash
0

The file .my.cnf located at user's home directory is used for mysql login. If this file exists, then

mysql -uYOURUSERNAME -pYOURPASSWORD database -e 'SOME SQL COMMAND'

can be replaced with

mysql database -e 'SOME SQL COMMAND'

It saves you from typing!

This is valid for mysqladmin and mysqldump commands as well.