Solved

can I use date in file or directory name

Posted on 2008-06-09
3
237 Views
Last Modified: 2013-12-26
this is the code in my script
I want to use the current date to name a file in a linux script

#!/bin/sh
declare RALPH
declare GARY="."
declare GARY2="log"
cd /
#CREATES CORRECT STRING
   RALPH=$(date)$GARY$GARY2
   echo $RALPH  >> /var/log/usb.log  
#does not create file
cp /var/log/usb.log /var/log/$(date)$GARY$GARY2
# ALSO TRIED THIS LINE
 cp /var/log/usb.log /var/log/$RALPH

the file does not create.

gary

0
Comment
Question by:javagair
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 9

Expert Comment

by:michofreiha
ID: 21747126
try this:
#!/bin/sh
declare RALPH
declare GARY="."
declare GARY2="log"
cd /
#CREATES CORRECT STRING
   RALPH=$date$GARY$GARY2
   echo $RALPH  >> /var/log/usb.log  

cp /var/log/usb.log /var/log/$date$GARY$GARY2
# ALSO TRIED THIS LINE
 cp /var/log/usb.log /var/log/$RALPH

0
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 21747159
What date format do you want to use?  Let's assume YYYYMMDD, so your script would look like


#!/bin/sh
GARY="."
GARY2="log"
RALPH=$(date +%Y%m%d)$GARY$GARY2
echo $RALPH  >> /var/log/usb.log  
cp /var/log/usb.log /var/log/$(date +%Y%m%d)$GARY$GARY2

Open in new window

0
 
LVL 48

Expert Comment

by:Tintin
ID: 21747183
BTW, if you *really* did want to create a file using the default date format (which is not a good naming format for a file), you need to use quotes, eg:



cp /var/log/usb.log "/var/log/$(date)$GARY$GARY2"

Open in new window

0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Fine Tune your automatic Updates for Ubuntu / Debian
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question