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 tagged replace from sorted by
Terminal - Commands tagged replace - 19 results
echo "text to prepend" | cat - file
2013-12-18 15:54:17
User: leni536
Functions: cat echo
Tags: cat replace
2

Prepend text to a file. It doen't need temporary files, ed or sed.

prepend () { array=("$@"); len=${#array[@]}; file=${array[$len-1]}; text=${array[@]:0:$len-1}; printf '%s\n' 0a "$text" . w | ed -s "$file"; }
2013-12-09 21:59:26
User: zlemini
Functions: ed printf
Tags: sed replace
3

Syntax:

$ prepend content to add [filename]

Uses ed, so no temp files created.

sed -i 's/^/ls -l /' output_files.txt
2013-10-07 15:12:53
User: sonic
Functions: sed
Tags: sed replace
0

The original command is great, but I often want to prepend to every line.

sed -i "s/\s*/ /g;s/\s*$//" input_file
sed -i "s/\(\x09\{1,\}\)\|\( \{1,\}\)/ /g;s/\(\x09\{1,\}$\)\|\( \{1,\}$\)//g" brisati.txt
2011-12-12 10:24:03
User: knoppix5
Functions: sed
-4

This command does the following:

- converts any sequence of multiple spaces/tabs to one space only

- completely removes any space(s)/tab(s) at the end of each line

(If spaces and tabs are mixed in a sequence i.e. [tab][tab][space][tab], you have to execute this command twice!)

find . -type f | xargs grep -n "Old Text" | tee filesChanged.txt | sed 's/:.*$//' | xargs sed -i 's/Old Text/New Text/g
sed "s/\s\+/;/g;s/^ //;s/ $//" filename.csv
sed -i '1s/^/text to prepend\n/' file1
2011-06-25 12:02:11
User: xeor
Functions: sed
Tags: sed replace
2

Using the sed -i (inline), you can replace the beginning of the first line of a file without redirecting the output to a temporary location.

:g/\n"/jo
2010-09-11 18:51:41
User: mensa13
-2

In case the line you want to join start with a char different than ", you may use \n.*"\n as regex.

:%s/\([^\"]\)\(\n\)/\1 /g
2010-09-03 11:03:49
User: godzillante
1

----

this line ends here

but must be concatenated with this one

"this line ends here"

and should NOT be concatenated with this one

grep -ZlRr -e BAD_SCRIPT_LINE * |xargs -0 sed -i 's/BAD_SCRIPT_LINE//g'
2010-08-30 22:12:57
User: homoludens
Functions: grep sed xargs
0

recursive find and replace. important stuff are grep -Z and zargs -0 which add zero byte after file name so sed can work even with file names with spaces.

cat file_with_tabs.txt | perl -pe 's/\t/ /g'
2010-07-11 13:01:22
User: nikc
Functions: cat perl
Tags: cat perl replace
-3

Replaces tabs in output with spaces. Uses perl since sed seems to work differently across platforms.

sed -i 's/oldname@example.com/newname@example.com/g' `grep oldname@example.com -rl .`
2010-02-18 18:26:09
User: and3k
Functions: sed
0

Do a recursive (-r) search with grep for all files where your old mail address is mentioned (-l shows only the file names) and use sed to replace it with your new address. Works with other search/replacement patterns too.

find . -type f |xargs -I% sed -i '/group name/s/>/ deleteMissing="true">/' %
2010-02-01 21:09:57
User: 4fthawaiian
Functions: find sed xargs
1

Changed out the for loop for an xargs. It's a tad shorter, and a tad cleaner.

for i in `find . -type f`; do sed -i '/group name/s/>/ deleteMissing="true">/' $i; done
2010-02-01 17:16:37
User: allrightname
Functions: sed
0

Recursively replace a string in files with lines matching string. Lines with the string "group name" will have the first > character replaced while other > characters on other lines will be ignored.

echo -e "swap=me\n1=2"|sed 's/\(.*\)=\(.*\)/\2=\1/g'
$rpl -R oldstring newstring folder
2009-12-09 03:15:31
User: johnraff
Tags: unix replace
3

If you can install rpl it's simpler to use and faster than combinations of find, grep and sed.

See man rpl for various options.

time on above operation: real 0m0.862s, user 0m0.548s, sys 0m0.180s

using find + sed: real 0m3.546s, user 0m1.752s, sys 0m1.580s

mate - `find * -type f -regex 'REGEX_A' | grep -v -E 'REGEX_B'`
2009-08-12 22:24:08
User: irae
Functions: grep
1

This does the following:

1 - Search recursively for files whose names match REGEX_A

2 - From this list exclude files whose names match REGEX_B

3 - Open this as a group in textmate (in the sidebar)

And now you can use Command+Shift+F to use textmate own find and replace on this particular group of files.

For advanced regex in the first expression you can use -regextype posix-egrep like this:

mate - `find * -type f -regextype posix-egrep -regex 'REGEX_A' | grep -v -E 'REGEX_B'`

Warning: this is not ment to open files or folders with space os special characters in the filename. If anyone knows a solution to that, tell me so I can fix the line.

for files in $(ls -A directory_name); do sed 's/search/replaced/g' $files > $files.new && mv $files.new $files; done;
2009-05-07 20:13:07
User: bassu
Functions: ls mv sed
-3

Yeah, there are many ways to do that.

Doing with sed by using a for loop is my favourite, because these are two basic things in all *nix environments. Sed by default does not allow to save the output in the same files so we'll use mv to do that in batch along with the sed.