Semplice semplice...
se do
find /home -iname *prova*|xargs rm
cancello tutti i prova,es prova.mp3 prova prova12
ma non
prova prova.txt prova com.mp3
Insomma,i nomi con spazi.
Come risolvere?
Grazie
Moderatore: Staff

Because Unix filenames can contain blanks and newlines, this default behaviour is
often problematic; filenames containing blanks and/or newlines are incorrectly
processed by xargs. In these situations it is better to use the -0 option, which
prevents such problems. When using this option you will need to ensure that the
program which produces the input for xargs also uses a null character as a separa-
tor. If that program is GNU find for example, the -print0 option does this for
you.

414N ha scritto:Come prima cosa, forse devi eseguire anche l'escape degli asterischi.
Dalla man page di xargs:
- Codice: Seleziona tutto
Because Unix filenames can contain blanks and newlines, this default behaviour is
often problematic; filenames containing blanks and/or newlines are incorrectly
processed by xargs. In these situations it is better to use the -0 option, which
prevents such problems. When using this option you will need to ensure that the
program which produces the input for xargs also uses a null character as a separa-
tor. If that program is GNU find for example, the -print0 option does this for
you.
Dovrebbe quindi bastarti l'opzione -print0 di find e il flag -0 di xargs.


spina ha scritto:Guarda che funziona. Solo che, come ti ha detto 414N, devi anche settare l'opzione '-print0' per find.
Emanuele



Meskalamdug ha scritto:L'ho usata..e ha cancellato tutto
$> ls /tmp
Serra-DonBosco
prova 1.mp3
prova casa.mp3
prova.js
sfondo.txt
$> find /tmp/ -iname "*prova*" -print0 | xargs -0 rm
$> ls -1 /tmp/
Serra-DonBosco
sfondo.txt
spina ha scritto:Meskalamdug ha scritto:L'ho usata..e ha cancellato tutto
In che senso? Prima di dare un rm devi sempre controllare l'output di find. Ma come mai ti ha cancellato tutto? Ho appena fatto una prova e a me ha funzionato correttamente:
- Codice: Seleziona tutto
$> ls /tmp
Serra-DonBosco
prova 1.mp3
prova casa.mp3
prova.js
sfondo.txt
$> find /tmp/ -iname "*prova*" -print0 | xargs -0 rm
$> ls -1 /tmp/
Serra-DonBosco
sfondo.txt
Emanuele


spina ha scritto:Sì, mi sa che l'errore è stato proprio quello. L'opzione '-print0' dice a find di stampare quello che trova, però se prima non hai filtrato i risultati con '-iname' allora ti stanpa tutto.
Il punto chiave qui è che find lavora con la catena di filtri che gli passi sulla command line, tutti i filtri sono messi in and di default e vengono usati così come glieli passi tu.
Nel mio caso tutti i risultati trovati vengono filtrati da 'iname' e quindi il sottoisieme che passa questo filtro si "scontra" con 'print0'. Nel tuo caso invece i risultati sono stampati prima ancora di essere filtrati.
Emanuele

find /path -iname pattern -delete
find /path -iname pattern -exec rm {} \;
rm file1
rm file2
rm file3rm file1 file2 file3
414N ha scritto:Non ho capito una cosa, però: perché non usi l'opzione -delete (oppure -exec rm {} \;) di find per cancellare direttamente i risultati della ricerca, senza bisogno di pipe?
- Codice: Seleziona tutto
find /path -iname pattern -delete
find /path -iname pattern -exec rm {} \;
Oppure l'intento era esercitarsi ad usare xargs?


Visitano il forum: boh e 2 ospiti