How to trim file name length?

I have approximately 600 pdf files with filenames that are too long to copy to my NAS. It is encrypted and only allows file names of 143 characters or less.

Is there a command or tool that will trim the long file names down to 143 characters or less?

Manually trimming 600 files would be a daunting task so I’m hoping there is a Linux command line approach to solve this.

I am running KDE Plasma desktop.

Thanks

Dependend on the DE you are using…but Krename for Plasma is a powerfull batch renamer.

1 Like

I am running KDE Plasma but have never used Krename. I’ll look it up and see if it works for me. Thanks!

Ok, I’ve got Krename running and the first issue is how to open the 600 files. They are scattered throughout my file system so I would need a way to put all of them in one directory so I could add them.

Is there a way to find all 600 files and then put them in a specific folder?

If you don’t want to use the filepicker from Krename, you need to use the terminal to find all files first.

As example if you google “linux find all file names with length” it throws some results like this:

or

There is no program that would read your mind. To do what you want programmatically you need to define rules how to find files and rules how to rename them. If no common rules can be found, you can only do it manually one by one. If you can describe how to find these files, someone may suggest suitable find invocation.

1 Like

And to add to avidjaar’s comment. you should really find out what the restrictions of that NAS are. You talk about number of characters in the file name, but I would suggest that you may mean bytes, not characters. Remind that nowadays (well since years and years) UTF-8 is used in Linux, also for file names and that thus a character can be more then one byte.

And think about what should happen when a shortened file name is the same as one that already exists (either all the time, or shortened just a few seconds ago) in the same directory.

1 Like

You’ll have to write a script which specifically trims the file names in such a way that, only redundant information is removed from each file’s name.

  • If, the pattern to be used to trim the redundant characters is not consistent then, you’ll have to use a different script for each affected directory.

The next consideration will be the choice of scripting language to be used:

  1. Bash – possible but, recursively searching directory trees and then acting on each instance found isn’t intuitive.
  2. awk – recursively walking through directory trees is quite simple and, acting on each instance found is intuitive.
  3. Perl – see awk.

In Bash, the code to rename the files looks something like this:

> for file in ./*.pdf; do mv "$file" "${file%%-*}.pdf"; done

However, you’ll have to apply “cut -d” and “tr -d” to “file” to use only the file name characters needed.

With awk and Perl cutting away the redundant file name characters is somewhat easier because, you can cut at specific string character sequences.


BTW: <https://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits>

1 Like

I had actually already read the first one you posted but not he second. Thanks for sharing that information.

I opened up a ticket with Synology to find out exactly what you’re suggesting.

This is very helpful. Once I here back from Synology about their encryption limits I can be more precise.

I’ve never used awk so this could be a fun learning experience.

It’s archaic, cryptical and, effective – given that, you can come to terms with Regular Expressions.

  • Which is one of the reasons to, maybe, also take a look at Perl.
1 Like

Oh yes, Perl is certainly modern and easy to understand.

1 Like

I heard back from Synology and they said that normal file name size limit is 255 bytes but for encryption it is 143 bytes.

So I would need to find all files with names larger than 143 bytes and trim them to 143 bytes or less.

You’re probably looking for the truncate utility.

The “truncate” program shrinks or, extends, the SIZE of a file to a specified SIZE.

  • It doesn’t change the file name in any way whatever …

Right, I thought we’re talking about file size, but file name it is.

Again, you know much, much more then we do. I assume that there are things that makes these file names different from all other. E.g. the names end in .pdf. But I do not know if that is enough to find them out (someone already said above, the system, nor we are mind readers).

So when the only criterium is that the names end in .pdf, then

find / -name '*.pdf' 2>/dev/null

would list them all. When you want to restrict to your home directory:

find /home/akorngold -name '*.pdf' 2>/dev/null

would be enough.

And then you can of course pipe the list through some more statements, either to move them to one directory (and hope there are no double names?) or rename them in situ.

And about the renaming itself, you also should design an algoritm. Only you can decide if you want characters to be cut from the begin of the name, or at the end of the name, but before the .pdf. And what to do when names clash with others.

It is you that should do the thinking and when you have made up your mind, we can help to put that in code.

1 Like