0

I need help copying a large bunch of files. Let me try to explain the problem here

SOURCE STRUCTURE is in /home/user/ToddCrimson/Pictures/iPhone-Media/ (It has JPGs, PNGs, MOVs etc, so I will need a script which first moves JPGs, then I change the lookup to MOVs and change the destination folder)

100APPLE
    IMG_0001.JPG - TimeStamp-A
    IMG_0002.jpg - TimeStamp-B
    IMG_0003.jpg - TimeStamp-C
    IMG_0004.MOV - TimeStamp-G
101APPLE
    IMG_0001.jpg - TimeStamp-D
    IMG_0002.jpg - TimeStamp-E
    IMG_0003.jpg - TimeStamp-F
    IMG_0004.MOV - TimeStamp-H
102APPLE
    IMG_0001.jpg - TimeStamp-X
    IMG_0002.jpg - TimeStamp-Y
    IMG_0003.jpg - TimeStamp-Z
    IMG_0004.MOV - TimeStamp-I
xxxAPPLE
    

DESTINATION STRUCTURE is in /home/user/ToddCrimson/Pictures/ (I'd run the script each for JPG, for PNG, for MOV etc etc, but preserving the original timestamps)

All_JPG
    Pic-00000001.jpg - TimeStamp-A
    Pic-00000002.jpg - TimeStamp-B
    Pic-00000003.jpg - TimeStamp-C
    Pic-00000004.jpg - TimeStamp-D
    Pic-00000005.jpg - TimeStamp-E
    Pic-00000006.jpg - TimeStamp-F
    -
    -
    -        -
    Pic-21897563.jpg - TimeStamp-Z
    
All_MOV
    Mov-00000001.MOV - TimeStamp-G
    Mov-00000002.MOV - TimeStamp-H
    -
    -
    -
    Mov-32188462.MOV - TimeStamp-XX
    

I've tried various combinations of RSYNC and CP, but seemingly keep failing. Any help with an exact command that I can run?

I'd like to Copy all thousands of JPGs. Have them all sequential names, or atleast unique names, but must preserve the original timestamp.

I'm on Debian 12. Can do it over Terminal or if a GUI program exists?

1 Answer 1

0

Iterate across all the directories. In each directory iterate across the files. Keep a sequential counter for the target filenames

#!/bin/bash
k=1
t='SingleDirectory'

for d in Dir*
do
    for f in "$d"/*
    do
        x="${f##*.}"
        echo cp -p "$f" "$t/$(printf "Pic-%08d.%s" "$k" "$x")"
        ((k++))
    done
done

If the target directory is on the same filesystem as the source directories you might want to use ln instead of cp -p. As you might expect, remove the echo from in front of the cp to enable execution.

Should you have too many files for the * wildcards it is possible to run this differently, but it's more complex and relies on a temporary file to hold the value of $k between loops:

#!/bin/bash
k=1
t='SingleDirectory'

echo "$k" >.k
find -maxdepth 1 -type d -name 'Dir*' -print0 |
    sort -z |
    while IFS= read -r -d '' d
    do
        find "$d" -maxdepth 1 -type f -print0 |
            sort -z |
            (
                k="$(cat .k 2>/dev/null)"
                while IFS= read -r -d '' f
                do
                    x="${f##*.}"
                    echo cp -p "$f" "$t/$(printf "Pic-%08d.%s" "$k" "$x")"
                    ((k++))
                done
                echo "$k" >.k
            )
    done
rm -f .k
9
  • Hi Chris, thanks for the detailed answer. I am not super versatile yet in bash scripts, but I wrote this one as close as I could, but got errors, and I'm assuming that's because in my example, the naming convention is very generic. Let me provide a proper naming convention that I'm dealing with below. Commented Oct 7, 2024 at 18:02
  • Hi Chris, sorry couldn't put that many characters in comments, so I updated the original question. See above. Commented Oct 7, 2024 at 18:14
  • I tried your second script above, that works, copies, but no file extensions. And there is no way to choose just JPGs either. Commented Oct 8, 2024 at 15:14
  • I finally had to go to one my colleagues, and got the answer. Its a complicated script, but does the job. Commented Oct 8, 2024 at 15:15
  • @ToddCrimson "but no file extensions" - simply include the required file extension in te wildcard pattern. For example, using the first script suggestion, instead of find "$d" -maxdepth 1 -type f -print0 you add a restriction that specifies JPEG files, find "$d" -maxdepth 1 -type f -name '*.jpg' -print0 Commented Oct 8, 2024 at 15:46

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.