Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm in a folder: /var/myfolder.

Inside there are some other folders like: /var/myfolder/A/ /var/myfolder/B/ `/var/myfolder/C/ etc.

Inside each there are some files with random names. How do I remove all the files from all the folders inside /var/myfolder?

The structure (all the directories, eg., A, B, C etc., inside /var/myfolder) should remain intact.

share|improve this question
up vote 14 down vote accepted

Try:

find /var/myfolder -type f -delete

This gets all the regular files under /var/myfolder and deletes them leaving only the directories.

share|improve this answer
    
Thanks for the solution. I should have mention that I'm doing this in Solaris 9, so my find dont have the -delete option, but that's a good starting point. – user6554 Aug 21 '12 at 12:27
1  
I was able to came up with a following solution: find /var/myfolder -type f -exec rm -f {} \; – user6554 Aug 21 '12 at 13:17
1  
Sorry. I use Debian or Ubuntu. Your solution is equivalent as far as I know. – StarNamer Aug 21 '12 at 16:08
4  
-type f != ! -type d – mikeserv Jan 22 at 6:42

With zsh, use the . glob qualifier to match only regular files:

rm -- **/*(.)

This deletes all the (non-hidden) regular files in the current directory and its subdirectories recursively. Add the D glob qualifier to delete hidden regular files (and regular files in hidden directories) as well.

share|improve this answer
    
I'm using bash. – user6554 Aug 21 '12 at 12:28
    
@user6554: ... which neither your question nor the used tags indicate ;) – 0xC0000022L May 4 '14 at 22:58
find . -depth -exec rm {} + 2>/dev/null

rm doesn't remove directories - so just run it on everything.

To preserve symlinks to directories:

find .  ! -type d -exec sh -c '
    for f do [ -d  "$f" ] || 
          set "$@" "$f";  shift
    done; rm  "$@"' sh  {} +

And I think this should also work in, perhaps, a slightly optimized way if there are a lot of files which might need testing in the above version:

find . ! \( -type l -o -type d \)  \
-exec  rm {} + -o -exec  sh -c '
       for f do [ -d "$f" ] ||
       unlink "$f";done' sh {} +
share|improve this answer
    
Note that GNU find has a -xtype option for that. – Stéphane Chazelas Jan 22 at 14:02
    
@StéphaneChazelas - ast does too. but I didn't need -depth at all, i dont think. i kept it in the top one to avoid so many error calls - silenced or not - but it was just silly in the others. – mikeserv Jan 22 at 14:30

You can run rm */* in /var/myfolder

share|improve this answer
# This will delete all directory contents, including hidden files and
# subdirectories, without deleting the directory itself

# With GNU find:
find /path/to/directoryToEmpty -mindepth 1 -delete

# OpenBSD (and probably other BSDs)
find /path/to/directoryToEmpty -mindepth 1 -depth -exec rm -f {} \;

# To see what it deletes, in the order it will delete it:
find /path/to/directoryToEmpty -mindepth 1 -depth -print
share|improve this answer

rm var/myfolder//

The will delete everything in the folders in the myfolder sub directories without touching the sub folders themselves . Er... not a goid explanation.

OK, The first * is for the subdirectories of myfolder itself A, B, etc. The second * is for the FILES in A, B, etc.

IF there are folders in the FILES A, B, etc. First run: rm /var/myfolder///* then run rm /var/myfolder//.

share|improve this answer

Simply use below command:

sudo rm -rf directory_in_which_you_have_content/*

---------------- OR --------------------------------------------------------

The '*' is stands for all so when you will use * that will delete all the content. so Either use command like this or go to the directory in which you want to delete all the content and just use below command:

sudo rm -rf *

share|improve this answer
    
This will remove everything. Have you bothered reading the question ? – don_crissti Jan 22 at 12:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.