Compare files in two folders and delete missing ones


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare files in two folders and delete missing ones
# 15  
Old 03-03-2010
Output:
Code:
ks2014631# /home/kaah/torrent_clean2.sh
ls: spaces.file: No such file or directory
ls: test: No such file or directory
ls: with: No such file or directory
ls: /home/kaah/test1/test: No such file or directory
ls: spaces.file.torrent: No such file or directory
ls: with: No such file or directory
test.file
/home/kaah/test1/test.file.torrent
test1.file
/home/kaah/test1/test1.file.torrent
test2.file
ls: /home/kaah/test1/test2.file.torrent: No such file or directory
test3.file
/home/kaah/test1/test3.file.torrent
test4.file
ls: /home/kaah/test1/test4.file.torrent: No such file or directory


Dir contents:

Code:
ks2014631# ls -l /home/kaah/test1
total 8
-rw-r--r--  1 root  wheel  2 Mar  3 14:19 test with spaces.file.torrent
-rw-r--r--  1 root  wheel  2 Mar  3 13:41 test.file.torrent
-rw-r--r--  1 root  wheel  2 Mar  3 13:41 test1.file.torrent
-rw-r--r--  1 root  wheel  2 Mar  3 13:41 test3.file.torrent
ks2014631# ls -l /home/kaah/test2
total 12
-rw-r--r--  1 root  wheel  2 Mar  3 14:19 test with spaces.file
-rw-r--r--  1 root  wheel  2 Mar  3 13:42 test.file
-rw-r--r--  1 root  wheel  2 Mar  3 13:42 test1.file
-rw-r--r--  1 root  wheel  2 Mar  3 13:42 test2.file
-rw-r--r--  1 root  wheel  2 Mar  3 13:42 test3.file
-rw-r--r--  1 root  wheel  2 Mar  3 13:42 test4.file

Adding " " to the commands make it work with filenames with spaces.
It seems like the if is not working properly. It gets looped even though the file is missing.

Last edited by kaah; 03-03-2010 at 11:05 AM..
# 16  
Old 03-03-2010
If you have spaces in the file names you have to quote the variables:
Code:
#!/bin/sh

for file in *
do
  ls "${file}"          # file current directory
  ls /home/kaah/test1/"${file}".torrent         # files torrent directory
done

# 17  
Old 03-03-2010
Code:
Code:
#!/bin/sh

for file in *
do
 if [ ! -e /www/kaah/test1/"${file}".torrent ]
then
   # ls "${file}"          # try this first with ls instead of rm
   # ls "/home/kaah/test1/${file}.torrent"
    ls "${file}"          # file current directory
    ls /home/kaah/test1/"${file}".torrent         # files torrent directory
fi
done

Output:
Code:
ks2014631# /home/kaah/torrent_clean2.sh
test with spaces.file
/home/kaah/test1/test with spaces.file.torrent
test.file
/home/kaah/test1/test.file.torrent
test1.file
/home/kaah/test1/test1.file.torrent
test2.file
ls: /home/kaah/test1/test2.file.torrent: No such file or directory
test3.file
/home/kaah/test1/test3.file.torrent
test4.file
ls: /home/kaah/test1/test4.file.torrent: No such file or directory

The if does not seem to work. It loops if though the file is not missing.
# 18  
Old 03-03-2010
Hi.

It seems that filenames are getting in the way. Perhaps incorporating the checksums would help. See man md5sum, man shasum ... cheers, drl
# 19  
Old 03-03-2010
Quote:
Originally Posted by drl
Hi.

It seems that filenames are getting in the way. Perhaps incorporating the checksums would help. See man md5sum, man shasum ... cheers, drl
Can you checksum just the filenames?
# 20  
Old 03-03-2010
A typo in your code:
Code:
#!/bin/sh

for file in *
do
 if [ ! -e /www/kaah/test1/"${file}".torrent ]
then
   # ls "${file}"          # try this first with ls instead of rm
   # ls "/home/kaah/test1/${file}.torrent"
    ls "${file}"          # file current directory
    ls /www/kaah/test1/"${file}".torrent         # files torrent directory
fi
done

# 21  
Old 03-03-2010
Quote:
Originally Posted by Franklin52
A typo in your code:
Code:
#!/bin/sh

for file in *
do
 if [ ! -e /www/kaah/test1/"${file}".torrent ]
then
   # ls "${file}"          # try this first with ls instead of rm
   # ls "/home/kaah/test1/${file}.torrent"
    ls "${file}"          # file current directory
    ls /www/kaah/test1/"${file}".torrent         # files torrent directory
fi
done

Doh! I feel like such a noob Smilie Works fine now! Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to compare files in 2 folders and delete the large file

Hello, my first thread here. I've been searching and fiddling around for about a week and I cannot find a solution.:confused: I have been converting all of my home videos to HEVC and sometimes the files end up smaller and sometimes they don't. I am currently comparing all the video files... (5 Replies)
Discussion started by: Josh52180
5 Replies

2. Shell Programming and Scripting

Script to compare partial filenames in two folders and delete duplicates

Background: I use a TV tuner card to capture OTA video files (.mpeg) and then my Plex Media Server automatically optimizes the files (transcodes for better playback) and places them in a new directory. I have another Plex Library pointing to the new location for the optimized .mp4 files. This... (2 Replies)
Discussion started by: shaky
2 Replies

3. Shell Programming and Scripting

Compare 2 files and find missing fields awk

Hello experts! I have 2 files. file1 is a list file containing uniquely names. e.g.: name1 number number name2 number number name5 number number name10 number number ... file2 is a data file arbitrary containing the names of file1 in paragraphs separated by "10" e.g. name4 ... (3 Replies)
Discussion started by: phaethon
3 Replies

4. Shell Programming and Scripting

Compare two files and get only missing names

I need to compare two files (oldfile1 & newfile). Need to ignore the values which are present in both files. At the same time, i need to get only records in new file. Tried using Join -v1 -v2 oldfile1 newfile (suspect it has not worked as expected). could anyone of you please help me here. (5 Replies)
Discussion started by: Selva_2507
5 Replies

5. Shell Programming and Scripting

Linux Script to compare two folders and copy missing files

Hi, I need help in shell scripting. If someone can help me, that would be great! Problem. I want Linux Script to compare two folders and copy missing files. Description. I have two directories /dir1 /dir2 I need to copy all distinct/new/unique/missing files from /dir1 and that... (1 Reply)
Discussion started by: S.Praveen Kumar
1 Replies

6. Shell Programming and Scripting

How to compare files in two folders using cmp?

i recently copied 400GB of data from a NTFS drive to a ext4 drive. I want to verify that the data is 100% identical to the original. I wanted to use cmp but it only does two files. The directory that was copied contains many subdirectories and all sorts of files (not just text). So I guess... (5 Replies)
Discussion started by: fuzzylogic25
5 Replies

7. Shell Programming and Scripting

Compare 2 folders to find several missing files among huge amounts of files.

Hi, all: I've got two folders, say, "folder1" and "folder2". Under each, there are thousands of files. It's quite obvious that there are some files missing in each. I just would like to find them. I believe this can be done by "diff" command. However, if I change the above question a... (1 Reply)
Discussion started by: jiapei100
1 Replies

8. UNIX for Dummies Questions & Answers

Delete folders and files in it - UNIX

I need to delete a folder and files in it of yesterdays and simply put this in cron. Folder name - "2010-03-2010" File name - "eprod_06_23.dmp" and "eprod_06_23.exp" Actually this folder and file is been created by a script which takes a backup of DB everyday.Thats why it creates folder and file... (3 Replies)
Discussion started by: j_panky
3 Replies

9. Shell Programming and Scripting

Compare the checksum of files in 2 different folders

Hi I have 2 different folders on different machines. they are supposed to be same but some time for unknown reason they are not. then we have to generate a report for files which are not matching. I was doing as below - cd folder1 find . -type f | sort | cksum >1.txt cd folder2 find .... (7 Replies)
Discussion started by: reldb
7 Replies

10. Shell Programming and Scripting

delete all folders/files and keep only the last 10 in a folder

Hi, I want to write a script that deletes all folders and keep the last 10 recent folders. I know the following: ls -ltr will sort the folders from old to recent. ls -ltr | awk '{print $9}' will list the folder names (with a blank line at the beginning) I want to get the 10th folder from... (3 Replies)
Discussion started by: melanie_pfefer
3 Replies
Login or Register to Ask a Question