从多个文件夹中提取特定文件并将它们移动到新的、具有单一文件结构的文件夹中,可以使用 mkdir
, find
, cp
, 和 xargs
命令。以下是三种不同的方案:
已知目录结构
代码实现
#!/bin/bash
# 目标文件夹基础路径
base_path="/seu_share/home/chaojie/230218989/20240120_olp/03_cellranger_counts"
# 新的目录路径
new_base_path="/seu_share/home/chaojie/230218989/20240120_olp/04_filtered_counts"
# 创建新的目录结构
mkdir -p "$new_base_path"
# 复制文件
for folder in Healthy01 Healthy02 OLP01 OLP02 OLP03 OLP04; do
mkdir -p "$new_base_path/$folder"
cp "$base_path/$folder/outs/filtered_feature_bc_matrix/"* "$new_base_path/$folder/"
done
最终结果:
#!/bin/bash
# 目标文件夹基础路径
base_path="/seu_share/home/chaojie/230218989/20240120_olp/03_cellranger_counts"
# 新的目录路径
new_base_path="/seu_share/home/chaojie/230218989/20240120_olp/04_html"
# 创建新的目录结构
mkdir -p "$new_base_path"
# 复制文件
for folder in Healthy01 Healthy02 OLP01 OLP02 OLP03 OLP04; do
mkdir -p "$new_base_path/$folder"
cp "$base_path/$folder/outs/"web_summary.html "$new_base_path/$folder/"
done
未经验证代码:
要实现从多个文件夹中提取特定文件并将它们移动到新的、具有单一文件结构的文件夹中,可以使用 mkdir, find, cp, 和 xargs 命令。以下是三种不同的方案:
方案 1: 使用循环和 cp 命令
这个方案遍历特定的文件夹,然后复制目标文件到新的位置。
bash
Copy code
#!/bin/bash
# 目标文件夹基础路径
base_path="/seu_share/home/chaojie/230218989/20240120_olp/03_cellranger_counts"
# 新的目录路径
new_base_path="/path/to/new/directory"
# 创建新的目录结构
mkdir -p "$new_base_path"
# 复制文件
for folder in Healthy01 Healthy02 OLP01 OLP02 OLP03 OLP04; do
mkdir -p "$new_base_path/$folder"
cp "$base_path/$folder/outs/filtered_feature_bc_matrix/"* "$new_base_path/$folder/"
done
方案 2: 使用 find 和 cp 命令
这个方案使用 find 命令来寻找目标文件,并使用 cp 命令将它们复制到新的位置。
bash
Copy code
#!/bin/bash
# 目标文件夹基础路径
base_path="/seu_share/home/chaojie/230218989/20240120_olp/03_cellranger_counts"
# 新的目录路径
new_base_path="/path/to/new/directory"
# 创建新的目录结构并复制文件
for folder in Healthy01 Healthy02 OLP01 OLP02 OLP03 OLP04; do
mkdir -p "$new_base_path/$folder"
find "$base_path/$folder/outs/filtered_feature_bc_matrix" -maxdepth 1 -type f \( -name "barcodes.tsv.gz" -o -name "features.tsv.gz" -o -name "matrix.mtx.gz" \) -exec cp {} "$new_base_path/$folder/" \;
done
方案 3: 使用 find 和 xargs 命令
这个方案结合使用 find 和 xargs 来查找并复制文件。这对于处理大量文件时更为高效。
bash
Copy code
#!/bin/bash
# 目标文件夹基础路径
base_path="/seu_share/home/chaojie/230218989/20240120_olp/03_cellranger_counts"
# 新的目录路径
new_base_path="/path/to/new/directory"
# 创建新的目录结构并复制文件
for folder in Healthy01 Healthy02 OLP01 OLP02 OLP03 OLP04; do
mkdir -p "$new_base_path/$folder"
find "$base_path/$folder/outs/filtered_feature_bc_matrix" -maxdepth 1 -type f \( -name "barcodes.tsv.gz" -o -name "features.tsv.gz" -o -name "matrix.mtx.gz" \) | xargs -I {} cp {} "$new_base_path/$folder/"
done
在这些脚本中,你需要将 "/path/to/new/directory" 替换为你想要创建新文件的目标路径。确保你有足够的权限在目标路径上创建文件夹和复制文件。