javagair
asked on
can I use date in file or directory name
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$GARY 2
# ALSO TRIED THIS LINE
cp /var/log/usb.log /var/log/$RALPH
the file does not create.
gary
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$GARY
# ALSO TRIED THIS LINE
cp /var/log/usb.log /var/log/$RALPH
the file does not create.
gary
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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"
#!/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