Replace space with underscore and make lowercase - filename

I am renaming files and directories. Basically, all I want to do is strip the spaces and replace them with underscores and finally make the lowercase letters. I could execute one command at a time:, $ rename "s/ /_/g" *

then the lowercase command. But I am trying to accomplish all of this in one line. Below is all I can accomplish is stripping the spaces and replacing with _

, but that doesn't lowercase. Why?

find /temp/ -depth -name "* *" -execdir rename 's/ /_/g; s,,?; ‘

      

Original filename:

test FILE   .txt

      

Result: (If there is a space at the end, remove)

test_file.txt

      

+3


source to share


2 answers


rename 's/ +\././; y/A-Z /a-z_/'

      

Or combined with find

:

find /temp/ -depth -name "* *" -exec rename 's/ +\././; y/A-Z /a-z_/' {} +

      

To list only files, not directories, add -type f

:

find /temp/ -depth -name "* *" -type f -exec rename 's/ +\././; y/A-Z /a-z_/' {} +

      



Abbreviated names

Is it possible to rename a file with the last three characters of the original file, for example from a large Dog.txt file to dog.txt?

Yes. Use this command rename

:

rename 's/ +\././; y/A-Z /a-z_/; s/[^.]*([^.]{3})/$1/'

      

+3


source


Try this line:
echo "test FILE .txt" | tr A-Z a-z | tr -s ' ' | tr ' ' ''| sed 's/././'



0


source







All Articles