整理目录中所有文件的文件扩展名的一部分-Linux [英] Trimming a part of file extension for all the files in directory - Linux

查看:30
本文介绍了整理目录中所有文件的文件扩展名的一部分-Linux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要修剪目录中所有文件的文件扩展名的要求:-

I have a requirement in which I want to trim the file extension for all the files contained in a directory: -

文件将像;

America.gz:2170
Europe.gz:2172
Africa.gz:2170
Asia.gz:2172

我需要从所有文件中修剪:2170和:2172,以便仅保留.gz扩展名.

what I need is to trim the :2170 and :2172 from all the files, so that only the .gz extension remains.

我知道借助下面的SED代码,可以对文件中的所有条目进行操作,但是我需要目录中的所有文件:-

I know that with the help of below SED code, it is possible for all the entries in a file, however I need for all the files in a directory: -

**sed 's/:.*//' file**

任何能解决此问题的bash或awk代码都将受到高度赞赏.

Any bash or awk code to fix this will be highly appreciated.

谢谢.

推荐答案

您可以在 bash 中进行此操作:

You can do this in bash:

shopt -s nullglob
for f in *.gz:*; do
    mv -- "$f" "${f%:*}"
done

"$ {f%:*}" 将删除变量 $ f

这篇关于整理目录中所有文件的文件扩展名的一部分-Linux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆