List file NOT present in other directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting List file NOT present in other directory
# 1  
Old 10-11-2012
List file NOT present in other directory

Dear community,
I have one LOG directory with some files. What I need to do is list ONLY the files that are not present in other directory.
Using Bash programming!

Code:
LOG DIR      |     SYNC DIR
FILE1        |      FILE1
FILE2        |      FILE3
FILE3        |      OTHER FILENAME
FILE4        |
FILE5        |

In this case there are 5 files under LOG directory and 3 files under SYNC directory, so the output list I'm expecting is
Code:
FILE2
FILE4
FILE5

Could someone help me? Thanks.
# 2  
Old 10-11-2012
Code:
ls dir1 | while read FILE
do
        [ -e "dir2/$FILE" ] || echo "$FILE"
done

# 3  
Old 10-11-2012
Thanks for reply Corona, but it doesn't work! :-(

Code:
#!/bin/bash
ls /log/*.DAT | while read FILE
do
        [ -e "/log/sync/$FILE" ] || echo "$FILE"
done

This is the LOG dir:
Code:
[root@RedHat5 ~]# ls /log
FILE1.DAT
FILE2.DAT
FILE3.DAT
FILE4.DAT
sync
[root@RedHat5 ~]# ls /log/sync
FILE2.DAT

Execution:
Code:
[root@RedHat5 ~]# ./test.sh 
/log/FILE1.DAT
/log/FILE2.DAT
/log/FILE3.DAT
/log/FILE4.DAT

FILE2.DAT should not be in the list!
# 4  
Old 10-11-2012
Please read what Corona688 has posted carefully. He didn't suggest globbing.
And if you need to use globbing to perform that operation on a select list of files from the directory, try a slight modification:
Code:
printf "%s\n" /log/*.DAT | while read FILE
do
   [[ -e /log/sync/${FILE##*/} ]] || echo "$FILE"
done

This User Gave Thanks to elixir_sinari For This Post:
# 5  
Old 10-11-2012
Sorry Elixir, what do you mean with "globbing"? Smilie
I applied exactly what Corona suggest me.
# 6  
Old 10-11-2012
That's the expansion of asterisk (in this case) by the shell to filenames.
Have you tried what I've suggested in my earlier post?
# 7  
Old 10-11-2012
Quote:
Originally Posted by elixir_sinari
That's the expansion of asterisk (in this case) by the shell to filenames.
Have you tried what I've suggested in my earlier post?
Ah ok! You had modified the previous post and I don't see the code.
Now it working fine. BTW, I'm used asterisk because under LOG directory there is the SYNC directory, so I've to use * to take only .DAT files! Smilie

Just another question, if the file doesn't exists I need to show it and then copy to SYNC dir (so next time will not be shown). How can I add the copy command to the following line?
Code:
[[ -e /log/sync/${FILE##*/} ]] || echo "$FILE" > /tmp/DAT.tmp

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Total record count of all the file present in a directory

Hi All , We need one help on the below requirement.We have multiple pipe delimited .txt file(around 100 .txt files) present on one directory.We need the total record count of all the files present in that directory without header.File format as below : ... (8 Replies)
Discussion started by: STCET22
8 Replies

2. Shell Programming and Scripting

Systemd errors of missing file “No such file or directory” inspite of file being present

The contents of my service file srvtemplate-data-i4-s1.conf is Description=test service for users After=network.target local-fs.target Type=forking RemainAfterExit=no PIDFile=/data/i4/srvt.pid LimitCORE=infinity EnvironmentFile=%I . . . WantedBy=multi-user.target (0 Replies)
Discussion started by: rupeshkp728
0 Replies

3. Shell Programming and Scripting

How to copy all the contents of a list of files present in a folder to a particular file?

Hi All, I want to copy all the contents of a list of files in a folder to a particular file. i am using following command: cat dir/* >> newFile.txtIt's not working. Could you please help? Thanks, Pranav (3 Replies)
Discussion started by: Pranav Bhasker
3 Replies

4. Solaris

how to find the list of all packages present in a directory

Hi I'm in a directory named /tmp/mq7 i need to upgrade the mq version from 6 to 7 but im not sure where is the package located ? which command in solaris will show me the list of all the packages present in the directory /tmp/mq7 ? my box is running with solaris version 10. (2 Replies)
Discussion started by: newtoaixos
2 Replies

5. Shell Programming and Scripting

Script to list files not present in audio.txt file

I am having following folder structure. /root/audios/pop /root/audios/jazz /root/audios/rock Inside those pop, jazz, rock folders there are following files, p1.ul, p2.ul, p3.ul, j1.ul, j2.ul, j3.ul, r1.ul, r2.ul, r3.ul And I have a file named as "audio.txt" in the path /root/audios,... (11 Replies)
Discussion started by: gopikrish81
11 Replies

6. Shell Programming and Scripting

Script to send an alert if a file is present in a directory for 10 min

hi, A script which look into a directory and send an alert via a mail. If there is any file exisiting in a directory for more then 10 min. (5 Replies)
Discussion started by: madfox
5 Replies

7. Shell Programming and Scripting

How to Check whether list file present in TXT file exist or not

Hi All, I have txt file which has list of files. I have to check whether these files exist or not. Thanks supriya (6 Replies)
Discussion started by: supriyabv
6 Replies

8. Shell Programming and Scripting

ls > file - Creating file containing the list of all files present in a directory

Hi All, I need to create a file which contains the list of all the files present in that directory. e.g., ls /export/home/user/*.dat > list_file.dat but what i am getting is: $ ls /export/home/user/*.dat > list_file.dat /export/home/user/*.dat: No such file or directory But I have... (1 Reply)
Discussion started by: pranavagarwal
1 Replies

9. Programming

Getting present working directory

How can I get the present working directory in unix system using c programming and stored it in a string ?? (0 Replies)
Discussion started by: winsonlee
0 Replies

10. UNIX for Dummies Questions & Answers

How one can list only the name of directories present in the directory by using ls co

How one can list only the name of directories present in the directory by using ls command. (2 Replies)
Discussion started by: amolpatil54321
2 Replies
Login or Register to Ask a Question