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.

Universal configuration monitoring and system of record for IT.

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:



May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!

Top Tags





Commands tagged Text Processing from sorted by
Terminal - Commands tagged Text Processing - 37 results
ls -1 | xargs ruby -e'puts ARGV.shuffle'
perl -ne '$. == 4 && print && exit'
2011-09-05 21:52:16
User: bashrc
Functions: perl

Perl version - just for completeness sake ;)

sed '4!d'
2011-09-05 17:54:57
User: totti
Functions: sed

d --> delete

!d ---> delete others

echo "abcde" | sed 's/./& /g'
sort -R
2011-07-15 15:35:27
User: RyanM
Functions: sort

Randomizes a file. The opposite of sort is sort -R!

awk 'BEGIN{RS="\0"}{gsub(/\n/,"<SOMETEXT>");print}' file.txt
2010-12-12 21:43:22
User: __
Functions: awk

awk version of 7210. Slightly longer, but expanding it to catch blank lines is easier:

awk 'BEGIN{RS="\0"}{gsub(/\n+/,"<SOMETEXT>");print}' file.txt
tr "\n" " " < file
2010-12-08 16:13:54
User: randy909
Functions: tr

Even shorter. Stolen from comment posted by eightmillion.

cat file | tr -d "\n"
2010-12-02 09:22:02
User: uzsolt
Functions: cat file tr

This command deletes the "newline" chars, so its output maybe unusable :)

cat file | tr "\n" " "
2010-12-02 09:21:02
User: uzsolt
Functions: cat file tr

It's works only when you replace '\n' to ONE character.

perl -pe 'eof()||s/\n/<SOMETEXT>/g' file.txt
2010-12-02 01:19:27
User: eightmillion
Functions: perl

This command turns a multi-line file into a single line joined with <SOMETEXT>. To skip blank lines, use:

perl -pe '(eof()||s/^\s*$//)||s/\n/<SOMETEXT>/g' file.txt
find . -name "*noticia*" -name "*jhtm*" -name "*.tpl" -exec grep -li "id=\"col-direita\"" '{}' \; | xargs -n1 mate
figlet gunslinger_
sort -t $'\t' -k 2 input.txt
2010-07-11 12:58:51
User: postrational
Functions: sort

Use this BASH trick to create a variable containing the TAB character and pass it as the argument to sort, join, cut and other commands which don't understand the \t notation.

sort -t $'\t' ... join -t $'\t' ... cut -d $'\t' ...
cut -f 1 three-column.txt > first-column.txt
2010-07-11 10:13:45
User: postrational
Functions: cut

There is a common command for outputting a field or list of fields from each line in a file. Why wouldn't you just use cut?

sed -n '4{p;q}'
tail -n +4 | head -n 1
awk '{print $1}' < three-column.txt > first-column.txt
while read col1 col23; do echo $col1; done < three-column.txt > first-column.txt
while read l; do echo ${l%% *}; done < three-column-list.txt > only-first-column.txt
2010-07-09 03:42:56
User: zed
Functions: echo read

The above is an example of grabbing only the first column. You can define the start and end points specifically by chacater position using the following command:

while read l; do echo ${l:10:40}; done < three-column-list.txt > column-c10-c40.txt

Of course, it doesn't have to be a column, or extraction, it can be replacement

while read l; do echo ${l/foo/bar}; done < list-with-foo.txt > list-with-bar.txt

Read more about parameter expansion here:


Think of this as an alternative to awk or sed for file operations

sed -n '4p'
head -n X | tail -n 1
2010-07-08 22:06:39
User: infinull
Functions: head tail

using tail first won't do it because tail counts from the bottom of the file. You could do it this way but I don't suggest it

awk 'NR==4'
tail -n 4 | head -n 1
2010-07-08 19:50:06
User: puddy
Functions: head tail

tail -n X | head -n 1

prints a specific line, where X is the line number

perl -wl -e '@f=<>; for $i (0 .. $#f) { $r=int rand ($i+1); @f[$i, $r]=@f[$r,$i] if ($i!=$r); } chomp @f; print join $/, @f;' try.txt
random -f <file>