简体   繁体   English

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

[英]Shell Script copy and paste a random file from a directory

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. Title几乎说了我要在正在处理的脚本中添加一行内容,该行将从目录〜/ Desktop / old复制一个随机文件,并将其粘贴到另一个目录中,例如〜/ 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 我只想在每次运行脚本时都将一个文件移动到新文件夹,我在Google上四处搜寻,只找到了回显随机文件的解决方案,但不知道如何复制随机文件,谢谢您对这个问题的帮助

You should not parse the output of 'ls': http://mywiki.wooledge.org/ParsingLs 您不应该解析“ 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. 此代码会将在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

Well if you can echo it just pass the result to cp using xargs. 好吧,如果您可以将其回显,只需使用xargs将结果传递给cp。 If you could provide the code to generate the random filename it would be helpful. 如果您可以提供代码以生成随机文件名,则将很有帮助。

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). 第一个使用wc获取行数(文件数)。 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 . 然后,当您将随机数除以该值时,它会为您提供余数,从而得到0..n-1 ,再加上1,您将得到1..n Let's assume it gives you 10 for a fifty-line file. 假设它为您提供50行文件的10。

The next line uses head to get the first ten lines, then pipes that through tail to get the last line of that set (ie, the tenth line from the file). 下一行使用head来获取前十行,然后通过tail将其通过管道传输以获取该集合的最后一行(即,文件中的第十行)。

Ruby (1.9+) 红宝石(1.9+)

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

这些答案大多数都可以在我的计算机上从终端运行时使用,但是它们都不在android终端上运行,所以我最终用Java编写了它,谢谢所有帮助

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

相关问题 从 shell 脚本中的目录中选择随机文件的最佳方法 - Best way to choose a random file from a directory in a shell script 使用 bash 脚本将粘贴文件从 Ubuntu 目录复制到 Windows 目录 - Copy paste file from an Ubuntu directory to Windows directory using bash script Shell脚本读取输入并复制和粘贴 - Shell Script read input and copy and paste 如何使用 shell 脚本将最新文件从 sftp 复制到本地目录? - How to copy latest file from sftp to local directory using shell script? Shell脚本-按键从文件复制行 - Shell script - copy lines from file by key bash shell脚本复制第一个文件,对当前目录具有一定的扩展名 - bash shell script copy first file with certain extension to current directory 从外部目录运行Shell脚本:没有此类文件或目录 - Running Shell Script From External Directory: No such file or directory Linux Shell脚本复制目录中文件的最后几行以追加到另一个目录中的文件 - Linux shell script to copy last lines of a file in a directory to append to a file in another directory 从期望脚本调用 bash shell 脚本失败:“没有这样的文件或目录” - Calling bash shell script from expect script fails : “no such file or directory” 使用 shell 脚本将特定单词从一个文件复制到另一个文件 - Copy specific word from a file to another file using shell script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM