我的服务器上文件的路径结构类似于以下所示,
/home/sun/sdir1/mp4/file.mp4
/home/sun/collection/sdir2/mp4/file.mp4
我想将“mp4”文件上移到一个级别(分别进入sdir1和sdir2)
所以输出应该是
/home/sun/sdir1/file.mp4
/home/sun/collection/sdir2/file.mp4
最佳答案
有多种解决问题的方法
cd /home/sun/
mv sdir1/mp4/file.mp4 sdir1/
mv sdir2/mp4/file.mp4 sdir2/
cd /home/sun/
mv sdir1/mp4/*.mp4 sdir1/
mv sdir2/mp4/*.mp4 sdir2/
编辑:
创建一个脚本并命名,然后使用您喜欢的编辑器(nano,vim,gedit等)进行编辑:
gedit folderIterator.sh
脚本文件的内容为:
#/bin/bash
# Go to the desired directory
cd /home/sun/
# Do an action over all the subdirectories in the folder
for dir in /home/sun/*/
do
dir=${dir%*/}
mv "$dir"/mp4/*.mp4 "$dir"/
# If you want to remove the subdirectory after moving the files, uncomment the following line
# rm -rf "$dir"
done
保存文件并赋予其执行权限:
chmod +x folderIterator.sh
并执行它:
./folderIterator.sh