Usando expressão regular para rm e ls

0

PERGUNTA:

O que está acontecendo aqui e como corrigi-lo?

PROBLEMA:

Quando faço:

$ls | grep -P "file[1-9]{3}.txt|file[1-9]{2}.txt"

Eu recebo:

file11.txt
file12.txt
file13.txt
file14.txt
file15.txt
file16.txt
file17.txt
file18.txt
file19.txt
file21.txt
file22.txt
file23.txt
file24.txt
file25.txt
file26.txt
file27.txt
file28.txt
file29.txt
etc.

Mas quando eu faço:

$ls | grep -P "file[1-9]{3}.txt|file[1-9]{2}.txt" | xargs -d"\n" rm

Eu recebo:

rm: cannot remove ''$'3''[38;5;253mfile11.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile12.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile13.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile14.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile15.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile16.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile17.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile18.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile19.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile21.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile22.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile23.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile24.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile25.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile26.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile27.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile28.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile29.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile31.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile32.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile33.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile34.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile35.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile36.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile37.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile38.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile39.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile41.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile42.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile43.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile44.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile45.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile46.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile47.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile48.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile49.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile51.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile52.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile53.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile54.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile55.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile56.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile57.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile58.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile59.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile61.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile62.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile63.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile64.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile65.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile66.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile67.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile68.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile69.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile71.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile72.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile73.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile74.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile75.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile76.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile77.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile78.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile79.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile81.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile82.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile83.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile84.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile85.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile86.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile87.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile88.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile89.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile91.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile92.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile93.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile94.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile95.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile96.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile97.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile98.txt'$'3''[0m': No such file or directory
rm: cannot remove ''$'3''[38;5;253mfile99.txt'$'3''[0m': No such file or directory
etc.

RESULTADO DESEJADO:

rm removes all the files that match the regex

    
por somethingSomething 12.10.2018 / 06:14

2 respostas

6

O ls ou grep está adicionando códigos de cores; provavelmente há um alias envolvendo --color=always em algum lugar. (Com relação à subquestão, todos os aliases ativos são exibidos quando você executa alias .)

Em qualquer caso, você não deve analisar ls . Use find (a variedade GNU):

find . -maxdepth 1 -regextype posix-extended -regex '.*/file[1-9]{2,3}.txt' -delete

(Você também deve alterar seu alias para usar --color=auto .)

    
por 12.10.2018 / 06:33
1

Você também pode tentar o mesmo com rm :

rm file[1-9][1-9][1-9].txt file[1-9][1-9].txt
    
por 12.10.2018 / 07:08