Perl file rename based on date




















This form is useful if we need other information from the inode table. If you only need the modification time, then a more compact way would be calling stat and without assigning to an array, on-the-fly, extract the 9th element. Then we see 3 ways we can format the result. The value we got back was the time since the epoch.

We can convert it into a more readable format either using the build-in localtime function or the heavy-weight DateTime module. The -M operator returns the script start time minus file modification time, in days. It only takes a minute to sign up. Connect and share knowledge within a single location that is structured and easy to search.

So, say, I have a directory with a bunch of files like g. How would I batch-rename all of the files like g. My answer to this question has been bothering me for years well, only when I remember it - on days like today when it gets another upvote so I'm finally updating it.

My original answer above still works, but the updated answer below is better. Like any batch file renaming operation, this should be done with the perl rename utility, not with some klunky shell for loop. This requires only perl and the Date::Format module a module that is so useful that it should be installed on any system with perl. IMO it, along with the author Graham Barr's Date::Parse module, should be part of perl's core module library but it isn't so you'll have to install it with cpan or a distro package like Debian's libtimedate-perl package.

BTW, this script skips any file that looks like it already has a date i. This version has no extra requirements because the File::Basename module has been included as a standard core module with perl for as long as I can remember a decade at least, probably longer.

Remove the -n or replace it with -v for verbose output when you're sure that it does what you want. BTW, I have used rename 's --nofullpath aka -d , --filenmame , --nopath option here to ensure that it renames only the filename portion of any filepaths found.

It's not needed in this particular case because the example rename scripts only change the end of the filename but is generally a good idea when you don't want to rename the path as well as the filename e.

The executable might be called prename or perl-rename or file-rename on your system, so try -V with those, and adjust the examples above to use the correct executable name. Quick-and-dirty Bash one-liner to rename all globbed files in the current directory from filename. Obviously, this doesn't do desirable things like checking whether a file already has something that looks like a date at the end.

And here's an extra which places the date before the extension, if any. As I understood we don't know beforehand what is the modification date. So we need to get it from each file, format the output and rename each file in a way so that it includes the modification date in the filenames. We invoke it with the target directory as the argument:. Here is version of cas's one-liner, based on goldschrafe's oneliner extended with idempotence ,.

Use-case: helpful if you add new files into directory and want to add date time prefix to those not having one yet. Sign up to join this community. The best answers are voted up and rise to the top. Net:FTP perl.

Hello All, I am trying to connect to ftp server and get the files. Also i need to rename the file in other ftp dir. Can some body help me to Perl script to rename file,error. Rename file based on first 3 characters of data in file. I'm looking to determine if I can use a grep command to read file and rename the file based on the first 3 characters of the data in the file. I tried to use the But the renaming does not work. Anybody got a clue?

Help with multiple file rename - change case of part of file name. They have a naming convention thus : prefix-date-key-suffix. RedHat Commands.

OpenSolaris Commands. Linux Commands. SunOS Commands. FreeBSD Commands. Full Man Repository. Advanced Search. Contact Us.

Forum Rules.



0コメント

  • 1000 / 1000