find . -name '*.txt' -exec sh -c 'x="{}"; mv "$x" "${x//2010/2022}"' \;
Whats under the hood?
- find command finds the files that ending with *.txt using -name parameter
- -exec parameter executes move (
mv
) command for each found file - second move argument
${x//2010/2022}
replaces string2010
with2022
in file names using shell parameter expansion
Files before command:
total 0
drwxr-xr-x 4 user staff 128 Aug 27 01:41 .
drwxr-xr-x@ 25 user staff 800 Aug 27 01:39 ..
-rw-r--r-- 1 user staff 0 Aug 27 01:39 a.2010.txt
-rw-r--r-- 1 user staff 0 Aug 27 01:39 b.2010.txt
Files after command:
total 0
drwxr-xr-x 4 user staff 128 Aug 27 01:42 .
drwxr-xr-x@ 25 user staff 800 Aug 27 01:39 ..
-rw-r--r-- 1 user staff 0 Aug 27 01:39 a.2022.txt
-rw-r--r-- 1 user staff 0 Aug 27 01:39 b.2022.txt