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 replace from sorted by
Terminal - Commands tagged replace - 21 results
for f in `ls`; do sed -i '/MATCHING STRING/ { s/ORIGINAL/REPLACEMENT/; }' ${f} ; done
2015-05-21 19:37:42
User: krizzo
Functions: sed

Find and replace specific characters in a single line in multiple files with sed.

sed -i 's/^.*?].*?:\s//g' skype-chat-log.txt
2014-05-26 07:48:36
Functions: sed

Regular expression search pattern to remove the Datetime and Name when you paste from skype chat into your text editor

From this Gist:


echo "text to prepend" | cat - file
2013-12-18 15:54:17
User: leni536
Functions: cat echo
Tags: cat replace

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


$ 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

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

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

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.

2010-09-11 18:51:41
User: mensa13

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


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

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

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

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

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

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

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

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

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.