SME_Paddington
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:
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 '{}' \;
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.
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.
ASKER
@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
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
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
ASKER
@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.
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:~$
Sorry about the syntax errors
Here is code
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
ASKER
@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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Ah, forgot that rm -i didn't work in a pipe
Using a hybrid between my code and farzanj's code:
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
ASKER
@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.
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.
ASKER
Excellent.
Personal thanks to both Farzanj and Tintin.
Cheers Guys.
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.
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
ASKER
@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.
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,
Best regards,
Open in new window