Created
March 4, 2025 07:50
-
-
Save mfrazi/02ed8922a76a793f86eb8020a47fa72d to your computer and use it in GitHub Desktop.
Count total files recursively under directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SFTP_HOST="" | |
SFTP_PORT="22" | |
SFTP_USER="" | |
PRIVATE_KEY="" | |
# Function to recursively list files in SFTP | |
count_files_recursive() { | |
local DIR="$1" | |
# Remove two line from stdout, command cd $DIR and ls -l | |
local FILE_COUNT=-2 | |
# Run SFTP command and capture the output | |
FILE_LIST=$(sftp -i "$PRIVATE_KEY" -P "$SFTP_PORT" "$SFTP_USER@$SFTP_HOST" <<EOF | |
cd $DIR | |
ls -l | |
EOF | |
) | |
# Loop through each line | |
while IFS= read -r LINE; do | |
SUB=$(echo "$LINE" | awk '{print $NF}') | |
[[ -z "$SUB" || "$SUB" == "." || "$SUB" == ".." ]] && continue | |
# Check if it's a directory (SFTP `ls` does not show file types explicitly) | |
if [[ ! "$LINE" =~ ^d ]]; then | |
((FILE_COUNT++)) | |
else | |
count_files_recursive "$DIR/$SUB" | |
fi | |
done <<< "$FILE_LIST" | |
# Print total files under a directory | |
echo "$DIR,$FILE_COUNT" | |
} | |
count_files_recursive "/" | |
# ./count_files.sh > output.csv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment