我在一个目录中有一些文件,如
FILE1.docx.txt
FILE2.docx.txt
FILE3.docx.txt
FILE4.docx.txt
FILE5.docx.txt我想从所有这些输出中删除.docx,以生成最后的输出,如
FILE1.txt
FILE2.txt
FILE3.txt
FILE4.txt
FILE5.txt我该怎么做?
发布于 2020-05-06 02:44:18
只需在包含文件的同一个文件夹中运行这个python脚本:
import os
for file in os.listdir(os.getcwd()):
aux = file.split('.')
if len(aux) == 3:
os.rename(file, aux[0] + '.' + aux[2])发布于 2020-05-06 02:48:49
使用参数展开和mv
for f in *.docx.txt; do
echo mv -vn "$f" "${f%%.*}.${f##*.}"
done一条龙
for f in *.docx.txt; do echo mv -vn "$f" "${f%%.*}.${f##*.}"; done 如果您认为输出是正确的,请删除echo,以重命名文件。
应该在没有任何脚本的任何POSIX兼容的shell中工作。
使用bash时,启用nullglob shell选项,以便在没有以.docx.txt结尾的文件时,glob *.docx.txt不会以文字*.docx.txt形式展开。
#!/usr/bin/env bash
shopt -s nullglob
for f in *.docx.txt; do
echo mv -vn "$f" "${f%%.*}.${f##*.}"
done更新:由于@Léa Gris添加nullglob,将glob更改为*.docx.txt,将-n添加到mv,尽管POSIX没有将-n和-v定义为per https://pubs.opengroup.org/onlinepubs/9699919799/utilities/mv.html,但-n和-v应该同时存在于GNU和BSD mv中
发布于 2020-05-06 05:55:09
您可以像这样使用sed和bash:
for i in *.docx.txt
do
mv "$i" "`echo $i | sed 's/.docx//'`"
donehttps://stackoverflow.com/questions/61626346
复制相似问题