简体   繁体   English

将文件移动到另一个文件夹,包括文件名中的创建日期

[英]Move files to another folder, including creation date in filename

I am able to move files from one folder to another but the issue is I want the new created file in the new folder as its created date and filename. 我能够将文件从一个文件夹移动到另一个文件夹,但是问题是我希望新文件夹中的新创建文件作为其创建日期和文件名。

For instance 例如

/scripts/a.log

moved to 搬去

/log/8june2012a.log
cp filename "`date +%Y%m%d`filename"

This copies filename as 20120608filename . 会将 文件名复制为20120608filename For your example this is what you want: 对于您的示例,这是您想要的:

cp filename "`date +%d%b%Y`filename"

This copies filename as 08jun2012filename . 会将 文件名复制为08jun2012filename If you want move your file instead of copying use mv instead of cp : 如果要移动文件而不是复制,请使用mv而不是cp

mv filename "`date +%d%b%Y`filename"

Using a couple of CPAN modules this can be made straightforward. 使用几个CPAN模块,可以使其变得简单明了。 File::Copy has been a core module since Perl v5.0, but Date::Format and Path::Class will need installing unless you already have them. 自Perl v5.0以来, File::Copy一直是核心模块,但是除非您已有Date::FormatPath::Class否则它们将需要安装。

I have taken your requirement literally, and this solution prefixes the original file with the creation date using %e%B%Y as the format, with upper case translated to lower case and spaces stripped. 我已经从字面上理解了您的要求,并且此解决方案使用%e%B%Y作为格式在原始文件的开头添加了创建日期,并将大写字母转换为小写字母并删除了空格。 However this isn't very readable and the directory listing will not automatically sort in date order, so I recommend using %Y-%m-%d- instead by replacing the line containing the call to strftime with 但是,这不是很容易strftime ,并且目录列表不会按日期顺序自动排序,因此,我建议使用%Y-%m-%d-来代替,将包含对strftime的调用的行替换为

my $date = lc strftime('%Y-%m-%d-', @date)

At present the code just prints a list of the files it is going to move and their destination. 目前,该代码仅打印将要移动的文件及其目的地的列表。 To actually do the move you should uncomment the call to move . 要真正进行移动,您应该取消对move的调用的注释。

use strict;
use warnings;

use Path::Class 'dir';
use Date::Format 'strftime';
use File::Copy 'move';

my $source  = dir '/scripts/';
my $dest = dir '/log/';

for my $file (grep { not $_->is_dir } $source->children) {

  my @date = localtime $file->stat->ctime;
  (my $date = lc strftime('%e%B%Y', @date)) =~ tr/\x20//d;

  my $newfile = $dest->file($date.$file->basename);

  print "move $file -> $newfile\n";
  #  move $file, $newfile;
}

Here is a solution in Perl. 这是Perl中的解决方案。

#!/usr/bin/perl

use strict;
use warnings;
use File::Copy 'move';
use Time::Piece 'localtime';

my $indir = '/scripts';
my $outdir = '/log';

# get all of the files in the scripts dir
chdir $indir;
my @files = grep -f, glob '*';

foreach my $infile (@files) {
    # get the date that the file was created
    my $file_created_date = localtime( (stat $infile)[9] );
    my $outfile = $file_created_date->strftime('%d%B%Y').$infile;
    move $infile, "$outdir/$outfile";
}

As an aside, I would format the date as %Y%m%d (yyyymmdd) as it gives you a consistent format and allows you to sort by date more easily. 顺便说一句,我将日期格式设置为%Y%m%d (yyyymmdd),因为它为您提供了一致的格式,并使您可以更轻松地按日期排序。

Another solution. 另一个解决方案。

use strict ;
use File::stat ;
use POSIX qw(strftime);

my $File = 'mv.pl';
my $NewFile=strftime("%d%B%Y",localtime(stat($File)->ctime)) . $File ;

rename $File, $NewFile;
use File::Copy;
move("a.log",$DIRECTORY.get_timestamp().".log");

Your get_timestamp function should generate the date. 您的get_timestamp函数应生成日期。

I wrote a demo for you, 我为你编写了一个演示

#!/bin/bash

DATE=`date +"%e%B%Y" | tr -d ' ' | tr A-Z a-z`


for FILENAME in *.log
do
        cp "${FILENAME}" "/log/${DATE}${FILENAME}"
done

you can run this in your "scripts" directory. 您可以在“脚本”目录中运行它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据创建日期移动文件 - Move files based on creation date 根据文件夹的日期和文件的日期将文件移动到linux中各自的文件夹 - Move files to their respective folder in linux based on the date of the folder and the date of the file 如何在 ssh 控制台中将文件夹的所有文件移动到另一个文件夹? - How to move all files of a folder to another folder in ssh console? Linux - 有没有办法获取目录的文件大小,但只包括最后修改/创建日期为 x 的文件? - Linux - is there a way to get the file size of a directory BUT only including the files that have a last modified / creation date of x? Linux将不以字符串开头的txt文件移动到另一个文件夹 - Linux Move txt files not start with String to Another folder 将特定文件从一个文件夹移动到另一个文件夹的脚本 - Script to move specific files from one folder to another Linux:批处理文件名更改添加创建日期 - Linux: batch filename change adding creation date 将创建时间或修改时间小于 25 分钟的文件移动到 ubuntu 中的不同文件夹 - move files whose creation time or modified time is less than 25 mins to a different folder in ubuntu 如何将所选文件一个文件夹移动到另一个带子文件夹的文件夹中 - How to move selected files one folder to another folder with sub folder also 如何使用find和xargs将文件从一个文件夹中的一个文件夹移动到另一个文件夹? - How do you use find and xargs to move files from a folder within a folder to another folder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM