$ mv myfile01.txt.bak myfile01.txt
If we have hundreds of such files, nobody would want to run hundreds of the similar commands:
$ mv myfile02.txt.bak myfile02.txt
$ mv myfile03.txt.bak myfile03.txt
...
We can use sed instead:
$ ls *.bak | sed -e 's/\(.*\)\(\.bak\)/mv \1\2 \1/' | sh
The explanation of this command:
- "ls *.bak" gets the list of the filenames and feed them to sed.
- For each filename, sed changes it to "mv something.bak something"
- In the first part of the "s" command of sed, a pattern matches anything ended with .bak. It uses \( and \) to group the filename into 2 parts. The 1st group \(.*\) matches anything before .bak. The 2st group \(\.bak\) matches the exact string .bak.
- In the second part of the "s" command of sed (mv \1\2 \1), \1 and \2 are substituted with the 2 groups described above. So \1 refers to anything before .bak, and \2 refers to .bak.
- The output of sed will be "mv something.bak something" and it is passed to sh command to run.
- This will be done for all the files from 'ls *.bak'.
No comments:
Post a Comment