I have a directory containing thousands of items �video files�, these items were generated by an application, which generates two items with the same name but with different extensions �.avi and .mp4�. There was a tool in my application to remove one item @ a time. Later I found this tool removes item with one extension �.avi� the other file �.mp4� remains.
I want to remove the items .mp4 after checking that the items .avi is not exist.
Thank you in advance
Try this:
#!/bin/ksh
ls *.mp4|while read line
do
print "Checking [$line] ... \c"
if [[ -f "${line%.mp4}.avi" ]]; then
print ".avi exists"
else
print "No matching .avi, deleting $line"
# remove the comment after validating
#rm -f "$line"
fi
done
exit 0
I may have misunderstood your requirements, but what's wrong with this bash "one" liner:
for i in directory/with/videos/files/*.mp4;do if [ ! -e ${i%.mp4}.avi ] ; then rm $i;fi;done
Or more explicitly:
for i in directory/with/videos/files/*.mp4;do # work through all mp4 files
if [ ! -e "${i%.mp4}.avi" ] ; then # if no matching avi file exists
rm "$i" # delete the mp4 file
fi
done
Note: Double quotes around variable references, will ensure that file names with spaces do get processed properly..
Doh, good catch. I updated my example.
many thanks for your help
solved