Reputation: 57
I'm making a Timelapse video but the camera is taking to many pictures (the highest time is 1pic/60sec but I need only 1pic/h). So I'd like to keep every 60th pic and delete the rest files.
Is there a bash script or a simple command to achieve this in the directory?
Upvotes: 0
Views: 48
Reputation: 2096
Answer:
find . -maxdepth 1 -type f -printf "%T+\t%p\n" | sort | cut -f 2 | awk 'NR % 60 != 0' | xargs -d '\n' rm -f
Explanation:
find . -maxdepth 1 -type f -printf "%T+\t%p\n"
- finds all regular files (-type f
) in the current directory (. -maxdepth 1
) and prints out file modification time and file name, separated by TAB, one line for each file (-printf "%T+\t%p\n"
)
sort
- sorts the resulting list in ascending order (as the modification time is at the beginning of the line, that means oldest files first). Use -r
to sort in reversed order
cut -f 2
- removes the modification times from the list and leaves only the file names (second field in the list)
awk 'NR % 60 != 0'
- removes every 60th line from the resulting list
xargs -d '\n' rm -f
- executes rm -f
for each line that is left in the list
The final result is that every 60th file is not deleted, every other regular file in current directory is deleted. Use at your own risk :)
Upvotes: 1