0

I'm trying to make bash script for ubuntu that will check for files in folder Downloads and copy everything that doesen't exist in Documents to Documents. But I'm stuck at checking if file exist, can somebody help me out?

#!/bin/bash
cd /home/rene/Downloads
for file in *
do  
  echo $file
  if [ ! -f /home/rene/Documents/$file ]; then
    echo "File not found!"
  fi
done
mouviciel
  • 66,855
  • 13
  • 106
  • 140
renekton
  • 61
  • 1
  • 8
  • 5
    How about using `rsync` instead? That is made to perform such tasks. – Alfe Dec 21 '17 at 12:45
  • 2
    Before going any further, [you need to quote your variables](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable). – tripleee Dec 21 '17 at 12:46
  • 2
    If all you need is "copy if it's not already there", even `cp` has a flag for that. – tripleee Dec 21 '17 at 12:47
  • is it possible with rsync that if I delete something in documents won't get Deleted in downloads? – renekton Dec 21 '17 at 12:48
  • 1
    Yes, it's easy with `cp` or `rsync` to not remove files from the destination which don't exist at the source. Not really a programming question, though. Peruse the manuals, perhaps? – tripleee Dec 21 '17 at 12:48
  • Looks like I complicated too much, thank you all – renekton Dec 21 '17 at 12:59

0 Answers0