Reputation: 21
I am currently studying shell scripting on Linux devices during one of my classes and I have managed to get an extra task that so far exceeds my skills.
I was tasked to write a shell script that finds symlinks that point to empty folders. Now the first two parts are easy - I know how to use the find
command with tests like -type l
and -xtype d
. The problem comes from checking if the folder itself is empty. I've asked my teacher but all I've got were cryptic hints about xargs
or exec
arguments which I'm actually not allowed to use yet according to the instruction. My best bet so far is to throw the results of the find into a for loop and test the path I get from the link in each loop, but that doesn't seem to work. So far my code looks like this:
for var in $`find $1/* -type l -xtype d -print`
do
if [ $`readlink -f $var` -empty ]
then
echo $`readlink -f $var`
fi
done
Any help would be greatly appreciated!
Upvotes: 2
Views: 566
Reputation: 177520
Are you looking for -empty
option to find?
$ mkdir empty_folder
$ ln -s empty_folder/ symlink
$ find symlink -type d -empty
symlink
This could also be done with filename expansion.
$ shopt -s dotglob failglob
$ true symlink/*
bash: no match: symlink/*
Upvotes: 2