继续重命名,直到目录中不存在文件 [英] keep renaming until file doesnt exist in directory

查看:49
本文介绍了继续重命名,直到目录中不存在文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查文件是否存在于目录中,如果确实通过在 original.txt.txt 中添加 original.txt 之类的扩展名来重命名该文件.但是检查重命名的文件是否仍然存在.

I need to check if a file exists in a directory, if it does rename it by adding an extra extension like original.txt to original.txt.txt. BUT check whether the renamed file still exist.

我的代码只更改了一次,并且在重命名之前没有检查其余内容.它只是覆盖了我所有的 original.txt.txt

My code only changes it one time and is not checking the rest of the contents before it renames it. It just overwrites all my original.txt.txt

问题:

如何检查所有文件并根据需要添加.txt多次,以免与目录中的任何其他名称冲突.

How to check all files and add .txt as many times as needed so it doesn't conflict with any other name in directory.

if [ -e "$destination_folder/$basename_file" ]
     then
     cp "$file_name" "$destination_folder/$basename_file".txt
     fi

推荐答案

如果我对您的理解正确,那么仅当没有其他新名称的文件时,才想重命名文件.您要防止意外覆盖另一个文件.

If I understand you correctly, you want to rename a file only if there is no other file of the new name. This you want to prevent accidental overwriting of this other file.

要实现此目的,您可以将 mv (我假设您用于重命名)与选项 -i 一起使用,以便在覆盖任何内容之前询问.如果您确定自己不想重命名,则可以将 yes n 用管道传输到 mv 命令:

To achieve this you can use mv (which I assume you use for renaming) with the option -i so it will ask before overwriting anything. In case you are quite sure you do not want to rename anything then, you can pipe yes n into the mv command:

yes n | mv -i "$old" "$new"

要防止看到难看的自动回答的问题,可以将命令的stderr重定向到/dev/null :

To prevent seeing ugly automatically answered questions, you can redirect the stderr of the commands to /dev/null:

(yes n | mv -i "$old" "$new") 2> /dev/null

现在,仅当新名称可用时,它才会重命名文件.否则它什么都不做.

This will now rename the file only if the new name is free. Otherwise it does nothing.

您可以使用循环来查找免费"名称:

You can use a loop to find a "free" name:

name=$original
while [ -e "$name" ]
do
  name="$name.txt"
done
mv "$original" "$name"

这样,您将找到一个免费名称,然后使用它.

This way you will find a free name and then use it.

请注意,这仍然可以覆盖刚刚创建的新名称的文件.Unix系统是多任务的,另一个进程可以在循环后 mv 命令前之前创建一个新名称的文件.

Be aware that this can still overwrite a just recently created file of the new name. Unix systems are multitasking and another process could create a file of the new name just after the loop and before the mv command.

为避免发生这种病理情况(以防万一您无法确定不会发生这种情况),您应该使用上面的我的方法仅在什么都不会被覆盖的情况下移动,然后再检查原始文件(带有原始文件名)仍然存在,如果仍然存在,请再次尝试整个操作:

To avoid this pathological case (in case you cannot be sure that this won't happen), you should use my method above to move only if nothing would be overwritten, and afterwards you should check if the original file (with the original file name) still exists, and if it still exists, try the whole thing again:

new=$old.txt
while true
do
  (yes n | mv -i "$old" "$new") 2> /dev/null
  if [ -e "$old" ]
  then
    new=$new.txt
  else
    break
  fi
done

这篇关于继续重命名,直到目录中不存在文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆