在linux中将日期附加到文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1795678/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:56:17  来源:igfitidea点击:

Append date to filename in linux

linuxubuntuappendrenamefilenames

提问by sami

I want add the date next to a filename ("somefile.txt"). For example: somefile_25-11-2009.txt or somefile_25Nov2009.txt or anything to that effect

我想在文件名(“somefile.txt”)旁边添加日期。例如: somefile_25-11-2009.txt 或 somefile_25Nov2009.txt 或任何类似的东西

Maybe a script will do or some command in the terminal window. I'm using Linux(Ubuntu).

也许脚本会在终端窗口中执行或执行某些命令。我正在使用 Linux(Ubuntu)。

Thanks in advance.

提前致谢。

oh i almost forgot to add that the script or command should update the filename to a new date everytime you want to save the file into a specific folder but still keeping the previous files. So there would be files like this in the folder eventually: filename_18Oct2009.txt , filename_9Nov2009.txt , filename_23Nov2009.txt

哦,我差点忘了添加脚本或命令应该在每次要将文件保存到特定文件夹但仍保留以前的文件时将文件名更新为新日期。所以最终文件夹中会有这样的文件: filename_18Oct2009.txt , filename_9Nov2009.txt , filename_23Nov2009.txt

回答by miku

You can use backticks.

您可以使用反引号。

$ echo myfilename-"`date +"%d-%m-%Y"`

Yields:

产量:

myfilename-25-11-2009

回答by Soufiane Hassou

cp somefile somefile_`date +%d%b%Y`

回答by Dave Webb

There's two problems here.

这里有两个问题。

1. Get the date as a string

1. 以字符串形式获取日期

This is pretty easy. Just use the datecommand with the +option. We can use backticks to capture the value in a variable.

这很容易。只需使用date带有+选项的命令即可。我们可以使用反引号来捕获变量中的值。

$ DATE=`date +%d-%m-%y` 

You can change the date format by using different %options as detailed on the date man page.

您可以使用日期手册页中%详述的不同选项来更改日期格式。

2. Split a file into name and extension.

2. 将文件拆分为名称和扩展名。

This is a bit trickier. If we think they'll be only one .in the filename we can use cutwith .as the delimiter.

这有点棘手。如果我们认为它们只是.文件名中的一个,我们可以使用cutwith.作为分隔符。

$ NAME=`echo $FILE | cut -d. -f1
$ EXT=`echo $FILE | cut -d. -f2`

However, this won't work with multiple .in the file name. If we're using bash- which you probably are - we can use some bash magic that allows us to match patterns when we do variable expansion:

但是,这不适.用于文件名中的多个。如果我们正在使用bash——你可能正在使用——我们可以使用一些bash 魔法,当我们进行变量扩展时,它允许我们匹配模式

$ NAME=${FILE%.*}
$ EXT=${FILE#*.} 

Putting them together we get:

把它们放在一起,我们得到:

$ FILE=somefile.txt             
$ NAME=${FILE%.*}
$ EXT=${FILE#*.} 
$ DATE=`date +%d-%m-%y`         
$ NEWFILE=${NAME}_${DATE}.${EXT}
$ echo $NEWFILE                 
somefile_25-11-09.txt                         

And if we're less worried about readability we do all the work on one line (with a different date format):

如果我们不太担心可读性,我们会在一行上完成所有工作(使用不同的日期格式):

$ FILE=somefile.txt  
$ FILE=${FILE%.*}_`date +%d%b%y`.${FILE#*.}
$ echo $FILE                                 
somefile_25Nov09.txt

回答by catwalk

a bit more convoluted solution that fully matches your spec

完全符合您的规格的更复杂的解决方案

echo `expr $FILENAME : '\(.*\)\.[^.]*'`_`date +%d-%m-%y`.`expr $FILENAME : '.*\.\([^.]*\)'`

where first 'expr' extracts file name without extension, second 'expr' extracts extension

其中第一个“expr”提取没有扩展名的文件名,第二个“expr”提取扩展名