Deleting multiple files in Linux?
Asked Answered
N

3

5

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?

Nuthatch answered 7/1, 2016 at 9:5 Comment(0)
G
3

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

Garton answered 7/1, 2016 at 9:8 Comment(4)
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??Nuthatch
use the Option -f --> rm-rf file*.txt so you will not get the question anymore.Garton
thnx.. now its on actual track .. :-)Nuthatch
@Garton rm -f file*.txt is enough, why he needs -recursive flag? He is deleting files, not directories.Chambray
F
5

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

Fritzsche answered 7/1, 2016 at 9:17 Comment(0)
G
3

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

Garton answered 7/1, 2016 at 9:8 Comment(4)
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??Nuthatch
use the Option -f --> rm-rf file*.txt so you will not get the question anymore.Garton
thnx.. now its on actual track .. :-)Nuthatch
@Garton rm -f file*.txt is enough, why he needs -recursive flag? He is deleting files, not directories.Chambray
C
1

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

Costanzia answered 7/1, 2016 at 9:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.