shell script: deleting files from a directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script: deleting files from a directory
# 1  
Old 07-09-2006
Question shell script: deleting files from a directory

i have the following problem:
use shell script:

Two directories have to be searched for files havin the same name.
then delete these files from one of these directories.
the directory names have to be accepted as command line arguments.

what i have done till now is:

1. run a loop through both directories and
2. tried to compare the filenames.
3. i am not able to compare the names of files since the filenames are extracted alongwith the directory (dirname/filename)
4. i tried using cut.
# 2  
Old 07-09-2006
Suppose you want to compare two directories test1 and test2 and want to delete files from test2 directory if found in test1, you can do something like this:
Code:
#! /bin/ksh
for i in ./test1/*; do
filename=`basename $i`
if [[ -f ./test2/$filename ]]; then
rm ./test2/$filename
fi
done

Regards,
Tayyab
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

How to check whether directory has files in it or not in shell script?

hi, I am having script in which i want to check if directory has any file in it or not. If directory contains a single or more files then and only then it should proceed to further operations... P.S.: Directory might have thousand number of files. so there might be chance of getting error... (4 Replies)
Discussion started by: VSom007
4 Replies

3. Shell Programming and Scripting

Shell : deleting only first 2 files in a directory

I have 4 files in a directory and want to delete only first 2 files only.. $ ls -ltr total 640 -rw-r--r-- 1 user other 148779 Oct 12 10:50 file1.xls -rw-r--r-- 1 user other 148779 Oct 12 10:50 file2.xls -rw-r--r-- 1 user other 148779 Oct 12 10:50 file3.xls... (4 Replies)
Discussion started by: giridhar276
4 Replies

4. Shell Programming and Scripting

AIX system.... deleting files in remote directory after retrieving files

Hi Friends, I am new to this , I am working on AIX system and my scenario is to retrive the files from remote system and remove the files from the remote system after retreving files. I can able to retrieve the files but Can't remove files in remote system. Please check my code and help me out... (3 Replies)
Discussion started by: vinayparakala
3 Replies

5. Shell Programming and Scripting

Need help with shell script for moving/deleting/renaming files

Hi. I need help with a little script that will be used to move some files to their parent directory, delete the directory, rename one file in the parent directory and delete another, then continue to the next. Here's an example: /var/media/Music/Genesis/1970 album - Trespass (2008 Box -... (4 Replies)
Discussion started by: aflower
4 Replies

6. Shell Programming and Scripting

deleting files inside shell script - ( using find)

Hi, I am using the command find /apps/qualdb/gpcn/scripts/cab_outbound/archive -name 'z*' -mtime +28 -exec rm {} \; in unix command prompt for deleting the files in my unix system under the specfied folder. It was succesfull. But when i running this command inside a shell script name... (2 Replies)
Discussion started by: Jayaram.Nambura
2 Replies

7. Shell Programming and Scripting

shell script reqd - deleting files

I have written a script that deletes files: Requirement: i need to delete the files and to know how many files are deleted i.e the count of files and even i need to display when the started time of deletion and the ending time of deletion. I need to display these two times. script: ... (2 Replies)
Discussion started by: venkatesht
2 Replies

8. Linux

deleting only directory not files

Hi Guys, I want to know wheather it is possible to delete directory not files, Example: In one directory there are 10 dirs and 100 files but my req is to delete only dirs not file Wheather it is possible ? (13 Replies)
Discussion started by: manoj.solaris
13 Replies

9. Shell Programming and Scripting

deleting empty files in a directory

Hello Gurus, I want to delete only empty files (files with 0 bytes) at once from the local directory. Rightnow I need to go through all the files one by one manually and check the empty files before deleting them. Is there any unix command that finds and deletes empty files in a directory?... (5 Replies)
Discussion started by: berlin_germany
5 Replies

10. UNIX for Advanced & Expert Users

Accidentally deleting directory/files

Hi, I accidentally deleted a big directory with all its sub-directories and bunch of source code files which I have been developing for about 2 years... What will I do now, how can I retrieve my files, directory hierarchy back ??? If anyone, please HELP ! ! ! ... (4 Replies)
Discussion started by: milhan
4 Replies
Login or Register to Ask a Question