将所有文件名截断为 255 个字符的命令

发布于 2024-07-18 07:09:03 字数 74 浏览 3 评论 0原文

NTFS 目录在 bash shell 中打开。 什么命令会递归地将目录中的所有文件名截断为 ext3 所需的 255 个字符限制?

An NTFS directory is open in a bash shell. what command will recursively truncate all filenames in a directory to the 255 character limit required for ext3?

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

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

发布评论

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

评论(3

把时间冻结 2024-07-25 07:09:03

如果您有权访问 Windows shell,则可以使用:(

@echo off
setlocal EnableDelayedExpansion

REM  loop over all files in the cwd
for /f %%a in ('dir /a-d /b') do (
   REM  store this filename in a variable so we can do substringing
   set ThisFileName=%%a
   REM  now take a substring
   set ThisShortFileName=!ThisFileName:~0,255!
   REM  finally, the rename:
   echo ren %%a !ThisShortFileName!
)


:EOF
endlocal

注意:我在重命名命令之前添加了一个 echo,以便您可以在实际运行之前直观地验证它是否有效。在我的机器上有效。)

我确信有人现在使用 *nix 机器的人可以为 bash 编写类似的脚本,但我被困在 Windows 世界中:)

祝你好运!

If you have access to a Windows shell, you can use:

@echo off
setlocal EnableDelayedExpansion

REM  loop over all files in the cwd
for /f %%a in ('dir /a-d /b') do (
   REM  store this filename in a variable so we can do substringing
   set ThisFileName=%%a
   REM  now take a substring
   set ThisShortFileName=!ThisFileName:~0,255!
   REM  finally, the rename:
   echo ren %%a !ThisShortFileName!
)


:EOF
endlocal

(Note: I have added an echo before the rename command just so you can visually verify that it works before actually running it. Works on my box.)

I'm sure somebody who's on a *nix box right now could make a similar script for bash, but I'm stuck in Windows world :)

Good luck!

愚人国度 2024-07-25 07:09:03

假设 shell 位于 NTFS 目录中,因为它是 PWD:

<代码>for f in *; 做 mv $f ${f:0:255}; 完成

与 Dave 的基于 sed 的版本类似,但避免了每个文件的 exec。 由于最大命令行限制,会在一个非常大的目录上爆炸,并且不会执行子目录。

Assuming that the shell is sitting in the NTFS directory as it's PWD:

for f in *; do mv $f ${f:0:255}; done

Similar to Dave's sed based version, but avoids an exec per file. Will blow up on a really huge dir, because of the max commandline limit, and doesn't do subdirs.

裂开嘴轻声笑有多痛 2024-07-25 07:09:03
$ cat truncname 
#!/bin/bash
# requires basename, dirname, and sed
mv $1 `dirname $1`/`basename $1 | sed 's/^\(.\{0,255\}\).*/\1/'`
$ chmod a+x truncname 
$ find . -exec ./truncname {} \;
$ cat truncname 
#!/bin/bash
# requires basename, dirname, and sed
mv $1 `dirname $1`/`basename $1 | sed 's/^\(.\{0,255\}\).*/\1/'`
$ chmod a+x truncname 
$ find . -exec ./truncname {} \;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文