1

I have a structure like this

  • file_1
  • file_2 ...
  • file_n
  • folder1/
    • sub_file_1
    • sub_file_2

I only want to delete file_1, file_2 and file_n but keep all folders and files in them.

2

Just run:

rm *

It's that simple. It won't touch the directories unless you add the -R or -r flag:

rm -r *

If you also want to delete empty directories, add the --dir flag:

rm --dir *
2

rm without the -r, disabling recursive directory removal flag and shell globbing:

rm /path/to/dir/*

Add the -f if you don't want to be prompted.

You can also use find:

find /path/to/dir -maxdepth 1 -type f -exec rm -rf '{}' \;

which works because of a maxdepth of 1, thus selecting only top-level files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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