Restore multiple backup files created with find and then sed

I need to edit files with a specific template in their names. These files are distributed in several hierarchical directories.

I use findand then sedusing xargsto achieve the same result:

find . -type f -name "file*" | sed -i.bak 's/word1/replace_word1/g'

My problem is that now I want to undo the changes from the backup files (* .bak). An example directory tree will look like the one shown below. I am looking for a linux command (maybe a chain) to achieve the same.

$ tree
.
|-- pdir1
|   |-- dir1
|   |   |-- dir2
|   |   |   |-- file2
|   |   |   `-- file2.bak
|   |   |-- file1
|   |   `-- file1.bak
|   `-- dir3
|       |-- file3
|       `-- file3.bak
`-- pdir2
    |-- file1
    `-- file1.bak

I can find all backup files using the following command find. But I can’t understand what to do next. It would be noticeable if you could help me solve this problem.

find . -type f -name "file*.bak"

Note

. , script. .

+4
2

rename find:

find . -type f -name "*.bak" -exec rename 's/\.bak$//' {} \;

foo.bak, foo . , -f rename:

find . -type f -name "*.bak" -exec rename -f 's/\.bak$//' {} \;

EDIT: rename, :

find . -name "*.bak" -exec sh -c 'mv -f $0 ${0%.bak}' {} \;

() : , find, , ( {}) mv -f $0 ${0%.bak}. $0 , ${0%.bak} .bak . , foo.bak mv -f foo.bak foo.

+5

for, . , bak.

for f in **/*.bak; do mv -- "$f" "${f%.bak}"; done
0

All Articles