在 BASH 中使用 mv 为文件名添加时间戳

我是个 Linux 新手,我对一个简单的 bash 脚本有疑问。

我有一个程序,可以在日志文件运行时添加它。随着时间的推移,日志文件变得越来越大。我想创建一个启动脚本,它将在每次运行之前重命名和移动日志文件,有效地为程序的每次运行创建单独的日志文件。以下是我目前为止得到的信息:

面糊

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

跑步的时候,我看到这个:

: command not found
program

当我 cd 到 log 目录并运行 dir 时,我看到:

201111211437\r.log\r

- 怎么回事?-我猜我漏掉了一些语法问题,但我好像没弄明白。


更新: 感谢 Shelter 在下面的评论,我发现问题是由于我正在编辑。然后通过 ftp 发送到服务器,在服务器上通过 ssh 运行文件。在文件上运行 dos2unix 之后,它就可以工作了。

新问题: 首先,如何正确地保存文件,以避免每次重新发送文件时都必须执行此修复?

186317 次浏览

Well, it's not a direct answer to your question, but there's a tool in GNU/Linux whose job is to rotate log files on regular basis, keeping old ones zipped up to a certain limit. It's logrotate

mv server.log logs/$(date -d "today" +"%Y%m%d%H%M").log

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.

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

I use it all they time when I'm working in cygwin and it works. Hope this helps

A single line method within bash works like this.

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

will create a file with a timestamp name with ver extension. A working file listing snap shot to a date stamp file name as follows can show it working.

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

Of course

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

or even simpler

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

I use this command for simple rotate a file:

mv output.log `date +%F`-output.log

In local folder I have 2019-09-25-output.log

First, thanks for the answers above! They lead to my solution.

I added this alias to my .bashrc file:

alias now='date +%Y-%m-%d-%H.%M.%S'

Now when I want to put a time stamp on a file such as a build log I can do this:

mvn clean install | tee build-$(now).log

and I get a file name like:

build-2021-02-04-03.12.12.log