Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding timestamp to a filename with mv in BASH

Tags:

linux

bash

Well, I'm a linux newbie, and I'm having an issue with a simple bash script.

I've got a program that adds to a log file while it's running. Over time that log file gets huge. I'd like to create a startup script which will rename and move the log file before each run, effectively creating separate log files for each run of the program. Here's what I've got so far:

pastebin

DATE=$(date +"%Y%m%d%H%M") mv server.log logs/$DATE.log echo program 

When run, I see this:

: command not found program 

When I cd to the logs directory and run dir, I see this:

201111211437\r.log\r 

What's going on? I'm assuming there's some syntax issue I'm missing, but I can't seem to figure it out.


UPDATE: Thanks to shellter's comment below, I've found the problem to be due to the fact that I'm editing the .sh file in Notepad++ in windows, and then sending via ftp to the server, where I run the file via ssh. After running dos2unix on the file, it works.

New question: How can I save the file correctly in the first place, to avoid having to perform this fix every time I resend the file?

like image 225
Cat5InTheCradle Avatar asked Nov 22 '11 14:11

Cat5InTheCradle


People also ask

Does MV preserve timestamp?

mv will always preserve all metadata, including modification time and user and group ownership.

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.


2 Answers

mv server.log logs/$(date -d "today" +"%Y%m%d%H%M").log 
like image 86
dqw Avatar answered Oct 07 '22 10:10

dqw


The few lines you posted from your script look okay to me. It's probably something a bit deeper.

You need to find which line is giving you this error. Add set -xv to the top of your script. This will print out the line number and the command that's being executed to STDERR. This will help you identify where in your script you're getting this particular error.

BTW, do you have a shebang at the top of your script? When I see something like this, I normally expect its an issue with the Shebang. For example, if you had #! /bin/bash on top, but your bash interpreter is located in /usr/bin/bash, you'll see this error.

EDIT

New question: How can I save the file correctly in the first place, to avoid having to perform this fix every time I resend the file?

Two ways:

  1. Select the Edit->EOL Conversion->Unix Format menu item when you edit a file. Once it has the correct line endings, Notepad++ will keep them.
  2. To make sure all new files have the correct line endings, go to the Settings->Preferences menu item, and pull up the Preferences dialog box. Select the New Document/Default Directory tab. Under New Document and Format, select the Unix radio button. Click the Close button.
like image 31
David W. Avatar answered Oct 07 '22 11:10

David W.