Site sobre software livre, cultura, arte, gimp, inkscape, wallpapers, vim, firefox, python, e-books, algoritmos, openoffice, ubuntu, debian, gnome, inkscape, bash, shell script, música, arte, mpb, dicas em geral, Geopolítica, politica, brasil, blogsfera, ativismo, rock

Minhas dicas favoritas do Bash

O artigo original (em inglês) foi postado aqui:
My Favorite bash Tips and Tricks

$ echo {one,two,red,blue}
one two red blue

Usando um echo simples eu obteria o mesmo resultado
echo one two red blue
Mas protegendo uma lista entre chaves eu obtenho mais que isso, veja:
$ echo fish{one,two,red,blue}
fishone fishtwo fishred fishblue

$ echo {"one ","two ","red ","blue "}fish
one fish two fish red fish blue fish
Gerando backup de um arquivo
$ cp /etc/httpd/conf/httpd.conf{,.bak}

Obs: Se você usar somente a vírgula ele imprime o nome duplicado veja...

echo nome{,}

agora faça:

echo nome{,.bak}

Essa abordagem (usando a expansão de variáveis) é também usada quando usamos o comando
mkdir para criar uma extutura completa de pastas:

mkdir -p docs/{img/{fotos,icons,wallpapers,svg},textos/{artigos,man},tmp}

A regra aqui é a seguinte:

para cada pasta que conterá subpastas use "nome/{}"
dentro das chaves coloque os nomes separados por vírgula
e não esqueça de usar o parâmetro '-p' no começo do comando!

O Bash simplesmente cria o arquivo 'bak' no caminho indicado sem a necessidade de fazer algo como:
$ cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bak

O comando cp tem ainda um jeito legal para fazer backups, no qual ele preserva
as permissões, não desreferencia links simbolicos e apenas escreve o que for mais recente.

cp -uvfa /origem /destino
  • u - update
  • v - verbose (detalhado)
  • f - força
  • a - Equivale às opções -dpR do cp


Formatando a data de um jeito mais simples:
$ date +%d-%b-%Y
12-Mar-2004
Ainda não li tudo mas o artigo tem muita coisa interessante, recomendo a leitura. :)

E tem mais...


Bash Shell Shortcuts

Bash, which is the default shell in Linux contains a whole lot of key bindings which makes it really easy to use . The most commonly used shortcuts are listed below :

____________CTRL Key Bound_____________
Ctrl + a - Jump to the start of the line
Ctrl + b - Move back a char
Ctrl + c - Terminate the command
Ctrl + d - Delete from under the cursor
Ctrl + e - Jump to the end of the line
Ctrl + f - Move forward a char
Ctrl + k - Delete to EOL
Ctrl + l - Clear the screen
Ctrl + r - Search the history backwards
Ctrl + R - Search the history backwards with multi occurrence
Ctrl + u - Delete backward from cursor
Ctrl + xx - Move between EOL and current cursor position
Ctrl + x @ - Show possible hostname completions
Ctrl + z - Suspend/ Stop the command
____________ALT Key Bound___________
Alt + < - Move to the first line in the history Alt + > - Move to the last line in the history
Alt + ? - Show current completion list
Alt + * - Insert all possible completions
Alt + / - Attempt to complete filename
Alt + . - Yank last argument to previous command
Alt + b - Move backward
Alt + c - Capitalize the word
Alt + d - Delete word
Alt + f - Move forward
Alt + l - Make word lowercase
Alt + n - Search the history forwards non-incremental
Alt + p - Search the history backwards non-incremental
Alt + r - Recall command
Alt + t - Move words around
Alt + u - Make word uppercase
Alt + back-space - Delete backward from cursor

----------------More Special Keybindings-------------------

Here "2T" means Press TAB twice

$ 2T - All available commands(common)
$ (string)2T - All available commands starting with (string)
$ /2T - Entire directory structure including Hidden one
$ 2T - Only Sub Dirs inside including Hidden one
$ *2T - Only Sub Dirs inside without Hidden one
$ ~2T - All Present Users on system from "/etc/passwd"
$ $2T - All Sys variables
$ @2T - Entries from "/etc/hosts"
$ =2T - Output like ls or dir

Abrindo o ultimo comando no vim
fc

Veja também:

1 comentários:

PotHix disse...

Æ!!

Legal cara!
Eu estou usando set -o vi, portanto eu posso usar os atalhos do vim no bash ao invés dos atalhos padrão emacs =)

Há braços