我的服务器上文件的路径结构类似于以下所示,

/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/
    
  • 如果要移动这些目录(sdir1和sdir2)上的所有mp4文件,请运行以下命令:

    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
    

    07-24 09:48
    查看更多