Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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
share|improve this answer
    
Can this be used for multiple files and directories? for example: cp /path/files/* /path/files/*-$(date +%Y%m%d) –  JDCar Jan 5 '12 at 16:43
    
@JDCar: not so simple, see modified answer. –  enzotib Jan 5 '12 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 '12 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 '12 at 17:28
    
That worked perfectly! Thank you! –  JDCar Jan 5 '12 at 18:49

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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