Rename multiple files in bash

2019-02-23 21:15发布

I have A.js, B.js, C.js in a certain directory and I want to write a SINGLE command line in bash shell to rename these files _A, _B, _C. How can I do this?

I tried find -name '*.sh' | xargs -I file mv file basename file .sh but it doesn't work, basename file .sh isn't recognized as a nested command

标签: linux bash shell
3条回答
别忘想泡老子
2楼-- · 2019-02-23 21:56

How about

rename 's/(.*).js/_$1/' *.js

Check the syntax for rename on your system.

The above command will rename A.js to _A & so on.

If you want to retain the extension, below should help:

rename 's/(.*)/_$1/' *.js
查看更多
乱世女痞
3楼-- · 2019-02-23 22:03

A simple native way to do it, with directory traversal:

find -type f | xargs -I {} mv {} {}.txt

Will rename every file in place adding extension .txt at the end.

And a more general cool way with parallelization:

find -name "file*.p" | parallel 'f="{}" ; mv -- {} ${f:0:4}change_between${f:8}'
查看更多
Ridiculous、
4楼-- · 2019-02-23 22:09

Assuming you still want to keep the extension on the files, you could do this:

$ for f in * ; do mv "$f" _"$f" ; done

It will get the name of each file in the directory, and prepend an "_".

查看更多
登录 后发表回答