Dec 052010
 

terminalCapita spesso di trovare nei propri filesystem dei file con nomi strani, ovvero con caratteri non alfanumerici o con spazi tra le varie parole, magari caricati da utenti, o file prodotti per errore da qualcuno o da qualche programma; e purtroppo non è spesso banale fare un mv < file > o rm < file >.

Vediamo come liberarci di questi orrori.



Caso #1, file separato da spazi.

Supponiamo che abbiate:

ls -1
file2.txt
test file.txt

Per rinominare o cancellare il file “test file.txt” usate il completamento automatico della shell.
In questo semplice caso, basta scrivere test< tab > e si dovrebbe vedere il nome automaticamente completato in: test file.txt

Fatto.

Caso #2, file separato da spazi.

Supponiamo che abbiate:

ls -1
file2.txt
test file.txt
test2.txt

Si tratta di un caso molto simile al precedente, ma questa volta vedrete che il completamento automatico non riuscirà, perché ci sono 2 file che iniziano con test.
Soluzione 1 usare una apice doppio: "file.txt test" rm di lavoro (funziona anche con un apice singolo)
Soluzione 2 utilizzare un backslash prima dello spazio: rm test file.txt

Caso #3, file che inizia con un – o —

Ora avete nella vostra directory:

 ls -1
 
file2.txt
test file.txt
test2.txt
-file

Se provate ad utilizzare i soliti comandi su quel file otterrete qualcosa del tipo:

root@laptop:/tmp/test# rm -file
rm: invalid option -- 'l'

Soluzione 1: In ubuntu viene suggerita direttamente dalla shell:

root@laptop:/tmp/test# rm -file
rm: invalid option -- 'l'
Try `rm ./-file' to remove the file `-file'.

Ed effettivamente utilizzare come argomento ./-file risolve il problema.

Soluzione 2: utilizzare il parametro –, questo è usato dal molti programmi unix per dire “le flag per il comando sono finite”

Quindi è possibile utilizzare ad esempio:

rm -- -file

E questo funzionerà correttamente.
Tale logica è applicabile anche per molti altri comandi quali: ls, rm, mv e cp. In generale la regola è:

 comand options -- '--filename'

Un altro caso con cui non funzionano i metodi precedenti

Con i metodi esposti dovreste essere in grado di coprire tutte le situazioni, ma nel caso non fossero abbastanza vi suggerisco infine di utilizzare l’inode del file.

Adesso avete:

root@laptop:/tmp/test# ls -1
-file
--file
file2.txt
test2.txt
test file.txt
+Xy +8

Diciamo che l’ultimo file è quello che non riuscite a cancellare con i metodi esposti precedentemente, cerchiamo il suo inode con:

root@laptop:/tmp/test# ls -i1
285134 -file
285136 --file
3901 file2.txt
285132 test2.txt
2302 test file.txt
285461 +Xy +8

Quindi 285461 è il suo numero di inode.
Ora usiamo il comando find per eseguire un rm su quell’indode:

find . -inum 285461 -exec rm -i {} ;

Tutti i comandi sono stati testati nelle shell Bash e Zsh

Grazie a: http://www.cyberciti.biz/tips/delete-remove-files-with-inode-number.html per l’ultima soluzione.

Popular Posts:

Flattr this!

  4 Responses to “Come gestire file con strani nomi”

  1. Grazieeeee!!!!
    … con il numero di inode son riuscito ad eliminarlo il tignosissimo file vuoto

  2. For this type of advice, you /should/ label it as “A duffers guide”, or something similar. All of your suggestions are overly complicated, or have unnecessary dependencies on possibly-unavailable features.

    #1: Delete the file called “test file.txt”
    A: rm “test file.txt”
    or rm test\ file.txt

    #2: Delete the file called “test file.txt”
    A: as above.

    #3: Delete the file called “-file”
    A: rm ./-file
    or rm /full/path/to/-file

    #4: Delete the file called “\+Xy \+\8”
    A: If this is the literal name (your ls is *not* showing binary values in the name), then
    rm ‘\+Xy \+\8′
    If this is the name with binary values shown as escape sequences (“ls -b” or “ls –escape”)
    rm $’\+Xy \+\8’

    • Lol Lew,
      Thanks for the comment, i wanted to do a simple guide but seem that this time i did an Epic Fail 😉
      Still my favorite methos is to use the — flag in rm.

      Bye

  3. Thanks for that, I have on the odd occasion I have needed to delete or rename a file with a space in it been able to use filename* using the first part of the file name, but knew that would be a problem when more than 1 file name started the same.

Leave a Reply to Dave Cancel reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

*