]> Sergey Matveev's repositories - nnn.git/commitdiff
plugins: cleanfilename: fixed to not overwrite existing file (#916)
authorBenawi Adha <43810055+wustho@users.noreply.github.com>
Thu, 25 Mar 2021 19:36:25 +0000 (02:36 +0700)
committerGitHub <noreply@github.com>
Thu, 25 Mar 2021 19:36:25 +0000 (01:06 +0530)
* plugins: cleanfilename: fixed to not overwrite existing file

* plugins: cleanfilename: fixed to not overwrite existing file

plugins/cleanfilename

index 924d5d042effd4856e5ffaa947b5701c8463c4e7..b7f2ba062c13ec2608560aa573542070ad625d60 100755 (executable)
@@ -1,12 +1,11 @@
 #!/usr/bin/env sh
 
-# Description: Clean filename (either hovered or selections)
+# Description: Clean filename or dirname (either hovered or selections)
 #              to be more shell-friendly.
 #              This script replaces any non A-Za-z0-9._- char
 #              with underscore (_).
 #
-#              Although the name is 'cleanfilename', but it should work
-#              on directories too.
+#              LIMITATION: Won't work with filename that contains newline
 #
 # Dependencies: sed
 #
@@ -51,6 +50,10 @@ fi
 
 for i in $targets; do
     if [ "$i" != "$(cleanup "$i")" ]; then
-        mv "$i" "$(cleanup "$i")";
+        tmp=''
+        if [ -e "$(cleanup "$i")" ]; then
+            tmp='_'
+        fi
+        mv "$i" "$tmp$(cleanup "$i")";
     fi
 done