4

How can I delete multiple files in Linux created at same date and time? How can I manage this without using date? The file have different names.

I have these .txt files:

-rw-r--r-- 1 root root        54 Jan  6 17:28 file1.txt
-rw-r--r-- 1 root root        33 Jan  6 17:28 file2.txt
-rw-r--r-- 1 root root        24 Jan  6 18:05 file3.txt
-rw-r--r-- 1 root root         0 Jan  6 17:28 file4.txt
-rw-r--r-- 1 root root         0 Jan  6 17:28 file5.txt

How can I delete all the files with one command?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Bhushan Ahire
  • 79
  • 1
  • 6

3 Answers3

4

You can use find command and specify the time range. In your example: if you would like to find all files with modified timestamp from 6. Jan 17:28 you can do something like:

find . -type f -newermt '2016-01-06 17:28' ! -newermt '2016-01-06 17:29'

if you would like to delete them, just use finds exec parameter:

find . -type f -newermt '2016-01-06 17:28' ! -newermt '2016-01-06 17:29' -exec rm {} \;

you can also include -name '*.txt' if you want to process only *.txt files, and check maxdepth parameter as well if you would like to avoid processing subdirectories

JanS
  • 75
  • 6
2

simply use rm -f file*.txt to delete all files which starts with file and ends with the extention .txt

Jens
  • 67,715
  • 15
  • 98
  • 113
  • thnx.. Its working but there is one problem terminal asks everytime while removing rm: remove regular file `file1.txt'? rm: remove regular file `file2.txt'? rm: remove regular file `file3.txt'? rm: remove regular empty file `file4.txt'? rm: remove regular empty file `file5.txt'? is there any attribute or solution to this?? i mean why it asked for each n every time it will cost to much time for processing delete operation on huge amount of files as it will asks every time to remove file is there any equivalent solution?? – Bhushan Ahire Jan 07 '16 at 09:53
  • 1
    use the Option -f --> `rm-rf file*.txt` so you will not get the question anymore. – Jens Jan 07 '16 at 09:59
  • thnx.. now its on actual track .. :-) – Bhushan Ahire Jan 07 '16 at 10:45
  • 2
    @Jens `rm -f file*.txt` is enough, why he needs `-r`ecursive flag? He is deleting files, not directories. – stek29 Jan 07 '16 at 11:17
0

If you know the minutes of the file modified then you can deleted all files using find command. consider the file was last modified ten minutes ago. Then you can use,

find -iname "*.txt" -mmin 10 -ok rm {} \;

If you don't need to prompt before deleting then use -exec.

 find -iname "*.txt" -mmin 10 -exec rm {} \;

If you need to delete the files using access time then you can use -amin

Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31