Shell 脚本从目录复制并粘贴随机文件
标题几乎说明了我希望在我正在处理的脚本中添加一行,该脚本将从目录(例如 ~/Desktop/old)复制随机文件并将其粘贴到另一个文件夹(例如 ~/Desktop/new)中。我只想在每次运行脚本时将一个文件移动到新文件夹,我在谷歌上搜索了一下,只找到了回显随机文件的解决方案,但无法弄清楚如何复制随机文件,谢谢您对这个问题的任何帮助
Title pretty much says it i'm looking to add a line to a script i'm working on that would copy a random file from a directory say ~/Desktop/old and paste it into another folder say ~/Desktop/new. I only want to move one file to the new folder each time the script is run i googled around and only found solutions to echo a random file but couldn't figure out how to copy a random one thank you for any help with this problem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
此演示脚本展示了如何从目录中选择随机文件,这应该是一个好的开始。
一些示例输出:
“神奇”在于这两行:
第一行使用
wc
来获取行数(文件数)。然后,它会给出随机数除以该值时的余数,最终得到0..n-1
,并且通过添加 1,得到1..n
代码>.假设对于 50 行的文件,它为您提供 10。下一行使用
head
获取前十行,然后通过tail
通过管道获取该集合的最后一行(即文件中的第十行)。This demo script shows how you can select a random file from a directory, and should be a good start.
And some sample output:
The "magic" lies in these two lines:
The first uses
wc
to get the line count (number of files). It then gives you the remainder when dividing a random number by this value so that you end up with0..n-1
and, by adding 1, you get1..n
. Let's assume it gives you 10 for a fifty-line file.The next line uses
head
to get the first ten lines, then pipes that throughtail
to get the last line of that set (i.e., the tenth line from the file).红宝石 (1.9+)
Ruby (1.9+)
这些答案中的大多数在我的计算机上的终端运行时都有效,但是它们都没有在 Android 终端上工作,所以我最终用 java 编写了它,谢谢所有提供帮助的人
Most of these answers worked when run from terminal on my computer however none of them worked on terminal for android so i ended up writing it in java thank you to all that have helped
您不应该解析 'ls' 的输出: http://mywiki.wooledge.org/ParsingLs
简洁版本:
此代码会将“src/”子目录中找到的随机文件移动到 dest/ 子目录。
You should not parse the output of 'ls': http://mywiki.wooledge.org/ParsingLs
terse version:
This code will move a random file found within a 'src/' subdirectory to a dest/ subdirectory.
好吧,如果你可以 echo 它,只需使用 xargs 将结果传递给 cp 即可。
如果您可以提供生成随机文件名的代码,那将会很有帮助。
Well if you can echo it just pass the result to cp using xargs.
If you could provide the code to generate the random filename it would be helpful.