0

I have a folder Accounts containing mail accounts, all of these folders have a folder Archive.folder. I want to move all of archive folders with a simple script to a new folder.

I tried this so far:

find . -mindepth 1 -maxdepth 1 ! -name archive -exec mv -t testnew/ {} + 

But it moves all folders, not just the ones called archive. I want to find all folders in the current folder called archive and move them to the folder testnew/. The path to the moved folder should be the same as before.

Existing situation:

Accounts
    - 1
          - archive
          - inbox
          - folder2
    - 2
          - archive
          - inbox
          - folder2

    - 3
          - archive
          - inbox
          - folder2

How it should look like:

testnew
    - 1
          - archive
    - 2
          - archive
    - 3
          - archive

1 Answers1

0

If it's OK to do this interactively, vidir may be a good way. See this answer.

In your case the procedure will be like:

  1. cd /path/to/Accounts
    find . -type d -name archive | vidir -
    
  2. vidir will run a text editor. Use "search and replace" and replace every leading ./ with whatever path you want, so the lines match your desired directory structure. Paths may be absolute or relative, they may contain ../. E.g. you can easily turn this

    ./1/archive
    ./2/archive
    ./3/archive
    

    into this

    ../testnew/1/archive
    ../testnew/2/archive
    ../testnew/3/archive
    
  3. Save the edited file and exit the editor.

Notes:

  • If your directory structure contains directories with names ending with . (so ./ substring appears not only at the beginning) then it will be better to replace <TAB>./ with <TAB>whatever/path/you/want/, where <TAB> indicates the tab character. It depends on the editor if/how you can include this character in "search and replace".
  • Choose your editor by specifying EDITOR, e.g. find … | EDITOR=kate vidir -.
  • vidir is not able to move files between filesystems. If all archive directories belong to one filesystem and you want testnew to be in another filesystem, then create testnew in the source filesystem, use vidir and finally move testnew to the target filesystem.
  • At the end of this another answer there is an experimental approach that (ab)uses EDITOR to make vidir non-interactive. However I find vidir useful in cases like yours mainly because it allows me to make sure the resulting paths are really what I want before any renaming/moving occurs. If I mess anything up, I can abort without saving changes. If you want a non-interactive solution to your problem then vidir is not the best tool.