Linux 在 BASH 中使用 mv 向文件名添加时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8228047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding timestamp to a filename with mv in BASH
提问by Cat5InTheCradle
Well, I'm a linux newbie, and I'm having an issue with a simple bash script.
好吧,我是 linux 新手,我在使用简单的 bash 脚本时遇到了问题。
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:
我有一个程序可以在运行时添加到日志文件中。随着时间的推移,日志文件变得很大。我想创建一个启动脚本,它将在每次运行之前重命名和移动日志文件,从而为程序的每次运行有效地创建单独的日志文件。这是我到目前为止所得到的:
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:
当我 cd 到日志目录并运行 dir 时,我看到:
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.
更新:感谢 shellter 在下面的评论,我发现问题是由于我在 Windows 中的 Notepad++ 中编辑 .sh 文件,然后通过 ftp 发送到服务器,在那里我通过 ssh 运行文件. 在文件上运行 dos2unix 后,它可以工作。
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?
新问题:首先如何正确保存文件,以避免每次重新发送文件时都必须执行此修复?
采纳答案by David W.
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.
您需要找到哪一行给您这个错误。添加set -xv
到脚本的顶部。这将打印出行号和正在执行的命令STDERR。这将帮助您确定在脚本中遇到此特定错误的位置。
BTW, do you have a shebangat 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.
顺便说一句,你的脚本顶部有一个shebang吗?当我看到这样的事情时,我通常认为这是 Shebang 的问题。例如,如果您#! /bin/bash
在顶部,但您的 bash 解释器位于 中/usr/bin/bash
,您将看到此错误。
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:
两种方式:
- Select the Edit->EOL Conversion->Unix Formatmenu item when you edit a file. Once it has the correct line endings, Notepad++ will keep them.
- To make sure all new files have the correct line endings, go to the Settings->Preferencesmenu item, and pull up the Preferencesdialog box. Select the New Document/Default Directorytab. Under New Documentand Format, select the Unixradio button. Click the Closebutton.
- 编辑文件时选择Edit->EOL Conversion->Unix Format菜单项。一旦它有正确的行尾,Notepad++ 将保留它们。
- 要确保所有新文件都有正确的行尾,请转到Settings->Preferences菜单项,然后拉出Preferences对话框。选择新建文档/默认目录选项卡。在New Documentand Format 下,选择Unix单选按钮。单击关闭按钮。
回答by Didier Trosset
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
嗯,这不是您问题的直接答案,但是 GNU/Linux 中有一个工具,它的工作是定期轮换日志文件,将旧文件压缩到一定限度。它是logrotate
回答by dqw
mv server.log logs/$(date -d "today" +"%Y%m%d%H%M").log
回答by mkrouse
You can write your scripts in notepad but just make sure you convert them using this -> $ sed -i 's/\r$//' yourscripthere
您可以在记事本中编写脚本,但请确保使用以下命令转换它们 -> $ sed -i 's/\r$//' yourscripthere
I use it all they time when I'm working in cygwin and it works. Hope this helps
当我在 cygwin 中工作时,我一直使用它并且它有效。希望这可以帮助
回答by Cyberience
A single line method within bash works like this.
bash 中的单行方法是这样工作的。
[some out put] >$(date "+%Y.%m.%d-%H.%M.%S").ver
[一些输出] >$(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.
将创建一个带有 ver 扩展名的时间戳名称的文件。列出快照文件名的工作文件如下所示可以显示它正在工作。
find . -type f -exec ls -la {} \; | cut -d ' ' -f 6- >$(date "+%Y.%m.%d-%H.%M.%S").ver
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
cat somefile.log > $(date "+%Y.%m.%d-%H.%M.%S").ver
or even simpler
甚至更简单
ls > $(date "+%Y.%m.%d-%H.%M.%S").ver
ls > $(date "+%Y.%m.%d-%H.%M.%S").ver
回答by alfiogang
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
在本地文件夹中我有 2019-09-25-output.log