我的目录和文件的结构是:
Folder1:
File1
Folder2:
File2
File3
File4
Folder3:
File5
File6
File7
file8
我想要实现的是复制File1, File2, fil6和fil7到一个文件夹4。我需要一个脚本因为我有多个这样的集合我想要一个由不同的文件夹组成的集合文件夹5:
Folder5:
Folder4a:
File1, File2, File6, File7
Filder4b:
File1, File2,File6, File7
等。所有的文件都有相同的前缀,但扩展名不同,并且包含链接在一起的不同信息。
非常感谢你的帮助!
#!/bin/bash
export output_dir='./Folder5'
if [ ! -e ${output_dir} ]
then
echo ${output_dir}
mkdir ${output_dir}
fi
for i in `ls file1`
do
export folder=`echo ${i} | awk -F . '{print $1}'`
if [ ! -f ${folder} ]
then
mkdir ./${output_dir}/${folder}
cp ${i} ./${output_dir}/${folder}
fi
done
这创建了Folder5,在Folder1中,我的File1是它复制它的前缀创建Folder4命名为File1的前缀,并将File1复制到该文件夹。现在我有一个问题,我如何访问子文件夹中的其他文件,以及如何将它们复制到文件夹4。
#!/usr/bin/env bash
mkdir -p /tmp/testing
cd /tmp/testing || exit
mkdir -vp folder{1..4}
touch folder1/file1
touch folder2/file{2..4}
touch folder3/file{5..8}
我试了这个,文件的结构不像我有一个。在我的例子中,folder3在folder2里面。这个脚本在文件夹1中创建文件夹2-4。
并进入子文件夹,然后从中复制文件,然后退出并移动到另一个子文件夹进行复制是我有问题的部分。在代码中,我张贴了我的问题,我只能复制文件1,这是在最顶级的目录。
使用shell和find
,例如:
#!/usr/bin/env bash
##: Save the files that needs to be move in an array.
files_to_move=(
file1
file2
file6
file7
)
##: Save the name of folder4 in a variable and export it
##: In order for `find` to use it.
destination=folder4
export destination
##: Set the files to be moved in a format that find understands.
for i in "${!files_to_move[@]}"; do
files+=(-o -name "${files_to_move[$i]}")
done
##: Execute find and feed the file names to it and copy it
##: To folder4 aka "$destination"
find . -type f ( "${files[@]:1}" ) -exec sh -c '
cp -v -- "$@" "$destination"' _ {} +
##: Save folder5 in a variable and create if it does not exists.
final_destination=folder5
[[ ! -d "$final_destination" ]] &&
mkdir -vp "$final_destination"
##: Copy folder4 in folder5 with the desired folder names.
cp -rv "$destination/" "$final_destination/${destination}a"
cp -rv "$destination/" "$final_destination/${destination}b"
对下面脚本创建的文件/目录进行测试。
#!/usr/bin/env bash
mkdir -p /tmp/testing
cd /tmp/testing || exit
mkdir -vp folder{1..4}
touch folder1/file1
touch folder2/file{2..4}
touch folder3/file{5..8}
这是一个脚本,用于创建OP的目录/文件结构,或者至少是描述的内容。
#!/usr/bin/env bash
mkdir -p /tmp/testing
cd /tmp/testing || exit
mkdir -vp folder1/folder2/folder3/folder4/
touch folder1/file1
touch folder1/folder2/file{2..4}
touch folder1/folder2/folder3/file{5..8}
创建的文件和目录如下所示。
/tmp/testing
└── folder1
├── file1
└── folder2
├── file2
├── file3
├── file4
└── folder3
├── file5
├── file6
├── file7
├── file8
└── folder4
4 directories, 8 files
脚本。
#!/usr/bin/env bash
##: Save the files that needs to be move in an array.
files_to_move=(
file1
file2
file6
file7
)
##: Save the name of folder4 in a variable and export it
##: In order for `find` to use it.
##: A hardcoded path below
##: destination=(folder1/folder2/folder3/folder4)
IFS= read -r destination < <(find -type d -name folder4)
export destination
##: Set the files to be moved in a format that find understands.
for i in "${!files_to_move[@]}"; do
files+=(-o -name "${files_to_move[$i]}")
done
##: Execute find and feed the file names to it and copy it
##: To folder4 aka "$destination"
find . -type f ( "${files[@]:1}" ) -exec sh -c '
cp -v -- "$@" "$destination"' _ {} +
##: Save folder5 in a variable and create if it does not exists.
##: A hardcoded path is the one below
##: final_destination=foler1/folder2/folder3/folder4/folder5
final_destination="${destination/4/5}"
[[ ! -d "$final_destination" ]] &&
mkdir -vp "$final_destination"
##: Copy folder4 in folder5 with the desired folder names.
cp -rv "$destination" "$final_destination/${destination##*/}a"
cp -rv "$destination" "$final_destination/${destination##*/}b"
在/tmp/testing/
内执行后
/tmp/testing
└── folder1
├── file1
└── folder2
├── file2
├── file3
├── file4
└── folder3
├── file5
├── file6
├── file7
├── file8
├── folder4
│ ├── file1
│ ├── file2
│ ├── file6
│ └── file7
└── folder5
├── folder4a
│ ├── file1
│ ├── file2
│ ├── file6
│ └── file7
└── folder4b
├── file1
├── file2
├── file6
└── file7
- 看到Understanding-the-exec-option-of-find