Sam Gilbert
Sam Gilbert

Reputation: 1702

How to remove files from directory with certain extenions using command line in linux

I'm trying to use a system command in R to remove all files from a directory that have an extension of either .html or .png

I can remove files with one given extension type for example like:

system("rm -f ~/folder_path/*.html")

But I can't figure out how to remove files with one extension type or another. I have tried:

system("rm -f ~/folder_path/\\(*.html|*.png\\)")

But this errors with sh: 1: *.png): not found

R session information:

> sessionInfo()
R version 3.2.1 RC (2015-06-10 r68509)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.3 LTS

Upvotes: 1

Views: 104

Answers (3)

Shoaib khan khattak
Shoaib khan khattak

Reputation: 11

Use the simple command

rm -rf /directory/*.file type

rm: for the rm gernal command -rf: for the remove all file forcefully *: for all files *.filetype: for all file extension like HTML, png, pdf etc

Real-world example: remove all pdf file from /home use this command:

rm -rf /home/*.pdf

Upvotes: 0

There is actually a simple way to do this using the rm command.

system("rm DIRECTORY/*.html;rm DIRECTORY/*.png")

Replace DIRECTORY with the name of your directory. This should remove all htmls and pngs from the directory.

Upvotes: 0

Timothée Poisot
Timothée Poisot

Reputation: 226

files_to_remove <- list.files(pattern=".html|.png", full.name=T)
file.remove(files_to_remove)

Try not to write system-specific code and look at ?file.

Upvotes: 3

Related Questions