1

I have a directory, Movies which contains many subdirectories, [MovieName]. Some of these [MovieName] subdirectories have a randomly named subdirectory with a VIDEO_TS subdirectory inside it. If I find VIDEO_TS, I'd like to move it to the [MovieName] parent of the parent directory and delete the current randomly named parent directory. If I don't find VIDEO_TS 2 layers down, I want to delete [MovieName]. To clarify...

Argo
  |-- ASDFJK
         |-- VIDEO_TS 
Birdcage
  |-- B7YEQ
         |-- [a few files - no VIDEO_TS] 

Since Argo has a VIDEO_TS sub-subdirectory, move it to Argo and delete ASDFJK. Since Birdcage doesn't have a VIDEO_TS, delete Birdcage. I've tried all sorts of things using find and -exec with {} but at best, I can only manage to move VIDEO_TS up one level (which leaves me a ton of cleanup still to do).

1
  • Your desired task, "move VIDEO_TS to parent dir && delete parent dir, unless VIDEO_TS doesn't exist && delete parent dir" is too complex a task for find with {}. Use find -exec to execute a bash script where this task is easier to express.
    – waltinator
    Jan 22, 2020 at 20:06

1 Answer 1

0

The following script should get the job done. Try it and when you are satisfied with the results, remove the comments # to do the actual job.

Back up your Movies directory before running the script on it. Please...

To create and use the script file, please follow these steps:

  1. Create and edit the script file in your home directory by running the following command in the terminal:

    nano ~/my_script.sh

  2. Copy and paste the following code into the editor changing /full/path/to/movies/ with the full path to your Movies directory:

#!/bin/bash

path="/full/path/to/movies/"

for directory in $(find "$path" -maxdepth 3 -mindepth 3 -type d -name "VIDEO_TS")
do

        parent=$(dirname $(dirname "$directory"))

        parent2=$(dirname "$directory")

        echo "$directory" " Will be copied to " " $parent "
        echo "$parent2" " Will be deleted!"

#       cp -Rp "$directory" "$parent"

#       rm -rf "$parent2"

done


for directory in $(find "$path" -maxdepth 3 -mindepth 3 -type d ! -name "VIDEO_TS")
do

        parent=$(dirname $(dirname "$directory"))

        echo "$parent" " Will be deleted!"

#       rm -rf "$parent" 

done
  1. Save the script file and exit the editor by pressing Ctrl + X then press Y.

  2. Make the script file executable by running the following command in the terminal:

    chmod +x ~/my_script.sh

  3. Run the script by running the following command in the terminal:

    bash ~/my_script.sh

Done

Best of luck

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.