Solved

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

Posted on 2011-05-07
403 Views
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

LVL 31

Expert Comment

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 Comment 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 Comment

@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
0

LVL 48

Expert Comment

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 Comment

@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

LVL 31

Expert Comment

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

LVL 31

Accepted Solution

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

LVL 31

Expert Comment

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

LVL 48

Expert Comment

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 Comment @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 Closing Comment Excellent. Personal thanks to both Farzanj and Tintin. Cheers Guys. 0 LVL 31 Expert Comment 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 Comment

@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

LVL 31

Expert Comment

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

Best regards,
0

Join & Write a Comment Already a member? Login.

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!