Issue
I am looking for a way to process my hughmungus amount of nested & numbered backups without changing structure or meta-data. After a bit of research I have a follow up to unix mv --backup=numbered ...
How to change the following mv
,sed
command to not treat .~x~
as a file ending ?
i.e.
- How to remove the "." before "~" ?
- How to apply it to non-ending-type files?
given answer that almost works
swap_file_extension_and_backup_number ()
{
IFS=$'\n'
for y in $(ls $1)
do
mv $1/`echo $y | sed 's/ /\\ /g'` $1/`echo "$y" | sed 's/\(\.[^~]\{3\}\)\(\.~[0-9]\{1,2\}~\)$/\2\1/g'`
done
}
which is called like this swap_file_extension_and_backup_number /folder_path/
and would rename like that:
/folder_path/imfine.jpg
/folder_path/sadly_i_dont_get_changed.~1~
/folder_path/def.txt.~1~
to
/folder_path/iamfine.jpg
/folder_path/sadly_i_dont_get_changed.~1~
/folder_path/def.~1~.txt
but should create
/folder_path/iamfine.jpg
/folder_path/sadly_i_dont_get_changed~1~
/folder_path/def~1~.txt
In short: I am unable to process the sed/regex line. Please don't lapidate me.
Solution
Try this:
swap_file_extension_and_backup_number ()
{
IFS=$'\n'
for y in $(ls $1)
do
mv $1/`echo $y | sed 's/ /\\ /g'` $1/` echo "$y" | sed 's/\(\.[^~]\{3\}\)\?\(\.\)\(~[0-9]\{1,2\}~\)$/\3\1/g'`
done
}
See Regex demo
Answered By - Alireza