Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove files using find and rm command?

Tags:

bash

shell

unix

find -mmin -19 -exec rm '{}'\;

It will find the modified files 1st and then remove them. but it gives me error as below, find: missing argument to `-exec'

Also tried various combinations like,

find -mmin -19 -exec rm '{}';\
find -mmin -19 -exec rm '{}'/;
like image 957
PRK Avatar asked Aug 02 '15 07:08

PRK


People also ask

Can you delete a file removed by the rm command?

To remove (or delete) a file in Linux from the command line, use either the rm (remove) or unlink command. The unlink command allows you to remove only a single file, while with rm , you can remove multiple files at once.

Which command is used to remove files rm?

Use the rm command to remove files you no longer need. The rm command removes the entries for a specified file, group of files, or certain select files from a list within a directory. User confirmation, read permission, and write permission are not required before a file is removed when you use the rm command.

How do I delete multiple files in rm?

Deleting multiple files To delete multiple files at once, simply list all of the file names after the “rm” command. File names should be separated by a space. With the command “rm” followed by multiple file names, you can delete multiple files at once.

How to search for files in a directory using rm command?

However, the rm command does not support search criteria. For example, find all “*.bak” files and delete them. For such necessities, you need to use the find command to search for files in a directory and remove them on the fly. You can combine find and rm command together.

How to remove a file with rm command in Linux?

Remove a file with rm Simplest form of this command is rm . So if we have a file called try1: Removing multiple files is done by either listing the files as separate command line parameters to rm:

How to delete all matched files in find command in Linux?

Modern version of find command has -delete option too. Instead of using the -exec rm -rf {} \;, use the -delete to delete all matched files.

How to find and delete files in a directory in Linux?

For example, find all “*.bak” files and delete them. For such necessities, you need to use the find command to search for files in a directory and remove them on the fly. You can combine find and rm command together.


2 Answers

You need space between the command and \;

find -mmin -19 -exec rm {} \;

find already provide -delete option, so you don't need to use -exec rm ..:

find -mmin -19 -delete

-delete

Delete files; true if removal succeeded. If the removal failed, an error message is issued. If -delete fails, find's exit status will be nonzero (when it eventually exits). Use of -delete automatically turns on the -depth option.

Warnings: Don't forget that the find command line is evaluated as an expression, so putting -delete first will make find try to delete everything below the starting points you specified. When testing a find command line that you later intend to use with -delete, you should explicitly specify -depth in order to avoid later surprises. Because -delete implies -depth, you cannot usefully use -prune and -delete together.

like image 183
falsetru Avatar answered Oct 14 '22 01:10

falsetru


You're missing an essential space to separate the braces from the semicolon.

find -mmin -19 -exec rm '{}' \;

but this does the same ting, is easier to type, and probably executes faster.

find -mmin -19 -delete
like image 41
Jasen Avatar answered Oct 14 '22 03:10

Jasen