通过插入和_字符重命名UNIX中的多个DateTime文件

发布于 2025-02-07 02:23:37 字数 429 浏览 1 评论 0原文

我想重命名的目录中有许多文件,以便根据某个约定可以识别它们:

SURFACE_OBS:2019062200
SURFACE_OBS:2019062206
SURFACE_OBS:2019062212
SURFACE_OBS:2019062218
SURFACE_OBS:2019062300

SURFACE_OBS:2019-06-22_00
SURFACE_OBS:2019-06-22_06
SURFACE_OBS:2019-06-22_12
SURFACE_OBS:2019-06-22_18
SURFACE_OBS:2019-06-23_00 

I have many files in a directory that I want to rename so that they are recognizable according to a certain convention:

SURFACE_OBS:2019062200
SURFACE_OBS:2019062206
SURFACE_OBS:2019062212
SURFACE_OBS:2019062218
SURFACE_OBS:2019062300

etc.

How can I rename them in UNIX to be as follows?

SURFACE_OBS:2019-06-22_00
SURFACE_OBS:2019-06-22_06
SURFACE_OBS:2019-06-22_12
SURFACE_OBS:2019-06-22_18
SURFACE_OBS:2019-06-23_00 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

佞臣 2025-02-14 02:23:37

使用MV和参数扩展的bash shell循环可以做到:

for file in *:[[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]]
do
  prefix=${file%:*}
  suffix=${file#*:}
  mv -- "${file}" "${prefix}:${suffix:0:4}-${suffix:4:2}-${suffix:6:2}_${suffix:8:2}"
done

此循环拾取与模式匹配的每个文件:

  • * - nothing
  • : - colon
  • [[:digit:]] [:digit:]] [:digit:]] [:digit:]] [:digit:]] [[:digit:]] [ :]]

[: digit 所需的位置。

我已经仔细选择了通配符,以便它试图匹配“输入”文件而不是更名的文件。如果您的实际文件名具有导致通配符失败的边缘案例,请根据需要调整模式(因此第二次将文件重命名)。

A bash shell loop using mv and parameter expansion could do it:

for file in *:[[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]]
do
  prefix=${file%:*}
  suffix=${file#*:}
  mv -- "${file}" "${prefix}:${suffix:0:4}-${suffix:4:2}-${suffix:6:2}_${suffix:8:2}"
done

This loop picks up every file that matches the pattern:

  • * -- anything
  • : -- a colon
  • [[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]] -- 10 digits

... and then renames it by inserting dashes and and underscore in the desired locations.

I've chosen the wildcard for the loop carefully so that it tries to match the "input" files and not the renamed files. Adjust the pattern as needed if your actual filenames have edge cases that cause the wildcard to fail (and thus rename the files a second time).

江湖彼岸 2025-02-14 02:23:37
#!/bin/bash

strindex() { 
  # get position of character in string
  x="${1%%"$2"*}"
  [[ "$x" = "$1" ]] && echo -1 || echo "${#x}"
}

get_new_filename() {
  # change filenames like:  SURFACE_OBS:2019062218
  # into filenames like:    SURFACE_OBS:2019-06-22_18

  src_str="${1}"

  # add last underscore 2 characters from end of string
  final_underscore_pos=${#src_str}-2
  src_str="${src_str:0:final_underscore_pos}_${src_str:final_underscore_pos}"

  # get position of colon in string
  colon_pos=$(strindex "${src_str}" ":")
  
  # get dash locations relative to colon position
  y_dash_pos=${colon_pos}+5
  m_dash_pos=${colon_pos}+8

  # now add dashes in date
  src_str="${src_str:0:y_dash_pos}-${src_str:y_dash_pos}"
  src_str="${src_str:0:m_dash_pos}-${src_str:m_dash_pos}"
  echo "${src_str}"
}

# accept path as argument or default to /tmp/baz/data
target_dir="${1:-/tmp/baz/data}"

while read -r line ; do
  # since file renaming depends on position of colon extract 
  # base filename without path in case path has colons 
  base_dir=${line%/*}
  filename_to_change=$(basename "${line}")
  echo "mv ${line} ${base_dir}/$(get_new_filename "${filename_to_change}")"

  # find cmd attempts to exclude files that have already been renamed 
done < <(find "${target_dir}" -name 'SURFACE*' -a ! -name '*_[0-9]\{2\}
)
#!/bin/bash

strindex() { 
  # get position of character in string
  x="${1%%"$2"*}"
  [[ "$x" = "$1" ]] && echo -1 || echo "${#x}"
}

get_new_filename() {
  # change filenames like:  SURFACE_OBS:2019062218
  # into filenames like:    SURFACE_OBS:2019-06-22_18

  src_str="${1}"

  # add last underscore 2 characters from end of string
  final_underscore_pos=${#src_str}-2
  src_str="${src_str:0:final_underscore_pos}_${src_str:final_underscore_pos}"

  # get position of colon in string
  colon_pos=$(strindex "${src_str}" ":")
  
  # get dash locations relative to colon position
  y_dash_pos=${colon_pos}+5
  m_dash_pos=${colon_pos}+8

  # now add dashes in date
  src_str="${src_str:0:y_dash_pos}-${src_str:y_dash_pos}"
  src_str="${src_str:0:m_dash_pos}-${src_str:m_dash_pos}"
  echo "${src_str}"
}

# accept path as argument or default to /tmp/baz/data
target_dir="${1:-/tmp/baz/data}"

while read -r line ; do
  # since file renaming depends on position of colon extract 
  # base filename without path in case path has colons 
  base_dir=${line%/*}
  filename_to_change=$(basename "${line}")
  echo "mv ${line} ${base_dir}/$(get_new_filename "${filename_to_change}")"

  # find cmd attempts to exclude files that have already been renamed 
done < <(find "${target_dir}" -name 'SURFACE*' -a ! -name '*_[0-9]\{2\}
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文