1

I created wrong names for log files. Example name have structure: YYYY-MM-DD_HH:MM:SS-main-DEBUG.log

Last time I had tried to copy files from dir to usb with cp command and i got exception. I googled and i found that cp does not like special signlike ":". Similar problem I found there cp: cannot create regular file: Invalid argument

How to change names (remove :) from:

2023-06-09_22:24:01-main-DEBUG.log

to:

2023-06-09_222401-main-DEBUG.log

I have a lot of logs files and i thing bash script with regular expression will be the best. Something like there:Rename multiple files in Linux following pattern

I copied solution from link above with changes here:

#! /bin/bash
for file in *[^.]log ; do
    prefix=${??????} <--what here?
    mv "$file" "$prefix".log
done

Regular expression should remove ":" from file name. How should looks regular expression for prefix variable? I can not build it. Regex is out of my mind. Please help.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
luki
  • 197
  • 11
  • Note that the problem copying files with colons in their names is not due to `cp`. The problem is probably due to the file system on the USB drive. Windows-compatible file systems such as NTFS, FAT32, and exFAT can't handle colons (and a few other characters) in filenames. See [Allowed characters in filename](https://stackoverflow.com/q/4814040/4154375). – pjh Jun 20 '23 at 13:42
  • That can be true, because usb was formatted under windows. Previously i did not copy log files via this usb. Now I made a test and UBUNTU let me make a copy on this usb, but it change self signs ":" to "_". – luki Jun 20 '23 at 13:47

2 Answers2

3

Try

for name in *:*.log; do
    newname=${name//:/-}
    echo mv -v -n -- "$name" "$newname"
done
pjh
  • 6,388
  • 2
  • 16
  • 17
0

i found that cp does not like special signlike ":".

Incorrect. The only restriction the cp command imposes on filenames is incidental: implementations generally will not correctly handle filenames containing embedded null bytes. Which is ok, because the underlying system forbids them.

Additionally, an underlying POSIX system will recognize the forward slash character (/) as a directory separator, so you cannot use that character for other purposes. All other characters are fair game as far as the system and commands such as cp are concerned. That's why you can have the filenames you're asking about in the first place.

But some filesystems are more limited, most notably filesystems designed for DOS and Windows, such as NTFS and the various flavors of FAT. That's the situation your first link is talking about. You might run into that issue on Linux via a preformatted external storage device, as these often come with exFAT. If you have such a device that you intend to use only with Linux, then you should consider reformatting it with ext3. (Note well that this will make it unusable with Windows and Mac.)

Anyway,

How to [] remove :[] from [filenames]

I have a lot of logs files and i thing bash script with regular expression

The POSIX shell itself does not itself speak any flavor of regex, but it does have its own (non-regex) pattern syntax, and related features that will help. For example,

#! /bin/bash
for file in *:*.log; do
    mv "$file" "${file//:}"
done

The key here is this form of parameter expansion: ${file//:}.

  • The / after the parameter name introduces a substitution.
  • Doubling it says to substitute all occurrences instead of just the first.
  • The : is the (glob) pattern describing the substrings to replace. This one just represents itself.
  • If you want the replacement to be an empty string, as you do here, then that's all you need.
John Bollinger
  • 160,171
  • 8
  • 81
  • 157