Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete contents in a folder using a bash script?

I would like to clear out my /bin folder in my project directory. How can I do this?

I tried rm -rf ~/bin but no luck

like image 991
Sheehan Alam Avatar asked Dec 25 '11 22:12

Sheehan Alam


People also ask

How do I delete contents of a folder?

Right-click the folder you want to delete and click Delete Folder. Click Yes to move the folder and its contents to the Deleted Items folder. When you empty the Deleted Items folder, everything in it — including any folders you've deleted — is permanently erased.


1 Answers

~ is a shorthand to a current user home directory. So unless it's also your project directory you are doing something wrong. Other than that, clearing a directory would be

rm -rf ~/bin/*

And if you also want to clear the hidden files

rm -rf ~/bin/* ~/bin/.[a-zA-Z0-9]*

Make sure you are not doing

rm -rf ~/bin/.*

especially as root as it will try to clear out your entire system.

UPD

Why? Since wildcard (*) is interpreted by shell as zero or more characters of any kind the .* will also match . (current directory) and .. (parent directory), which will result in going all the way up and then down, trying to remove each file in your filesystem tree.

like image 80
dimir Avatar answered Oct 21 '22 20:10

dimir