Bash脚本在所有文件前添加一个随机数 [英] Bash script to prepend a random number to all files

查看:54
本文介绍了Bash脚本在所有文件前添加一个随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台似乎没有音乐随机播放功能的智能"电话,因此,下一个最好的事情是编写一个bash脚本,在当前目录中的所有文件名前添加一个随机数.

I have a "smart" phone that doesn't seem to have a music shuffle function, so the next best thing is to write a bash script to prepend all filenames in the current directory with a random number.

这很难吗?

推荐答案

不,这并不难.但是,它将破坏您精心设计的文件名,并且可能很难撤消.

No, this is not hard to do. It will however mess up your carefully crafted filenames, and might be hard to undo.

您可以将 $ RANDOM 用作bash中随机数的简单来源.为您情况:

You can use $RANDOM as a simple source of random numbers in bash. For your case:

#!/bin/bash
for file in *; do
  mv "$file" $RANDOM-"$file"
done

我没有对此进行测试.您可能希望自己在一些小型设备上进行测试样本以确保您知道它的作用.

I didn't test this. You probably want to test this yourself on some small sample to make sure you know what it does.

这篇关于Bash脚本在所有文件前添加一个随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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