gpt4 book ai didi

linux - 什么是linux命令将子目录的文件分别向上移动一级

转载 作者:太空狗 更新时间:2023-10-29 12:02:15 25 4
gpt4 key购买 nike

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

/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

I have no idea to do this, so not tried yet anything...

最佳答案

有多种方法可以解决您的问题

  1. 如果您只想移动那些特定文件,请运行这些命令:

    cd /home/sun/
    mv sdir1/mp4/file.mp4 sdir1/
    mv sdir2/mp4/file.mp4 sdir2/
  2. 如果您想移动这些目录(sdir1 和 sdir2)上的所有 mp4 文件,请运行这些命令:

    cd /home/sun/
    mv sdir1/mp4/*.mp4 sdir1/
    mv sdir2/mp4/*.mp4 sdir2/

编辑:

  1. 制作一个遍历所有目录的脚本:

创建脚本并命名并使用您喜欢的编辑器(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

关于linux - 什么是linux命令将子目录的文件分别向上移动一级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30836658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com