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
Solved

can I use date in file or directory name

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
installing LSI MegaRAID Storage Manager on CentOS 7 22 722
Coding C# in Linux 8 122
blank screen when trying to setup Unity on Ubuntu 14.04 9 42
Linux MD5 Hash 7 35
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

808 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