Reputation: 559
I'm trying to figure out how I would search through multiple directories, defined with pushd, in order to find the disk space of the folders in the list, as well as whether any of the folders are sym links. Right now I have some very inefficient for loops that don't work over multiple directories (it works fine if there's just one, but once I start defining multiple working directories with pushd it mucks things up).
UPDATE:
Contents of $COMPARE for reference:
COMPARE=`comm -23 "$HOMEOUT" "$USEROUT" |
comm -23 - <(
for f in "${FILTER[@]}"; do
echo "$f"
done | sort)`
Here's my code and my results when executing it:
DIRS=`ls -lah / | grep home | awk '{ print $9 }'`
for i in "$DIRS"; do
pushd /$i/ >/dev/null
done
# Find the disk space of each folder
for x in "$DIRS"; do
du -s /$x/$COMPARE | sort -n | cut -f 2-|xargs -i du -sh {}
done
# Check for and output symlinks
SYM=`for y in "$COMPARE"; do
find /$DIRS/$y -maxdepth 1 -type l -print
done`
Results:
+ DIRS='home
home2
home3
old_home'
+ for i in '"$DIRS"'
+ pushd /home home2 home3 old_home/
+ for x in '"$DIRS"'
+ sort -n
+ du -s /home home2 home3 old_home/someuser someuser2 someuser3 someuser4
+ cut -f 2-
+ xargs -i du -sh '{}'
As you can see, this isn't working as I want it to search through each user in the list like so (depending on where their folder is located):
/home/user /home/user2 /home2/user3 /home3/user4
Can someone suggest a better/more efficient way of doing this that actually works? I'd like to figure out how I can condense this, overall.
Bash version:
GNU bash, version 3.2.25(1)-release-(x86_64-redhat-linux-gnu)
Thanks in advance!
Upvotes: 0
Views: 1403
Reputation: 56089
"$DIRS"
will expand to "home home2 ..."
, so for
will see it as a single token. And don't parse ls
, it's highly fragile. Use:
for x in /*home*; do
du -sh "$x" | sort -h
done
(note that sort -h
is a gnu extension, but as you're on redhat you should have it).
You haven't shown us how you construct $COMPARE
, but I think you're looking for this instead of the second loop:
find /*home*/* -maxdepth 1 -type l
If you only want some users, you'll have to show us how you construct $COMPARE
; you'll probably want to make it an array and use find /*home*/"${COMPARE[@]}" ...
.
Construct COMPARE
to be an array by wrapping it in ()
.
IFS=$'\n' COMPARE=($(comm -23 "$HOMEOUT" "$USEROUT" |
comm -23 - <(printf "%s\n" "${FILTER[@]}" | sort))
and use it:
find /*home*/"${COMPARE[@]}" -maxdepth 1 -type l
Upvotes: 1