In short, I want to backup a directory, but I want to omit all folders that do not contain a particular filetype (.cst for this case)

I use a simulation software that uses the extension .cst for simulation models. And every simulation file creates a very large folder containing files that I am not interested in (I will call these automatically created folders) for backup purposes. I only want to backup these .cst files. I could search for "*.cst" but the files are organized in many many folders (that I created myself for organization purposes).

Is there a smart way to go about this?

  • Just to clarify, for folders that contain .cst files, do you also want to back up the other (i.e. non-.cst files) or just the .cst files themselves? – Mr Ethernet Sep 17 at 16:58

A simple shell script will do the job:


while read D; do
  test -d "$D" || continue
  mkdir -p "$BACKUPPATH/$D" || exit 1
  cp "$D/"*.cst "$BACKUPPATH/$D/" || exit 2
done < <(find . -type f -name '*.cst' -exec dirname {} \; | sort -u)


Since there was no indication of Windows involved in the OQ (with simulation software Linux is the default) this is a valid answer for Linux (and its unix-y friends). It won't run on Windows.

  • This will not erase anything, correct? How do I run this? I am using a win7 system. – grdgfgr Sep 13 at 15:12
  • 1
    This will not erase anything, but it is of course a shell script. Windows lacks the brains to run it. You might want to add "Windows 7" to your Question's tags,because running big simulations on Windows is quite exotic. – Eugen Rieck Sep 13 at 15:32
  • @Eugen Rieck Would this work if GNU's Core Utilities for Windows was installed? gnuwin32.sourceforge.net/packages/coreutils.htm – K7AAY Sep 13 at 16:31
  • @grdgfgr Sorry, I don't know - no Windows used here anywhere. – Eugen Rieck Sep 13 at 17:12

We could start arguing that even since the Olden Times of the Microsoft Disk Operating System such thing was possible and that we have just started analyzing the syntax of the FOR command in order to achieve the same result on the Microsoft Windows Operating System.

However, a very quick solution for you is to install GHISLER_Christian's Total Commander Static Software Application and just to press the F5 Key on the Folder that you want to be copied and to specify only the *.cst File Extension that you are interested in.

We have just tested this solution and it is working very fine. It is keeping the Folder Structure as you desired.

The _Total Commander_ Solution


On a second thought, there is also no need even to dig into the intricacies of the Batch-Files Processing System of the Microsoft Corporation. This Issue must have arisen so often that it was solvable even since the Olden Times of the xcopy Command that has preceded the robocopy Command.

The <code>xcopy</code> Solution

As you can see above, we have also tested this Secondary Solution and it is also working out just fine. It is maintaining the Folder Structure as you wished. You only have to adjust the above-mentioned xcopy Command in order to accommodate your Source Folder, your Destination Folder and your *.cst File Extension.

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.