我有一个文件名,文件名如下:
file1.txt
PCSTW.ABO.8.inv.term.2.Combined.inweb3.Pancreas_inweb3.txt.0.solution
PCSTW.ABO.8.inv.term.3.Combined.inweb3.Pancreas_inweb3.txt.0.solution
PCSTW.ABO.8.inv.term.2.Combined.inweb3.Arteria_inweb3.txt.0.solution
PCSTW.ABO.8.inv.term.3.Combined.inweb3.Arteria_inweb3.txt.0.solution
PCSTW.ABO.8.inv.term.2.Combined.inweb3.Adipose_inweb3.txt.0.solution
我想要的是基于Combined.inweb3
之后和_inweb3.txt.0.solution
之后的唯一名称创建新目录,然后将具有唯一名称的文件移动到新目录,这里是一个描述性的想法我想实现
# Create directories
mkdir Pancreas
mkdir Arteria
mkdir Adipose
# Move files to new directories
mv *.Pancreas_*.solution Pancreas/
mv *.Arteria_*.solution Arteria/
mv *.Adipose_*.solution Adipose/
到目前为止,我正在使用grep
和mv
,但我有很多文件并需要一些时间
答案 0 :(得分:2)
像这样的小bash
脚本应该这样做:
#!/bin/bash
while read f; do
# Get the directory name and make it
d=$(sed -e 's/_inweb3.*//' -e 's/.*inweb3.//' <<< "$f")
echo Directory: $d
mkdir -p "$d"
echo cp "$f" "$d"
done < file1
示例输出
Directory: Pancreas
cp PCSTW.ABO.8.inv.term.2.Combined.inweb3.Pancreas_inweb3.txt.0.solution Pancreas
Directory: Pancreas
cp PCSTW.ABO.8.inv.term.3.Combined.inweb3.Pancreas_inweb3.txt.0.solution Pancreas
Directory: Arteria
cp PCSTW.ABO.8.inv.term.2.Combined.inweb3.Arteria_inweb3.txt.0.solution Arteria
Directory: Arteria
cp PCSTW.ABO.8.inv.term.3.Combined.inweb3.Arteria_inweb3.txt.0.solution Arteria
Directory: Adipose
cp PCSTW.ABO.8.inv.term.2.Combined.inweb3.Adipose_inweb3.txt.0.solution Adipose