[英]Renaming multiple files to a random string renames only one file
尝试将文件重命名为随机字符串。 我从这个问题的答案中提取了一些代码,以生成随机字符串。
#!/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)
为了练习,我在$HOME/practice
创建了一个目录,其中包含一些纯文本文件。
/Users/me/practice/testfile1.txt
/Users/me/practice/testfile2.txt
/Users/me/practice/testfile3.txt
当尝试将这些文件重命名为随机字符串时,我没有获得 3 个随机名称,而是将 1 个文件重命名为随机字符串。
for file in $HOME/practice/*
do
mv "$file" $HOME/practice/"$output"
done
#result
/Users/me/practice/i6TP3wiMDD
用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
我的问题是两部分:
我还要说,上面的随机字符脚本超出了我目前的理解。 我知道它适用于生成随机字符。 但它的内部运作对我来说仍然有些不清楚。
问题是您只生成随机值的实例并将其存储在output
变量中,并使用它来重命名循环中的所有文件。 请参阅在定义 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
以上是我的示例数据。 最好的方法是通过在扩展时调用函数来修复函数重命名中的 for 循环。 像下面
#!/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
现在通过上述修复,您可以看到我的.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.