Adding a timestamp to a file name with mv in BASH

Well, I'm new to Linux and I have a problem with a simple bash script.

I have a program that adds a log file while it is running. Over time, this log file becomes huge. I would like to create a startup script that will rename and move the log file before each run, effectively creating separate log files for each run of the program. Here is what I have so far:

pastebin

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

At startup, I see the following:

 : command not found program 

When I connect to the log directory and run dir, I see the following:

 201111211437\r.log\r 

What's happening? I assume there is some kind of syntax issue that I am missing, but I cannot figure out how to do this.




UPDATE: thanks to the comment below, I found that the problem is that I edit the .sh file in Notepad ++ on Windows and then send via ftp to the server, where I run the file through ssh. After running dos2unix in the file, it works.

A new question: how can I save the file correctly in the first place, in order to avoid the need to perform this correction every time I send the file again?

+75
linux bash
Nov 22
source share
5 answers

A few lines that you sent from your script look good to me. This is probably something deeper.

You need to find which line gives you this error. Add set -xv to the top of the script. This will print the line number and the command executed in STDERR. This will help you determine where in your script you are getting this particular error.

By the way, do you have a shebang at the top of your script? When I see something like this, I usually expect this to be a problem with Shebang. For example, if you have #! /bin/bash #! /bin/bash on top, but your bash interpreter is in /usr/bin/bash , you will see this error.

EDIT

A new question: how can I save the file correctly in the first place, in order to avoid the need to perform this correction every time I send the file again?

Two ways:

  • When editing a file, select the menu item Edit-> EOL Conversion-> Unix Format. After it has the correct line endings, Notepad ++ will save them.
  • To make sure that all new files have the correct line endings, go to the "Settings-> Settings" menu item and drag the "Settings" dialog box. Click on the tab "New document / directory by default." Under New Document and Format, select the Unix radio button. Click the Close button.
+24
Nov 22 '11 at 16:02
source share
 mv server.log logs/$(date -d "today" +"%Y%m%d%H%M").log 
+65
Nov 22 2018-11-11T00:
source share

The single line method inside bash works as follows.

[some out put]> $ (date "+% Y.% m.% d-% H.% M.% S"). ver

will create a file with a timestamp name with the extension ver. A list of work files by clicking the snapshot with the name of the date file as follows can show that it works.

find . -type f -exec ls -la {} \; | cut -d ' ' -f 6- >$(date "+%Y.%m.%d-%H.%M.%S").ver

Sure,

cat somefile.log > $(date "+%Y.%m.%d-%H.%M.%S").ver

or even easier

ls > $(date "+%Y.%m.%d-%H.%M.%S").ver

+7
Sep 08 '15 at 8:09
source share

Well, this is not a direct answer to your question, but there is a tool in GNU / Linux whose task is to regularly rotate log files, keeping old ones wired to a certain limit. This is logrotate

+4
Nov 22 2018-11-11T00:
source share

You can write your scripts in notepad, but just make sure you convert them using this -> $ sed -i 's / \ r $ //' yourscripthere

I use all this when I work in cygwin and it works. Hope this helps

+1
Jul 11 '13 at 14:13
source share



All Articles