Páginas

Showing posts with label unix. Show all posts
Showing posts with label unix. Show all posts

Wednesday, September 14, 2011

Grep e Less com cores!

Esta é uma nota mental.

egrep -r --include="*.rkt" "[a-z-]+#" . --color=always | less -R

Para usar o Grep com cores, geralmente temos um alias grep='grep --color=auto' no nosso ~/.bashrc.

Mas acontece que muitas das vezes o resultado é grande é fazemos um pipe pro less. Como triste consequência perdemos as cores...
Para forçar o grep (egrep, fgrep, ...) a exibir cores use o --color=always, assim ele manda o output pro less com cores.
A opção -R do less faz com que ele interprete os caracteres de escape de cores do terminal.

grep "alguma coisa" /caminho/da/busca --color=always | less -R

Nota: pode ser usado também com o more:

grep "alguma coisa" /caminho/da/busca --color=always | more

[Update]
Como meu amigo Breno Oliveira sugeriu, uma alternativa interessante ao grep é o ack.
http://betterthangrep.com/

Usando o ack o comando fica:

ack "regexp perl" [caminho] --color | less -R

Monday, April 19, 2010

Como encontrar arquivos usando o GNU find e ignorando alguns diretórios

Esta dica foi tirada de um comentário anônimo em http://www.linux.com/archive/feed/49304.
Fica aqui para referência futura...

Algumas vezes queremos usar o GNU find para procurar por arquivos recursivamente. O problema é quando queremos excluir alguns diretórios da busca. O jeito é usar mais de uma condição. Por exemplo:

find / -path '/proc' -prune -o -path '/dev' -prune -o -name foo.txt

Este comando procura por arquivos chamados "foo.txt" na raiz de sistema, ignorando tudo em /proc e /dev. O '-o' é um "ou" e o '-prune' faz com que o find ignore o caminho encontrado.


Original em inglês:
Sometimes you may want to exclude parts of the directory tree. It took me a while to figure out how to do this because it isn't very intuitive.
If you wanted to search for foo.txt but don't want to descend into /proc or /dev you can do this
find / -path '/proc' -prune -o -path '/dev' -prune -o -name foo.txt

The '-o' means 'or', so there in this case there are three conditions, separated by 'or', which will cause find to take action. The first two is when the path is '/proc' or '/dev' the action find will take is to prune them from the search path. The third condition is when the name is foo.txt. In this case find will print, since no other action is specified.