0

I need to move files out from a directory inside a directory inside a directory and remove folder that I move them from. I would need a recursive script. The directory structure:

/Folder0/Alphabetic_folder/Folder_name/DeleteFolder

/Folder0/A/Folder1/DeleteFolder
/Folder0/B/Folder2/DeleteFolder

DeleteFolder always has the same name which is info. I would need something like:

  1. go inside directory (alphabetic)
  2. for all folders there go inside, if there is an info folder mv *.* to the current folder, remove info (that would be: mv info/* ./ && rm info)
  3. if info is not there exit folder
  4. move to the next folder

I am sure that this is a trivial problem with some script skills, but my script skill are low in this problem.

1

UPD. that will work better :)

find -depth -print0 |  while read -d '' -r dir; do if [[ $dir == *info ]]; then mv "$dir"/* /tmp; rmdir "$dir"; fi; done

old answer here:

#!/bin/bash
cd /Folder0
for i in `ls`; do #get list of files and dirs in a folder0
    if [ -d $i ]; then #if list item is a folder
    cd $i   #then go inside (in you ex its folder A)
    for j in `ls`; do #list folders and files
            if [ -d $j ]; then #if item is folder
                    cd $j            #go inside (in your ex - Folder1)
                    mv info/* /any_folder_you_want #it will not move files if there is an error
                    rmdir info/ #it will not remove dir if it is not empty
                    cd .. #(go down)
            fi #(go to the next folder Folder2)
    done;  #end of folder A
    cd ..  #go down 
    fi     #next folder B...
done

You have to change part with mv and rmdir for testing that you are getting right results just put ls there and comment lines with mv and rmdir:

#mv info/* /any_folder_you_want #it will not move files if there is an error
ls
#rmdir info/ #it will not remove dir if it is not empty

you have to run this script outside Folder1.

questions?

  • folders inside A,B,C with space are not affected: Folder0/A/A - copy/info – Michal Sapsa Mar 20 '14 at 9:22
  • find -depth -print0 | while read -d '' -r dir; do if [[ $dir == info ]]; then mv "$dir"/ /tmp; rmdir "$dir"; fi; done – Gena Gordeev Mar 20 '14 at 9:44
  • /tmp should be "$dir/../" and then it works like it should. Thanks you for the help – Michal Sapsa Mar 20 '14 at 11:04
1

Assuming search is for all alpha directories from the working directory:

dirs=$(find [a-z][A-Z]* -type d -name info) 
for f in $dirs; do echo "$f/*" done     # first test it works to requirements

for f in $dirs; do mv "$f/*" "$f"/..; rmdir "$f"; done # working version with mv and rmdir

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.