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

23,555

Solution 1

You should say "... my bin folder", not "my /bin folder". /bin is an absolute path, bin is a relative path.

rm -rf ~/bin removes $HOME/bin, so not what you want either.

Now, it depends on where you are: if you are in your project directory when you type the command, just type rm -rf bin.

Solution 2

~ 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.

Share:
23,555
Sheehan Alam
Author by

Sheehan Alam

iOS, Android and Mac Developer. i can divide by zero.

Updated on January 30, 2021

Comments

  • Sheehan Alam
    Sheehan Alam over 3 years

    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

  • muon
    muon over 7 years
    is -rf necessary? wouldn't rm ~/bin/* suffice