2016-11-07 4 views
0

다음과 같은 폴더 구조가 있습니다. Photos라는 큰 상위 폴더. 이 폴더에는 a_000, a_001, a_002 등의 900 개 이상의 하위 폴더가 포함되어 있습니다.하위 폴더에서 부모 디렉토리 (유닉스, 터미널)로 파일을 이동하는 방법

하위 폴더에는 각각 dir_001, dir_002 등의 하위 폴더가 있습니다. 각 하위 폴더에는 많은 이름의 사진이 있습니다.

a_xxx의 하위 디렉토리에있는이 모든 그림을 a_xxx 안에 옮기고 싶습니다. bash는 스크립트를하고있다

for file in *; do 
    if [ -d $file ]; then 
    cd $file; mv * ./; cd ..; 
    fi 
done 

내가 가진 또 다른 방법 : 주변에 비슷한 질문을보고 한 후

(xxx는 001, 002 등이 될 수있는 곳)이 내가 생각 해낸 가장 가까운 솔루션입니다

#!/bin/bash 
dir1="/path/to/photos/" 
subs= `ls $dir1` 

for i in $subs; do 
    mv $dir1/$i/*/* $dir1/$i/ 
done 

그래도 뭔가 빠졌어. 도와 주겠니?

+0

으로 실행 않을 경우. 그러나 힌트로서'find와 같은 각 a_xxx 디렉토리 안에 뭔가를하십시오. -t f -exec mv \ {\}. \;'당신이 찾고있는 것일 수 있습니다 – infixed

+0

안녕하세요. 나는 900+ 폴더를 가지고 있는데, 나는 그들 각각에게 이것을 할 것이다. SO가 게시 할 곳이 아니면 미안합니다. 그것은 여기에 내가 더 많은 관련 예제를 발견 : [link1] (http://stackoverflow.com/questions/23546294/copy-files-from-subfolders-to-the-nearest-parent-directory-in-unix) 및 [ link2] (http://stackoverflow.com/questions/22228718/using-for-loop-to-move-files-from-subdirectories-to-parent-directories) – Mpampirina

+0

'mv * ./;'부분에 대해 확실합니까? 왜냐하면'mv * ../;'아마도 – arhak

답변

2

다음과 같은 bash는 스크립트를 시도 할 수 있습니다 : 당신은 편집기 또는 less와 연 다음이

#!/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을 시도 할 수 있습니다

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

Here is why you don't parse ls in bash

1

이 파일이 존재하는 디렉토리가 올바른지 (완전한) 다음 스크립트에 있는지 확인합니다 (그런 다음이 빈 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 

파일은 단지 인쇄 할 수 없습니다. 스크립트가 원하는 것을 수행했다면, 에코 라인을 주석 처리하고 "진짜"로 실행하십시오.

1

를 볼 경우 모습이 마음에 노력하고있어.

그 다음 정말 프로그래밍 질문이 아니므로 당신은 아마, 유닉스 LINIX에 SE를이 요청해야 sh -x /tmp/newscript.sh

관련 문제