[英]how to move Files to a folder using mv in bash with condotions?
我正在编写这段代码来组织我的下载文件夹,这样它就不会乱七八糟了。 我希望将文件移动到正确的文件夹,例如 mp4,它应该转到 Videos 文件夹。 但代码不起作用
#! /bin/bash
if [[ -d Videos/ ]] && [[ -d Compressed/ ]] && [[ -d Music/ ]] && [[ -d Pictures/ ]] && [[ -d Executables/ ]] && [[ -d Misc/ ]]
then
#Compressed Files
if [[ -f *.7z ]]
then
mv *.7z Compressed/
fi
if [[ -f *.rar ]]
then
mv *.rar Compressed/
fi
if [[ -f *.zx ]]
then
mv *.xz Compressed/
fi
if [[ -f *.zip ]]
then
mv *.zip Compressed/
fi
if [[ -f *.gz ]]
then
mv *.gz Compressed/
fi
if [[ -f *.iso ]]
then
mv *.iso Compressed/
fi
#Videos
if [[ -f *.mp4 ]]
then
mv *.mp4 Videos/
fi
if [[ -f *.mov ]]
then
mv *.mov Videos/
fi
if [[ -f *.wvm ]]
then
mv *.wvm Videos/
fi
if [[ -f *.mkv ]]
then
mv *.mkv Videos/
fi
#Musics
if [[ -f *.mp3 ]]
then
mv *.mp3 Music/
fi
if [[ -f *.wav ]]
then
mv *.wav Music/
fi
if [[ -f *.ogg ]]
then
mv *.ogg Music/
fi
#Pictures
if [[ -f *.png ]]
then
mv *.png Pictures/
fi
if [[ -f *.jpg ]]
then
mv *.jpg Pictures/
fi
if [[ -f *.jpeg ]]
then
mv *.jpeg Pictures/
fi
if [[ -f *.svg ]]
then
mv *.svg Pictures/
fi
if [[ -f *.webp ]]
then
mv *.webp Pictures/
fi
#documents
if [[ -f *.doc ]]
then
mv *.doc Documents/
fi
if [[ -f *.docx ]]
then
mv *.docx Documents/
fi
if [[ -f *.pdf ]]
then
mv *.pdf Documents/
fi
if [[ -f *.pptx ]]
then
mv *.pptx Documents/
fi
if [[ -f *.ppt ]]
then
mv *.ppt Documents/
fi
if [[ -f *.xls ]]
then
mv *.xls Documents/
fi
if [[ -f *.xlsx ]]
then
mv *.xlsx Documents/
fi
#Executables
if [[ -x *.64 ]]
then
mv *.64 Executables/
fi
if [[ -f *.deb ]]
then
mv *.deb Executables/
fi
if [[ -f *.rpm ]]
then
mv *.rpm Executables/
fi
#Misc FIles (Unknown file formats)
else
echo "No Directories found"
sleep 1
echo "Creating Directories"
mkdir Videos/ Compressed/ Music/ Pictures/ Executables/ Documents/ Misc/
sleep 1
echo "moving files"
#Compressed Files
if [[ -f *.7z ]]
then
mv *.7z Compressed/
fi
if [[ -f *.rar ]]
then
mv *.rar Compressed/
fi
if [[ -f *.zx ]]
then
mv *.xz Compressed/
fi
if [[ -f *.zip ]]
then
mv *.zip Compressed/
fi
if [[ -f *.gz ]]
then
mv *.gz Compressed/
fi
if [[ -f *.iso ]]
then
mv *.iso Compressed/
fi
#Videos
if [[ -f *.mp4 ]]
then
mv *.mp4 Videos/
fi
if [[ -f *.mov ]]
then
mv *.mov Videos/
fi
if [[ -f *.wvm ]]
then
mv *.wvm Videos/
fi
if [[ -f *.mkv ]]
then
mv *.mkv Videos/
fi
#Musics
if [[ -f *.mp3 ]]
then
mv *.mp3 Music/
fi
if [[ -f *.wav ]]
then
mv *.wav Music/
fi
if [[ -f *.ogg ]]
then
mv *.ogg Music/
fi
#Pictures
if [[ -f *.png ]]
then
mv *.png Pictures/
fi
if [[ -f *.jpg ]]
then
mv *.jpg Pictures/
fi
if [[ -f *.jpeg ]]
then
mv *.jpeg Pictures/
fi
if [[ -f *.svg ]]
then
mv *.svg Pictures/
fi
if [[ -f *.webp ]]
then
mv *.webp Pictures/
fi
#documents
if [[ -f *.doc ]]
then
mv *.doc Documents/
fi
if [[ -f *.docx ]]
then
mv *.docx Documents/
fi
if [[ -f *.pdf ]]
then
mv *.pdf Documents/
fi
if [[ -f *.pptx ]]
then
mv *.pptx Documents/
fi
if [[ -f *.ppt ]]
then
mv *.ppt Documents/
fi
if [[ -f *.xls ]]
then
mv *.xls Documents/
fi
if [[ -f *.xlsx ]]
then
mv *.xlsx Documents/
fi
#Executables
if [[ -x *.64 ]]
then
mv *.64 Executables/
fi
if [[ -f *.deb ]]
then
mv *.deb Executables/
fi
if [[ -f *.rpm ]]
then
mv *.rpm Executables/
fi
#Mics FIles (Unknown file formats)
fi
我猜它来自'if'语句,但我不知道如何解决它,因为我对 bash 很陌生。 mkdir 正在工作,但所有“if [[ -f *.mp4 ]]”都没有。
文件名扩展不在 bash 复合命令[[...]]
中执行。 因此,例如, [[ -f *.7z ]]
*.7z
的 *.7z 字面意思是文件名*.7z
。 这就是为什么你的脚本不起作用的原因。 下面的脚本可能是您想要的:
#!/bin/bash
mkdir -p Videos Compressed Music Pictures Documents Executables Misc || exit
for f in ./*.{7z,rar,zx,zip,gz,iso}; do
[[ -f $f ]] && mv "$f" Compressed
done
for f in ./*.{mp4,mov,wvm,mkv}; do
[[ -f $f ]] && mv "$f" Videos
done
for f in ./*.{mp3,wav,ogg}; do
[[ -f $f ]] && mv "$f" Music
done
for f in ./*.{png,jpg,jpeg,svg,webp}; do
[[ -f $f ]] && mv "$f" Pictures
done
for f in ./*.{doc,docx,pdf,pptx,ppt,xls,xlsx}; do
[[ -f $f ]] && mv "$f" Documents
done
注意mkdir
中-p
选项的使用。 此选项将创建缺少的目录并忽略现有目录而不会出错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.