?
Solved

can I use date in file or directory name

Posted on 2008-06-09
3
Medium Priority
?
239 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 2000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
In this post we will learn different types of Android Layout and some basics of an Android App.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

771 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