Skip to content
Advertisement

Renaming long file names in bulk

I have file names like:

JavaScript

I want something like:

JavaScript

Are there good ways to rename these files in linux?

Advertisement

Answer

You could try using a loop to extract the important part of the filename:

JavaScript

This will simply give you a new list of filenames. You can then move them:

JavaScript

To break this down a little:

  • loop over the *.gz files
  • create a variable which strips out the unnecessary content from the name
  • move the file name to that new name

I expect there are better ways to do this, but it’s what I came up with off the top of my head.

Test:

JavaScript

Note I’m doing this in bash 4.4.5

EDIT Given I’m not entirely sure which columns in the name are the most important, awk might work better:

JavaScript

This will split the filename by _ and allow you to reference the columns you want using $X:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement