¿Cómo limpiar simplemente un nombre de archivo?

¿Cómo limpiar simplemente un nombre de archivo?

SO: Kubuntu 22.04.4 LTS x86_64

para mostrar arriba:
neofetch --stdout |grep 'OS:'

.

¿Cómo limpiar simplemente un nombre de archivo, no su contenido?
eliminando de un nombre de archivo:

  • \n nuevas líneas
  • \t pestañas
  • caracteres no imprimibles
  • espacios

para microsoft windows no < > : " \ / | ? * en el nombre del archivo.

< (less than)
> (greater than)
: (colon - sometimes works, but is actually NTFS Alternate Data Streams)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)

.

Ejemplo 1:
Cómo crear un nombre de archivo problemático.
Agregue una nueva línea a un nombre de archivo con mv
mientras está en la Terminal:

touch a

mv a $'b\nc'  # move (rename) files 
ls            # 'b'$'\n''c'
ls -b         # b\nc

con GUI, presione F2 = cambiar nombre:

b
c

.

Ejemplo 2:
nombre más largo.

touch 'This filename will have Tabs and Newlines_.txt'

mv 'This filename will have Tabs and Newlines_.txt' $'This\tfilename\twill\thave\tTabs\nand\nNewlines_.txt'  
ls            # 'This'$'\t''filename'$'\t''will'$'\t''have'$'\n''Tabs'$'\n''and'$'\n''Newlines_.txt'
ls -b         # This\tfilename\twill\thave\nTabs\nand\nNewlines_.txt

con GUI, presione F2 = cambiar nombre:

This    filename    will    have    Tabs
and
Newlines_.txt

.

Ejemplo 3:
Más involucrado.
touch 'This filename will have Tabs and Newlines & SPACES & colon: _.txt'

mv 'This filename will have Tabs and Newlines & SPACES & colon: _.txt' $'This\tfilename\twill\thave\tTabs\nand\nNewlines & SPACES & colon: _.txt'
ls         # 'This'$'\t''filename'$'\t''will'$'\t''have'$'\t''Tabs'$'\n''and'$'\n''Newlines & SPACES & colon: _.txt'
ls -b      # This\tfilename\twill\thave\tTabs\nand\nNewlines\ &\ SPACES\ &\ colon:\ _.txt

con GUI, presione F2 = cambiar nombre:

This    filename    will    have    Tabs
and
Newlines & SPACES & colon: _.txt

.

nombre de archivo de limpieza de bash en el Ejemplo 3:

#!/bin/bash   
clear
# FILE : original_filename comes from inotifywait command, On access, auto detect a file in /home/xxx/Downloads to eventually do a CLAM virus scan on FILE. 
filename1=$FILE
filename1=$'This\tfilename\twill\thave\tTabs\nand\nNewlines\ &\ SPACES\ &\ colon:\ _.txt'
echo "$filename1" 
filename2="${filename1//[$'\t'$'\n'$'\e'$'\r'$'\f'$'\v'$'\b'$'\a'$'\0']/-}"  # Replace Non Printable Characters with dash - 
echo "$filename2" 
filename3="${filename2//[$'\ ']/_}"    # Replace space with underscore _                                                   
echo "$filename3" 
filename4="${filename3//[$':']/_}"     # Replace colon : with underscore _                                                  
echo "$filename4" 

.

resultado de bash Ejemplo 3:

This    filename        will    have    Tabs
and
Newlines\ &\ SPACES\ &\ colon:\ _.txt  

This-filename-will-have-Tabs-and-Newlines\ &\ SPACES\ &\ colon:\ _.txt
This-filename-will-have-Tabs-and-Newlines__&__SPACES__&__colon:___.txt
This-filename-will-have-Tabs-and-Newlines__&__SPACES__&__colon____.txt

Se reemplazaron los caracteres no imprimibles con guión, incluidos

  • \n nuevas líneas
  • \t pestañas
    Se reemplazó el espacio con guión bajo _
    Se reemplazaron los dos puntos :con guión bajo _

.

referencia 1:
Lista completa de caracteres no imprimibles
https://fjolt.com/article/linux-non-printable-characters

Name                 Binary  Decimal  Hexadecimal  Octal  Caret     Escape
                                                          Notation  Sequence  
Null               000 0000        0           00    000     ^@           \0 
Beep(BEL)          000 0111        7           07    007     ^G           \a 
Backspace(BS)      000 1000        8           08    010     ^H           \b        
HorizontalTab(HT)  000 1001        9           09    011     ^I           \t 
LineFeed(LF)       000 1010       10           0A    012     ^J           \n 
VerticalTab(VT)    000 1011       11           0B    013     ^K           \v 
FormFeed(FF)       000 1100       12           0C    014     ^L           \f 
CarriageReturn(CR) 000 1101       13           0D    015     ^M           \r 
Escape(ESC)        001 1011       27           1B    033     ^[           \e 

9 Escape Sequences:  
\0 
\a 
\b 
\t 
\n 
\v 
\f 
\r 
\e 


used: 
column -t -o '  ' a.txt b.txt 

.

referencia 2:
https://www.linuxquestions.org/questions/general-10/remove-newline-and-tab-characters-from-filename-4175690259/

orig_filename=$'TN1\tThis\nFileName\nHas\tTabsandNewlines'
echo test > "$orig_filename"
new_filename="${orig_filename//[$'\t'$'\n']/-}"
mv --no-clobber "$orig_filename" "$new_filename"

.

referencia 3:
Expansión de parámetros de Shell
${parameter//pattern/string}
https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

Pregunta:
¿Cómo limpiar simplemente un nombre de archivo?
¿Cuáles son los comandos más simples?

--

Respuesta1

Puedes usar una aplicación comodesintoxicaciónque tiene conjuntos incorporados de lo que quizás quieras limpiar. Se les llamasecuencias. Uso de ejemplo:

detox -s safe -v /some/folder/or/file

Esto sólo utilizaría una secuencia que haga que el nombre sea más seguro para el uso de Unix. -vLa bandera detallada especifica con precisión los nombres afectados.

También puede crear sus propias secuencias combinadas en el detoxrcarchivo de configuración como se describe en la documentación anterior.

información relacionada