Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting content of folder with shell script

Im having problems trying to empty a folder in my script.

This is working in my command line:

rm -r Folder1/Folder2/*

But if in my script I do this:

DIR="Folder1/Folder2/"
rm -r "$DIR*"

It says "rm: Folder1/Folder2/*: No such file or directory", where is the problem?

Im running the script in the same folder as I tried the command.

like image 534
Antonio MG Avatar asked May 07 '12 14:05

Antonio MG


People also ask

How to delete a file from a directory using shell script?

To delete a file from a directory we use the rm command in Linux/UNIX . rm [OPTION]... [FILE]... Using shell script, we have to delete the input file from every directory above the present working directory. The filename will be passed as an argument.

How to delete files and folders in Linux?

The generic command is: To delete files and folders, use the Get-ChildItem command and use the Delete () method on the output. Here, this command will delete a file called “testdata.txt”. The above command may not work for folders, especially if they are not empty.

How to remove files and folders in PowerShell?

You can either use the Remove-Item cmdlet of PowerShell or the Delete () method that comes by default with every PowerShell object, including folders and files. Both require some knowledge of PowerShell scripts to use the available parameters and efficiently remove files and folders from your system.

How do I delete files and folders in a Python file?

The generic command is: To delete files and folders, use the Get-ChildItem command and use the Delete () method on the output. Here, this command will delete a file called “testdata.txt”.


2 Answers

Glob expansion doesn't happen inside quotes.

Try:

rm -r -- "$DIR"*

(Just make really sure you don't put a space after the quotes.)

like image 159
Mat Avatar answered Oct 18 '22 00:10

Mat


rm -r $DIR*

That should work, without quotes

like image 45
egoroveo Avatar answered Oct 17 '22 23:10

egoroveo