我有几个文件夹,名称如下:
里面有一个同名的pdf文件:
我想移动到每个文件夹并移动 .pdf,以便它们都在一个文件夹中,例如带有脚本的“allpdf”
我试过这个:
for folder in *; do
cd $folder
mv "*.pdf" "../"
done
答案 0 :(得分:0)
你的第一个 cd 把你放在目录中,你留在那里,所以下一个 cd 不能工作(你比你想象的低一级) .
所以,要么你这样做:
for folder in *; do
cd $folder
mv "*.pdf" "../"
cd ..
done
或者(在我看来最好),你这样做:
for folder in *; do
mv "$folfer/*.pdf" "."
done
答案 1 :(得分:0)
如果您使用的是 bash shell,请尝试以下操作: 您应该位于所有包含 pdf 文件的子目录的父目录中。因此,让我们将您的父目录称为 pdf_parent_dir,该目录中应包含文件夹 4553、6723、7765 等,其中包含 pdf 文件,然后运行以下命令:
# moving to the desired parent directory
cd pdf_parent_directory
# to move files to this directory
mkdir allpdf
# to enable bash star globbing
shopt -s globstar
# finally moving all pdf files from subdirectories to allpdf directory
mv **/*.pdf allpdf/
根据你提到的,不应该有任何同名的文件,但如果有,那么 mv 命令将失败,因为它试图将同名的文件移动到最终的 allpdf/ 目录中
我在我的 Linux 机器上尝试了这个,创建了一个与你提到的类似的文件夹和文件结构,它按预期工作。如果您遇到任何错误,请告诉我