Nowadays, linux is widely used in our daily life and work. Releasing users from managing files through tedious mouse clicks, Linux has provided great convenience for us to manage files. Especially in the case of renaming multiple files, using Linux commands to conduct batch operation for our applications is a really effective way.
Take the case of using Linux to rename pictures as an example. Suppose we have a folder containing multiple pictures whose names are in a chaotic pattern. We can use the following command to easily rename them “image_1.jpg”, “Image_2.jpg” and so on.
`cd`
`for i in {1..50} ; do mv Image.jpg Image_${i}.jpg; done`
Basically, what this command does is looping through “1 to 50” and use “mv” attribute to execute the renaming process while substituting the suffix “Image_${i}.jpg”.
But if we want to go further and customize the naming pattern, and also avoid name conflicts (e.g., Image_2.jpg already exists), we may need more complex commands:
`ls|grep “Image”|cut -d “.” -f 1|xargs -I file num=`ls|grep “Image”|grep -c “^file”`; mv file.jpg Image_${num}.jpg`
The command is composed of different segments.
`ls` is used to check all the files in the current folder, `grep “Image”` is to limit the names of files to “Image” and `cut -d “.” -f 1` is to get rid of suffix.
`xargs` converts the output of “ls|grep “Image”” as the input parameter of “num=`ls|grep “Image”|grep -c “^file”`”, which is used to generate a number after “Image_”.
At last, `mv file.jpg Image_${num}.jpg` is to complete the renaming process.
By carrying out the commands above, we can easily customize the naming patterns for our files and make them in order without conflicts. Hope this article has offered you a good insight into the way of using Linux to rename multiple files.
香港服务器选创新互联,2H2G首月10元开通。
创新互联(www.cdcxhl.com)互联网服务提供商,拥有超过10年的服务器租用、服务器托管、云服务器、虚拟主机、网站系统开发经验。专业提供云主机、虚拟主机、域名注册、VPS主机、云服务器、香港云服务器、免备案服务器等。
新闻名称:使用Linux批量重命名文件(批量重命名linux)
分享URL:http://www.mswzjz.cn/qtweb/news24/468274.html
攀枝花网站建设、攀枝花网站运维推广公司-贝锐智能,是专注品牌与效果的网络营销公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 贝锐智能