2

How would I copy a batch of files into the same directory with an appended filename via SSH Bash Script? For example.

/path/files/file /path/files/file-20120105

The trick is for it to be a one liner so I don't have to do any fancy for loop or some other mechanism to pass it via ssh.

2 Answers 2

4

You can do

cp /path/files/file /path/files/file-$(date +%Y%m%d) 

To do the same for multiple files e direcories:

timestamp=$(date +%Y%m%d)
for f in /path/files/*; do
    cp -a "$f" "$f-$timestamp"
done
4
  • Can this be used for multiple files and directories? for example: cp /path/files/* /path/files/*-$(date +%Y%m%d)
    – JDCar
    Jan 5, 2012 at 16:43
  • @JDCar: not so simple, see modified answer.
    – enzotib
    Jan 5, 2012 at 17:17
  • How do I pass that via an SSH Session? ssh user@server timestamp=$(date +%Y%m%d); for f in /path/files/*; do; cp -a "$f" "$f-$timestamp"; done;
    – JDCar
    Jan 5, 2012 at 17:23
  • 2
    You could do: ssh user@server bash -c 'timestamp=$(date +%Y%m%d); for f in /path/files/*; do cp -a "$f" "$f-$timestamp"; done'.
    – enzotib
    Jan 5, 2012 at 17:28
0

I would give scp (secure copy) a try:

From server to local:

scp -r username@server:folder/foldertocopy destination

From local to server:

scp -r foldertocopy username@server:folder/destination

Check man scp for extra options.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .