gpt4 book ai didi

linux - 试图将目录中的第 n 个文件复制到另一个文件夹

转载 作者:太空宇宙 更新时间:2023-11-04 11:00:05 27 4
gpt4 key购买 nike

我想生成一个随机数,然后用这个随机数来抓取目录中的相应文件,这将反过来创建一个随机的音乐歌曲播放列表。我有 3 个参数,你的音乐路径,新播放列表的名称,以及你想要在该播放列表中播放多少首歌曲。由于某种原因,在 while 循环的每次迭代中,它都会复制多个文件,而不仅仅是第 n 个随机文件

#!/bin/bash

#accepts 3 arguments path to music, name of the new playlist, and amount of songs you want in the playlist
path_to_music=$1;
playlistname=$2;
count=$3;

#finds number of songs in directory, allows for more effective randomness


#cd to the path where music is
if [ -d "$path_to_music" ];
then
eval cd $path_to_music
numOfFiles=$(find . -type f | wc -l)
else
echo "Directory does not exist, enter correct path."
read path_to_file
fi

ran=$[ ( $RANDOM % 5 ) + 1 ]
let ran=numOfFiles-ran;
#make new folder where music is, will contain random songs (new playlist)
mkdir $playlistname

i=1
while : #while loop finds a random song and copies it to folder created, $cnt is randomly generated so songs are random
do
if [ "$i" -ne "$count" ];
then
cnt=$[ ( $RANDOM % 34) + 1 ]
let "cnt*=(-1)"
find -type f | head $cnt tail $cnt | xargs -I % cp % $path_to_music/$playlistname ;
let "i+=1"
echo $i
else
break;
fi
done;

最佳答案

关于:

printf '%s\n' * | shuf | head -1 | xargs -I% cp % /another/dir/%

关于linux - 试图将目录中的第 n 个文件复制到另一个文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27301663/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com