Shell 脚本从目录复制并粘贴随机文件

发布于 2024-10-16 11:52:01 字数 174 浏览 5 评论 0原文

标题几乎说明了我希望在我正在处理的脚本中添加一行,该脚本将从目录(例如 ~/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

り繁华旳梦境 2024-10-23 11:52:02

此演示脚本展示了如何从目录中选择随机文件,这应该是一个好的开始。

#!/bin/bash

# Set up test data.

rm -rf tmpdata ; mkdir tmpdata
touch tmpdata/fileA tmpdata/fileB tmpdata/fileC tmpdata/fileD tmpdata/fileE

# From and To directories

fromdir=./tmpdata
todir=./tmpdata2

# Get a list of the files to a temporary file.

ls -1 ${fromdir} >/tmp/filelist.$

# Select a number from 1 to n where n is the line count of that file.
# Then use head and tail to get the line.

filenum=$(expr $RANDOM % $(cat /tmp/filelist.$ | wc -l) + 1)
file=$(head -${filenum} /tmp/filelist.$ | tail -1)

# DEBUG stuff.

cat /tmp/filelist.$ | sed 's/^/DEBUG file: /'
echo "DEBUG nmbr: ${filenum}"

echo "'cp ${fromdir}/${file} ${todir}'"

# Remove temporary file.

rm -f /tmp/filelist.$

一些示例输出:

pax$ ./cprnd.sh
DEBUG file: fileA
DEBUG file: fileB
DEBUG file: fileC
DEBUG file: fileD
DEBUG file: fileE
DEBUG nmbr: 3
'cp ./tmpdata/fileC ./tmpdata2'

pax$ ./cprnd.sh
DEBUG file: fileA
DEBUG file: fileB
DEBUG file: fileC
DEBUG file: fileD
DEBUG file: fileE
DEBUG nmbr: 1
'cp ./tmpdata/fileA ./tmpdata2'

pax$ ./cprnd.sh
DEBUG file: fileA
DEBUG file: fileB
DEBUG file: fileC
DEBUG file: fileD
DEBUG file: fileE
DEBUG nmbr: 5
'cp ./tmpdata/fileE ./tmpdata2'

“神奇”在于这两行:

filenum=$(expr $RANDOM % $(cat /tmp/filelist.$ | wc -l) + 1)
file=$(head -${filenum} /tmp/filelist.$ | tail -1)

第一行使用 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.

#!/bin/bash

# Set up test data.

rm -rf tmpdata ; mkdir tmpdata
touch tmpdata/fileA tmpdata/fileB tmpdata/fileC tmpdata/fileD tmpdata/fileE

# From and To directories

fromdir=./tmpdata
todir=./tmpdata2

# Get a list of the files to a temporary file.

ls -1 ${fromdir} >/tmp/filelist.$

# Select a number from 1 to n where n is the line count of that file.
# Then use head and tail to get the line.

filenum=$(expr $RANDOM % $(cat /tmp/filelist.$ | wc -l) + 1)
file=$(head -${filenum} /tmp/filelist.$ | tail -1)

# DEBUG stuff.

cat /tmp/filelist.$ | sed 's/^/DEBUG file: /'
echo "DEBUG nmbr: ${filenum}"

echo "'cp ${fromdir}/${file} ${todir}'"

# Remove temporary file.

rm -f /tmp/filelist.$

And some sample output:

pax$ ./cprnd.sh
DEBUG file: fileA
DEBUG file: fileB
DEBUG file: fileC
DEBUG file: fileD
DEBUG file: fileE
DEBUG nmbr: 3
'cp ./tmpdata/fileC ./tmpdata2'

pax$ ./cprnd.sh
DEBUG file: fileA
DEBUG file: fileB
DEBUG file: fileC
DEBUG file: fileD
DEBUG file: fileE
DEBUG nmbr: 1
'cp ./tmpdata/fileA ./tmpdata2'

pax$ ./cprnd.sh
DEBUG file: fileA
DEBUG file: fileB
DEBUG file: fileC
DEBUG file: fileD
DEBUG file: fileE
DEBUG nmbr: 5
'cp ./tmpdata/fileE ./tmpdata2'

The "magic" lies in these two lines:

filenum=$(expr $RANDOM % $(cat /tmp/filelist.$ | wc -l) + 1)
file=$(head -${filenum} /tmp/filelist.$ | tail -1)

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 with 0..n-1 and, by adding 1, you get 1..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 through tail to get the last line of that set (i.e., the tenth line from the file).

倦话 2024-10-23 11:52:02

红宝石 (1.9+)

require 'fileutils'
files=[]
Dir["*"].each { |file| test(?f,file) && files << file }
FileUtils.cp(files[ rand(files.size) ] , File.join("/tmp")  )

Ruby (1.9+)

require 'fileutils'
files=[]
Dir["*"].each { |file| test(?f,file) && files << file }
FileUtils.cp(files[ rand(files.size) ] , File.join("/tmp")  )
—━☆沉默づ 2024-10-23 11:52:02

这些答案中的大多数在我的计算机上的终端运行时都有效,但是它们都没有在 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

仅此而已 2024-10-23 11:52:01

您不应该解析 'ls' 的输出: http://mywiki.wooledge.org/ParsingLs

简洁版本:

files=(src/*)
mv "${files[$RANDOM % ${#files[@]}]}" dest/

此代码会将“src/”子目录中找到的随机文件移动到 dest/ 子目录。

files=(src/*)                    #creates an array of all the files within src/ */
filecount="${#files[@]}"         #determines the length of the array
randomid=$((RANDOM % filecount)) #uses $RANDOM to choose a random number between 0 and $filecount
filetomove="${files[$randomid]}" #the random file wich we'll move
mv "$filetomove" dest/           #does the actual moving

You should not parse the output of 'ls': http://mywiki.wooledge.org/ParsingLs

terse version:

files=(src/*)
mv "${files[$RANDOM % ${#files[@]}]}" dest/

This code will move a random file found within a 'src/' subdirectory to a dest/ subdirectory.

files=(src/*)                    #creates an array of all the files within src/ */
filecount="${#files[@]}"         #determines the length of the array
randomid=$((RANDOM % filecount)) #uses $RANDOM to choose a random number between 0 and $filecount
filetomove="${files[$randomid]}" #the random file wich we'll move
mv "$filetomove" dest/           #does the actual moving
故事↓在人 2024-10-23 11:52:01

好吧,如果你可以 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文