4

Lets say that I have the following structure:

./
├── Dir1
├── Dir2
│   ├── SubDir1
│   │   └── SubSubDir1
│   └── SubDir2
└── Dir3
    └── SubDir1

Use this to make the test structure:

mkdir -p Dir1 Dir2/SubDir1/SubSubDir1 Dir2/SubDir2 Dir3/SubDir1

I would like to create a Texture folder in every directory to include the current location so it would look like this (All new Texture directories are marked with *):

./
├── Dir1
│   └── Textures*
├── Dir2
│   ├── SubDir1
│   │   ├── SubSubDir1
│   │   │   └── Textures*
│   │   └── Textures*
│   ├── SubDir2
│   │   └── Textures*
│   └── Textures*
├── Dir3
│   ├── SubDir1
│   │   └── Textures*
│   └── Textures*
└── Textures*

Then I would also be able to delete these same folders after I work with them. You can assume that there are no other directories named Textures that need to be saved.


So I would like to create a Textures directory in the current directory and all of its subdirectories. And I would like to be able to delete every directory named Textures when I am done moving files around.

4

You can use a loop...

shopt -s globstar                   # turn on recursive globbing
for d in **/; do                    # for every directory
    echo mkdir -p "$d"Textures      # make a new directory inside named Textures
done

Remove echo after testing to actually create the directories.

You can turn off globstar with shopt -u globstar, but it is off by default, and will be off with you start a new shell.

Do your things...

Then to remove the directories, first find them

find -type d -name "Textures"

Then if the output looks OK, add -delete

find -type d -name "Textures" -delete
  • 1
    Just a minute faster than me... Good job ;-) – Byte Commander Oct 2 '17 at 20:13
  • Could you shorten the loop to mkdir -p **/Textures? I would test it myself, but I'm not at home. – wjandrea Oct 2 '17 at 20:19
  • @wjandrea that just created a directory named "**" with a child directory of Textures. – kingcobra1986 Oct 2 '17 at 20:20
  • @kingcobra1986 Ah, whoops. I forgot how globs work. :| – wjandrea Oct 2 '17 at 20:21
  • Thats alright I tried something earlier and it was trying to recursively create a Texture directories inside of themselves making it look like ./Textures/Textures/Textures/Textures/.... and it kept on going. – kingcobra1986 Oct 2 '17 at 20:26

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.