DEV Community

Discussion on: Automating Simple Things with Python is Awesome

Collapse
 
rrampage profile image
Raunak Ramakrishnan • Edited

Here's one way of doing this using the standard Linux utils

source <(find . -type f -name "*.1" | sed -nr "s/(^.*)\.(.*)\.1/mv \1.\2.1 \1.\2/p")

Explanation:

  • source <(CMD) executes text output of CMD as a command.
  • find . -type f -name "*.1" finds all files in current directory ending with .1
  • sed -nr "s/(^.*)\.(.*)\.1/mv \1.\2.1 \1.\2/p" uses a regex to get the basename and extension before .1 and outputs a move command with .1 removed.
  • The output of this pipeline is executed by source

We can also use exec option from find itself but I didn't go down that path :)

Collapse
 
rpalo profile image
Ryan Palo

Neat! Thanks for sharing πŸ˜„ you could also do it in pure Bash with a for loop and string indexing. But it’s not as nifty as your way.

Collapse
 
rrampage profile image
Raunak Ramakrishnan • Edited

Yes. Another way I found was using awk instead of source as follows:

find . -type f -name "*.1" | sed -nr "s/(^.*)\.(.*)\.1/mv \1.\2.1 \1.\2/p" | awk '{system($0)}'

This makes the eval of strings part of the pipeline.