I have a large directory containing many subdirectories inside, in each subdirectory there are 4 different files (C1,C2,C3,C4).

What I want to do is to collect the same subdirectories but with only one file (C1) inside. So the point is to delete the other files (C2,C3,C4) and keep the same hierarchy of directories.

I want to have a simple script to do that because it´s impossible to do it manually since I have a very large main directory. The structure of the directory of just one simple case is : sampling/time´i´/C´j´. with i =0.0001:10 and j=1:4

I have this case on cluster.

closed as unclear what you're asking by Rahul, Networker, taliezin, Anthon, Stephen Harris Aug 11 '16 at 12:10

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    what do you mean by "collecting files" ? It would be better if you provide some example, sample folder structure and what you want to do with that ? – Rahul Aug 11 '16 at 9:15
  • What Linux version are you using? – Kevdog777 Aug 11 '16 at 9:17
  • Acctually I am running a simulation on cluster where on each time step (t) I calculate the concentrations (C1,C2,C3,C4).So inside the main folder named <Sampling> I have many subfolders presenting the time from 0.005s to 10s and inside each subfolder (time folder) there 4 files (C1,C2,C3,C4), What I need is how to delete the files C1,C2,C3 and keep C4 inside each time folder – R.Bichi Aug 11 '16 at 9:18
  • THe Linux version is : CentOS Linux release 7.2.1511 – R.Bichi Aug 11 '16 at 9:22
  • these folders has only C1..C4 or are there other files also ? – Rahul Aug 11 '16 at 9:23

Make a backup of the parent folder, say the new folder name is parentbackup

cp parent parentbackup

then do

find parentbackup -type f -not -name C1 -exec rm {} \;

or using regex

find parentbackup -type f -regex 'C[^1]' -exec rm {} \;

If you're allowed to modify the parent directory, then do


find /path/to/parent -type f -regex 'C[^1]' -exec rm {} \;
  • One could also use cp -al to create a shadow tree of hard links. – Law29 Aug 11 '16 at 9:25
  • @Law29 : Not sure if the original directory structure could/should be modified. If so i would apply rm on the original folder itself. – sjsam Aug 11 '16 at 9:30
  • I can´t just copy the directory, it is super large – R.Bichi Aug 11 '16 at 9:48
  • @R.Bichi : Are you allowed to modify the original directory? – sjsam Aug 11 '16 at 9:53
  • yes I can, it is just so large that I can´t even list its sub directories – R.Bichi Aug 11 '16 at 9:55

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