# this script (maybe) only working on mac# FTP server credentialsFTP_USERNAME="username"FTP_PASSWORD="password"FTP_SERVER="257.257.257.257"#change this ^_^now="/firstdir"OUTPUT="./output/now.txt"#the functionfunction getFiles() { local path="$1" if [[ ${path: -1} != "/" ]]; then path="${path}/" fi local scriptpath=$(echo "$path" | sed 's/ /\\ /g' | sed 's/&/\\&/g') local output=$OUTPUT local remote_files local size line local filename local first lftp -u "$FTP_USERNAME","$FTP_PASSWORD" "$FTP_SERVER" <<EOF cd $scriptpath ls > /tmp/files.txt # list all files and directories and save to a temporary file byeEOF while read line; do filename=$(echo "$line" | sed -n -r 's/^.*[Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec]{3} [0-9]{2}[[:space:]]+([0-9]{4}|[0-9]{2}:[0-9]{2}) (.*)$/\2/p') echo $line >> "${output}" first=${line:0:1} if [[ $first == 'd' ]] ; then echo "$path$filename" getFiles "$path$filename" fi done < /tmp/files.txt}getFiles "$now"