Solved

can I use date in file or directory name

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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…

705 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

22 Experts available now in Live!

Get 1:1 Help Now