I have a group of files within a directory which look like:
我在目录中有一组文件,如下所示:
Filename - XXXXXXXXXXXXXXXXXXX.mp3
Where there are 19 X's. I need a bash script or similar to loop and remove the X's for all of the files within my directory.
哪里有19个X.我需要一个bash脚本或类似的循环并删除我的目录中的所有文件的X.
I have looked all over this site and others and have seen example using the sed command but I personally haven't been be to adapt it to my specific needs. I apologise for my lack of information, I am fairly new to this language and don't know where to start.
我已经浏览了这个网站和其他人,并看到了使用sed命令的示例,但我个人并不是要根据我的具体需求进行调整。我为我缺乏信息道歉,我对这种语言很新,不知道从哪里开始。
3 个解决方案
#1
0
This is a python script that should rename all your files and remove the x's.
这是一个python脚本,应重命名所有文件并删除x。
(You should read all the lines starting with # except the 1st one)
(你应该阅读以#开头的所有行,除了第一行)
#!/usr/bin/env python
import os
for i in os.listdir("."):
#You can change the number to whatever number of X's you want
x = "X"*19
#If you don't want to get rid of the ' - ', then put a '#' (without quotes) in front of the next line
x = " - "+x
new = i.replace(x, "")
print(os.rename(i,new))
Save this file as XXX.py then give it the right permissions by doing:
将此文件另存为XXX.py,然后通过执行以下操作为其授予权限:
sudo chmod +x XXX.py
then run it using:
然后运行它:
./XXX.py
or if your user doesn't have the right permissions:
或者如果您的用户没有正确的权限:
sudo ./XXX.py
If the program returns a error output then:
如果程序返回错误输出,则:
Try with sudo or try:
尝试使用sudo或尝试:
sudo python XXX.py
with or without sudo.
有或没有sudo。
#2
0
You don't really need sed
for this. Just use a parameter expansion.
你真的不需要sed。只需使用参数扩展。
for file in *XXXXXXXXXXXXXXXXXXX.mp3; do
mv "$file" "${file//XXXXXXXXXXXXXXXXXXX}"
done
If typing the Xs really bugs you…
如果输入Xs真的很烦你......
printf -v Xs 'X%.0s' {1..19}
for file in *$Xs.mp3; do
mv "$file" "${file//$Xs}"
done
#3
0
The Python answer might be preferable as it is treacherous to copy paste "bash" one-liners from the web, but here we go:
Python的答案可能更好,因为从网上复制粘贴“bash”单行是危险的,但是我们走了:
The following for loop will:
以下for循环将:
- list all mp3 files and select only those with 19 (bug alert: or more Xes before the extension)
- echo each file to the screen, but removing 19 consecutive Xes first
列出所有mp3文件并仅选择那些带有19的文件(错误警报:或扩展前的更多Xes)
将每个文件回显到屏幕,但首先删除19个连续的Xes
Test:
for FILE in *XXXXXXXXXXXXXXXXXXX.mp3; do echo $(echo ${FILE} | sed -E 's/X{19}//'); done
对于* XXXXXXXXXXXXXXXXXXX.mp3中的文件; do echo $(echo $ {FILE} | sed -E's / X {19} //'); DONE
This baby changes the name of the files instead of "echo":
这个宝贝改变了文件的名称而不是“echo”:
Change names:
for FILE in *XXXXXXXXXXXXXXXXXXX.mp3; do mv ${FILE} $(echo ${FILE} | sed -E 's/X{19}//'); done
对于* XXXXXXXXXXXXXXXXXXX.mp3中的文件;做mv $ {FILE} $(echo $ {FILE} | sed -E's / X {19} //'); DONE
Use with caution...
谨慎使用......
#1
0
This is a python script that should rename all your files and remove the x's.
这是一个python脚本,应重命名所有文件并删除x。
(You should read all the lines starting with # except the 1st one)
(你应该阅读以#开头的所有行,除了第一行)
#!/usr/bin/env python
import os
for i in os.listdir("."):
#You can change the number to whatever number of X's you want
x = "X"*19
#If you don't want to get rid of the ' - ', then put a '#' (without quotes) in front of the next line
x = " - "+x
new = i.replace(x, "")
print(os.rename(i,new))
Save this file as XXX.py then give it the right permissions by doing:
将此文件另存为XXX.py,然后通过执行以下操作为其授予权限:
sudo chmod +x XXX.py
then run it using:
然后运行它:
./XXX.py
or if your user doesn't have the right permissions:
或者如果您的用户没有正确的权限:
sudo ./XXX.py
If the program returns a error output then:
如果程序返回错误输出,则:
Try with sudo or try:
尝试使用sudo或尝试:
sudo python XXX.py
with or without sudo.
有或没有sudo。
#2
0
You don't really need sed
for this. Just use a parameter expansion.
你真的不需要sed。只需使用参数扩展。
for file in *XXXXXXXXXXXXXXXXXXX.mp3; do
mv "$file" "${file//XXXXXXXXXXXXXXXXXXX}"
done
If typing the Xs really bugs you…
如果输入Xs真的很烦你......
printf -v Xs 'X%.0s' {1..19}
for file in *$Xs.mp3; do
mv "$file" "${file//$Xs}"
done
#3
0
The Python answer might be preferable as it is treacherous to copy paste "bash" one-liners from the web, but here we go:
Python的答案可能更好,因为从网上复制粘贴“bash”单行是危险的,但是我们走了:
The following for loop will:
以下for循环将:
- list all mp3 files and select only those with 19 (bug alert: or more Xes before the extension)
- echo each file to the screen, but removing 19 consecutive Xes first
列出所有mp3文件并仅选择那些带有19的文件(错误警报:或扩展前的更多Xes)
将每个文件回显到屏幕,但首先删除19个连续的Xes
Test:
for FILE in *XXXXXXXXXXXXXXXXXXX.mp3; do echo $(echo ${FILE} | sed -E 's/X{19}//'); done
对于* XXXXXXXXXXXXXXXXXXX.mp3中的文件; do echo $(echo $ {FILE} | sed -E's / X {19} //'); DONE
This baby changes the name of the files instead of "echo":
这个宝贝改变了文件的名称而不是“echo”:
Change names:
for FILE in *XXXXXXXXXXXXXXXXXXX.mp3; do mv ${FILE} $(echo ${FILE} | sed -E 's/X{19}//'); done
对于* XXXXXXXXXXXXXXXXXXX.mp3中的文件;做mv $ {FILE} $(echo $ {FILE} | sed -E's / X {19} //'); DONE
Use with caution...
谨慎使用......