如何将文件从子文件夹移动到它们的父目录(unix,终端)
How to move files from subfolders to their parent directory (unix, terminal)
我的文件夹结构如下:
一个名为 Photos 的大父文件夹。此文件夹包含 900 多个名为 a_000、a_001、a_002 等的子文件夹。
每个子文件夹都包含更多子文件夹,名为 dir_001、dir_002 等。每个子文件夹都包含很多图片(具有唯一名称)。
我想把a_xxx子目录下的这些图片全部移动到a_xxx里面。 (其中 xxx 可以是 001、002 等)
在查看周围的类似问题后,这是我想出的最接近的解决方案:
for file in *; do
if [ -d $file ]; then
cd $file; mv * ./; cd ..;
fi
done
我得到的另一个解决方案是做一个 bash 脚本:
#!/bin/bash
dir1="/path/to/photos/"
subs= `ls $dir1`
for i in $subs; do
mv $dir1/$i/*/* $dir1/$i/
done
不过,我还是遗漏了一些东西,你能帮忙吗?
(那么丢弃空的dir_yyy就好了,不过目前问题不大)
确保文件所在的目录在以下脚本中是正确的(并且是完整的)并尝试一下:
#!/bin/bash
BigParentDir=Photos
for subdir in "$BigParentDir"/*/; do # Select the a_001, a_002 subdirs
for ssdir in "$subdir"/*/; do # Select dir_001, … sub-subdirs
for f in "$ssdir"/*; do # Select the files to move
if [[ -f $f ]]; do # if indeed are files
echo \
mv "$ssdir"/* "$subdir"/ # Move the files.
fi
done
done
done
不会移动任何文件,只是打印。如果您确定脚本执行您想要的操作,请注释 echo 行并 运行 它 "for real".
您可以尝试以下 bash 脚本:
#!/bin/bash
#needed in case we have empty folders
shopt -s nullglob
#we must write the full path here (no ~ character)
target="/path/to/photos"
#we use a glob to list the folders. parsing the output of ls is baaaaaaaddd !!!!
#for every folder in our photo folder ...
for dir in "$target"/*/
do
#we list the subdirectories ...
for sub in "$dir"/*/
do
#and we move the content of the subdirectories to the parent
mv "$sub"/* "$dir"
#if you want to remove subdirectories once the copy is done, uncoment the next line
#rm -r "$sub"
done
done
你可以试试这个
#!/bin/bash
dir1="/path/to/photos/"
subs= `ls $dir1`
cp /dev/null /tmp/newscript.sh
for i in $subs; do
find $dir1/$i -type f -exec echo mv \'\{\}\' $dir1/$i \; >> /tmp/newscript.sh
done
然后用编辑器打开 /tmp/newscript.sh
或 less
看看是否像您想要做的那样。
如果确实如此,则使用 sh -x /tmp/newscript.sh
执行它
我的文件夹结构如下: 一个名为 Photos 的大父文件夹。此文件夹包含 900 多个名为 a_000、a_001、a_002 等的子文件夹。
每个子文件夹都包含更多子文件夹,名为 dir_001、dir_002 等。每个子文件夹都包含很多图片(具有唯一名称)。
我想把a_xxx子目录下的这些图片全部移动到a_xxx里面。 (其中 xxx 可以是 001、002 等)
在查看周围的类似问题后,这是我想出的最接近的解决方案:
for file in *; do
if [ -d $file ]; then
cd $file; mv * ./; cd ..;
fi
done
我得到的另一个解决方案是做一个 bash 脚本:
#!/bin/bash
dir1="/path/to/photos/"
subs= `ls $dir1`
for i in $subs; do
mv $dir1/$i/*/* $dir1/$i/
done
不过,我还是遗漏了一些东西,你能帮忙吗?
(那么丢弃空的dir_yyy就好了,不过目前问题不大)
确保文件所在的目录在以下脚本中是正确的(并且是完整的)并尝试一下:
#!/bin/bash
BigParentDir=Photos
for subdir in "$BigParentDir"/*/; do # Select the a_001, a_002 subdirs
for ssdir in "$subdir"/*/; do # Select dir_001, … sub-subdirs
for f in "$ssdir"/*; do # Select the files to move
if [[ -f $f ]]; do # if indeed are files
echo \
mv "$ssdir"/* "$subdir"/ # Move the files.
fi
done
done
done
不会移动任何文件,只是打印。如果您确定脚本执行您想要的操作,请注释 echo 行并 运行 它 "for real".
您可以尝试以下 bash 脚本:
#!/bin/bash
#needed in case we have empty folders
shopt -s nullglob
#we must write the full path here (no ~ character)
target="/path/to/photos"
#we use a glob to list the folders. parsing the output of ls is baaaaaaaddd !!!!
#for every folder in our photo folder ...
for dir in "$target"/*/
do
#we list the subdirectories ...
for sub in "$dir"/*/
do
#and we move the content of the subdirectories to the parent
mv "$sub"/* "$dir"
#if you want to remove subdirectories once the copy is done, uncoment the next line
#rm -r "$sub"
done
done
你可以试试这个
#!/bin/bash
dir1="/path/to/photos/"
subs= `ls $dir1`
cp /dev/null /tmp/newscript.sh
for i in $subs; do
find $dir1/$i -type f -exec echo mv \'\{\}\' $dir1/$i \; >> /tmp/newscript.sh
done
然后用编辑器打开 /tmp/newscript.sh
或 less
看看是否像您想要做的那样。
如果确实如此,则使用 sh -x /tmp/newscript.sh