试图将特定文件复制到特定文件夹,但有一个问题
Trying to copy specific files to specific folders, but there's a catch
我正在尝试将特定文件复制到特定文件夹,但文件名类似于 Long_John_Silver
、Miss_Havisham
和 Master_Pip
,文件夹名称类似于 Long
、Miss
和 Master
。
所以基本上我试图将文件复制到它们各自的文件夹中,例如Master_Pip.txt 到名为 Master 的文件夹中。
因此,我试图做的是捕获文件名的第一个单词,并以某种方式将其用作参考,但是,正是在这一点上,我犹豫不决。
for fldr in /home/playground/genomes* ; do
find . -name *fna.gz | while read f ; do
f_name=$( echo $fldr | cut -d '/' -f 7 | cut -d '_' -f 1) ;
#echo $f_name ;
if [ "$f_name" == /home/playground/port/"$f_name" ]; then
cp -r "$f" /home/playground/port/"$f_name"/ ;
else
continue
fi
done
done
编辑---------------------------------------- ---
for fldr in /home/p995824/scripts/playground/genomes_2/* ; do
find . -name *fna.gz | while read f ; do
basenm=${fldr##*/} ; f_name=${basenm%%_*} ;
cp -r $f /home/p995824/scripts/playground/port/$f_name/ ;
done
done
此脚本将所有文件复制到所有文件夹。但是我正在努力构建一个条件语句,该语句将特定于将每个文件复制到哪个文件夹。如您所见,我已经尝试了 if 语句,但我一定是错误地构建了它。非常感谢任何帮助。
我认为您的比较 if [ "$f_name" == /home/playground/port/"$f_name" ]
不正确。 $f_name
是文件名的前缀,您将其与完整路径进行比较。
试试这个:
for fldr in /home/playground/genomes* ; do
find . -name *fna.gz | while read f ; do
# basename will truncate the path leaving only the filename
f_name=$( basename ${fldr} | cut -d'_' -f1) ;
# $f_name is the filename prefix "Long", "Master", etc
# if the directory exists (-d)
if [ -d "/home/playground/port/$f_name" ]; then
# I don't think you need (-r) here if you're just copying a single file
cp "$f" "/home/playground/port/$f_name/" ;
else
continue
fi
done
done
这是一个可能的解决方案:
for fldr in `find /tmp/playground/ -type f`
do
NAME=`basename $fldr | cut -d_ -f1`
DEST=/tmp/playground/$NAME
if [ -d "$DEST" ]
then
cp $fldr $DEST
fi
done
也许你可以反过来做?搜索与文件夹名称匹配的文件并复制它们,而不是从文件名的第一个单词搜索文件夹。不过我可能错了.. :)
for folder_name in /home/playground/port/*
do
cp /home/playground/folder_name* $folder_name
done
我意识到我正在根据文件类型将所有文件复制到所有文件夹中,即 .fna.gz,所以我指定了我想要读取的 fna.gz 文件类型,并且然后复制。不需要 if 语句,因为通过参数扩展隐含了特异性。它现在完美运行。
for fldr in /home/scripts/playground/genomes_2/* ; do
basenm=${fldr##*/} ; f_name=${basenm%%_*} ;
find . -name $f_name*fna.gz | while read f ; do
cp -r $f /home/scripts/playground/port/$f_name/ ;
done
done
我正在尝试将特定文件复制到特定文件夹,但文件名类似于 Long_John_Silver
、Miss_Havisham
和 Master_Pip
,文件夹名称类似于 Long
、Miss
和 Master
。
所以基本上我试图将文件复制到它们各自的文件夹中,例如Master_Pip.txt 到名为 Master 的文件夹中。
因此,我试图做的是捕获文件名的第一个单词,并以某种方式将其用作参考,但是,正是在这一点上,我犹豫不决。
for fldr in /home/playground/genomes* ; do
find . -name *fna.gz | while read f ; do
f_name=$( echo $fldr | cut -d '/' -f 7 | cut -d '_' -f 1) ;
#echo $f_name ;
if [ "$f_name" == /home/playground/port/"$f_name" ]; then
cp -r "$f" /home/playground/port/"$f_name"/ ;
else
continue
fi
done
done
编辑---------------------------------------- ---
for fldr in /home/p995824/scripts/playground/genomes_2/* ; do
find . -name *fna.gz | while read f ; do
basenm=${fldr##*/} ; f_name=${basenm%%_*} ;
cp -r $f /home/p995824/scripts/playground/port/$f_name/ ;
done
done
此脚本将所有文件复制到所有文件夹。但是我正在努力构建一个条件语句,该语句将特定于将每个文件复制到哪个文件夹。如您所见,我已经尝试了 if 语句,但我一定是错误地构建了它。非常感谢任何帮助。
我认为您的比较 if [ "$f_name" == /home/playground/port/"$f_name" ]
不正确。 $f_name
是文件名的前缀,您将其与完整路径进行比较。
试试这个:
for fldr in /home/playground/genomes* ; do
find . -name *fna.gz | while read f ; do
# basename will truncate the path leaving only the filename
f_name=$( basename ${fldr} | cut -d'_' -f1) ;
# $f_name is the filename prefix "Long", "Master", etc
# if the directory exists (-d)
if [ -d "/home/playground/port/$f_name" ]; then
# I don't think you need (-r) here if you're just copying a single file
cp "$f" "/home/playground/port/$f_name/" ;
else
continue
fi
done
done
这是一个可能的解决方案:
for fldr in `find /tmp/playground/ -type f`
do
NAME=`basename $fldr | cut -d_ -f1`
DEST=/tmp/playground/$NAME
if [ -d "$DEST" ]
then
cp $fldr $DEST
fi
done
也许你可以反过来做?搜索与文件夹名称匹配的文件并复制它们,而不是从文件名的第一个单词搜索文件夹。不过我可能错了.. :)
for folder_name in /home/playground/port/*
do
cp /home/playground/folder_name* $folder_name
done
我意识到我正在根据文件类型将所有文件复制到所有文件夹中,即 .fna.gz,所以我指定了我想要读取的 fna.gz 文件类型,并且然后复制。不需要 if 语句,因为通过参数扩展隐含了特异性。它现在完美运行。
for fldr in /home/scripts/playground/genomes_2/* ; do
basenm=${fldr##*/} ; f_name=${basenm%%_*} ;
find . -name $f_name*fna.gz | while read f ; do
cp -r $f /home/scripts/playground/port/$f_name/ ;
done
done