- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我写了一个脚本来一次重命名几个文件并添加前导零。该脚本将要重命名的文件作为第一个参数,第二个参数是新名称,第三个参数是您可以提供新的扩展名
只要文件不包含空格(test asd 1.txt/test asd 2.txt),它实际上就可以工作,因为输出是:
~/Desktop $ gpRenameWithZero test\ asd\* test_ mp3
ls: cannot access test: No such file or directory
ls: cannot access asd*: No such file or directory
ls: cannot access test: No such file or directory
ls: cannot access asd*: No such file or directory
这是脚本:
#!/bin/bash
#rename a group of files with adding padding zero: gpRenameWithZero $1=filesToBeRenamed $2=newName $3=filetype: gpRenameWithZero \* newName_ jpg
#123 files -> length of number are 3 digits
numberOfDigits=$(ls $1| wc -l | xargs expr length)
#take extension from command line or take from filename
if [ $# -gt 2 ]; then
extension=$3
else
extension=$(ls -rt $1 | head -n 1 | rev | cut -d . -f1 | rev)
fi
#Preview
ls -rt $1 | cat -n | while read n f; do echo mv "$f" `printf "$2%0$numberOfDigits"d".$extension" $n`; done
read -p "Do you wish to rename [y/n]?" yn
case $yn in
[Yy]* ) ls -rt $1 | cat -n | while read n f; do mv "$f" `printf "$2%0$numberOfDigits"d".$extension" $n`; done;;
[Nn]* ) ;;
esac
我已经尝试过引用/双引号变量和参数,转义/不转义。
如何解决这个问题?或者是否有一个更简单的脚本,它将要重命名的文件、新名称和扩展名作为参数)来重命名多个文件。
最佳答案
你自己明白了,为什么是真的,评论是怎么说的:不要解析 ls
的输出.
在这种情况下,您需要处理以 NULL 结尾的文件名。这里有许多可以使用这种以 NULL 结尾的字符串的命令。
find
命令可以使用 arg -print0
输出以 NULL 结尾的文件名或完全指定 -print "<format>\0"
.
因为您想要按修改时间 (ls -rt
) 对(编号)文件进行排序,所以您需要使用一些技巧。所以
find
命令(GNU版本)可以打印出文件修改时间这可以通过以下命令来实现:
find . -mindepth 1 -maxdepth 1 -name "*" -printf "%T@:%p\0" |sort -zrn |sed -z 's/[^:]*://'
^^^^^^^^ ^^^ ^^^^^^^^^^
modification-time-in-seconds<colon>path-name<NULL>---+ | |
sort the NULL-terminated lines numerically (by time) ---+ |
remove the <time><colon> part, so only the filename remains ----------+
您可以将以上内容放入 bash 函数中以便于使用,例如:
sort_files_by_time() {
find "${1:-.}" -mindepth 1 -maxdepth 1 -type f -name "${2:-*}" -printf "%T@:%p\0" | sort -zrn | sed -z 's/[^:]*://'
}
上述函数可以接受两个可选参数:
现在您有一个按时间排序的文件名的 NULL 终止列表,需要阅读和使用它们。下一个循环
while IFS= read -d $'\0' -r path ; do
dir=$(dirname "$path")
file=$(basename "$path")
#do what you want
done < <(sort_files_by_time)
总是 双引号 "$variables"可以包含空格。
关于Linux 脚本 : mass rename of files with whitespaces,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25523203/
请参阅此示例: http://jsfiddle.net/EwtaC/1/ 图像通过display:inline-block设置样式。我知道空格会以某种方式对此产生影响..(无论出于何种原因)所以我删除
我在这里设置了一个 CSS 菜单: http://pans.saebermedia.com/ 问题是,我注意到 IE 无法正确显示菜单,现在我又开始使用装有 IE8 的计算机来修复它。我正在使用兼容模
如果你尝试这样做: var x = document.createElement("div"); x.appendChild(document.createTextNode(" te
我创建了一个报表,该报表的左侧有一个图像,该图像根据传递给该报表的参数有条件地可见。页面右侧还有另一个文本框。 我观察到,当图像的“隐藏”属性设置为“True”时,右侧的文本框将保留在正确的位置。当图
Closed. This question is off-topic。它当前不接受答案。 想要改善这个问题吗? Update the question,所以它是用于堆栈溢出的on-topic。 已关闭
当我使用 ``# `` 在我的 Sphinx 文档中,我收到以下警告: WARNING: Inline literal start-string without end-string. 试 :samp
我正在尝试对 Linux 系统上具有 Windows 行结尾的文件应用补丁,但由于文件中的回车符而出现冲突。 -l 选项(忽略空格)不会忽略 EOL 字符。 有没有办法获得补丁来忽略Windows风格
您实际上如何更改vscode的“editor.tabSize”和“editor.insertSpaces”值?我打开了文件>首选项>用户设置,并添加了: // Place your settings
有谁知道一个命令行 exe/工具可以 可靠 从 C# 语言的行尾修剪空格? 最佳答案 它必须是命令行吗? Visual Studio 会在你做 Format Document 时( Ctrl+E+D
尝试创建一个背景色为白色的内容框。它位于页眉 div 和页脚 div 之间,它们都是图像。我无法让它与两个 div 对齐并且没有空白,只有一个或另一个。 这是 CSS: #content {
我想要对空白进行精细控制,但仍然有可读的模板。 只是想通过简单的用例看看其他人的解决方案。 {{name}} {{#if age}} , {{age}} {{/if}} # outputs {{n
如果我在Atom设置中选择Show Invisibles,则所有不可见的字符都变为可见: 我想隐藏EOL,因为它们会污染 View 。 这是可以实现的吗? 最佳答案 没有ui选项,但是可以通过Atom
我知道之前已经有人问过如何用前导空格分割字符串的问题,例如: String str = " I want to be split \t!" String[] sarr = str.split("\\
我不确定 slider 顶部和底部的额外空白来自哪里。是填充还是边距?如果有人可以帮助我,我将不胜感激。 jsfiddle.net/fH3EL 最佳答案 这是因为 bx-slider CSS 文件造成
我有一种自定义脚本语言,我正在尝试使用 XTEXT 进行语法检查。它归结为格式为单行命令 COMMAND:PARAMETERS 在大多数情况下,xtext 运行良好。我目前遇到的唯一问题是如何处理需要
我有一个包含超过 1,000,000 个条目的数据库,其中一些在值的开头/结尾包含空格字符。 我已经尝试了以下查询并且它有效,但我将不得不检查 1,000,000 条记录,因为所有 ID 都是唯一的
Perl 6 清除了其前身中的一些奇怪情况,不允许在某些地方使用空格,但在其他地方也执行工作。空间在哪里重要?有一个完整的引用会很好,因此我添加了一个社区 wiki 答案,我会用你的代表答案来更新。示
我想知道有什么方法可以在 Doxygen 的 html 的评论中插入空格吗?我在网上和 Doxygen 手册上搜索过,但找不到任何可以做到这一点的东西。 例如,我试图添加评论如下: //! mot
有人可以澄清一下空格在 Perl 6 语法中的规则中什么时候很重要吗?我通过反复试验学习了一些,但似乎无法在文档中找到实际规则。 示例 1: rule number { \d '.'? \d
我有一个代码库,由于尾随空格而使我发疯。我想把它清理干净。 我想: 删除所有尾随空格 删除文件末尾的所有换行符 将所有行尾转换为 unix (dos2unix) 将所有前导空格转换为制表符,即将 4
我是一名优秀的程序员,十分优秀!