I have two folders "downloads", and "files". I want to move all .txt files form downloads to files. before moving them to files folder, I check to see if it already exists in the "files" folder. If does not exist, then i will move them.
but for some reasons, my scripts always return file does not exists.
#!/bin/sh
SOURCE_PATH="/Users/johnqin/Downloads/*.txt"
DEST_PATH="/Volumns/files/"
ls -l $SOURCE_PATH > /dev/null 2>&1
# check if there are any .txt files
if [ "$?" = "0" ]; then
# loop through all .txt files
for file in $SOURCE_PATH
do
# get only file name
fname=`basename $file`
#echo $fname
targetFile="$DEST_PATH$fname"
# echo $targetFile
# check if this file exist in the target folder
if [ -e "$targetFile" ]
then
echo "$fname exists, do nothing"
# will do nothing
else
echo "$fname does not exist, move the file over"
# will move file over
fi
done
else
echo "did not find file"
fi
UPDATE:
I think I know what the problem is. my code is fine. "/Volumns/files" folder is a shared folder on another Ubuntu server. I auto mount this folder when I login. The address of this folder is "smb://ubuntunas/files".
still need to figure out why my code works for other folders but not this one.