Just annotations of little "how to's", so I know I can find how to do something I've already done when I need to do it again, in case I don't remember anymore, which is not unlikely. Hopefully they can be useful to others, but I can't guarantee that it will work, or that it won't even make things worse.
New file name generator script/function, to avoid overwrites w/o user input
Posted 03-27-2016 at 07:11 PM by the dsc
Usage examples:
cp/mv/ln/etc file11.txt $(newname "/some/path/file11.txt")
cp/mv/ln/etc file11.txt $(newname "/some/path/whatever.txt")
If there's a file11.txt or whatever.txt already there, it will rename automatically to file11(1).txt or whatever(1).txt, and (N+1) from then on.
The whole path/name must be between quotes because of spaces and such things.
May be dangerous and somehow make you lose files even though it is what it's intended to avoid. Use it at your own risk, or don't. I don't know how it would behave as a standalone "function-command" in something like ".bash_functions".
cp/mv/ln/etc file11.txt $(newname "/some/path/file11.txt")
cp/mv/ln/etc file11.txt $(newname "/some/path/whatever.txt")
If there's a file11.txt or whatever.txt already there, it will rename automatically to file11(1).txt or whatever(1).txt, and (N+1) from then on.
The whole path/name must be between quotes because of spaces and such things.
May be dangerous and somehow make you lose files even though it is what it's intended to avoid. Use it at your own risk, or don't. I don't know how it would behave as a standalone "function-command" in something like ".bash_functions".
Code:
#!/bin/bash file=$1 num=1 path=$(dirname "$1") file=$(basename "$1") until [ ! -f "$path/$file" ] ; do file="$(echo $file | sed 's|([0-9]*)\.|.|' | sed "s|\.|($num).|")" num=$((num+1)) done echo $path/$file
Total Comments 0