Checking of file exist in different folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking of file exist in different folder
# 1  
Old 04-18-2016
Checking of file exist in different folder

Hi All,

Seeking for your assistance to compare if the file in working directory is found on the DIR2 directory and if not found move the file in DIR2.

Code:
ex. WORKING_DIR=/home/dir1/
file1.txt

DIR2=/home/admin/users
file1.txt

what i did was
Code:
found_nonempty=''
  for file in $WORKING_DIR* ; do
    if [[ -s "$file" ]] ; then
      found_nonempty=1
    fi
  done
  if [[ "$found_nonempty" ]] ; then
    echo found one
  else
    echo found none
  fi

but this was only to check if file exist in current directory.

Please advise,

Thanks,

---------- Post updated at 10:27 AM ---------- Previous update was at 08:35 AM ----------

i now solved the issue. thx Smilie



Moderator's Comments:
Mod Comment Good to hear! Why don't you share your solution for the benefit of others?

Last edited by RudiC; 04-19-2016 at 01:24 AM.. Reason: Corrected code tags.
# 2  
Old 04-19-2016
I do not understand what you are trying to do.

Your stated goal:
Quote:
Seeking for your assistance to compare if the file in working directory is found on the DIR2 directory and if not found move the file in DIR2.
doesn't say anything about file sizes (which is all that the code snippet you have shown us tests).

If a file found in $DIR2 and is not found in $WORKING_DIR, what do you want to happen?

If a file is present in $WORKING_DIR that is not found in $DIR2, is anything supposed to happen? If so, what?

Does the size of the file make any difference in what is supposed to happen if a file is found in one of these two directories and not in the other? If so, what?
# 3  
Old 04-19-2016
This is what i done Mod.


Code:
WORKING_DIR=/home/working_dir
DIR2=/home/target_dir

#check if there's a zip file in the WORKING_DIR
if [ "`ls -A $WORKING_DIR/*.ZIP 2>/dev/null`" ]
then
echo "contains files (or is a file)"
cd $WORKING_DIR
        for i in *.ZIP
                do
                        a=$(find $DIR2 -iname "$i" -print | wc -l) -- i used find command to check if there's a file in $DIR2 equals to $i and count
                        if [[ $a > 0 ]]; then
                                 echo "File Exist in $DIR2"
                        else
                                 echo "File Not Exist in $DIR2"
                                 echo "Copying $i to $DIR2"
                        cp $i $DIR2
        fi
done
else
echo "No such file in the Directory"
fi

Hope it helps Smilie

Thanks,
# 4  
Old 04-19-2016
Assuming that filenames are identical if they exist in both directories (i.e., no case differences), you might find the following to be more concise and more efficient in terms of CPU and disk usage:
Code:
#!/bin/ksh
WORKING_DIR=/home/working_dir
DIR2=/home/target_dir

cd $WORKING_DIR
for i in *.[Zz][Ii][Pp]
do	if [ "$i" = "*.[Zz][Ii][Pp]" ]
	then	echo "No such file in the Directory"
		exit
	fi
	if [ -f "$DIR2/$i" ]
	then	echo "File $i Exist in $DIR2"
	else	echo "File $i Not Exist in $DIR2"
		echo "Copying $i to $DIR2"
		cp "$i" "$DIR2"
        fi
done

I used the Korn shell for testing this (you didn't say what shell you're using), but this should work with any shell that follows basic Bourne shell syntax.
# 5  
Old 04-19-2016
I think this should do it:

Code:
rsync --ignore-existing $WORKING_DIR $DIR2

I didn't test it, so please try it with the option -n first (this would show what is copied, without actually copying anything).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Checking Multiple File existance in a UNIX folder(Note: File names are all different)

HI Guys, I have some 8 files with different name and extensions. I need to check if they are present in a specific folder or not and also want that script to show me which all are not present. I can write if condition for each file but from a developer perspective , i feel that is not a good... (3 Replies)
Discussion started by: shankarpanda003
3 Replies

2. Shell Programming and Scripting

Check if folder exist using case

Is it possible to use case when checking if a folder exist? I need to check if these folder exist . All of them could be present but I still need to check. If a folder is present, something will be processed. Different folder do different stuff. is this possible?' Thanks (4 Replies)
Discussion started by: erin00
4 Replies

3. Shell Programming and Scripting

Checking FTP server file exist or not

I am trying to delete old file in ftp server, after transferring new file successfully . but here i am checking both the file available or not using ls command. if both the file available means i need to get file_new and file_old file size as greater than zero. but i am getting only for... (3 Replies)
Discussion started by: elango963
3 Replies

4. Shell Programming and Scripting

file exist in folder

Hi guys, I am new to shell script, i want to write shell script, the script details has mentioned below Two files are to be check in one particular directory, if the file exist in particular folder then the file is existed and also if the file exist more than three hours then send... (2 Replies)
Discussion started by: madhavi_bodi
2 Replies

5. UNIX for Dummies Questions & Answers

Checking if multiple directories exist

I need to create multiple directories if those directories do not exist already. How would you go by doing this. What I have so far. array=(one two three) for I in ${array} do if ] then mkdir ${I} fi doneI have a good feeling this is done incorrectly. The error I am... (2 Replies)
Discussion started by: jrymer
2 Replies

6. Shell Programming and Scripting

awk check if folder exist

Hello Im trying to build a script that (among other stuff) checks each line in a file for the $4 "variabel" and if there are a folder in the directory that has that not has the same name as that it create a folder, otherwise it wont do anything. I guess i could just send the errors to /dev/null,... (2 Replies)
Discussion started by: fisk4
2 Replies

7. Web Development

Checking if a folder is empty or not using PHP

Hi, I am sharing this tip with you all.The codes given below will explain /** * Checking a folder is empty or not. * @param string $folderName * $folderName should be folder name or path * @return TRUE/FALSE (If any file or folder found/Empty folder) */ function... (1 Reply)
Discussion started by: elizas
1 Replies

8. Shell Programming and Scripting

delete all the files in folder a which exist in folder b

Hi , I need a script which basically deltes all files in folder a which are alreasy present in folder b say folder a has files abc.txt pqr .txt and b has abc.txt pqr.txt rmr.txt then file abc.txt and pqr.txt from a should be deleted (6 Replies)
Discussion started by: viv1
6 Replies

9. Shell Programming and Scripting

File exist checking error

Hi all, I have following code: export USERID=user export BATCHHOSTIP=190.113.3.324 remotePath=/home/user fileName=redbook.txt EXIST=`ssh $USERID@$BATCHHOSTIP 'if ; then echo 0; else echo 1 ; fi'` echo $EXIST II run the code from other server. The redbook.txt file is exist in... (1 Reply)
Discussion started by: suigion
1 Replies

10. Shell Programming and Scripting

Checking if any folder is opened or not?

Alogorithm: ============= Whenever any user open any specific directory: 1) cd /usr/lib The prompt should show an Alert message like 2) echo "You have opened this folder" How should I write shell script this one?.. It should be irrespective of users and... (11 Replies)
Discussion started by: Niroj
11 Replies
Login or Register to Ask a Question