Remove all files with name linux

How to remove many files of similar names?

I have many txt files, named like file1.txt file2.txt file3.txt . file1000.txt How can I delete all these files? I am new to unix and concerned about accidentally deleting other files in my directory. I’ve looked at other questions but I’m unsure, could I do something like

rm -v !("differentfile"|"anotherfile"|"otherfile"|"finalfile") 

and therefore delete all my files with the same name and just different numbers and keep the few I want? And this line should only delete files in my working directory/folder correct? Is there a way to use rm but only specific the files I want to delete all at once?

3 Answers 3

rm file*.txt will remove all files in the current directory that has names that start with the string file and ends with the string .txt .

If there are many of these, you will get an «Argument list too long» error. In this case, you may use

find . -maxdepth 1 -name 'file*.txt' -delete 

This will use the find command to find any file whose name matches file*.txt in the current directory, and delete these. The -maxdepth 1 option restricts find from entering any subdirectories other than . (the current directory).

The command does not make a difference between files and directories (just like rm file*.txt wouldn’t do either). If you want to make sure to only delete regular files, use

find . -maxdepth 1 -type f -name 'file*.txt' -delete 

For find implementations that does not have the non-standard -delete and/or -maxdepth options, use

find . ! -path . -prune -type f -name 'file*.txt' -exec rm -f <> + 

Or, you could just resort to a simple but slow loop:

for name in file*.txt; do rm -f "$name" done 

Источник

Delete all files whose filenames contain a particular string?

I changed my wordpress theme. The older one created so much images on server. My new theme doesnt need them, so I want to remove all. How can I do that? For example:
Default image: 12_angry_men_lone_holdout.jpg I want to delete:

12_angry_men_lone_holdout-290x166.jpg 12_angry_men_lone_holdout-700x300.jpg 12_angry_men_lone_holdout-50x50.jpg 

Looks like these images are the automatically created thumbnails from images uploaded to your WorldPress media library. If so, then don’t delete those files in terminal on the server. Open your WordPress admin page, open ‘Settings’ > ‘Media’. Set desired thumbnail image sizes there. Then install the plugin Regenerate Thumbnails and re-create all thumbnails (may take a few minutes, depending of the amount of images in your media library).

Читайте также:  Линукс посмотреть сколько свободного места

5 Answers 5

Use find to recursively find and delete files with «text» in their names:

find -type f -name '*text*' -delete 

You might also want run find -type f -name ‘*text*’ (without the -delete ) before that to make sure you won’t delete any files you didn’t intend to delete.

In fact, you can place wildcards anywhere in the search string, so -name ’12_angry_men_lone_holdout-*.jpg’ might be more suitable in your case.

@AvinashRaj That would be redundant. As per man find : If no paths are given, the current directory is used.

@AvinashRaj Turns out the POSIX specification for find actually does require a path. Defaulting to . is a modification added by GNU find. If this were Unix & Linux, I’d add that to my answer, but since Ubuntu comes with GNU find by default, I’d rather not confuse newcomers more than necessary. 😉

If they are in the same folder use * wildcard to achieve that:

Where text is string that filename contains.

I kept getting permission denied with this command, even using sudo . the find command below did work though.

find . -type f -name '*4x7*' -delete 

Run this in the parent directory. This is going to delete all files that have a digit followed by an ‘x’ character followed by another digit in their name.

Still be careful, this might delete original files too, if their name contains the above pattern (unlikely). Run it first without ‘-delete’ to see if you have any files that have such a name. If that’s the case, you’ll just need to find a more restrictive pattern.

Читайте также:  Linux cat вывести все файлы

Источник

Delete a list of files with find and grep

I want to delete all files which have names containing a specific word, e.g. «car». So far, I came up with this:

12 Answers 12

or pass the output of your pipeline to xargs :

find | grep car | xargs rm -f 

Note that these are very blunt tools, and you are likely to remove files that you did not intend to remove. Also, no effort is made here to deal with files that contain characters such as whitespace (including newlines) or leading dashes. Be warned.

+1. But on the first few runs: change «rm» to «echo rm» . and verify that it only outputs file you really want to delete, and no others ^^

@nhahtdh: xargs: illegal option — d ; all the world is not LInux. And what about directories that contain \n in their name? If you worry about whitespace, you should use find —print0 . xargs -0

Please be aware that this will not work if your files contain unusual characters (like spaces) or start with a dash. Please see my answer for a fix.

To view what you are going to delete first, since rm -fr is such a dangerous command:

find /path/to/file/ | grep car | xargs ls -lh 

Then if the results are what you want, run the real command by removing the ls -lh , replacing it with rm -fr

find /path/to/file/ | grep car | xargs rm -fr 

It does exactly what you ask, logically running rm -rf on the what grep car returns from the output of find . which is a list of every file and folder recursively.

You really want to use find with -print0 and rm with — :

find [dir] [options] -print0 | grep --null-data [pattern] | xargs -0 rm -- 

A concrete example (removing all files below the current directory containing car in their filename):

find . -print0 | grep --null-data car | xargs -0 rm -- 
  • -print0 , —null-data and -0 change the handling of the input/output from parsed as tokens separated by whitespace to parsed as tokens separated by the \0 -character. This allows the handling of unusual filenames (see man find for details)
  • rm — makes sure to actually remove files starting with a — instead of treating them as parameters to rm . In case there is a file called -rf and do find . -print0 | grep —null-data r | xargs -0 rm , the file -rf will possibly not be removed, but alter the behaviour of rm on the other files.
Читайте также:  Astra linux cifs windows

Источник

How do I remove all files that match a pattern?

When I revert in Mercurial, it leaves several .orig files. I would like to be able to run a command to remove all of them. I have found some sources that say to run:

rm: cannot remove `**/*.orig': No such file or directory 

4 Answers 4

Use the find command (with care!)

I’ve commented out the delete command but once you’re happy with what it’s matching, just remove the # from the line and it should delete all those files.

@FrankBarcenas Yeah — find does everything recursively. If you want to limit how that works, you can play with the -maxdepth or -mindepth arguments.

Definitely leave the -delete at the end of the flags. find . -delete -name ‘*.orig’ will ignore the filter and clobber your whole directory.

@kamal I’d probably still use find but with its -regex or -iregex predicates. Parsing filenames (when you’re piping them around) can be hard to do safely sometimes.

«find» has some very advanced techniques to search through all or current directories and rm files.

find ./ -name ".orig" -exec rm -rf <> \; 

I have removed all files that starts with .nfs000000000 like this

The below is what I would normally do

find ./ -name "*.orig" | xargs rm -r 

It’s a good idea to check what files you’ll be deleting first by checking the xargs . The below will print out the files you’ve found.

If you notice a file that’s been found that you don’t want to delete either tweak your initial find or add a grep -v step, which will omit a match, ie

find ./ -name "*.orig" | grep -v "somefiletokeep.orig" | xargs rm -r 

Источник

Оцените статью
Adblock
detector