rename and shorten many files with bash

Reading Time: < 1 minute

Bash supports a surprising number of string manipulation functions.

For example if you want to get rid of the beginning of the name of a file or a folder on many files at a time you could do the following:

 
bash> ls
mylongfile1
mylongfile2
mylongfile3
mylongfile4
mylongfile5
mylongfile6
mylongfile7
 
bash> for i in *
> do
> mv $i ${i:6}
> done
bash> ls
file1
file2
file3
file4
file5
file6
file7
So to extract the end of a variable after a NUMBER of characters you just need to specify ${variable:NUMBER}
 
Reading Time: < 1 minute

Bash supports a surprising number of string manipulation functions.

For example if you want to get rid of the beginning of the name of a file or a folder on many files at a time you could do the following:

 
bash> ls
mylongfile1
mylongfile2
mylongfile3
mylongfile4
mylongfile5
mylongfile6
mylongfile7
 
bash> for i in *
> do
> mv $i ${i:6}
> done
bash> ls
file1
file2
file3
file4
file5
file6
file7
So to extract the end of a variable after a NUMBER of characters you just need to specify ${variable:NUMBER}
 

By Marc Olivier Meunier

Marc has spent the past few years putting oil on the fire of growth companies. He leads by example and puts a lot of emphasis on inclusion, constantly working to create a safe environment. A warm leader with a passion for memorable experiences and innovation.
Marc is available for contracting as a fractional Head of Customer Experience.
Find Marc on Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *