beginner_user beginner_user - 1 month ago 6
Bash Question

How to delete one file at a time sequentially?

I have multiple files in a dir as below

Movie22017-06-14_12:18:01.tmp.mp4
Movie22017-06-14_12:18:40.tmp.mp4
Movie22017-06-14_12:19:19.tmp.mp4
Movie22017-06-14_12:19:58.tmp.mp4
Movie22017-06-14_12:20:37.tmp.mp4


Question I want to delete these files but not all at once. I want to first delete "Movie22017-06-14_12:18:01.tmp.mp4" than 5 seconds later delete "Movie22017-06-14_12:18:40.tmp.mp4" and so on. How can I achieve this? Basically first in first out.

Answer Source

You could loop through all files in a directory, and then run some code on that, such as the following:

#!/bin/bash
FILES=/path/to/files/here/*
for f in $FILES; do
  echo "Deleting '$f'..."
  rm -f "$f"
  sleep 5
done

Note: this will break if there are any spaces in the file path