Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to append the current date to the end of a file name like this:

TheFile.log.2012-02-11

Here is what I have so far:

set today = 'date +%Y'
mkdir -p The_Logs &
find . -name The_Logs -atime -1 -type d -exec mv \{} "The_Logs_+$today" \; &

However all I get is the name of the file, and it appends nothing. How do I append a current date to a filename?

share|improve this question

4 Answers 4

up vote 3 down vote accepted

More than likely it is your use of set. That will assign 'today', '=' and the output of the date program to positional parameters (aka command-line arguments). You want to just use C shell (which you are tagging this as "bash", so likely not), you will want to use:

today=`date +%Y-%m-%d.%H:%M:%S` # or whatever pattern you desire

Notice the lack of spaces around the equal sign.

You also do not want to use & at the end of your statements; which causes the shell to not wait for the command to finish. Especially when one relies on the next. The find command could fail because it is started before the mkdir.

share|improve this answer
    
Thanks for that mate, its working %100 fine now, Cheers for the heads-up & and mkdir –  I AM L Dec 5 '12 at 0:20

You seem to have mixed up several things.

set today = 'date +%Y' looks like tcsh syntax, but even in tcsh it assigns the string date +%Y to the variable today, it doesn't run the date command. As you're probably using bash or some other POSIX shell, the syntax of an assignment is today=some_value (with no spaces around the equal sign). To run the command and assign its output to the variable, use command substitution:

today=$(date +%Y-%m-%d)

(I've also completed the date specification). You can use backquotes instead of dollar-parentheses, but it's prone to being visually confused with forward quotes, and the rules for when you need quotes inside a backquoted command are pretty complex and implementation-dependent, so it's better not to stick to $(…) (which has the same effect with a saner syntax).

You used & at the end of several commands. That makes the command execute in the background, which is not wanted here. I suspect you meant &&, which means to execute the next command only if the first command succeeded.

today=$(date +%Y-%m-%d)
mkdir -p The_Logs &&
find …

An alternative to using && after each command is to start your script with set -e. This tells the shell to stop executing the script as soon as any command returns a nonzero status (except for commands in if conditions and a few other cases).

set -e
today=$(date +%Y-%m-%d)
mkdir -p The_Logs
find …

Your find command is fine but probably doesn't do what you intend to do (though I don't know for sure what that is).

You're creating a directory with mkdir and then immediately traversing it with find. That won't be useful unless the directory already exists. Did you mean to create a directory for today's logs and move recent files from The_Logs to a directory called e.g. The_Logs.2012-02-11?

mkdir -p "The_Logs.$today"
find The_Logs -mtime -1 -exec mv {} "The_Logs.$today" \;

Or did you mean to rename today's log files to add the suffix $today? That requires calculating the different file name for each file to move.

find The_Logs -mtime -1 -exec sh -c 'mv "$0" "$0.$today"' {} \;

Note that I used -mtime, to move files based on their modification time, and not -atime, which is the time the file was last read (if your system keeps track of that — if it doesn't, the atime may be as far back as the mtime).

share|improve this answer

This:

find . -name The_Logs -atime -1 -type d -exec mv {} "The_Logs_${today}" \; &

should work. Although, it seems like you are using csh and my solution of removing the + sign may not work for you but it won't hurt to try. Enclosing your variable in {} curly brackets, is always a good idea to prevent unexpected variable expansions.

share|improve this answer
    
Thanks mate, after adding your correction with the other answer, its working fine. –  I AM L Dec 5 '12 at 0:23

Bash script to inject a date into a filename:

This bash code in file called a.sh

#!/bin/bash
today=`date '+%Y_%m_%d__%H_%M_%S'`;
filename="/home/el/myfile/$today.ponies"
echo $filename;

When run, prints:

eric@dev ~ $ chmod +x a.sh

eric@dev ~ $ ./a.sh
/home/el/myfile/2014_08_11__15_55_25.ponies

Explanation of the code:

Interpret the script with the /bin/bash interpreter. Make a new variable called today. Execute the date command, passing in the Y, m, d, H, M, S flags to configure the output. Place the result into the date variable.

Create a new variable called filename, surround the $today variable with the rest of the static filename text. then echo the filename to screen.

Cram it into a one-liner to increase lulz:

echo "/home/el/myfile/`date '+%Y_%m_%d__%H_%M_%S'`.ponies"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.