Hi,

Could someone tell me how I can delete files using a shell script or c++? The condition for deleting files: compare all files (say *.deb files) in folder A to those in folder B. If any file in B is also contained A, then delete the one in B.
This could probably done in two lines of shell scripting but I'm very new to programming.

Thanks in advance for your help. Regards, h.y.

Recommended Answers

All 4 Replies

what operating system? MS-Windows? use FindFirstFile() and FindNextFile() to get a list of all the files in dir A, store the filenames in a string array. Then use the same functions to get the files in directory B. For each file in B search the array of filenames you created from directory A, if found the delete the file.

Thanks. I use Ubuntu and Windows. I'm looking for a shell script that does this. But I'll try what you're saying as well. Thanks.

Thanks. I use Ubuntu and Windows. I'm looking for a shell script that does this. But I'll try what you're saying as well. Thanks.

Shell scripting is much easier. But this is not the forum for that. There is a UNIX support forum for scripting in daniweb. But you could try this.

#!/bin/bash
pathA='/blah/blah/blah/'
pathB='/blah/blah/blah/blah/'
ext="deb"
for file in $(ls "$pathA"); do
	#
	# Only process DEB files.
	#
	file_ext=${file##*.}
	case $file_ext in
		deb | DEB )
		if [ -e "$pathB$file" ]; then
			echo deleting "$pathB$file"
			rm -f "$pathB$file"
		fi
	esac
done

Shell scripting is much easier. But this is not the forum for that. There is a UNIX support forum for scripting in daniweb. But you could try this.

#!/bin/bash
pathA='/blah/blah/blah/'
pathB='/blah/blah/blah/blah/'
ext="deb"
for file in $(ls "$pathA"); do
    #
    # Only process DEB files.
    #
    file_ext=${file##*.}
    case $file_ext in
        deb | DEB )
        if [ -e "$pathB$file" ]; then
            echo deleting "$pathB$file"
            rm -f "$pathB$file"
        fi
    esac
done

Thanks very much for the code, and for directing me to the right forum. I really appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.