0

I've been stuck trying to figure this one out. Here is an example of my files:

~/download/Video/CE/
~/download/Video/CE/153
~/download/Video/CE/153/2022-03-04
~/download/Video/CE/153/2022-03-05
~/download/Video/CE/281
~/download/Video/CE/281/2022-03-04
~/download/Video/CE/281/2022-03-05
~/download/Video/GA/
~/download/Video/GA/154
~/download/Video/GA/154/2022-03-04
~/download/Video/GA/154/2022-03-05
~/download/Video/GA/615
~/download/Video/GA/615/2022-03-04
~/download/Video/GA/615/2022-03-05
...etc

There are several dozen folders in /download/Video and then several hundred folders in /download/video/*/

I am trying to move all folders that contain "2022-03-04" and all of it's contents into a new directory. My desired outcome is:

/mnt/d/archive/Video/CE/153/2022-03-04
/mnt/d/archive/Video/CE/281/2022-03-04
/mnt/d/archive/Video/GA/154/2022-03-04
...etc

I have tried various things but my biggest problems is that I can't just simply move the video folder to /mnt/d/archive because I want to keep the 2022-03-05 folders (and the superdirectories) on the main drive.

Here is what I've tried so far:

cd ~/download/Video/CE; for subdir in *; do mv $subdir/2022-03-04 /mnt/d/archive/Video/CE/$subdir/; done;

^ and then just copy and paste this code for GA and all the other subdirectories in /Video/

This created the following:

/mnt/d/archive/Video/CE/153/file.mp4
/mnt/d/archive/Video/CE/153/file2.mp4
/mnt/d/archive/Video/CE/281/file.mp4
...etc

Which is great, except it doesn't copy the '2022-03-04' folder itself. I need the 2022-03-04 folder. So I tried this code instead:

cd ~/download/Video/CE; for subdir in *; do mv $subdir/2022-03-04 ~/mnt/d/archive/Video/CE/$subdir/2022-03-04; done;

However, now I ran into another problem. None of the folders nor files copied this time. I just get a "No such file or directory" error.

Any ideas?

2 Answers 2

1

My choice would be tar because it's fast.

(cd ~/download/Video && find . -type d -name 2022-03-04 | tar -T - -cpf - --remove-files | tar -C "/mnt/d/archive/Video/" -xpf -)
1

If I understand your problem correctly, something like this should work:

cd ~/download/Video
for i in $(find * -type d -name '2022-03-04'); do
    mkdir -p /mnt/d/archive/Video/"$i"
    mv "$i"/* /mnt/d/archive/Video/"$i" && rm -r "$i"
done

This will:

  1. Find all directories named '2022-03-04'.
  2. Recreate the directory tree as needed in the archive location.
  3. Move the contents of '2022-03-04' to the corresponding archive directory and remove the original '2022-03-04' directory.

(standard disclaimer that this will break if file names have strange characters)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .