Untitled

 avatar
unknown
plain_text
5 months ago
754 B
3
Indexable
#!/bin/bash

# Output file
output_file="users_info.csv"

# Add CSV header
echo "Username,Number of Files,Number of Folders,Home Directory Size" > "$output_file"

# Loop through each user's home directory in /home
for dir in /home/*; do
    if [ -d "$dir" ]; then
        username=$(basename "$dir") # Extract the username from the directory name
        num_files=$(find "$dir" -type f 2>/dev/null | wc -l) # Count files
        num_folders=$(find "$dir" -type d 2>/dev/null | wc -l) # Count folders
        dir_size=$(du -sh "$dir" 2>/dev/null | awk '{print $1}') # Get directory size

        # Append the data to the CSV file
        echo "$username,$num_files,$num_folders,$dir_size" >> "$output_file"
    fi
done

echo "Data written to $output_file"
Editor is loading...
Leave a Comment