Solved

can I use date in file or directory name

Posted on 2008-06-09
3
230 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
  • 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best way to split and output to csv in bash 2 60
Debug VNC connection on CentOS7 server 22 64
Linux  Script File Attributes 5 52
Can't See Site After DNS Resolved 7 47
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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.

912 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now