Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何将文件从子文件夹移动到其父目录(unix,终端)_Bash_Unix_Filesystems_Subdirectory_File Move - Fatal编程技术网

Bash 如何将文件从子文件夹移动到其父目录(unix,终端)

Bash 如何将文件从子文件夹移动到其父目录(unix,终端),bash,unix,filesystems,subdirectory,file-move,Bash,Unix,Filesystems,Subdirectory,File Move,我的文件夹结构如下: 名为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 $

我的文件夹结构如下: 名为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
不过,我还是错过了一些东西,你能帮忙吗


(那么最好放弃空目录_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

不会移动任何文件,仅打印。如果您确定脚本符合您的要求,请注释回显行并“真实地”运行它。

确保以下脚本中存在文件的目录正确(且完整),然后重试:

#!/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行并“真实地”运行它。

您可以尝试以下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

您可以尝试以下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执行它

#!/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

执行它,您可能应该在unix linix SE中问这个问题,因为这实际上不是一个编程问题。但作为提示,在每个a_xxx目录中执行一些操作,如
find-f型-exec mv\{\}.\可能就是你想要的嗨。我有900多个文件夹,不可能对每个文件夹都这样做。我很抱歉,如果不是这样的地方张贴。就是在这里,我找到了更相关的例子:[link1]()和[link2]()确定
mv*零件?因为在我看来,在某些地方需要两点(对于父目录),比如
mv*可能是空的,一点也不确定。。。我不知道bash脚本。我们将尝试稍微不同的版本,看看会发生什么。事实上,您有两个搜索要做,一个是迭代您的a_xxx控制器,然后在每个控制器中进行另一个搜索,将数据文件向上移动到特定的a_xxx目录中。用一个命令很难做到这一点。将它们全部移动到一个地方会更容易,您可能应该在unix linix SE中提出这个问题,因为这实际上不是一个编程问题。但作为提示,在每个a_xxx目录中执行一些操作,如
find-f型-exec mv\{\}.\可能就是你想要的嗨。我有900多个文件夹,不可能对每个文件夹都这样做。我很抱歉,如果不是这样的地方张贴。就是在这里,我找到了更相关的例子:[link1]()和[link2]()确定
mv*零件?因为在我看来,在某些地方需要两点(对于父目录),比如
mv*可能是空的,一点也不确定。。。我不知道bash脚本。我们将尝试稍微不同的版本,看看会发生什么。事实上,您有两个搜索要做,一个是迭代您的a_xxx控制器,然后在每个控制器中进行另一个搜索,将数据文件向上移动到特定的a_xxx目录中。用一个命令很难做到这一点。把他们都搬到一个地方会容易得多