我有几个相同名称的文件(比如somefile.php)分散在/ var /
如果我想用位于/ home / me / somefile.php中的新版本的somefile.phpreplace它们,我将如何做? 我发现每个search结果都与replace文件中的文本有关,但我想replace整个文件 。
我知道我可以find所有的文件:
find /var/ -type f -name somefile.php
但我不知道如何将/home/me/somefile.php移动到结果中,并将其全部replace。
find / var -type -f -name somefile.php -exec cp /home/me/somefile.php {} \;
小心!
正如你所看到的,这也适用于带有空格的文件:
hoeha@dw:/tmp/test$ mkdir my\ dir{1,2,3}/ ; touch my\ dir{1,2,3}/file\ to\ replace hoeha@dw:/tmp/test$ tree . ├── my dir1 │ └── file to replace ├── my dir2 │ └── file to replace └── my dir3 └── file to replace 3 directories, 3 files hoeha@dw:/tmp/test$ echo "the new one" > the\ new\ one hoeha@dw:/tmp/test$ find . -type f -name 'file to replace' -exec cp the\ new\ one \{\} \; -execdir mv \{\} the\ new\ one \; hoeha@dw:/tmp/test$ tree . ├── my dir1 │ └── the new one ├── my dir2 │ └── the new one ├── my dir3 │ └── the new one └── the new one 3 directories, 4 files hoeha@dw:/tmp/test$ cat my\ dir1/the\ new\ one the new one hoeha@dw:/tmp/test$ find -version | head -1 find (GNU findutils) 4.4.2 hoeha@dw:/tmp/test$
这是你想要的吗?
GNU-extension-free版本:
find /var -name somefile.php | while read LINE; do cp /home/me/somefile.php "$LINE"; done