Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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

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[@]}"
[[ -f $File ]] && cp -Rp $File ${Dir2}/`basename ${File}`_${Date};
share|improve this answer
-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
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


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.