Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In a folder on my system, I have a C++ project that I'm trying to compile.

This process uses some C++ idioms which are not supported by my compiler version. In particular, often inside the code it is used the data value 'nullptr'.

What I want to do is to replace all occurrences of nullptr with NULL.

I can do this in VIM with the command:


How can I do this for all the files in a directory once, without opening each file and executing the command?

share|improve this question
You may want to try #define nullptr NULL in a few headers instead. Easier to do, and easier to undo (or qualify with #ifdef, something you may want to investigate WRT to C++ features and particular compilers). – goldilocks Apr 26 '13 at 15:14
thanks. very good hint. – Aslan986 Apr 26 '13 at 15:42
Found a discussion of this stackoverflow.com/questions/10496824/… – goldilocks Apr 26 '13 at 15:47
The "C++ idiom" is in fact the new standard: C++11 unix.stackexchange.com/questions/73801/…. I guess you're compiling in a special environment because GCC for example support the standard (at least partially) for quite a long time now. Isn't there a upgrade for your compiler that would allow you to use C++11? – lgeorget Apr 26 '13 at 17:52
up vote 4 down vote accepted

Two ways to do this:


You can use vim from the command line like so:

$ vim -c "%s/nullptr/NULL/g|wq" foo.txt

Wrap in a for loop:

$ for i in *; do vim -c "%s/nullptr/NULL/g|wq" -- "$i"; done


$ perl -p -i -e 's/nullptr/NULL/g' foo.txt

Wrap in a for loop:

$ for i in *; do perl -p -i -e 's/nullptr/NULL/g' -- "$i"; done

Or simply:

$ perl -p -i -e 's/nullptr/NULL/g' -- *
share|improve this answer
for file in /path/to/dir/*; do
  sed -i s/nullptr/NULL/g "$file"

This does not affect subdirectories, though.

Edit 1

The faster solution (with GNU):

find /path/to/dir -type f -print0 | xargs -0 sed -i s/nullptr/NULL/g

This would affect subdirectories (and dotfiles), too. Add -maxdepth 1 if that is not wanted.

share|improve this answer

With GNU tools:

grep -rlZ nullptr /path/to/dir |
  xargs -r0 sed -i s/nullptr/NULL/g

To avoid having to rewrite every file.

As always with sed -i, keep in mind that it may break links (hard and soft), alter the ownership and permission of files and change the sparseness and potentially other uncommon attributes of files. Also beware that versions of GNU grep prior to 2.12 follow symlinks when descending the directory tree with -r.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.