How do you recurse directories, unzip, and move everything up?

vega

Say I have the following directory tree

.
├── a1
│   └── sub1
│       └── a1.zip
└── a2
    └── sub2
        └── a2.zip

The current directory contains a1 and a2 directories. I want to recursively search, locate and extract all zip files, and move everything up to a1 and a2 respectively. The goal state is:

.
├── a1
│   ├── file1
│   └── file2
└── a2
    ├── file1
    └── file2

Where the files came from the zip. How do I accomplish that? What tools should I use in Terminal for that?

EDIT

To be more specific, I mention the recursion since we don't know how many subdirectories there are. It could be:

.
├── a1
│   └── sub1
│       └── sub...
│                └── a1.zip
│                └── text1.txt
└── a2
    └── sub2
        └── sub...
                 └── a2.zip
                 └── text2.txt

Goal:

.
├── a1
│   ├── file1_unzipped
│   └── text1.txt
└── a2
    ├── file1_unzipped
    └── text2.txt
dessert

1. Unzip all .zip archives

find . -type f -name "*.zip" -exec unzip {} +

Explanations

  • find . – search current directory for
  • -type f – files
  • -name "*.zip" – whose name match *.zip (so ends in .zip),
  • -exec unzip {} + – make one long list of all matches and and run unzip on it

2. Move all files from lower levels to one level under the current

for i in */; do find "$i" -type f -exec mv {} "$i" \; && rm -r "$i"*/; done

Explanations

  • for i in */; do …; done – loop over all directories on the current level
  • find "$i" -type f – search all files in the currently processed directory and below
  • -exec mv {} "$i" \; – move them to the currently processed directory
  • && do the following only if the previous command finished successfully
  • rm -r "$i"*/ – remove every subdirectory in the currently processed directory

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do you recurse if statements

From Dev

How do you bulk move files up one directory safely?

From Dev

How do you recurse through two return statements? [JAVA]

From Dev

mogrify - how do you recurse through subfolders in Windows

From Dev

How do you recurse through two return statements? [JAVA]

From Dev

unzip how to trim top directories

From Dev

How do I recurse tuples?

From Dev

how to unzip all files in sub directories

From Dev

How to unzip all files in sub-directories?

From Dev

How do you uninstall everything to do with a specific program (redshiftgui)?

From Dev

How do I move files and directories to the parent folder in Linux?

From Dev

How to Find All .tex Files in Directories in Recurse Grep?

From Dev

how do I move up a division a little

From Dev

How do you move a div element in jquery

From Dev

How do you use move-resourcepool?

From Dev

How do you move shapes in LibGDX?

From Dev

How do you move an envelope to a specific point?

From Dev

How do I scale up everything on my display?

From Dev

How do you have a default #[cfg] target in rust for 'everything else'?

From Dev

How do you find the licenses for everything installed on your system?

From Dev

With Bash, How do I find all directories with two directories and move them to a copied tree structure?

From Java

How do you use the 'files' and 'directories' properties in package.json?

From Dev

mv: "Directory not Empty" - how do you merge directories with `mv`?

From Dev

how do you find all files and directories without sticky bit?

From Dev

how do you find all files and directories without sticky bit?

From Dev

How do you use PHP to list the directories at the parent directory level?

From Dev

How do you replace sed and wc with awk? (counting names in directories)

From Dev

Move files several directories up for several directories with similar layout

From Dev

Move files two directories up & delete directories afterwards, using wildcards

Related Related

  1. 1

    How do you recurse if statements

  2. 2

    How do you bulk move files up one directory safely?

  3. 3

    How do you recurse through two return statements? [JAVA]

  4. 4

    mogrify - how do you recurse through subfolders in Windows

  5. 5

    How do you recurse through two return statements? [JAVA]

  6. 6

    unzip how to trim top directories

  7. 7

    How do I recurse tuples?

  8. 8

    how to unzip all files in sub directories

  9. 9

    How to unzip all files in sub-directories?

  10. 10

    How do you uninstall everything to do with a specific program (redshiftgui)?

  11. 11

    How do I move files and directories to the parent folder in Linux?

  12. 12

    How to Find All .tex Files in Directories in Recurse Grep?

  13. 13

    how do I move up a division a little

  14. 14

    How do you move a div element in jquery

  15. 15

    How do you use move-resourcepool?

  16. 16

    How do you move shapes in LibGDX?

  17. 17

    How do you move an envelope to a specific point?

  18. 18

    How do I scale up everything on my display?

  19. 19

    How do you have a default #[cfg] target in rust for 'everything else'?

  20. 20

    How do you find the licenses for everything installed on your system?

  21. 21

    With Bash, How do I find all directories with two directories and move them to a copied tree structure?

  22. 22

    How do you use the 'files' and 'directories' properties in package.json?

  23. 23

    mv: "Directory not Empty" - how do you merge directories with `mv`?

  24. 24

    how do you find all files and directories without sticky bit?

  25. 25

    how do you find all files and directories without sticky bit?

  26. 26

    How do you use PHP to list the directories at the parent directory level?

  27. 27

    How do you replace sed and wc with awk? (counting names in directories)

  28. 28

    Move files several directories up for several directories with similar layout

  29. 29

    Move files two directories up & delete directories afterwards, using wildcards

HotTag

Archive