Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何重命名文件夹中的所有文件在linux中删除空格字符后的所有内容?_Regex_Linux_Bash_Shell_Batch File - Fatal编程技术网

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
我正在使用Mac OS X,但我也可以在Ubuntu上尝试,我想为bash制作一个脚本,删除第一个空格后的所有字符,以获得如下解决方案:

  • 50912000.jpg
  • 50913714.jpg
用于文件夹中的所有文件。 感谢您的帮助。 关于

使用sed

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