脚本与基本名称更改没有在linux改扩建 [英] Script to change basename with without changing extension in linux

查看:121
本文介绍了脚本与基本名称更改没有在linux改扩建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个文件, a.dat A.TXT A。 MP3 b.dat b.txt b .MP3 b.zip b.rar c.mp3 等。我想重命名基本名称的所有文件a到基名X。

I have several files with a.dat, a.txt, a.mp3, b.dat, b.txt, b.mp3, b.zip, b.rar, c.mp3 and so on. I want to rename all files with basename "a" to basename "x".

使得文件变得 x.dat x.txt x.mp3 , <$c$c>b.dat,b.txt<$c$c>,b.mp3<$c$c>,b.zip<$c$c>,b.rar<$c$c>,c.mp3`等

Such that files become x.dat, x.txt, x.mp3, b.dat,b.txt,b.mp3,b.zip,b.rar,c.mp3` and so on.

在Linux中,这可以通过终端来实现,但需要大量的输入。我想一个脚本做任务给我。

In Linux this can be done via terminal but requires lot of typing. I want a script to do the task for me.

推荐答案

我会提出一个方法,我想这可能是工作。这是一个有点怪异的方式我认为,不要笑。 10日STD数学。

I ll suggest a way, I think this could work. This is a little weird way I think, don't laugh. 10th std math.

首先使用grep命令所有的文件夹中使用ls和grep命令的组合名称

First you grep all the names in the folder using the combination of ls and grep command


  • LS | grep的^一,这将列出你所有与作为第一个字母的文件。您可以使用常规的前pression这个,如果你需要一个作为名称的文件。

  • ls | grep ^a this will list you with all the files with a as the first letter. You can use a regular expression with this if you need only files with a as the name.

读取文件名逐一while循环

Read the file names one by one with a while loop

存储的文件名到一个变量(比如$名称1)。而使用sed和awk,提取文件名的第二部分(即除去点到空间和打印第二coloumn)这些信息存储在另一个变量(例如$分机)。

Store the file name into a variable (say $name1).And using sed and awk, extract the second part of the filename(ie. remove the dots into spaces and print the second coloumn) store this in another variable (say $extn).

您可以使用存储在变量(名称1 $),指定哪些文件中的第一个名称重命名文件并使用第二个变量来指定新名的扩展名($分机)...

you can rename the files using the first name stored in the variable ($name1) for specifying which file and use the second variable to specify the extension ($extn) for the new name...

这是一个loooong路线:)我相信这会工作。尝试一下。认为这是算法和script.I很抱歉,我力提供script.Little有点懒。

This is a loooong route :) I am sure this will work. Try it. Consider this as the algorithm and script.I am sorry that I dint provide a script.Little bit lazy.

这篇关于脚本与基本名称更改没有在linux改扩建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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