English 中文(简体)
Rename files based on sorted creation date?
原标题:

I have a directory filled with files with random names. I d like to be able to rename them file 1 file 2 etc based on chronological order, ie file creation date. I could be writing a short Python script but then I wouldn t learn anything. I was wondering if there s a clever 1 line command that can solve this. If anyone could point me in the right direction.

I m using zsh.

Thanks!

最佳回答

For zsh:

saveIFS="$IFS"; IFS=$  ; while read -A line; do mv "${line[2]}" "${line[1]%.*}.${line[2]}"; done < <(find -maxdepth 1 -type f -printf "%T+ %f
"); IFS="$saveIFS"

For Bash (note the differences in the option to read and zero-based indexing instead of one-based):

saveIFS="$IFS"; IFS=$  ; while read -a line; do mv "${line[1]}" "${line[0]%.*}.${line[1]}"; done < <(find -maxdepth 1 -type f -printf "%T+%f
"); IFS="$saveIFS"

These rename files by adding the modification date to the beginning of the original filename, which is retained to prevent name collisions.

A filename resulting from this might look like:

2009-12-15+11:08:52.original.txt

Because a null is used as the internal field separator (IFS), filenames with spaces should be preserved.

问题回答

暂无回答




相关问题
KornShell- Creating a fixed width text file

I need to create a simple fixed width text file in KornShell (ksh). My current attempt using printf to pad the string isn t working out very well. What s the shortest, cleanest way to create a fixed ...

unix find command

how to use the find command to find files/directories which are not matching the pattern. for eg: find <some options > -name "dontfile.txt" should give me output of all the find whose file ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

1. 露台式照像

在Windows XP中,没有一时出现指挥时,是否有办法操作一种灰色文字? 我常常需要把“善待”(工作)与“灰色”同起来,即使我的文字没有产出,......

热门标签