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

问题描述 投票:0回答:4

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

/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

我不知道该怎么做,所以还没有尝试过......

linux find mv
4个回答
2
投票

有不同的方法可以解决您的问题

  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

1
投票

你可以这样做:

# move all .mp4 files from sdir1/mp4 to sdir1 directory
user@host:~/home/sun$ mv sdir1/mp4/*.mp4 sdir/

# move all .mp4 files from collection/sdir2/mp4 to collection/sdir2 directory
user@host:~/home/sun$ mv collection/sdir2/mp4/*.mp4 collection/sdir2/

# move only 1 file
user@host:~/home/sun$ mv sdir1/mp4/file.mp4 sdir/
user@host:~/home/sun$ mv collection/sdir2/mp4/file.mp4 collection/sdir2/

0
投票

我建议你使用

find
之类的东西

cd /home/sun/sdir1/mp4/
find . -name "*" -exec mv {} /home/sun/sdir1/ \;
cd /home/sun/collection/sdir2/mp4/
find . -name "*" -exec mv {} /home/sun/collection/sdir2/ \;

或者,您可以使用

tar
之类的东西

cd /home/sun/sdir1/mp4/
tar cfp - * | (cd ../ ; tar xvvf -)
# Make sure everything looks good
rm -rf mp4
cd /home/sun/collection/sdir2/mp4/
tar cfp - * | (cd ../ ; tar xvvf -)
# Make sure everything looks good
rm -rf mp4

0
投票

将文件(或目录)上移一级的命令是:

mv /home/sun/sdir1/mp4/file.mp4 ..

通配符可用于选择更多文件和目录,您也可以一次提供多个目录。

mv /home/sun/sdir1/mp4/*.mp4 /home/sun/collection/sdir2/mp4/*.mp4 ..
© www.soinside.com 2019 - 2024. All rights reserved.