Delete all files in a directory unix command


















Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. How to remove all files from a directory?

Ask Question. Asked 10 years, 4 months ago. Active 1 year, 4 months ago. Viewed 1. Improve this question. See answer here, this can remove hidden files as well with out any warning and deleting directory itself stackoverflow. Parentheses run the contained commands in a subshell.

Add a comment. Active Oldest Votes. Improve this answer. Rinzwind Rinzwind k 38 38 gold badges silver badges bronze badges. If you also want to delete hidden files run shopt -s dotglob before running rm Rinzwind, more accurately, the asterisk means "zero or more of any character". It would match the filenames happy , apple , a or la. At least this way you can get rid of the hidden files too — Kellen Stuart.

Show 7 more comments. Lilian A. Moraru Lilian A. Moraru 5, 2 2 gold badges 22 22 silver badges 21 21 bronze badges. I found this to be the more comprehensive and helpful answer, over and above the answer that was marked as Accepted.

Haven't tried it. Moraru, I did some testing today. Note that this is common and not specific to rm. Show 3 more comments. Leonid V. Fedorenchik Leonid V. Fedorenchik 7 7 silver badges 10 10 bronze badges. This should be the beautiful accepted answer. Thank you! Doesn't this glob match ". This does not work on Mac console, hidden files are still there with that command line.

Thanks for your advice and I've added more explanation for the links. Richard Holloway Richard Holloway GNU find as a -delete predicate. You really ought to add a -- after the rm. Without that if you have a file names -rf or similar will be interpreted as arguments to rm. That will cause it to split the rm commands to having a maximum number of arguments i.

You can also add -maxdepth 1 to ensure that find does not return files from any subdirectories. Learn more. How to remove all the files in a directory? Ask Question. Asked 10 years, 8 months ago. Active 4 years, 5 months ago. Viewed k times. How can I do this in linux? Improve this question.

Add a comment. Active Oldest Votes. Improve this answer. Jordan Lewis Jordan Lewis. This is gnu-find. DarkHeart rm. It is extremely unlikely you'll find a system where rm. EDIT: to do it safe, use rm -rf. See also: find. This won't remove hidden files ones starting with ". Mandar Shinde Mandar Shinde 2, 10 10 gold badges 35 35 silver badges 53 53 bronze badges.

You need to also use the -mindepth switch as others have said. Maddes Maddes 31 2 2 bronze badges. Eyal Levin Eyal Levin 2 2 silver badges 3 3 bronze badges. So, you have to state the appropriate path explicitly, e. Zaur Nasibov Zaur Nasibov 5 5 bronze badges. Dharmendra Rajput Dharmendra Rajput 1. This works perfectly for me, tested several variations worked every time. From the directory that holds the images directory.

To delete all files and subdirectories in the current directory, including hidden ones, without error message : rm -rf. Celada 40k 5 5 gold badges 86 86 silver badges 99 99 bronze badges.

This will fail in on filenames that contain whitespace. I would recommend against using the output of ls as input to other commands, it is generally unsafe.



0コメント

  • 1000 / 1000