0

How do I replace a file called config.dat on multiple files across multiple directories?

example:

/var/www/site1/configs/config.dat
/var/www/site2/configs/config.dat
/var/www/site3/configs/config.dat

I want to replace all config.data with my new /home/john/config.dat.

Thanks in advance.

2 Answers 2

1

Open the terminal and run:

cd /var/www
sudo find . -type f  -name 'config.dat' -exec cp /home/john/config.dat {} \;

Here I have assumed that the /var/www directory is owned by root, but if you are the owner of the /var/www directory, then you don't need to use sudo in the command.

2
  • 1
    That seems to work! What does the '{} \' at the end mean?
    – John
    Dec 23, 2013 at 18:45
  • -exec COMMAND \; Carries out COMMAND on each file that find matches. The command sequence terminates with ; (the ";" is escaped to make certain the shell passes it to find literally, without interpreting it as a special character). If COMMAND contains {}, then find substitutes the full path name(s) of the selected file(s) for "{}".
    – karel
    Dec 23, 2013 at 18:58
1

Assuming the location of the files you want to replace follows the pattern /var/www/*/configs/config.dat, then execute this in a terminal:

for f in /var/www/*/configs/config.dat; do cp /home/john/config.dat "$f"; done

The for-loop iterates over the expanded list of target filenames using variable f. The cp-statement copies the contents of /home/john/config.dat over each of them.

2
  • 1
    can you explain?
    – Braiam
    Dec 23, 2013 at 18:42
  • @Braiam Better?
    – zwets
    Dec 24, 2013 at 6:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .