Home > OS >  MacOS Bash - How to label a huge collection of files sequentially and move files to folders in block
MacOS Bash - How to label a huge collection of files sequentially and move files to folders in block

Time:05-09

I am using this piece of code to rename my tracks in a unity project for the unity asset store sequentially

ls -v | cat -n | while read n f; do mv -n "$f" "cinematic$n.mp3"; done 

how do i combine this with a feature to separate files in different folders, using blocks of 150 files? The purpose of this is to label files of for a unity sample pack accordingly, without falling into severe deviations

looking forward

kind/warm/best regards/wishes

thank you guys. both solutions work. after reading your responses here's my addendum to your contributions

f=1 d=1 n=150
for file in *.mp3
do
    if [ "$(( f % n ))" -eq 1 ]
    then
        dir=dir$d
        mkdir "$dir"
        echo mkdir "$dir"
        d=$(( d   1 ))
    fi
    mv -n "$file" "$dir/cinematic$f.mp3"
    echo mv -n "$file" "$dir/cinematic$f.mp3"
    f=$(( f   1 ))
done

#############################################

a=0
b=0
n=150
files=(*.mp3)

while (( ${#files[@]} > 0 )); do
    dir="./dir$((  a))"
    mkdir -p "$dir"
    echo mkdir -p "$dir"

    for f in "${files[@]:n * a: n}"; do
        mv -nv "$f" "$dir/cinematic$((  b)).mp3"
        echo mv -nv "$f" "$dir/cinematic$((  b)).mp3"
    done
done

CodePudding user response:

I'd read all the files into an array:

a=0
b=0
n=150
files=(*.mp3)

while (( ${#files[@]} > 0 )); do
    dir="./dir$((  a))"
    mkdir -p "$dir"

    for f in "${files[@]:0:n}"; do
        mv -nv "$f" "$dir/cinematic$((  b)).mp3"
    done

    files=( "${files[@]:n}" )    
done

CodePudding user response:

thank you guys. both solutions work. here's my addendum to your contributions

f=1 d=1 n=150
for file in *.mp3
do
    if [ "$(( f % n ))" -eq 1 ]
    then
        dir=dir$d
        mkdir "$dir"
        echo mkdir "$dir"
        d=$(( d   1 ))
    fi
    mv -n "$file" "$dir/cinematic$f.mp3"
    echo mv -n "$file" "$dir/cinematic$f.mp3"
    f=$(( f   1 ))
done

#############################################

a=0
b=0
n=150
files=(*.mp3)

while (( ${#files[@]} > 0 )); do
    dir="./dir$((  a))"
    mkdir -p "$dir"
    echo mkdir -p "$dir"

    for f in "${files[@]:n * a: n}"; do
        mv -nv "$f" "$dir/cinematic$((  b)).mp3"
        echo mv -nv "$f" "$dir/cinematic$((  b)).mp3"
    done
done
  •  Tags:  
  • bash
  • Related