Regex 如何重命名文件夹中的所有文件在linux中删除空格字符后的所有内容?
你好,我不能很好地使用正则表达式。我整天都在网上搜索。 我有一个包含许多图片的文件夹:Regex 如何重命名文件夹中的所有文件在linux中删除空格字符后的所有内容?,regex,linux,bash,shell,batch-file,Regex,Linux,Bash,Shell,Batch File,你好,我不能很好地使用正则表达式。我整天都在网上搜索。 我有一个包含许多图片的文件夹: 50912000 Bicchiere.jpg 50913714 Sottobottiglia Bernini.jpg 我正在使用Mac OS X,但我也可以在Ubuntu上尝试,我想为bash制作一个脚本,删除第一个空格后的所有字符,以获得如下解决方案: 50912000.jpg 50913714.jpg 用于文件夹中的所有文件。 感谢您的帮助。 关于使用sed sed 's/ .*\./\./g'
- 50912000 Bicchiere.jpg
- 50913714 Sottobottiglia Bernini.jpg
- 50912000.jpg
- 50913714.jpg
sed 's/ .*\./\./g'
注意*
使用纯BASH之前的空格:
f='50912000 Bicchiere.jpg'
mv "$f" "${f/ *./.}"
或者使用find
一次修复所有文件:
find . -type f -name "* *" -exec bash -c 'f="$1"; s="${f/_ / }"; mv -- "$f" "${s/ *./.}"' _ '{}' \;
您可以结合使用查找和小脚本
prompt> find . -name "* *" -exec move_it {} \;
mv "./50912000 Bicchiere.jpg" ./50912000
mv "./50913714 Sottobottiglia Bernini.jpg" ./50913714
prompt> cat move_it
#!/bin/sh
dst=`echo $1 | cut -c 1-10`
# remove the echo in the line below to actually rename the file
echo mv '"'$1'"' $dst
与
伟大的那么对于文件夹中的所有文件<代码>文件=/path/to/*对于$FILES中的f,执行echo“正在处理$f文件…”操作每个文件$f store current file name cat$f done我刚刚编辑了一个find命令,该命令将所有带空格的文件移动到非空格。我已经尝试过了,但可能是我写错了这个问题。我需要删除第一个空格后的所有字符串。我得到了50912000Bicchiere.jpg1时刻:D我正在处理一些新的原始文件完美!我能问最后一件事吗?有些结果类似于这个339005_u5.jpg,我怎么能用第二个脚本删除它呢?
rename 's/.*\s+//' *files