1

I'd like to copy a total of 11 GB of files (all of the type .mp3 for the sake of this question) from folder ~/ABC/ to ~/DEF/. And I like to do that for the files sorted descending by date last edited/modified in ABC (i.e. the file modified most recently should get copied first).

The file names contain spaces and or characters like &, . and / and alike.

I have looked around a bit and so far, and using code from (somewhat) related questions does not look sensible imho (not that I would be a good judge anyway).

How can I go about that? :)

0

I'm sure there's a better way to do this. LIMIT is the amount to copy in kilobytes.

#! /bin/bash 

LIMIT='11534336'
size='0'

if [[ -e $1 && -d $2 ]];then
  echo "copying contents from $1 to $2"
else
  echo "Usage: $0 SOURCE DEST"
  exit
fi  

for i in $(ls -t $1); do
  var="$(echo $(du -s $(realpath $1/$i)))"
  IFS=" " read var1 var2 <<< "$var"
  let "size += $var1"
  if [[ $size -lt $LIMIT ]];then
    echo "copying " $var2 "to $2"
    cp -r $var2 $2
  else
    exit
  fi  
done

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.