Find and truncate all files in the oldest subdirectory

Find and truncate all files in the oldest subdirectory

image 12 - Find and truncate all files in the oldest subdirectory

This command will search for directories in the current directory and its subdirectories, find the oldest subdirectory in each directory, and display them on to screen:

find . -maxdepth 1 -type d -exec sh -c 'oldest_folder=$(ls -ltr "$1" | awk "/^d/ {print \$NF; exit}"); echo "$1/$oldest_folder"' sh {} \;
image 13 - Find and truncate all files in the oldest subdirectory

To truncate all files in those oldest subdirectories (snap.X on the screenshot above) use:

find . -mindepth 1 -type d -exec sh -c 'oldest_folder=$(ls -ltr "$1" | awk "/^d/ {print \$NF; exit}"); > "$1/$oldest_folder"/*' sh {} \;

whoami
Stefan Pejcic
Join the discussion

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.