简体   繁体   English

将多个文件重命名为随机字符串只会重命名一个文件

[英]Renaming multiple files to a random string renames only one file

Trying to rename files to a random string.尝试将文件重命名为随机字符串。 I took some code from an answer to this question in order to generate a random string of characters.我从这个问题的答案中提取了一些代码,以生成随机字符串。

#!/bin/bash

chars=( {a..z} {A..Z} {0..9} )

function rand_string {
    local c=$1 ret=
    while((c--)); do
        ret+=${chars[$((RANDOM%${#chars[@]}))]}
    done
    printf '%s\n' "$ret"
}

output=$(rand_string 10)

For practice I made a directory at $HOME/practice with a few plain text files.为了练习,我在$HOME/practice创建了一个目录,其中包含一些纯文本文件。

/Users/me/practice/testfile1.txt
/Users/me/practice/testfile2.txt
/Users/me/practice/testfile3.txt

When trying to rename these files a random string, instead of getting 3 random names, I am instead left with 1 file renamed to a random string.当尝试将这些文件重命名为随机字符串时,我没有获得 3 个随机名称,而是将 1 个文件重命名为随机字符串。

for file in $HOME/practice/*
do
    mv "$file" $HOME/practice/"$output" 
done

#result
/Users/me/practice/i6TP3wiMDD

Replacing mv "$file" ~/practice/"$output" with echo "$file" "$output" shows me that the random string is being repeated after every file instead of generating a new random string for every file.echo "$file" "$output"替换mv "$file" ~/practice/"$output"告诉我随机字符串在每个文件之后重复,而不是为每个文件生成一个新的随机字符串。

/Users/me/practice/testfile1.txt i6TP3wiMDD
/Users/me/practice/testfile2.txt i6TP3wiMDD
/Users/me/practice/testfile3.txt i6TP3wiMDD

My question is two part:我的问题是两部分:

  1. Why is only 1 file being renamed?为什么只有 1 个文件被重命名?
  2. How can I generate a new random string for each file being renamed?如何为每个被重命名的文件生成一个新的随机字符串?

I will also say that the above random character script is above my current understanding.我还要说,上面的随机字符脚本超出了我目前的理解。 I know that it works for generating random characters.我知道它适用于生成随机字符。 But the inner workings of it are still somewhat unclear to me.但它的内部运作对我来说仍然有些不清楚。

The problem is you are generating only instance of the random value and storing it in the output variable and using the same to rename all the files in the loop.问题是您只生成随机值的实例并将其存储在output变量中,并使用它来重命名循环中的所有文件。 See the shell script expansion which comes when run with the set -x option after the defining the she-bang( #!/bin/bash ).请参阅在定义 she-bang( #!/bin/bash ) 后使用set -x选项运行时出现的 shell 脚本扩展。

+++ rand_string 10
+++ local c=10 ret=
+++ (( c-- ))
+++ ret+=O
+++ (( c-- ))
+++ ret+=k
+++ (( c-- ))
+++ ret+=H
+++ (( c-- ))
+++ ret+=Q
+++ (( c-- ))
+++ ret+=1
+++ (( c-- ))
+++ ret+=u
+++ (( c-- ))
+++ ret+=9
+++ (( c-- ))
+++ ret+=4
+++ (( c-- ))
+++ ret+=c
+++ (( c-- ))
+++ ret+=C
+++ (( c-- ))
+++ printf '%s\n' OkHQ1u94cC     # <--- See the same random file-names used throughout the files
++ output=OkHQ1u94cC
++ for file in '*.txt'
++ mv 1.txt OkHQ1u94cC
++ for file in '*.txt'
++ mv 2.txt OkHQ1u94cC
++ for file in '*.txt'
++ mv 5000.txt OkHQ1u94cC
++ for file in '*.txt'
++ mv 5100.txt OkHQ1u94cC
++ for file in '*.txt'
++ mv abc.txt OkHQ1u94cC
++ for file in '*.txt'
++ mv file.txt OkHQ1u94cC
++ for file in '*.txt'
++ mv final.txt OkHQ1u94cC
++ for file in '*.txt'
++ mv ini1.txt OkHQ1u94cC
++ for file in '*.txt'
++ mv ini2.txt OkHQ1u94cC
++ for file in '*.txt'
++ mv listing.txt OkHQ1u94cC
++ for file in '*.txt'
++ mv names.txt OkHQ1u94cC
++ for file in '*.txt'
++ mv parameters.txt OkHQ1u94cC
++ for file in '*.txt'
++ mv properties.txt OkHQ1u94cC
++ for file in '*.txt'
++ mv result.txt OkHQ1u94cC

The above is with my sample data.以上是我的示例数据。 The best way is to fix the for loop in function rename by calling the function at time of expansion.最好的方法是通过在扩展时调用函数来修复函数重命名中的 for 循环。 Like below像下面

#!/bin/bash

for file in $HOME/practice/*
do
    mv "$file" $HOME/practice/"$(rand_string 10)"   # <-- Here the random string length can be controlled as you need.
done

Now with the above fix, you can see my script of actual renaming of my .csv files expanded as现在通过上述修复,您可以看到我的.csv文件实际重命名脚本扩展为

+++ rand_string 10
+++ local c=10 ret=
+++ (( c-- ))
+++ ret+=7
+++ (( c-- ))
+++ ret+=j
+++ (( c-- ))
+++ ret+=U
+++ (( c-- ))
+++ ret+=K
+++ (( c-- ))
+++ ret+=l
+++ (( c-- ))
+++ ret+=V
+++ (( c-- ))
+++ ret+=l
+++ (( c-- ))
+++ ret+=6
+++ (( c-- ))
+++ ret+=8
+++ (( c-- ))
+++ ret+=8
+++ (( c-- ))
+++ printf '%s\n' 7jUKlVl688
++ output=7jUKlVl688
++ for file in '*.csv'
+++ rand_string 10
+++ local c=10 ret=
+++ (( c-- ))
+++ ret+=N
+++ (( c-- ))
+++ ret+=B
+++ (( c-- ))
+++ ret+=O
+++ (( c-- ))
+++ ret+=p
+++ (( c-- ))
+++ ret+=j
+++ (( c-- ))
+++ ret+=5
+++ (( c-- ))
+++ ret+=T
+++ (( c-- ))
+++ ret+=b
+++ (( c-- ))
+++ ret+=S
+++ (( c-- ))
+++ ret+=R
+++ (( c-- ))
+++ printf '%s\n' NBOpj5TbSR
++ mv 1.csv NBOpj5TbSR                  # <- Unique file names generated.
+++ rand_string 10
+++ local c=10 ret=
+++ (( c-- ))
+++ ret+=N
+++ (( c-- ))
+++ ret+=R
+++ (( c-- ))
+++ ret+=Y
+++ (( c-- ))
+++ ret+=C
+++ (( c-- ))
+++ ret+=C
+++ (( c-- ))
+++ ret+=X
+++ (( c-- ))
+++ ret+=L
+++ (( c-- ))
+++ ret+=0
+++ (( c-- ))
+++ ret+=e
+++ (( c-- ))
+++ ret+=l
+++ (( c-- ))
+++ printf '%s\n' NRYCCXL0el
++ mv 2.csv NRYCCXL0el                    # <- Unique file names generated.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM