delete duplicate file based on modified time, limited by minutes

Here is a simple script to remove files which are less than one minute apart, assuming the file names sort them by time stamp.

prev=0
for file in /path/to/files/*; do
    timestamp=$(stat -c %Y -- "$file")
    if ((timestamp - prev < 60)); then
        rm -- "$file"
    else
        prev=$timestamp
    fi
done

The shell expands the wildcard * to an alphabetical listing; I am assuming this is enough to ensure that any two files which were uploaded around the same time will end up one after the other.

The arguments to stat are system-dependent; on Linux, -c %Y produces the file’s last modification time in seconds since the epoch.

The -- should not be necessary as long as the wildcard has a leading path, but I’m including it as a “belt and suspenders” solution in case you’d like to change the wildcard to something else. (Still, probably a good idea to add ./ before any relative path, which then again ensures that the -- will not be necessary.)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top