linux - Script shell for renaming and rearranging files -
i rearrange , rename files. have tree structure of files :
ada/rda/0.05/alpha1_freesurface.md ada/rda/0.05/p_freesurface.md ada/rda/0.05/u_freesurface.md ada/rda/0.1/alpha1_freesurface.md ada/rda/0.1/p_freesurface.md ada/rda/0.1/u_freesurface.md
i want files renamed , rearranged structure below:
ada/rda/ada-0.05-alpha1.md ada/rda/ada-0.05-p.md ada/rda/ada-0.05-u.md ada/rda/ada-0.1-alpha1.md ada/rda/ada-0.1-p.md ada/rda/ada-0.1-u.md
using perl rename
(sometimes called prename
) utility:
rename 's|ada/rda/([^/]*)/([^_]*).*|ada/rda/ada-$1-$2.md|' ada/rda/*/*
(note: default, distributions install rename
command util-linux
package. command incompatible. if have such distribution, see if perl version available under name prename
.)
how works
rename
takes perl commands argument. here argument consists of single substitute command. new name file found applying substitute command old name. allows not give file new name new directory above.
in more detail, substitute command looks s|old|new|
. in our case, old
ada/rda/([^/]*)/([^_]*).*
. captures number in group 1 , beginning of filename (the part before first _
) in group 2. new
part ada/rda/ada-$1-$2.md
. creates new file name using 2 captured groups.
Comments
Post a Comment