14

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.

8 Answers 8

19

Try:

find /var/myfolder -type f -delete

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

5
  • 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.
    – IroeN
    Aug 21, 2012 at 12:27
  • 2
    I was able to came up with a following solution: find /var/myfolder -type f -exec rm -f {} \;
    – IroeN
    Aug 21, 2012 at 13:17
  • 2
    Sorry. I use Debian or Ubuntu. Your solution is equivalent as far as I know.
    – StarNamer
    Aug 21, 2012 at 16:08
  • 6
    -type f != ! -type d
    – mikeserv
    Jan 22, 2016 at 6:42
  • Upvoted for using an absolute path. Using "." and the like here is asking for trouble.
    – markgraf
    Feb 14 at 12:41
7

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.

2
  • I'm using bash.
    – IroeN
    Aug 21, 2012 at 12:28
  • 1
    @user6554: ... which neither your question nor the used tags indicate ;) May 4, 2014 at 22:58
3

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

3
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 {} +
2
  • Note that GNU find has a -xtype option for that. Jan 22, 2016 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, 2016 at 14:30
1
rm var/myfolder/*/*

The will delete everything inside the sub folders without touching the sub folders themselves.

The first * is for the subdirectories of myfolder itself (A, B, ...). The second * is for the files in A, B, etc.

IF there are more folders inside A, B, etc. First run: rm /var/myfolder/*/*/* then run rm /var/myfolder/*/*.

1
# This will delete all directory contents, including hidden files and
# subdirectories, without deleting the directory itself
# mindepth should be two if executed from the current directory
# This does not delete files in the directory, only in subfolders


# 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
0

You should also be able to cd to the top of the directory tree in question, then run:

find . -type f -print0 | xargs -0 rm -rf

and that should delete all the files while leaving the directory tree intact. The -print0 and -0 options may be omitted if you are confident you do not have any file or directory names that contain spaces.

The find command distinguishes between files ( -type f) and links ( -type l) so this should leave links intact, if any. Not tested though. If in doubt, run:

find . -type l

and see if anything shows up before you run the earlier find command.

4
  • Can you explain how how rm flag -r, -R, --recursive - remove directories and their contents recursively fits into this? Jul 18, 2017 at 15:01
  • Because we aren't running rm -rf, we are running find . -type f and then piping the output from that to rm -rf. So it only removes files, it doesn't touch the directories. That's what the | xargs bit does, it takes the output from the command on the left and sends it to the command on the right.
    – rjh427
    Jul 18, 2017 at 19:28
  • My point exactly. If you are not passing any directories, what use is a flag that only works on directories? Jul 18, 2017 at 19:30
  • Because muscle memory, and it still works. You could raise the same objection on the -f "force" option, I'd give you the same answer.
    – rjh427
    Jul 18, 2017 at 19:40
-3

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 *

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .