Linux remove files by mask recursive

Unix: How to delete files listed in a file

I need delete them. Tried rm `cat 1.txt` and it says the list is too long. Found this command, but when I check folders from the list, some of them still have files xargs rm

Even though it’s six years later: Would you mindaccepting one of the answers? This’ll mark the question as resolved and help other users as well.

13 Answers 13

This is not very efficient, but will work if you need glob patterns (as in /var/www/*)

for f in $(cat 1.txt) ; do rm "$f" done 

If you don’t have any patterns and are sure your paths in the file do not contain whitespaces or other weird things, you can use xargs like so:

Assuming that the list of files is in the file 1.txt , then do:

The -r option causes recursion into any directories named in 1.txt .

If any files are read-only, use the -f option to force the deletion:

Be cautious with input to any tool that does programmatic deletions. Make certain that the files named in the input file are really to be deleted. Be especially careful about seemingly simple typos. For example, if you enter a space between a file and its suffix, it will appear to be two separate file names:

is actually two separate files: file and .txt .

This may not seem so dangerous, but if the typo is something like this:

Then instead of deleting all files that begin with myoldfiles , you’ll end up deleting myoldfiles and all non-dot-files and directories in the current directory. Probably not what you wanted.

Источник

How can I delete all files with a particular extension in a particular folder?

If I set the current/working directory (navigating to it using cd ) to some particular directory and then type:

What will this command do? Is it true that the above command will only delete files with the extension .xvg only in the working directory? I was nervous about trying this before asking, because I want to be absolutely sure that the above command will only delete .xvg files LOCATED IN THE WORKING DIRECTORY.

4 Answers 4

Yes, rm *.xvg will only delete the files with the specified extension in your current directory.

A good way to make sure you are indeed in the directory you want delete your files is to use the pwd command which will display your current directory and then do an ls to verify you find the files you are expecting.

If you are bit apprehensive about issuing the rm command, there are 2 things you can do:

  1. type ls *.xvg to see a list of what files would be affected by this command.
  2. Unless you have a lot of files, you could always also use the -i command line switch for rm (also exists for cp and mv ). Using rm -i *.xvg would prompt you for each individual file if it was ok to delete it, so you could be sure nothing you didn’t expect was getting deleted. (This will be tedious if you have a lot of files though 🙂
Читайте также:  File description in linux

You don’t need to navigate to the dir, just use

In the case you have a typo or similar mistake in the path, where /som/dir does not exist:

will accidentally delete all .xvg-files in the current dir. The first command will not, and you don’t need to cd back again.

An alternative way would be to use find:

find /some/dir/ -maxdepth 1 -type f -name "*.xvg" -delete 

find /some/dir/ -maxdepth 1 -type f -name «*.xvg» -delete worked for me! use -maxdepth «n» to delete matching files in sub directory if given path

Yes, rm *.xvg will only delete files ending with .xvg in your current directory. Here’s why.

When you type a command like this, work is split up between the shell you are using (let’s assume bash) and the command binary.

You can locate the binary by typing which rm . This little program takes care of unlinking files. Programs like this can be started from the command line and can read a list of arguments prog arg1 arg2 arg3 when they start up. In the case of rm , they are interpreted as a list of fully qualified filenames to be deleted. So if you are in a directory containing the file foo.bar , typing delete ‘foo.*’ will result in rm: foo.*: No such file or directory . Note the single quotes around the file pattern, they tell the shell to pass the argument to the shell as it is.

However if you type rm *.bar in the same directory, it will delete the file. What’s happening here is that your shell, which is the program you are using to type in commands, is performing some transformations before passing the arguments on to the command. One of these is called ‘file name expansion’, otherwise know as ‘globbing’. You can see a list of bash file name expansions here. One of the most common expansions is * , which is expanded to filenames in the current directory.

A simple way to look at globs at work is to use echo , which prints back all arguments passed to it through the shell. So typing echo * in the same directory will output foo.bar . So when you type rm *.bar , what’s actually happening is that the shell expands the argument list to foo.bar , then passes that to the rm command.

There are some ways of controlling globbing. In recent versions of bash, for example, you can turn on an option called globstar which will do recursive expansion. Typing echo **/*.bar will show a list of all files ending in .bar in all subfolders. So typing rm **/*.bar in globstar enabled bash will indeed recursively delete all matching files in subfolders.

Источник

How to Remove Files with Specific Extension in Linux

To remove files with a specific extension, we use the ‘rm‘ (Remove) command, which is a basic command-line utility for removing system files, directories, symbolic links, device nodes, pipes, and sockets in Linux.

Читайте также:  Как прокрутить терминал линукс вверх

The command is quite simple to use and the basic syntax is:

Here, ‘filename1‘, ‘filename2‘, etc. are the names of the files including full path. If the files are in the same directory then, as you might already know, there is no need to write down full paths.

We can also use wildcard expressions to specify files that have similar or incremental names or to specify files with a specific file extension.

Remove Files in Linux Using Substring

For example, to remove all files contain the substring ‘test‘, we can run:

Here, the ‘*’ implies ‘any string‘. Hence the pattern ‘*test*’ considers all files with names containing the substring ‘test‘.

Remove File Using SubString of Filename

Remove Files with File Extension in Linux

Let’s take another example. We will try to delete all GIF files from the folder using the following:

Delete Files with Particular Extension

However, this syntax works only for files. Using the argument ‘-r’ we can delete both files as well as folders:

$ rm -r   .  

Note that this will delete the folder recursively in its entirety, i.e., it will delete the entire folder structure beneath it; the subfolders and all the files. Hence, there is no way to delete specific files with a pattern of filenames, or files with a specific extension recursively. Every time the entire folder structure is deleted.

Remove Files Recursively with File Extension in Linux

To achieve this, we can make use of the find command and pipe its output to ‘rm’. The find command is simply used to search for files recursively based on parameters like the filename, extension, size, etc.

For example, to search recursively for files with extension “.png” , we run the following:

Search Files Recursively with Extension

Now, we simply pipe this output to ‘rm’ in the following way:

Delete Files Recursively with Extension

The ‘xargs’ command is simply used to pass the output of ‘find’ to ‘rm‘ as arguments. In this way, we have recursively deleted files of the extension PNG from the whole folder structure.

Conclusion

In this article, we learned about the ‘rm‘ command, how to delete files and folders using it, and how to delta files recursively with specific extensions.

If you have any questions or feedback, let us know in the comments below.

Источник

Удаление старых файлов Linux (старше N дней)

Удаление старых файлов в Linux является операцией необходимой для того чтобы не допустить заполнения диска. Обычно удалению подлежат однотипные старые файлы — логи, часто — файлы сессий РНР. Если система их ротирования, сжатия и удаления не настроена оптимально логи могут занимать значительное пространство, если же свободного места на сервере остается меньше 10% в его работе могут наблюдаться трудности.

Команды для удаления большого количества файлов

Удаление старых файлов linux по маске чаще всего осуществляется следующим образом:

Веб-сервер указан для примера. Синтаксис очень прост — команда принудительно удалит все файлы с расширением .log в указанном каталоге, при этом удаление будет рекурсивным(-r — recursive) и подтверждения система при этом спрашивать не будет (-f — force).

Читайте также:  Приложение камера для линукс

При удалении таким образом очень большого количества файлов из определенного каталога может выдавать ошибки — причина в том, что система на самом деле не видит маски и разворачивает передаваемое ей выражение — срабатывает ограничение на количество аргументов, и выполнение команды прекращается.

Если rm -rf не помогает — удалять файлы следует в цикле for. Для каждого файла будет отдельная операция удаления и никаких ограничений системы здесь ожидать не приходится.

Синтаксис в простейшем случае может выглядеть так:

for f in /var/log/apache2/*.log; do rm «$f»; done

В цикл for можно добавить любую дополнительную логику.

Удаление по Cron файлов старше N дней

Логи или другие файлы (сессии РНР) можно удалять как используя циклы (так приходится делать обычно если логов или других файлов накопилось действительно очень много).

Если система не запущена для большого количества пользователей или только создается лучше использовать регулярно выполняемое задание Cron, согласно которому с заданной периодичностью будут удаляться файлы определенных типов. При регулярном выполнении операции очень много файлов скопиться не успеет и цикл не потребуется.

# m h dom mon dow command

23 3 * * * find /var/www/site/www/var/session/ -type f -mtime 7 -exec rm -f <> \;

В примере удаляются сессии РНР для сайта (тип документа ‘файл’, время создания — больше 7 дней).

Как любое cron задание команду на удаление файлов можно проверить выполнив её вручную. При успешном выполнении вывода не будет:

Удаление старых файлов Linux

В цикле то же самое можно сделать так:

for f in /tmp/logs/*.log

find $f -mtime +7 -exec rm <> \;

Приведенное выражение, как и любой другой цикл можно поместить в файл, сделать его исполняемым и с тем же успехом выполнять по Cron

Источник

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