linux 将子目录的文件分别上移一层的命令是什么
What is the linux command to move the files of subdirecties into one level up respectively
我服务器上文件的路径结构类似于下图,
/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...
你可以这样做:
# 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/
我建议你使用 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
有多种方法可以解决您的问题
如果您只想移动那些特定文件,运行这些命令:
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
将文件(或目录)上移一级的命令是:
mv /home/sun/sdir1/mp4/file.mp4 ..
通配符可用于select更多文件和目录,您也可以一次提供多个目录。
mv /home/sun/sdir1/mp4/*.mp4 /home/sun/collection/sdir2/mp4/*.mp4 ..
我服务器上文件的路径结构类似于下图,
/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...
你可以这样做:
# 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/
我建议你使用 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
有多种方法可以解决您的问题
如果您只想移动那些特定文件,运行这些命令:
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
将文件(或目录)上移一级的命令是:
mv /home/sun/sdir1/mp4/file.mp4 ..
通配符可用于select更多文件和目录,您也可以一次提供多个目录。
mv /home/sun/sdir1/mp4/*.mp4 /home/sun/collection/sdir2/mp4/*.mp4 ..