Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.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
Bash:如何将多个同名文件复制到多个文件夹_Bash_Copy_Copy Paste - Fatal编程技术网

Bash:如何将多个同名文件复制到多个文件夹

Bash:如何将多个同名文件复制到多个文件夹,bash,copy,copy-paste,Bash,Copy,Copy Paste,我在Linux机器上工作。 我有很多同名文件,目录结构如下: P45_input_foo/result.dat P45_input_bar/result.dat P45_input_tar/result.dat P45_input_cool/result.dat ... 很难一个接一个地复制它们。我想将它们复制到另一个名为data的文件夹中,文件夹名和文件名类似: /data/foo/result.dat /data/bar/result.dat /data/tar/result.dat /d

我在Linux机器上工作。 我有很多同名文件,目录结构如下:

P45_input_foo/result.dat
P45_input_bar/result.dat
P45_input_tar/result.dat
P45_input_cool/result.dat ...
很难一个接一个地复制它们。我想将它们复制到另一个名为
data
的文件夹中,文件夹名和文件名类似:

/data/foo/result.dat
/data/bar/result.dat
/data/tar/result.dat
/data/cool/result.dat ...

我应该做什么,而不是一个接一个地复制它们?

您需要提取“\u”之后的第三项:

P45_输入_foo-->foo

创建目录(如果需要)并将文件复制到其中。类似这样的内容(未测试,可能需要编辑):

启动_DIR=“/”
cd“$STARTING_DIR”
VAR=$(ls-1)
读DIR时;做
TARGET_DIR=$(回显“$DIR”| cut-d'-f3)
NEW_DIR=“/data/$DIR”
如果[!-d“$NEW_DIR”];然后
mkdir“$NEW_DIR”
fi
cp“$DIR/result.dat”$NEW_DIR/result.dat”
如果[$?-ne 0];
echo“错误:复制时遇到错误”
fi

在bash中使用for循环完成:

# we list every files following the pattern : ./<somedirname>/<any file>
# if you want to specify a format for the folders, you could change it here
# i.e. for your case you could write 'for f in P45*/*' to only match folders starting by P45 
for f in */*
do
    # we strip the path of the file from its filename
    # i.e. 'P45_input_foo/result.dat' will become 'P45_input_foo'
    newpath="${f%/*}"

    # mkdir -p /data/${newpath##*_} will create our new data structure
    # - /data/${newpath##*_} extract the last chain of character after a _, in our example, 'foo'
    # - mkdir -p will recursively create our structure
    # - cp "$f" "$_" will copy the file to our new directory. It will not launch if mkdir returns an error
    mkdir -p /data/${newpath##*_} && cp "$f" "$_"
done

#我们按照以下模式列出每个文件:./.

一点
find
和一些
bash
技巧,下面的脚本可以为您完成这些技巧。请记住在不使用
mv
的情况下运行脚本,并查看
“/data/“$folder”/“
是否是要移动文件的实际路径

#/bin/bash
而IFS=read-r-d“”文件
做
fileNew=“${file%/*}”#最后一个“\”之前的所有内容
fileNew=“${fileNew#*/}”最后一个“\”之后的所有内容

IFS=“\u”读取\uuuuuuuuuuuu”文件夹您是在Linux还是Windows机器上?您需要
bash
batch
解决方案吗?@Inian我正在linux机器上工作。所以我需要一个bash解决方案。谢谢你提醒我嗨,可能是我解释得不对。P45是一个目录中PXX的一个系列。@Lbj\ux抱歉。我还是不明白你的意思。你能举个例子吗?在起始目录中:我有P45_input_*系列文件夹。以及P46和P47等文件夹。我想我需要在开头指定DIR,对吗?@Lbj_x没关系。您只需要指定P45、P46的位置。。。是e、 g.文件位于/test/P45、/test/P46中,然后启动_DIR=“/test”注意:
if[!-d“$NEW_DIR”];然后是mkdir“$NEW_DIR”;fi
可以缩短为
mkdir-p“$NEW\u DIR”
谢谢您的回答您能提供更多细节吗
{newpath###*.}
对我来说似乎很奇怪。你能帮我投票吗?我没有重复我的问题。@Lbj\u x:我没有投你反对票,但不管是谁投的,都可能投了反对票,因为你没有向我们展示任何你试图解决问题的代码。
# we list every files following the pattern : ./<somedirname>/<any file>
# if you want to specify a format for the folders, you could change it here
# i.e. for your case you could write 'for f in P45*/*' to only match folders starting by P45 
for f in */*
do
    # we strip the path of the file from its filename
    # i.e. 'P45_input_foo/result.dat' will become 'P45_input_foo'
    newpath="${f%/*}"

    # mkdir -p /data/${newpath##*_} will create our new data structure
    # - /data/${newpath##*_} extract the last chain of character after a _, in our example, 'foo'
    # - mkdir -p will recursively create our structure
    # - cp "$f" "$_" will copy the file to our new directory. It will not launch if mkdir returns an error
    mkdir -p /data/${newpath##*_} && cp "$f" "$_"
done
#!/bin/bash

while IFS= read -r -d '' file
do

    fileNew="${file%/*}"       # Everything before the last '\'
    fileNew="${fileNew#*/}"    # Everything after the last '\'

    IFS="_" read _ _ folder <<<"$fileNew"

    mv -v "$file"  "/data/"$folder"/"

done < <(find . -type f -name "result.dat" -print0)