Search code examples
fish

How do I create backup copies of all files in a directory?


I have a directory that contains some files and I want to create backups of all files in the same directory.

I tried cp ./* ./*.bak (or cp * *.bak and several other variations including quotations) to create a copy of everything with .bak appended to the filenames but this hasn't worked.

I suspect I am missing something but I don't know what.

How can I achieve this?

If it is relevant, I am on Windows 10 and using Cygwin with the fish shell.


Solution

  • Use a loop.

    for f in *
        cp -r -- "$f" "$f.bak"
    end
    
    • -r means copy directories recursively,
    • -- marks the end of options, without it a filename beginning with a dash may cause problems.