Skip to content
Advertisement

Solve a shell script where i have to delete the files with size 0?

I have to delete with permission the files with size 0 created by a user from a certain path. The user and the path are positional parameters. i thought of making a “for” where i go through the files from a certain user and then with “find” to remove the ones empty from the wanted path. Something like this:

#!/bin/bash
for file in /$1/*

do

find /$2 -size -0b -type 'f' -exec rm -i {} ;

done

But it’s not working, can you tell me why?

Advertisement

Answer

(recommend editing the posting where the script is complete and presented as code; eg the leading # is missing; note: invert the script and click the {} button)

As shown, the script seems somewhat odd; recommend checking stuff like whether the cmd-line arg-count is correct and then whether args $1 and $2 are valid; the expression /$1/* indicates you want to repeat the same search based on the wildcard * result of everything within the absolute path /$1, but the trailing /* is not needed (find will normally descend into subdirs), meanwhile the directory passed to find is the invariant abs path /$2. Here’s a revision with some elements guessed:

#!/bin/bash

[ $# -ne 2 ] && { echo Usage: ${0##*/} dir user 1>&2; exit 1; }

# search the unmodified dir $1 for any file owned by $2 with size 0 and run an interactive delete
find $1 -user $2 -type f -size 0 -exec rm -i {} ;
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement