Link to home
Start Free TrialLog in
Avatar of SME_Paddington
SME_PaddingtonFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Find files in a given directory with an even size to delete - Linux

Dear Experts,

I need to find files within a directory of an even size to be iteratively deleted. I would also like to input a confirmation, before they are deleted with each evenly sized file found within the directory.

Thanks in Advance...

My code so far is as follows:


#!/bin/bash

echo "DIR containing even size files to be removed:"
echo $@
echo "Even files to be removed in the DIR are:"
find $@ -size '*0|*2|*4|*6|*8' -print -exec ls -al '{}' \;

Open in new window

Avatar of farzanj
farzanj
Flag of Canada image

You need something like

#!/bin/bash

unalias rm
FILES=/tmp/files
find $@ -type f > $FILES

cat $FILES | while read file
do
    size=$(ls -l $file | awk '{print $5}')
    if (( size % 2 == 0 ))
        rm -i $file
    fi

done

Open in new window

Avatar of SME_Paddington

ASKER

Sorry what I meant was:

a shell script, which deletes files in the directory given, as the script argument. These files should be a size of an even number. The deletion of each file should be confirmed by pressing/ typing "d". Script execution as :

$ script directory_name

Thank you very much though, this looks extremely promising.
@farzanj

When attempting to run your code I receive the following errors:

G1:~$ ./filedelete.sh Documents
The directory specified, with evenly sized files to be deleted is:
/home/G1/Documents

The even files to be deleted within the directory are:
./filedelete.sh: line 8: unalias: rm: not found
./filedelete.sh: line 17: syntax error near unexpected token `fi'
./filedelete.sh: line 17: `    fi'
G1:~$

Please keep it up, as I think you are on the right track.

Kind Regards
Avatar of Tintin
Tintin

Try this one.


#!/bin/bash
echo "DIR containing even size files to be removed:"
echo $@
find $@ -type f -printf "%p %s\n"|egrep "(0|2|4|6|8)$"|awk '{print $1}'|xargs rm -i

Open in new window

@Tintin

Yes, this solution is part way there, but requires the iterative described step that 'farzanj' coded.

I think that if one of you two was able to piece together/ integrate each others coding we would have a solution.

Who ever does so, will be awarded justly.

Kind regards, and thanks to both of you - for all your input so far.
Output from your code Tintin:

G1:~$ ./filedelete.sh Documents
The directory specified, with evenly sized files to be deleted is:
/home/G1/Documents

The even files to be deleted within the directory are:
Documents
rm: remove regular file `Documents/ntuser.ini'? rm: cannot remove `Documents/DNS': No such file or directory
G1:~$ ls Documents
DNS Attacks.txt  DNS Lookups.txt  ntuser.ini
G1:~$ ./filedelete.sh ^C
G1:~$

Open in new window

Sorry about the syntax errors

Here is code
#!/bin/bash

unalias rm 2> /dev/null
FILES=/tmp/files
find $@ -type f > $FILES

cat $FILES | while read file
do
    size=$(ls -l $file | awk '{print $5}')
    if (( size % 2 == 0 ))
    then
        rm -i $file
    fi

done

Open in new window

@farzanj

I have tested your code, and received the output as follows:

G1:~$ pico ftest.sh
G1:~$ chmod +x ftest.sh
G1:~$ ./ftest.sh Documents
rm: remove regular file `Documents/ntuser.ini'? G1:~$ ls -a Documents
.  ..  DNS Attacks.txt  DNS Lookups.txt  ntuser.ini
G1:~$

It shows the prompt for deletion, but does not wait for my input.

Please note: Thank you for your assistance.
ASKER CERTIFIED SOLUTION
Avatar of farzanj
farzanj
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
With Tintin's code you can probably do something like this:

#!/bin/bash
echo "DIR containing even size files to be removed:"
echo $@
for file in $(find $@ -type f -printf "%p %s\n"|egrep "(0|2|4|6|8)$"|awk '{print $1}')
do
    \rm -i $file
done

Open in new window

Ah, forgot that rm -i didn't work in a pipe

Using a hybrid between my code and farzanj's code:


#!/bin/bash
echo "DIR containing even size files to be removed:"
echo $@
find $@ -type f -printf "%p %s\n" | while read file size
do
    (( size % 2 == 0 )) && \rm -i $file
done

Open in new window

@farzanj

At the current time, yours is looking like the strongest candidate.

In regards to your post ---> at 07/05/11 06:24 PM, ID: 35714066

All I need to do, is have the deletion performed/ substituted with the D key, is this possible?

I will award you with the solution, but I do ask, if it could be modified for my personal use, and substitute the Y key with the D key?

Kind regards, and many thanks to both of you.
Excellent.

Personal thanks to both Farzanj and Tintin.

Cheers Guys.
Here you go.

So now you need to do D or d to delete and just hit enter (no other key) for not deleting.
#!/bin/bash

for file in $(find $@ -type f)
do
        size=$(ls -l $file | awk '{print $5}')
        if (( size % 2 == 0 ))
        then
                printf "Delete $file (D/d)? "
                read response
                if [[ $response =~ [Dd] ]]
                then
                        \rm -f $file
                fi
        fi
done

Open in new window

@farzanj

Thank you very much for all your support, and I will follow you, so I can ask you future questions - that are related to shell scripting. I have a few more questions to ask, over the space of the next few days, so I appreciate your input very much and will be in touch.

Many thanks.
I am glad to hear I was some help.  Looking forward to be helping you.

Best regards,