Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 按扩展名选择随机文件,然后选择";“已连接”;文件夹_File_Unix_Random - Fatal编程技术网

File 按扩展名选择随机文件,然后选择";“已连接”;文件夹

File 按扩展名选择随机文件,然后选择";“已连接”;文件夹,file,unix,random,File,Unix,Random,好吧,我被难住了。我需要一个shell脚本,它将随机选择扩展名为.sct的文件。使用文件名的一部分选择六个相关的.mot文件。然后将它们全部移动到另一个文件夹。我还需要一个用户输入的文件数量随机选择 我有一个这样的文件结构: 123-12345-00.sct 123-12345-00.mot 123-12345-01.mot 123-12345-02.mot 123-12345-03.mot 123-12345-04.mot 123-12345-05.mot 123-12346-00.sct

好吧,我被难住了。我需要一个shell脚本,它将随机选择扩展名为.sct的文件。使用文件名的一部分选择六个相关的.mot文件。然后将它们全部移动到另一个文件夹。我还需要一个用户输入的文件数量随机选择

我有一个这样的文件结构:

123-12345-00.sct 123-12345-00.mot 123-12345-01.mot 123-12345-02.mot 123-12345-03.mot 123-12345-04.mot 123-12345-05.mot 123-12346-00.sct 123-12346-00.mot 123-12346-01.mot 123-12346-02.mot 123-12346-03.mot 123-12346-04.mot 123-12346-05.mot 123-12345-00.sct 123-12345-00.mot 123-12345-01.mot 123-12345-02.mot 123-12345-03.mot 123-12345-04.mot 123-12345-05.mot 123-12346-00.sct 123-12346-00.mot 123-12346-01.mot 123-12346-02.mot 123-12346-03.mot 123-12346-04.mot 123-12346-05.mot 等等。需要随机选择文件.sct并将其及其相关文件移动到另一个目录。希望我已经解释得足够好了

谢谢你的帮助。我可以在VB中实现这一点,但UNIX的问题让我感到困惑。现在,我们通过数千个文件手动执行此操作


Scott

此脚本将
123-12345-*.sct
123-12345-*.mot
文件移动到名为
123-12345
的目录中,依此类推

注意:这不是随机选择一个文件,而是选择目录中的所有文件。您可以修改此选项以接受随机文件数的命令行参数。然后需要修改此命令
ls[0-9]*.sct | grep-oe'[0-9]\{3\}-[0-9]\{5\}
,以使用命令行参数,该参数是一个文件计数,并返回随机数目的前缀

将以下内容复制到与sct和mot文件位于同一目录下的文件中,例如
mv_sct_mot.sh

#!/bin/bash

for prefix in `ls [0-9]*.sct | grep -oe '[0-9]\{3\}\-[0-9]\{5\}'`; do
  mkdir -p ${prefix};
  mv ${prefix}-*.{mot,sct} ${prefix};
done
要使文件可执行,请修改其权限,如:

chmod +x mv_sct_mot.sh
像这样运行:

./mv_sct_mot.sh
#/usr/bin/env bash
dir=“$1”
count=“$2”
[“$dir”]&&[$count-gt 0]&&{
如果[!-d“$dir”];则回显“$0:$dir:无此类目录”;退出;fi;
随机=$(日期+%s)#初始随机种子
((C=0;C< $计数;C++));
files=(*.sct)#创建sct文件数组
ct=${{文件[@]}计算数组长度
如果[$ct-eq 0];则break;fi#不再有.sct文件,退出
sct=${files[$[($RANDOM%$ct)]}选择随机文件
#您可能希望根据您的文件名对此进行更改
#最后一个破折号“-”(包括)之前的所有内容都将被记录
#作为前缀
前缀=$(echo$sct | sed's:\(.*-\).*:\1:')
mot_files=($prefix*.mot)#创建所有匹配的.mot的数组
mv$sct$dir#将.sct移动到$dir
如果[${#mot_文件[@]}-gt 0];则
mv${mot_files[@]}$dir#将每个匹配的.mot移动到$dir
fi
完成
}| | echo“用法:$0”
我会的


/tmp/r>ls 123-12345-00.mot 123-12345-05.mot 123-12346-04.mot 123-12345-00.sct 123-12346-00.mot 123-12346-05.mot 123-12345-01.mot 123-12346-00.sct 123-12348-00.mot 123-12345-02.mot 123-12346-01.mot 123-12348-00.sct 123-12345-03.mot 123-12346-02.mot-foo 123-12345-04.mot 123-12346-03.mot /tmp/r>mkdir条 /tmp/r>/foo bar 2 /tmp/r>ls 123-12346-00.mot 123-12346-02.mot 123-12346-05.mot 123-12346-00.sct 123-12346-03.mot棒 123-12346-01.mot 123-12346-04.mot-foo /tmp/r>负载感应棒 123-12345-00.mot 123-12345-02.mot 123-12345-05.mot 123-12345-00.sct 123-12345-03.mot 123-12348-00.mot 123-12345-01.mot 123-12345-04.mot 123-12348-00.sct
以下是我的做法:

#!/bin/bash
## use the first param as count (1 as default value)
count=${1:-1}
## list all .sct files, random sort and pick the first $count
ls $SRCDIR/*.sct | sort -R | head -n $count | 
    while read file; do
        ## for each file, figure out the prefix and move prefix*
        prefix="${file%-*}-"
        mv -v $prefix* $DESTDIR
    done

编辑:我最初错过了文件数作为参数的部分,现在更新了脚本。为了清楚起见,我跳过了设置SRCDIR和DESTDIR的部分。

为什么选择mot文件以在以后丢弃它们?还有:随机部分从何而来?为清晰起见,对随机部分进行了编辑。mot文件不会被丢弃。它们被移动到目标目录。我的意思是:ls选择所有的.sct和.mot文件,只要选择.sct文件就足够了。当没有double.sct文件时(double的意思是:123-12345-00.sct和123-12345-01.sct),您甚至不需要这样排序和uniq。您是对的,不需要选择mot文件。这将避免重复前缀,我们可以完全取消sort和uniq。谢谢大家:-)。编辑答案。非常感谢您的帮助。我现在正在尝试使用它。我把代码完全按照它写的那样放进去,我得到了“没有这样的目录”错误。我肯定错过了一些简单的事情。正如我可能已经提到的,这超出了我的舒适区。也许可以在这里找到想法:+1:太棒了!斯科特,你欠伯特兰一瓶最好的香槟!祝大家好运。 /tmp/r > ls 123-12345-00.mot 123-12345-05.mot 123-12346-04.mot 123-12345-00.sct 123-12346-00.mot 123-12346-05.mot 123-12345-01.mot 123-12346-00.sct 123-12348-00.mot 123-12345-02.mot 123-12346-01.mot 123-12348-00.sct 123-12345-03.mot 123-12346-02.mot foo 123-12345-04.mot 123-12346-03.mot /tmp/r > mkdir bar /tmp/r > ./foo bar 2 /tmp/r > ls 123-12346-00.mot 123-12346-02.mot 123-12346-05.mot 123-12346-00.sct 123-12346-03.mot bar 123-12346-01.mot 123-12346-04.mot foo /tmp/r > ls bar 123-12345-00.mot 123-12345-02.mot 123-12345-05.mot 123-12345-00.sct 123-12345-03.mot 123-12348-00.mot 123-12345-01.mot 123-12345-04.mot 123-12348-00.sct
#!/bin/bash
## use the first param as count (1 as default value)
count=${1:-1}
## list all .sct files, random sort and pick the first $count
ls $SRCDIR/*.sct | sort -R | head -n $count | 
    while read file; do
        ## for each file, figure out the prefix and move prefix*
        prefix="${file%-*}-"
        mv -v $prefix* $DESTDIR
    done