简体   繁体   English

正则表达式重命名所有文件递归删除字符“?”命令行后的所有内容

[英]Regex to rename all files recursively removing everything after the character “?” commandline

I have a series of files that I would like to clean up using commandline tools available on a *nix system. 我有一系列文件,我想使用* nix系统上提供的命令行工具进行清理。 The existing files are named like so. 现有文件的名称如此。

filecopy2.txt?filename=3
filecopy4.txt?filename=33
filecopy6.txt?filename=198
filecopy8.txt?filename=188
filecopy3.txt?filename=19
filecopy5.txt?filename=1
filecopy7.txt?filename=5555

I would like them to be renamed removing all characters after and including the "?". 我希望它们被重命名后删除所有字符,包括“?”。

filecopy2.txt
filecopy4.txt
filecopy6.txt
filecopy8.txt
filecopy3.txt
filecopy5.txt
filecopy7.txt

I believe the following regex will grab the bit I want to remove from the name, 我相信以下正则表达式会抓住我想从名称中删除的位,

\?(.*)

I just can't figure out how to accomplish this task beyond this. 我无法弄清楚除此之外如何完成这项任务。

If all files are in the same directory (ignoring .dotfiles ): 如果所有文件都在同一目录中(忽略.dotfiles ):

$ rename -n 's/\?filename=\d+$//' -- *

If you want to rename files recursively in a directory hierarchy: 如果要在目录层次结构中递归重命名文件:

$ find . -type f -exec rename -n 's/\?filename=\d+$//' {} +

Remove -n option, to do the renaming. 删除-n选项,进行重命名。

find . -depth -name '*[?]*' -exec sh -c 'for i do
  mv "$i" "${i%[?]*}"; done' sh {} +

With zsh : 使用zsh

autoload zmv
zmv '(**/)(*)\?*'  '$1$2'

Change it to: 将其更改为:

zmv -Q '(**/)(*)\?*(D)' '$1$2'

if you want to rename dot files as well. 如果你想重命名点文件。

Note that if filenames may contain more than one ? 请注意,如果文件名可能包含多个? character, both will only trim from the rightmost one. 角色,两者都只会从最右边的一个修剪。

A bash command: 一个bash命令:

for file in *; do
  mv $file ${file%%\?filename=*}
done

I this case you can use the cut command: 在这种情况下,您可以使用cut命令:

echo 'filecopy2.txt?filename=3' | cut -d? -f1

example: 例:

find . -type f -name "*\?*" -exec sh -c 'mv $1 $(echo $1 | cut -d\? -f1)' mv {}  \;

You can use rename if you have it: 如果你有,你可以使用重命名

rename 's/\?.*$//' *

I use this after downloading a bunch of files where the URL included parameters and those parameters ended up in the file name. 我在下载了一堆文件后使用了这个文件,其中包含参数的URL和那些参数最终都在文件名中。

This is a Bash script. 这是一个Bash脚本。

for file in *; do
  mv $file ${file%%\?*};
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何重命名文件夹中的所有文件,删除linux中空格字符后的所有内容? - How to rename all files in a folder removing everything after space character in linux? 正则表达式,仅当某个字符出现在特定数量的字符之后时才删除该特定字符之后的所有内容 - Regex for removing everything after a certain character only if the character occurs after a specific number of characters 删除字符后的所有内容,包括该字符 - Removing everything after a character, including that character 正则表达式忽略字符后的所有内容 - Regex ignore everything after a character 如何匹配正则表达式,在匹配后删除所有内容 - How to match a regex, removing everything after the match 对Coldfusion的REGEX帮助 - 在URL中,删除? 之后的一切 - REGEX Help for Coldfusion - In a URL, removing the ? And everything after RegEx用于删除定界符之前和之后的所有内容 - RegEx for removing everything before and after a delimiter 使用Javascript正则表达式删除字符后的所有内容 - Deleting Everything After a Character with Javascript Regex PHP Regex删除字符后的所有内容 - PHP Regex to remove everything after a character Perl正则表达式可以匹配@字符后的所有内容 - Perl Regex to match everything after @ character
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM