Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 404

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.

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 '{}' \;

0
• 7
• 6
• 2
1 Solution

Commented:
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  0 Author Commented: 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.
0

Author Commented:
@farzanj

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
0

Commented:
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

0

Author Commented:
@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:~$

0

Commented:

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  0 Author Commented: @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.

0

Commented:
Sorry again.

The problem with all the above codes that rm -i was perceiving continuous stream of file names as a response to yes or no.  In this situation no other technique like for example 'read' wasn't working.  So I changed the code a little bit.  I hope it would do what you wanted.

#!/bin/bash

for file in $(find$@ -type f)
do
size=$(ls -l$file | awk '{print $5}') if (( size % 2 == 0 )) then \rm -i$file
fi
done

0

Commented:
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

0

Commented:
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  0 Author Commented: @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. 0 Author Commented: Excellent. Personal thanks to both Farzanj and Tintin. Cheers Guys. 0 Commented: 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
`
0

Author Commented:
@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.
0

Commented:
I am glad to hear I was some help.  Looking forward to be helping you.

Best regards,
0

Featured Post

• 7
• 6
• 2
Tackle projects and never again get stuck behind a technical roadblock.