Unix script to find files & amp; delete them based on a given size

advertisements

I'm working on a unix script that has 2 input parameters - path and size.

The script will check all the files in the given path with the given size and deletes them. If the delete operation fails, the respective file-name is recorded into a file. For any other case, the file is rendered without any action.

I have written a short code (don't know whether it works).

find $path -type f -size +${byte_size}c -print | xargs -I {}
if $?=1;
then
rm -rf {};
else
echo {} >> Error_log_list.txt'

where
$path is the path where we search for the files.
size is the input size.
Error_log_list.txt is the file where we send the non-deletable filenames.

Can anyone please help me verify whether it is correct?


Executing your script results in the following syntax error:

   ./test.sh: line 9: unexpected EOF while looking for matching `''
   ./test.sh: line 11: syntax error: unexpected end of file

Moreover the condition of the if statement seems not correct. If I am not wrong it tests the return code of the "rm" command before to execute the command.

I am not familiar with xargs and I tried to rewrite your script using a while loop construct. Here my script

#!/bin/bash

path=$1
byte_size=$2

find $path -type f -size +${byte_size}c -print | while read file_name
do
    rm -f $file_name
    if [ ! $? -eq 0 ]; then
        echo $file_name >> Error_log_list.txt
   fi
done

I tested it trying to delete files without the right permission and it works.