如何使用sed MV来重命名带有尾随空格的文件?

时间:2019-06-25 19:46:53

标签: bash sed

我们有一个非常大的文件结构,该文件结构构建得很差。路径在破折号周围包含很多空格@。

所有这些都托管在Synology NAS上,因此我无法访问通常包含的所有工具(例如rename)。

我正在尝试重命名文件AND文件夹名称,该名称和名称前导和尾随空格。

# Global vars
tstamp=$(date +%Y-%m-%d_%H%M%S)

# Change for separator to newline
IFS=$'\n'

echo "$tstamp - Renaming files with leading space: \n"

for filename in $(find . -type f -name '[[:space:]]*')
do
newFilename=$(echo $filename |sed 's/\/[[:space:]]/\//g')
        echo "original: $filename"
        echo "new     : $newFilename"
        mv -i -v -n $filename $newFilename
        echo "\n"
done

echo "$tstamp - Renaming files with trailing space: \n"

for filename in $(find . -type f -name '*[[:space:]]')
do
newFilename=$(echo $filename |sed 's/[[:space:]]$//g')
        echo "original: $filename"
        echo "new     : $newFilename"
        mv -i -v -n $filename $newFilename
        echo "\n"
done


# A slash "/" in a filename is not possible thus it's not verified
echo "$tstamp - Renaming files with unsupported characters (\ / \" : < > ; | * ?):"

for filename in $(find . -type f -name '*\**' -o -name '*\\*' -o -name '*"*' -o -name '*:*' -o -name '*<*' -o -name '*>*' -o -name '*;*' -o -name '*|*' -o -name '*\?*')
do
newFilename=$(echo $filename |sed 's/\(\\\|"\|:\|<\|>\|;\||\|\*\|\?\)//g')
        echo "original: $filename"
        echo "new     : $newFilename"
        mv -i -v -n $filename $newFilename
        echo "\n"
done

echo "Done."

#EOF

使用不支持的字符重命名文件效果很好,但是前导和训练空格不起作用。

这是实际输出,出于安全考虑,我替换了一些名称:

原始:

./ABC- Financing/2018 - ABC Capital Bl Fund 2018 (VCCI)/0 - Dataroom/8 - Vérification diligente/3. Governance/ 2017Q1/ Documents de Julie/@eaDir/ PPP@SynoResource

新:

./ABC - Financing/2018 - ABC Capital Innovation Fund 2018 (GGGG)/0 - Dataroom/8 - Vérification diligente/3. Governance/2017Q1/Documents de Julie/@eaDir/PPP@SynoResource

./ABC - Financing/2018 - ABC Capital Innovation Fund 2018 (GGGG)/0 - Dataroom/8 - Vérification diligente/3. Governance/ 2017Q1/ Documents de Julie/@eaDir/ CDP@SynoResource./ABC - Financing/2018 - ABC Capital Innovation Fund 2018 (GGGG)/0 - Dataroom/8 - Vérification diligente/3. Governance/2017Q1/Documents de Julie/@eaDir/PPP@SynoResource

mv: cannot move "./ABC - Financing/2018 - ABC Capital Innovation Fund 2018 (GGGG)/0 - Dataroom/8 - Vérification diligente/3. Governance/ 2017Q1/ Documents de Julie/@eaDir/ PPP@SynoResource" to "./ABC - Financing/2018 - ABC Capital Innovation Fund 2018 (GGGG)/0 - Dataroom/8 - Vérification diligente/3. Governance/2017Q1/Documents de Julie/@eaDir/PPP@SynoResource": No such file or directory

我不明白为什么mv命令找不到该文件。

2 个答案:

答案 0 :(得分:1)

从此开始(使用find和sed的GNU版本):

#/bin/env bash

readarray -d '' paths < <(find . -depth -print0)
for old in "${paths[@]}"; do
     printf 'Working on path %q\n' "$old" >&2
     new=$(
         printf '%s' "$old" |
         sed -z '
             s#[\\":<>;|*?]##g
             s#[[:space:]]*/[[:space:]]*#/#g
             s#[[:space:]]*$##
         '
     )
     if [[ "$new" != "$old" ]]; then
         printf 'old: %q\n' "$old" >&2
         printf 'new: %q\n' "$new" >&2
         [[ -f "$new" ]] && printf 'Warning: %q already exists.\n' "$new" >&2
         mv -i -v -n -- "$old" "$new"
         printf '\n'
     fi
done

您可以用一些bash内置替换printf | sed来提高性能,但是生活太短了,以至于我无法弄清楚上面的内容对于您需要进行的任何其他更改应该足够简单明了。

以上内容未经测试,因此请确保对文件进行备份并在临时目录上进行彻底测试,然后再运行真实文件。

答案 1 :(得分:0)

让我们尝试通过这种方式安全,正确地执行此操作:

.container {
  width: 200px;
  height: 100px;
  display: flex;
  overflow-x: auto;
}

.item {
  width: 100px;
  flex-shrink: 0;
  height: 100px;
}
相关问题