简体   繁体   English

如果按字母顺序,使用Sed仅删除文件名中的第一个字符

[英]Using Sed to remove only first character in file name if it's alphabetical

I have bunch of files in a folder which I need to copy with another name in case, that if file first charcter is alphabetical, then remove this character and leave all other character asis either those are numbers, alphabeticals, special marks or what ever. 我在一个文件夹中有一堆文件,如果我的文件的第一个字符是字母,则需要用其他名称复制,然后删除该字符并保留所有其他字符,包括数字,字母,特殊标记或任何其他形式。 So few exsapmles: 如此少的例子:

R123456.txt --> should be copied as 123456.txt R123456.txt->应该复制为123456.txt

Y1235465.jpeg --> should be copied as 1235465.jpeg Y1235465.jpeg->应该复制为1235465.jpeg

736628487.txt --> do nothing 736628487.txt->什么都不做

I have used this command: 我使用了以下命令:

for file in * ; do cp  $file $(echo $file |sed 's/[A-Za-z]//g'); done

But this removes also file extensions which I would like to leave as is... 但这也会删除我想保留的文件扩展名...

br Jii br Jii

Use 采用

s/^[A-Za-z]//

At the start of the buffer a single alphabetic character 在缓冲区的开头,单个字母字符

you can also try this; 您也可以尝试

 for file in * ; do cp -n $file $(echo $file |sed 's/^[A-Za-z]//') 2>/dev/null; done

man cp ; 男人cp; -n, --no-clobber do not overwrite an existing file (overrides a previous -i option) -n,-no-clobber不会覆盖现有文件(覆盖先前的-i选项)

2>/dev/null : to drop any for "'736628487.txt' are the same file". 2> / dev / null:删除“ 736628487.txt”是同一文件”中的任何一个。

Eg: 例如:

user@host:/tmp/test/1$ ls
736628487.txt  R123456.txt  Y1235465.jpeg

user@host:/tmp/test/1$  for file in * ; do cp -n $file $(echo $file |sed 's/^[A-Za-z]//') 2>/dev/null; done

user@host:/tmp/test/1$ ls
123456.txt  1235465.jpeg  736628487.txt  R123456.txt  Y1235465.jpeg

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM