HaemEternal
HaemEternal

Reputation: 2269

Batch removing a sub folder in several parent folders

In linux, I have a group of folders, that all contain the same sub folder structure. E.g.

FolderA/x/y/z/file1.txt

FolderA/x/y/z/file2.txt

FolderB/x/y/z/file1.txt

FolderC/x/y/z/file1.txt

I want to run a batch process to remove one of the subfolders, but leave all files and folders beneath unchanged. E.g. if I were to remove folder "y":

FolderA/x/z/file1.txt

FolderA/x/z/file2.txt

FolderB/x/z/file1.txt

FolderC/x/z/file1.txt

I've tried putting together some combination of find and mv, but can't quite get it right

Upvotes: 0

Views: 679

Answers (2)

tripleee
tripleee

Reputation: 189317

find . -name y -type d -exec sh -c '
  for d; do echo mv "$d"/* "$d"/..; echo rmdir "$d"; done' _ {} +

Remove the echos if the results look like what you expect.

Upvotes: 1

C0deDaedalus
C0deDaedalus

Reputation: 381

Considering You want to remove one of the subfolders, but leave all files and folders beneath unchanged, here is something you can do. It's NOT an exact Solution, kind of trick that might work for you. I have done it many times on my computer.

You can recursively copy the subfolder's contents to it's parent folder and then traverse to parent directory and then finally recursively remove the subfolder.

  1. Traverse to subfolder

    $ cd path/to/SubFolder

  2. Copy all Contents Recursively to Parent Folder

    $ cp -R * ..

  3. Traverse to Parent Folder

    $ cd ..

  4. Remove recursively Subfolder

    $ rm -rf SubFolder/

Say you have a folder y, then,


    $ cd path/to/folder/y
    $ cp -R * ..
    $ cd ..
    $ rm -rf y/

For running a Batch Process You can figure out the path to subfolder using find command.

Notice that this trick can consume a lot of time & resources, If you have a lot of folders in the folders. But that works !

Upvotes: 0

Related Questions