Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to write a shell script on Solaris that copies files from one directory to another. I need it to prompt the user for a date, and add that as a suffix to the destination filenames. How do I go about doing that?

share|improve this question

2 Answers 2

#!/bin/bash

src="/dir1"
dest="/dir2"

read -p "Date: " date

cp -Rp "${src%/}/" "${dest}"
find "${dest}" -type f -exec mv "{}" "{}_${date}" \;

cp -Rp copies the content of src/ recursively and keeps permissions

${src%/}/" makes sure that the source directory has a trailing /, because if it doesn't, cp -R copies the directory itself instead of it's content.

share|improve this answer
#!/bin/bash
Dir1="/dir1"
Dir2="/dir2"

read -p "Enter Date :- " Date

# if date is not set then use system date
[[ -z $Date ]] && Date=$(date +%F)

[[ -d "${Dir1}" ]] || { echo "ERROR: $Dir1 does not exists.."; exit; } && cd $Dir1

List=( * )

for File in "${List[@]}"
do
[[ -f $File ]] && cp -Rp $File ${Dir2}/`basename ${File}`_${Date};
done
share|improve this answer
1  
-z STRING True if string is empty.” – So your logic is wrong, you set Date to current date if it already has value. –  manatwork Dec 20 '12 at 6:51
    
@manatwork please let me know your email id –  Rahul Patil Dec 20 '12 at 7:02
1  
You use find here to find all files in Dir1, also those in subdirectories, but you do not recreate the directory structure in Dir2, all files will end up in Dir2: –  aktivb Dec 20 '12 at 8:54
    
updated without find command –  Rahul Patil Dec 20 '12 at 9:28

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.