Script which removes files from the first directory if there is a file in the second directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script which removes files from the first directory if there is a file in the second directory
# 1  
Old 10-26-2011
Question Script which removes files from the first directory if there is a file in the second directory

Script must removes files from the first directory if there is a file with same name in the second directory

Script passed to the two directories, it lies with them in one directory:
sh script_name dir1 dir2

This is my version, but it does not work Smilie
Code:
set - $2/*
for i
do
  set - $1/*
    for j
    do
      if [ "$i" = "$j" ]
        then rm $j
      fi
    done
done

Please, write your versions of this script

P.S.
Sorry for my bad English
# 2  
Old 10-26-2011
Code:
d1=$1
d2=$2

cd "$d1"
for file in *
do
  if [ -f "$d2/$file" ]
  then
    printf "%s exists in both\n" "$file"
  fi
done

This User Gave Thanks to cfajohnson For This Post:
# 3  
Old 10-26-2011
Quote:
Originally Posted by cfajohnson
Code:
d1=$1
d2=$2

cd "$d1"
for file in *
do
  if [ -f "$d2/$file" ]
  then
    printf "%s exists in both\n" "$file"
  fi
done

$ sh scrdel dir1 dir2
$ sh scrdel dir1 dir2
$ sh scrdel dir1 dir2

Thanks, but the console does not show this message "%s exists in both\n", and the file should be removed (in first directory). I have change it
Code:
printf "%s exists in both\n" "$file"

to
Code:
 rm "$file"

and it also don't work. Smilie Please, help me.
# 4  
Old 10-26-2011
Pass the full path to the directories (or at least to the second one), e.g.:
Code:
scrdel dir1 "$PWD/dir2"

This User Gave Thanks to cfajohnson For This Post:
# 5  
Old 10-26-2011
MySQL

Quote:
Originally Posted by cfajohnson
Pass the full path to the directories (or at least to the second one), e.g.:
Code:
scrdel dir1 "$PWD/dir2"

Thank you very much, you really helped me!!!Smilie Is it impossible to write this script and it will work sh scrdel dir1 dir2 without passing the full path?Smilie
# 6  
Old 10-26-2011
Quote:
Originally Posted by SLAMUL
Thank you very much, you really helped me!!!Smilie Is it impossible to write this script and it will work sh scrdel dir1 dir2 without passing the full path?Smilie
Code:
d1=$1
d2=$2

for file in "$d1"/*
do
  file2=$d2/${file##*/}
  if [ -f "$file2" ]
  then
    printf "%s exists as %s\n" "$file" "$file2"
  fi
done

This User Gave Thanks to cfajohnson For This Post:
# 7  
Old 10-26-2011
MySQL

Quote:
Originally Posted by cfajohnson
Code:
d1=$1
d2=$2

for file in "$d1"/*
do
  file2=$d2/${file##*/}
  if [ -f "$file2" ]
  then
    printf "%s exists as %s\n" "$file" "$file2"
  fi
done

Thank you, you are the best!!!Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to find directory is getting files in every 10 mins, if not then when last time file received

Dears, I am looking for a script which will work as a watch directory. I ha directory which keep getting files in every 10 mins and some time delay. I want to monitor if the directory getting the files in every 10 mins if not captured the last received file time and calculate the delay. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

2. Shell Programming and Scripting

Shell script cannot create directory and move the file to that directory

I have a script, which is checking if file exists and move it to another directory if then mkdir -p ${LOCL_FILES_DIR}/cool_${Today}/monthly mv report_manual_alloc_rpt_A_I_ASSIGNMENT.${Today}*.csv ${LOCL_FILES_DIR}/cool_${Today}/monthly ... (9 Replies)
Discussion started by: digioleg54
9 Replies

3. Shell Programming and Scripting

Moving Files one directory to another directory shell script

Hi, Could you please assist how to move the gz files which are older than the 90 days from one folder to another folder ,before that it need to check the file system named "nfs" if size is less than 90 or not. If size is above 90 then it shouldn't perform file move and exit the script throwing... (4 Replies)
Discussion started by: venkat918
4 Replies

4. Shell Programming and Scripting

List files with date, create directory, move to the created directory

Hi all, i have a folder, with tons of files containing as following, on /my/folder/jobs/ some_name_2016-01-17-22-38-58_some name_0_0.zip.done some_name_2016-01-17-22-40-30_some name_0_0.zip.done some_name_2016-01-17-22-48-50_some name_0_0.zip.done and these can be lots of similar files,... (6 Replies)
Discussion started by: charli1
6 Replies

5. AIX

How to set owner and permission for files/directory in directory in this case?

Hi. My example: I have a filesystem /log. Everyday, log files are copied to /log. I'd like to set owner and permission for files and directories in /log like that chown -R log_adm /log/* chmod -R 544 /log/*It's OK, but just at that time. When a new log file or new directory is created in /log,... (8 Replies)
Discussion started by: bobochacha29
8 Replies

6. Shell Programming and Scripting

Shell scripting-I need a script which should watch a directory for a file with specific directory

I need a script which should watch a directory for a file with specific directory. If it finds a file in directory, it should search for few specific keyword in the file. if the keyword exists, it should trim string from specific column. The file should be moved to another directory and the a... (8 Replies)
Discussion started by: akashdeepak
8 Replies

7. Shell Programming and Scripting

Grepping file names, comparing them to a directory of files, and moving them into a new directory

got it figured out :) (1 Reply)
Discussion started by: sHockz
1 Replies

8. Shell Programming and Scripting

FTP files from different directory from remote server to one directory in local

Hi All, I want to search for .log files from folders and sub folders in remote server and FTP them to one particular folder in the local machine. I dont want to copy the entire directory tree structure, just have to take all the .log files from all the folders by doing a recursive search from the... (3 Replies)
Discussion started by: dassv
3 Replies

9. UNIX for Advanced & Expert Users

How to rsync or tar directory trees, with hidden directory, but without files?

I want to backup all the directory tress, including hidden directories, without copying any files. find . -type d gives the perfect list. When I tried tar, it won't work for me because it tars all the files. find . -type d | xargs tar -cvf a.tar So i tried rsync. On my own test box, the... (4 Replies)
Discussion started by: fld2007
4 Replies
Login or Register to Ask a Question