I don't understand if something weird is going on with cygwin's version of find.
In Z:\ANIME E CARTONI\# DA SISTEMARE ED ESTRARRE _ DVD\# 22
I have these folders
folder1
folder2
folder3
folder1 and folder2 have a size > 4mb so in theory they should be moved to the over40gb folder but this does not happen
#!/bin/bash
z="/cygdrive/Z/ANIME E CARTONI/# DA SISTEMARE ED ESTRARRE _ DVD/# 22"
target="/cygdrive/Z/ANIME E CARTONI/# DA SISTEMARE ED ESTRARRE _ DVD/# 22/over40gb/"
size="3M"
mkdir -p "$target"
find "$z" -maxdepth 1 -type f -size "$size" -exec mv "{}" "$target" \;
EDIT: this solution works
z=/cygdrive/C/Users/Administrator/Desktop/test/
mkdir -p /cygdrive/C/Users/Administrator/Desktop/test/over40gb/
for x in $(ls -1 --color=never -d ${z}*/); do
y=$(du --max-depth=0 --block-size=1M $x | awk '{print $1}')
if [ $y -ge 4 ]; then
mv ${x} /cygdrive/C/Users/Administrator/Desktop/test/over40gb/
fi
done
But it return me an error message
mv: cannot move '/cygdrive/C/Users/Administrator/Desktop/test/over40gb/' to a subdirectory of itself, '/cygdrive/C/Users/Administrator/Desktop/test/over40gb/over40gb'
but when I try to use path with spaces like
z="/cygdrive/Z/ANIME E CARTONI/# DA SISTEMARE ED ESTRARRE _ DVD/# 22"
mkdir -p "/cygdrive/Z/ANIME E CARTONI/# DA SISTEMARE ED ESTRARRE _ DVD/# 22/over40gb/"
for x in $(ls -1 --color=never -d ${z}*/); do
y=$(du --max-depth=0 --block-size=1M $x | awk '{print $1}')
if [ $y -ge 4 ]; then
mv ${x} "/cygdrive/Z/ANIME E CARTONI/# DA SISTEMARE ED ESTRARRE _ DVD/# 22/over40gb/"
fi
done
it return me this error
CodePudding user response:
You need to double-quote your variables expansions and filter out the directory over40gb
. Also, using ls
is no good.
z="/cygdrive/Z/ANIME E CARTONI/# DA SISTEMARE ED ESTRARRE _ DVD/# 22"
mkdir -p "$z/over40gb" || exit 1
for dir in "$z"/*/
do
[ "$z/over40gb/" = "$dir" ] && continue
size=$(du -sk "$dir" | awk 'NR == 1 {print $1}')
[ "$size" -ge 4096 ] && mv "$dir" "$z/over40gb/"
done