sixtyfootersdude
sixtyfootersdude

Reputation: 27231

Bash: for loop, copy, backup

Hey, not 100% sure what this error means.

% for f in "*" ; do cp $f ../backup/backup$f ; done
cp: ../backup/backup* not found

The purpose is to copy all the files into a folder into a backup folder and rename the files to backup.

Upvotes: 2

Views: 25343

Answers (3)

sud03r
sud03r

Reputation: 19759

Or may be this:

cp -b * ../backup 

If you want them to be renamed:

% for f in * ; do cp "$f" "../backup/${f}-backup" ; done

Upvotes: 3

janmoesen
janmoesen

Reputation: 8020

You are quoting the wrong things: quote the variables, not the wildcards!

% for f in *; do cp "$f" "../backup/$f" ; done

BTW, in this case, you can simply do:

% cp * ../backup/

Upvotes: 7

Mark Byers
Mark Byers

Reputation: 838276

The * shouldn't be in quotes:

for f in * ; do cp $f ../backup/$f ; done

When you use quotes this prevents the shell from expanding it, so it is looking for a file called *, not all files in the directory which is what you meant.

Upvotes: 11

Related Questions