Download your favorite Linux distribution at LQ ISO.
Go Back > Blogs > linux-related notes
User Name


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.
Rate this Entry

"Alpha" of an auto-renaming existing files "mv" script/function/alias

Posted 02-18-2014 at 01:02 PM by the dsc

Please don't test this on files you don't care to lose/folders you don't care to mess. I just tested once with an explicit (non-variable/"$2") destination, it seems to have worked. I don't have it as an alias or anything, it was just an "one liner" I came up with.

for on in "$1" ; do nn=$on ; num=1 ; until [ ! -f "$2/$nn" ] ; do nn="$(echo $nn | sed 's| ([0-9]*)||' | sed "s|\.| ($num).|")" ; num=$((num+1)) ; echo $nn ; done ; mv $on "$2/$nn" ; done
on="original name", multiple "input" files given by "*", such as "*.pdf", "*whatever*.ext".
nn="new name"

I intend to do something to check whether the user is intending to use mv to rename the file instead of or at the same time he's moving, but with an specific name already, rather than just adding "(#)" to the file name when a file with the same name already exists. Possibly by checking whether $2 is a folder or not.

Eventually it would be interesting to have the whole thing actually recognize different copies of the same file and overwrite/skip when that's the case, but auto-rename only different files with conflicting file names. But that's something for the distant future, if ever.
Posted in Uncategorized
Views 636 Comments 0
« Prev     Main     Next »
Total Comments 0




All times are GMT -5. The time now is 11:04 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration