Capítulo 1. Manuais de GNU/Linux

Índice

1.1. Bases de consola
1.1.1. A linha de comandos shell
1.1.2. A linha de comandos shell sob X
1.1.3. A conta root
1.1.4. A linha de comandos shell do root
1.1.5. GUIs de ferramentas de administração do sistema
1.1.6. Consolas virtuais
1.1.7. Como abandonar a linha de comandos
1.1.8. Como desligar o sistema
1.1.9. Recuperar uma consola sã
1.1.10. Sugestões de pacote adicionais para o novato
1.1.11. Uma conta de utilizador extra
1.1.12. configuração do sudo
1.1.13. Hora de brincar
1.2. Sistema de ficheiros tipo Unix
1.2.1. Noções básicas de ficheiros Unix.
1.2.2. Internos do sistema de ficheiros
1.2.3. Permissões do sistema de ficheiros
1.2.4. Control de permissões para ficheiros acabados de criar: umask
1.2.5. Permissões para grupos de utilizadores (group)
1.2.6. Marcas temporais (Timestamps)
1.2.7. Links (ligações)
1.2.8. Pipes nomeados (FIFOs)
1.2.9. Sockets
1.2.10. Ficheiros de dispositivo
1.2.11. Ficheiros de dispositivos especiais
1.2.12. procfs e sysfs
1.3. Midnight Commander (MC)
1.3.1. Personalização do MC
1.3.2. Arrancar o MC
1.3.3. Gestor de ficheiros em MC
1.3.4. Truques de linha de comandos no MC
1.3.5. O editor interno em MC
1.3.6. O visualizador interno no MC
1.3.7. Funcionalidades de auto-arranque do MC
1.3.8. Sistema de ficheiros FTP virtual no MC
1.4. O ambiente de trabalho estilo Unix básico
1.4.1. A shell de login
1.4.2. Personalizar o bash
1.4.3. Teclas especiais
1.4.4. Operações do rato ao estilo Unix
1.4.5. O pager
1.4.6. O editor de texto
1.4.7. Definir um editor de texto predefinido
1.4.8. Personalizar o vim
1.4.9. Gravar as actividades da shell
1.4.10. Comandos Unix básicos
1.5. O simples comando de shell
1.5.1. Execução de comando e variável de ambiente
1.5.2. variável "$LANG"
1.5.3. variável "$PATH"
1.5.4. variável "$HOME"
1.5.5. Opções de linha de comandos
1.5.6. Glob da shell
1.5.7. Valor de retorno do comando
1.5.8. Sequências de comandos típicas e redireccionamento da shell
1.5.9. Command alias
1.6. Processamento de texto estilo Unix
1.6.1. Ferramentas de texto de Unix
1.6.2. Expressões regulares
1.6.3. Expressões de substituição
1.6.4. Substituição global com expressões regulares
1.6.5. Extrair dados de tabela de ficheiro de texto
1.6.6. Script snippets for piping commands

I think learning a computer system is like learning a new foreign language. Although tutorial books and documentation are helpful, you have to practice it yourself. In order to help you get started smoothly, I elaborate a few basic points.

The powerful design of Debian GNU/Linux comes from the Unix operating system, i.e., a multiuser, multitasking operating system. You must learn to take advantage of the power of these features and similarities between Unix and GNU/Linux.

Don't shy away from Unix oriented texts and don't rely solely on GNU/Linux texts, as this robs you of much useful information.

[Nota] Nota

If you have been using any Unix-like system for a while with command line tools, you probably know everything I explain here. Please use this as a reality check and refresher.

1.1. Bases de consola

1.1.1. A linha de comandos shell

Upon starting the system, you are presented with the character based login screen if you did not install X Window System with the display manager such as gdm. Suppose your hostname is foo, the login prompt looks as follows.

login de foo:

If you did install a GUI environment such as GNOME or KDE, then you can get to a login prompt by Ctrl-Alt-F1, and you can return to the GUI environment via Alt-F7 (see Secção 1.1.6, “Consolas virtuais” below for more).

No aviso de login, você escreve o seu nome de utilizador, ex pinguim, e carrega na tecla Enter, depois escreve a sua palavra-passe e carrega na tecla Enter mais uma vez.

[Nota] Nota

Seguindo a tradição do Unix, o nome de utilizador e palavra-passe do sistema Debian são sensíveis a maiúsculas/minúsculas. O nome de utilizador é geralmente escolhido apenas em minúsculas. A primeira conta de utilizador é geralmente criada durante a instalação. Podem ser criadas contas de utilizador adicionais com adduser(8) pelo root.

O sistema inicia com a mensagem de boas vindas armazenada em "/etc/motd" (Mensagem do Dia) e apresenta um aviso de comando.

Debian GNU/Linux lenny/sid foo tty1
foo login: pinguim
Password:
Last login: Sun Apr 22 09:29:34 2007 on tty1
Linux snoopy 2.6.20-1-amd64 #1 SMP Sun Apr 15 20:25:49 UTC 2007 x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
foo:~$

Aqui, a parte principal da mensagem de boas vindas pode ser personalizada ao editar o ficheiro "/etc/motd.tail". A primeira linha é gerada a partir de informação do sistema usando "uname -snrvm".

Agora você está na shell. A shell interpreta os seus comandos.

1.1.2. A linha de comandos shell sob X

If you installed X Window System with a display manager such as GNOME's gdm by selecting "Desktop environment" task during the installation, you are presented with the graphical login screen upon starting your system. You type your username and your password to login to the non-privileged user account. Use tab to navigate between username and password, or use the mouse and primary click.

You can gain the shell prompt under X by starting a x-terminal-emulator program such as gnome-terminal(1), rxvt(1) or xterm(1). Under the GNOME Desktop environment, clicking "Applications" → "Accessories" → "Terminal" does the trick.

Pode ver também a secção abaixo Secção 1.1.6, “Consolas virtuais”.

Under some other Desktop systems (like fluxbox), there may be no obvious starting point for the menu. If this happens, just try (right) clicking the center of the screen and hope for a menu to pop-up.

1.1.3. A conta root

The root account is also called superuser or privileged user. From this account, you can perform the following system administration tasks.

  • Lê, escreve e remove quaisquer ficheiros no sistema independentemente das suas permissões
  • Define o dono e permissões de quaisquer ficheiros no sistema
  • Define a palavra-passe de quaisquer utilizadores não privilegiados do sistema.
  • Login em quaisquer contas sem as suas palavras-passe

Este poder ilimitado da conta root querer que você seja atencioso e responsável quando a usa.

[Atenção] Atenção

Nunca partilhe a palavra-passe de root com outros.

[Nota] Nota

File permissions of a file (including hardware devices such as CD-ROM etc. which are just another file for the Debian system) may render it unusable or inaccessible by non-root users. Although the use of root account is a quick way to test this kind of situation, its resolution should be done through proper setting of file permissions and user's group membership (see Secção 1.2.3, “Permissões do sistema de ficheiros”).

1.1.4. A linha de comandos shell do root

Aqui estão alguns métodos básicos de ganhar o aviso de shell de root ao usar a palavra-passe do root.

  • Escreva root no aviso de login baseado em caracteres.
  • Clique "Aplicações" → "Acessórios" → "Terminal de Root", sob o ambiente de trabalho GNOME.
  • Escreva "su -l" no aviso de shell de qualquer utilizador.

    • Isto não preserva o ambiente do utilizador actual.
  • Escreva "su" no aviso de shell de qualquer utilizador.

    • Isto preserva algum do ambiente do utilizador actual.

1.1.5. GUIs de ferramentas de administração do sistema

When your desktop menu does not start GUI system administration tools automatically with the appropriate privilege, you can start them from the root shell prompt of the X terminal emulator, such as gnome-terminal(1), rxvt(1), or xterm(1). See Secção 1.1.4, “A linha de comandos shell do root” and Secção 7.8.4, “Correr clientes X como root”.

[Atenção] Atenção

Never start the X display/session manager under the root account by typing in root to the prompt of the display manager such as gdm(1).

[Atenção] Atenção

Never run untrusted remote GUI program under X Window when critical information is displayed since it may eavesdrop your X screen.

1.1.6. Consolas virtuais

In the default Debian system, there are six switchable VT100-like character consoles available to start the command shell directly on the Linux host. Unless you are in a GUI environment, you can switch between the virtual consoles by pressing the Left-Alt-key and one of the F1F6 keys simultaneously. Each character console allows independent login to the account and offers the multiuser environment. This multiuser environment is a great Unix feature, and very addictive.

If you are under the X Window System, you gain access to the character console 1 by pressing Ctrl-Alt-F1 key, i.e., the left-Ctrl-key, the left-Alt-key, and the F1-key are pressed together. You can get back to the X Window System, normally running on the virtual console 7, by pressing Alt-F7.

Você pode alternativamente mudar para outra consola virtual, por exemplo para a consola 1, a partir da linha de comandos.

# chvt 1

1.1.7. Como abandonar a linha de comandos

You type Ctrl-D, i.e., the left-Ctrl-key and the d-key pressed together, at the command prompt to close the shell activity. If you are at the character console, you return to the login prompt with this. Even though these control characters are referred as "control D" with the upper case, you do not need to press the Shift-key. The short hand expression, ^D, is also used for Ctrl-D. Alternately, you can type "exit".

Se você está no emulador-terminal-x(1), você pode fechar a janela do emulador-terminal-x com isto.

1.1.8. Como desligar o sistema

Just like any other modern OS where the file operation involves caching data in memory for improved performance, the Debian system needs the proper shutdown procedure before power can safely be turned off. This is to maintain the integrity of files, by forcing all changes in memory to be written to disk. If the software power control is available, the shutdown procedure automatically turns off power of the system. (Otherwise, you may have to press power button for few seconds after the shutdown procedure.)

Você pode desligar o sistema sob o modo normal de multi-utilizador a partir da linha de comandos.

# shutdown -h now

Você pode desligar o sistema sob o modo único-utilizador a partir da linha de comandos.

# poweroff -i -f

Alternatively, you may type Ctrl-Alt-Delete (The left-Ctrl-key, the left-Alt-Key, and the Delete are pressed together) to shutdown if "/etc/inittab" contains "ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -h now" in it. See inittab(5) for details.

Veja Secção 6.9.6, “Como desligar o sistema remoto em SSH”.

1.1.9. Recuperar uma consola sã

When the screen goes berserk after doing some funny things such as "cat <some-binary-file>", type "reset" at the command prompt. You may not be able to see the command echoed as you type. You may also issue "clear" to clean up the screen.

1.1.10. Sugestões de pacote adicionais para o novato

Although even the minimal installation of the Debian system without any desktop environment tasks provides the basic Unix functionality, it is a good idea to install few additional commandline and curses based character terminal packages such as mc and vim with apt-get(8) for beginners to get started by the following.

# apt-get update
 ...
# apt-get install mc vim sudo
 ...

Se você já tiver estes pacotes instalados, não serão instalados novos pacotes.

Tabela 1.1. Lista de pacotes de programas de modo de texto interessantes

pacote popcon tamanho descrição
mc * V:12, I:28 6508 Um gestor de ficheiro de écran completo em modo de texto
sudo * V:42, I:71 668 Um programa que permite privilégios de root limitados aos utilizadores
vim * V:15, I:33 1792 O editor de texto de Unix Vi IMproved, um editor de texto de programadores (versão standard)
vim-tiny * V:16, I:92 776 O editor de texto de Unix Vi IMproved, um editor de texto de programadores (versão compacta)
emacs23 * V:3, I:4 13016 O Projecto Emacs do GNU, o editor de texto extensível baseado no Lisp (versão 23)
w3m * V:24, I:84 1992 Exploradores WWW de modo de texto
gpm * V:3, I:4 484 O cortar-e-colar estilo Unix na consola de texto (daemon)

Pode ser uma boa ideia ler algumas documentações informativas.

Tabela 1.2. Lista de pacotes de documentação informativa

pacote popcon tamanho descrição
doc-debian * I:82 408 Documentação do Projecto Debian, (FAQ da Debian) e outros documentos
debian-policy * I:3 3500 Manual de Politicas Debian e documentos relacionados
developers-reference * I:1.0 1388 Guias e informação para programadores de Debian
maint-guide * I:0.7 776 Guia dos Novos Maintainers de Debian
debian-history * I:0.3 3736 História do Projecto Debian
debian-faq * I:66 1224 FAQ da Debian
doc-linux-text * I:82 8616 Manuais de Linux e FAQ (texto)
doc-linux-html * I:0.7 62564 Manuais de Linux e FAQ (html)
sysadmin-guide * I:0.2 964 O Guia do Administrador de Sistema Debian

Você pode instalar alguns destes pacotes com o seguinte.

# apt-get install nome_do_pacote

1.1.11. Uma conta de utilizador extra

Se você não deseja usar a sua conta de utilizador principal para as seguintes actividades de treino, você pode criar uma conta de utilizador para treinos, por exemplo fish fazendo o seguinte.

# adduser fish

Responder a todas as questões.

Isto cria uma nova conta chamada fish. Após praticar, você pode remover esta conta de utilizador e o seu directório home fazendo o seguinte.

# deluser --remove-home fish

1.1.12. configuração do sudo

For the typical single user workstation such as the desktop Debian system on the laptop PC, it is common to deploy simple configuration of sudo(8) as follows to let the non-privileged user, e.g. penguin, to gain administrative privilege just with his user password but without the root password.

# echo "penguin  ALL=(ALL) ALL" >> /etc/sudoers

Alternativamente, é também comum fazer como se segue para permitir a um utilizador não privilegiado, ex. penguin, ganhar privilégios administrativos sem qualquer palavra-passe.

# echo "penguin  ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

Este truque só deve ser usado na estação de trabalho de um único utilizador o qual você administra e onde você é o único utilizador.

[Atenção] Atenção

Não configure assim as contas de utilizadores normais numa estação de trabalho de multi-utilizadores porque seria muito mau para a segurança do sistema.

[Cuidado] Cuidado

The password and the account of the penguin in the above example requires as much protection as the root password and the root account.

[Cuidado] Cuidado

Administrative privilege in this context belongs to someone authorized to perform the system administration task on the workstation. Never give some manager in the Admin department of your company or your boss such privilege unless they are authorized and capable.

[Nota] Nota

For providing access privilege to limited devices and limited files, you should consider to use group to provide limited access instead of using the root privilege via sudo(8).

[Nota] Nota

With more thoughtful and careful configuration, sudo(8) can grant limited administrative privileges to other users on a shared system without sharing the root password. This can help with accountability with hosts with multiple administrators so you can tell who did what. On the other hand, you might not want anyone else to have such privileges.

1.1.13. Hora de brincar

Agora você está pronto para brincar com o sistema Debian sem riscos desde que use a conta de utilizador sem-privilégios.

This is because the Debian system is, even after the default installation, configured with proper file permissions which prevent non-privileged users from damaging the system. Of course, there may still be some holes which can be exploited but those who worry about these issues should not be reading this section but should be reading Securing Debian Manual.

Nós aprendemos o sistema Debia com um sistema tipo Unix com o seguinte.

1.2. Sistema de ficheiros tipo Unix

In GNU/Linux and other Unix-like operating systems, files are organized into directories. All files and directories are arranged in one big tree rooted at "/". It's called a tree because if you draw the filesystem, it looks like a tree but it is upside down.

These files and directories can be spread out over several devices. mount(8) serves to attach the filesystem found on some device to the big file tree. Conversely, umount(8) detaches it again. On recent Linux kernels, mount(8) with some options can bind part of a file tree somewhere else or can mount filesystem as shared, private, slave, or unbindable. Supported mount options for each filesystem are available in "/share/doc/linux-doc-2.6.*/Documentation/filesystems/".

Directories on Unix systems are called folders on some other systems. Please also note that there is no concept for drive such as "A:" on any Unix system. There is one filesystem, and everything is included. This is a huge advantage compared to Windows.

1.2.1. Noções básicas de ficheiros Unix.

Aqui estão algumas noções básicas de ficheiros Unix.

  • Os nomes de ficheiro são sensíveis a maiúsculas/minúsculas. Isto é, "MEUFICHEIRO" e "MeuFicheiro" são ficheiros diferentes.
  • The root directory means root of the filesystem referred as simply "/". Don't confuse this with the home directory for the root user: "/root".
  • Every directory has a name which can contain any letters or symbols except "/". The root directory is an exception; its name is "/" (pronounced "slash" or "the root directory") and it cannot be renamed.
  • Each file or directory is designated by a fully-qualified filename, absolute filename, or path, giving the sequence of directories which must be passed through to reach it. The three terms are synonymous.
  • All fully-qualified filenames begin with the "/" directory, and there's a "/" between each directory or file in the filename. The first "/" is the top level directory, and the other "/"'s separate successive subdirectories, until we reach the last entry which is the name of the actual file. The words used here can be confusing. Take the following fully-qualified filename as an example: "/usr/share/keytables/us.map.gz". However, people also refers to its basename "us.map.gz" alone as a filename.
  • The root directory has a number of branches, such as "/etc/" and "/usr/". These subdirectories in turn branch into still more subdirectories, such as "/etc/init.d/" and "/usr/local/". The whole thing viewed collectively is called the directory tree. You can think of an absolute filename as a route from the base of the tree ("/") to the end of some branch (a file). You also hear people talk about the directory tree as if it were a family tree: thus subdirectories have parents, and a path shows the complete ancestry of a file. There are also relative paths that begin somewhere other than the root directory. You should remember that the directory "../" refers to the parent directory. This terminology also applies to other directory like structures, such as hierarchical data structures.
  • There's no special directory path name component that corresponds to a physical device, such as your hard disk. This differs from RT-11, CP/M, OpenVMS, MS-DOS, AmigaOS, and Microsoft Windows, where the path contains a device name such as "C:\". (However, directory entries do exist that refer to physical devices as a part of the normal filesystem. See Secção 1.2.2, “Internos do sistema de ficheiros”.)
[Nota] Nota

While you can use almost any letters or symbols in a file name, in practice it is a bad idea to do so. It is better to avoid any characters that often have special meanings on the command line, including spaces, tabs, newlines, and other special characters: { } ( ) [ ] ' ` " \ / > < | ; ! # & ^ * % @ $ . If you want to separate words in a name, good choices are the period, hyphen, and underscore. You could also capitalize each word, "LikeThis". Experienced Linux users tend to avoid spaces in filenames.

[Nota] Nota

A palavra "root" pode significar o "utilizador root" ou o "directório raiz (root)". O contexto da sua utilização deve torná-lo claro.

[Nota] Nota

The word path is used not only for fully-qualified filename as above but also for the command search path. The intended meaning is usually clear from the context.

The detailed best practices for the file hierarchy are described in the Filesystem Hierarchy Standard ("/usr/share/doc/debian-policy/fhs/fhs-2.3.txt.gz" and hier(7)). You should remember the following facts as the starter.

Tabela 1.3. Lista de utilização de directórios chave

directório utilização do directório
/ o directório raiz
/etc/ ficheiros de configuração de todo o sistema
/var/log/ ficheiros log do sistema
/home/ todos os directórios home de todos os utilizadores não privilegiados.

1.2.2. Internos do sistema de ficheiros

Following the Unix tradition, the Debian GNU/Linux system provides the filesystem under which physical data on hard disks and other storage devices reside, and the interaction with the hardware devices such as console screens and remote serial consoles are represented in an unified manner under "/dev/".

Each file, directory, named pipe (a way two programs can share data), or physical device on a Debian GNU/Linux system has a data structure called an inode which describes its associated attributes such as the user who owns it (owner), the group that it belongs to, the time last accessed, etc. If you are really interested, see "/usr/include/linux/fs.h" for the exact definition of "struct inode" in the Debian GNU/Linux system. The idea of representing just about everything in the filesystem was a Unix innovation, and modern Linux kernels have developed this idea ever further. Now, even information about processes running in the computer can be found in the filesystem.

This abstract and unified representation of physical entities and internal processes is very powerful since this allows us to use the same command for the same kind of operation on many totally different devices. It is even possible to change the way the kernel works by writing data to special files that are linked to running processes.

[Dica] Dica

If you need to identify the correspondence between the file tree and the physical entity, execute mount(8) with no arguments.

1.2.3. Permissões do sistema de ficheiros

Filesystem permissions of Unix-like system are defined for three categories of affected users.

  • O utilizador que é dono do ficheiro (u)
  • Other users in the group which the file belongs to (g)
  • All other users (o) also referred to as "world" and "everyone"

Para o ficheiro, cada permissão correspondente permite as seguintes acções.

  • A permissão read (r) permite ao dono examinar o conteúdo do ficheiro.
  • A permissão write (w) permite ao dono modificar o ficheiro.
  • A permissão execute (x) permite ao dono correr o ficheiro como um comando.

Para o directório, cada permissão correspondente permite as seguintes acções.

  • A permissão read (r) permite ao dono listar o conteúdo do directório.
  • A permissão write (w) permite ao dono adicionar ou remover ficheiros no directório.
  • A permissão execute (x) permite ao dono aceder aos ficheiro no directório.

Here, the execute permission on a directory means not only to allow reading of files in that directory but also to allow viewing their attributes, such as the size and the modification time.

ls(1) is used to display permission information (and more) for files and directories. When it is invoked with the "-l" option, it displays the following information in the order given.

  • Tipo de ficheiro (primeiro caractere)
  • Permissão de acesso do ficheiro (nove caracteres, consistindo de três caracteres cada para utilizador, grupo, e os outros por esta ordem)
  • Número de hard links para o ficheiro
  • Nome do utilizador que é dono do ficheiro
  • Nome do grupo ao qual o ficheiro pertence
  • Tamanho do ficheiro em caracteres (bytes)
  • Data e hora do ficheiro (mtime)
  • Nome do ficheiro

Tabela 1.4. Lista do primeiro caractere da saída de "ls -l"

caractere significado
- ficheiro normal
d directório
l link simbólico
c nó de dispositivo de caractere
b nó de dispositivo de bloco
p pipe nomeado
s socket

chown(1) is used from the root account to change the owner of the file. chgrp(1) is used from the file's owner or root account to change the group of the file. chmod(1) is used from the file's owner or root account to change file and directory access permissions. Basic syntax to manipulate a foo file is the following.

# chown <novo_dono> foo
# chgrp <novo_grupo> foo
# chmod  [ugoa][+-=][rwxXst][,...] foo

For example, you can make a directory tree to be owned by a user foo and shared by a group bar by the following.

# cd /qualquer/localização/
# chown -R foo:bar .
# chmod -R ug+rwX,o=rX .

Existem mais três bits de permissão especiais.

  • The set user ID bit (s or S instead of user's x)
  • The set group ID bit (s or S instead of group's x)
  • The sticky bit (t or T instead of other's x)

Here the output of "ls -l" for these bits is capitalized if execution bits hidden by these outputs are unset.

Setting set user ID on an executable file allows a user to execute the executable file with the owner ID of the file (for example root). Similarly, setting set group ID on an executable file allows a user to execute the executable file with the group ID of the file (for example root). Because these settings can cause security risks, enabling them requires extra caution.

Setting set group ID on a directory enables the BSD-like file creation scheme where all files created in the directory belong to the group of the directory.

Setting the sticky bit on a directory prevents a file in the directory from being removed by a user who is not the owner of the file. In order to secure contents of a file in world-writable directories such as "/tmp" or in group-writable directories, one must not only reset the write permission for the file but also set the sticky bit on the directory. Otherwise, the file can be removed and a new file can be created with the same name by any user who has write access to the directory.

Aqui estão alguns exemplos interessantes de permissões de ficheiros.

$ ls -l /etc/passwd /etc/shadow /dev/ppp /usr/sbin/exim4
crw------- 1 root root   108, 0 2007-04-29 07:00 /dev/ppp
-rw-r--r-- 1 root root     1427 2007-04-16 00:19 /etc/passwd
-rw-r----- 1 root shadow    943 2007-04-16 00:19 /etc/shadow
-rwsr-xr-x 1 root root   700056 2007-04-22 05:29 /usr/sbin/exim4
$ ls -ld /tmp /var/tmp /usr/local /var/mail /usr/src
drwxrwxrwt 10 root root  4096 2007-04-29 07:59 /tmp
drwxrwsr-x 10 root staff 4096 2007-03-24 18:48 /usr/local
drwxrwsr-x  4 root src   4096 2007-04-27 00:31 /usr/src
drwxrwsr-x  2 root mail  4096 2007-03-28 23:33 /var/mail
drwxrwxrwt  2 root root  4096 2007-04-29 07:11 /var/tmp

There is an alternative numeric mode to describe file permissions with chmod(1). This numeric mode uses 3 to 4 digit wide octal (radix=8) numbers.

Tabela 1.5. O modo numérico para permissões de ficheiros em comandos chmod(1)

digito significado
1º digito opcional sum of set user ID (=4), set group ID (=2), and sticky bit (=1)
2º digito sum of read (=4), write (=2), and execute (=1) permissions for user
3º digito idem para grupo
4º digito idem para outros

This sounds complicated but it is actually quite simple. If you look at the first few (2-10) columns from "ls -l" command output and read it as a binary (radix=2) representation of file permissions ("-" being "0" and "rwx" being "1"), the last 3 digit of the numeric mode value should make sense as an octal (radix=8) representation of file permissions to you.

Por exemplo, tente o seguinte

$ touch foo bar
$ chmod u=rw,go=r foo
$ chmod 644 bar
$ ls -l foo bar
-rw-r--r-- 1 penguin penguin 17 2007-04-29 08:22 bar
-rw-r--r-- 1 penguin penguin 12 2007-04-29 08:22 foo
[Dica] Dica

If you need to access information displayed by "ls -l" in shell script, you should use pertinent commands such as test(1), stat(1) and readlink(1). The shell builtin such as "[" or "test" may be used too.

1.2.4. Control de permissões para ficheiros acabados de criar: umask

What permissions are applied to a newly created file or directory is restricted by the umask shell builtin command. See dash(1), bash(1), and builtins(7).

 (permissões de ficheiros) = (permissões de ficheiros pedidas) & ~(valor umask)

Tabela 1.6. Exemplos do valor umask

umask permissões do ficheiro criadas permissões do directório criadas utilização
0022 -rw-r--r-- -rwxr-xr-x apenas pode ser escrito pelo utilizador
0002 -rw-rw-r-- -rwxrwxr-x pode ser escrito pelo grupo

The Debian system uses a user private group (UPG) scheme as its default. A UPG is created whenever a new user is added to the system. A UPG has the same name as the user for which it was created and that user is the only member of the UPG. UPG scheme makes it is safe to set umask to 0002 since every user has their own private group. (In some Unix variants, it is quite common to setup all normal users belonging to a single users group and is good idea to set umask to 0022 for security in such cases.)

1.2.5. Permissões para grupos de utilizadores (group)

In order to make group permissions to be applied to a particular user, that user needs to be made a member of the group using "sudo vigr".

[Nota] Nota

Alternatively, you may dynamically add users to groups during the authentication process by adding "auth optional pam_group.so" line to "/etc/pam.d/common-auth" and setting "/etc/security/group.conf". (See Capítulo 4, Autenticação.)

Os dispositivos de hardware são apenas outro tipo de ficheiros no sistema Debian. Se tiver problemas a aceder a dispositivos como o CD-ROM e memórias USB a partir de uma conta de utilizador, você deve tornar esse utilizador um membro do grupo relevante.

Alguns grupos notáveis disponibilizados pelo sistema permitem aos seus membros aceder a ficheiros e dispositivos particulares sem privilégios de root.

Tabela 1.7. Lista de grupos notáveis disponibilizados pelo sistema para acesso a ficheiros

grupo descrição para ficheiros e dispositivos acessíveis
dialout acesso completo e directo a portas série ("/dev/ttyS[0-3]")
dip Acesso limitado a portas série para ligação Dialup IP a peers de confiança
cdrom drives CD-ROM, DVD+/-RW
audio dispositivo de áudio
video dispositivo de vídeo
scanner scanner(s)
adm logs (relatórios) de monitorização do sistema
staff alguns directórios para trabalho administrativo júnior: "/usr/local", "/home"

[Dica] Dica

You need to belong to the dialout group to reconfigure modem, dial anywhere, etc. But if root creates pre-defined configuration files for trusted peers in "/etc/ppp/peers/", you only need to belong to the dip group to create Dialup IP connection to those trusted peers using pppd(8), pon(1), and poff(1) commands.

Alguns grupos notáveis disponibilizados pelo sistema permitem aos seus membros executar comandos particulares sem privilégios de root.

Tabela 1.8. Lista de grupos notáveis disponibilizados pelo sistema para execuções de comandos particulares

grupo comandos acessíveis
sudo executa sudo sem a sua palavra-passe
lpadmin executa comandos para adicionar, modificar e remover impressoras das bases de dados de impressoras
plugdev executa pmount(1) para dispositivos amovíveis tal como as memórias USB

For the full listing of the system provided users and groups, see the recent version of the "Users and Groups" document in "/usr/share/doc/base-passwd/users-and-groups.html" provided by the base-passwd package.

Veja passwd(5), group(5), shadow(5), newgrp(1), vipw(8), vigr(8), and pam_group(8) para comandos de gestão para o sistema de utilizador e grupo.

1.2.6. Marcas temporais (Timestamps)

Existem três tipos de marcas temporais para um ficheiro de GNU/Linux.

Tabela 1.9. Lista dos tipos de marcas temporais

tipo significado
mtime a hora de modificação do ficheiro (ls -l)
ctime a hora de alteração de estado do ficheiro (ls -lc)
atime a hora do último acesso ao ficheiro (ls -lu)

[Nota] Nota

ctime não é o tempo de criação do ficheiro.

  • Overwriting a file changes all of the mtime, ctime, and atime attributes of the file.
  • Changing ownership or permission of a file changes the ctime and atime attributes of the file.
  • Ler um ficheiro altera o atime do ficheiro.
[Nota] Nota

Even simply reading a file on the Debian system normally causes a file write operation to update atime information in the inode. Mounting a filesystem with "noatime" or "relatime" option makes the system skip this operation and results in faster file access for the read. This is often recommended for laptops, because it reduces hard drive activity and saves power. See mount(8).

Use o comando touch(1) para alterar as marcas temporais de ficheiros existentes.

For timestamps, the ls command outputs different strings under the modern English locale ("en_US.UTF-8") from under the old one ("C").

$ LANG=en_US.UTF-8  ls -l foo
-rw-r--r-- 1 penguin penguin 3 2008-03-05 00:47 foo
$ LANG=C  ls -l foo
-rw-r--r-- 1 penguin penguin 3 Mar  5 00:47 foo
[Dica] Dica

Veja Secção 9.2.5, “Amostragem personalizada de hora e data” para personalizar a saída do "ls -l".

1.2.7. Links (ligações)

Existem dois métodos de associar um ficheiro "foo" com um nome de ficheiro diferente "bar".

Veja o seguinte exemplo para alterações nas contagens do link e as diferenças subtis nos resultados do comando rm.

$ echo "Conteúdo Original" > foo
$ ls -li foo
2398521 -rw-r--r-- 1 penguin penguin 17 2007-04-29 08:15 foo
$ ln foo bar     # hard link
$ ln -s foo baz  # symlink
$ ls -li foo bar baz
2398521 -rw-r--r-- 2 penguin penguin 17 2007-04-29 08:15 bar
2398538 lrwxrwxrwx 1 penguin penguin  3 2007-04-29 08:16 baz -> foo
2398521 -rw-r--r-- 2 penguin penguin 17 2007-04-29 08:15 foo
$ rm foo
$ echo "Novo Conteúdo" > foo
$ ls -li foo bar baz
2398521 -rw-r--r-- 1 penguin penguin 17 2007-04-29 08:15 bar
2398538 lrwxrwxrwx 1 penguin penguin  3 2007-04-29 08:16 baz -> foo
2398540 -rw-r--r-- 1 penguin penguin 12 2007-04-29 08:17 foo
$ cat bar
Conteúdo Original
$ cat baz
Novo Conteúdo

The hardlink can be made within the same filesystem and shares the same inode number which the "-i" option with ls(1) reveals.

The symlink always has nominal file access permissions of "rwxrwxrwx", as shown in the above example, with the effective access permissions dictated by permissions of the file that it points to.

[Cuidado] Cuidado

It is generally good idea not to create complicated symbolic links or hardlinks at all unless you have a very good reason. It may cause nightmares where the logical combination of the symbolic links results in loops in the filesystem.

[Nota] Nota

Geralmente é preferível usar links simbólicos em vez de hard links, a menos que tenha boas razões para usar um hardlink.

The "." directory links to the directory that it appears in, thus the link count of any new directory starts at 2. The ".." directory links to the parent directory, thus the link count of the directory increases with the addition of new subdirectories.

If you are just moving to Linux from Windows, it soon becomes clear how well-designed the filename linking of Unix is, compared with the nearest Windows equivalent of "shortcuts". Because it is implemented in the filesystem, applications can't see any difference between a linked file and the original. In the case of hardlinks, there really is no difference.

1.2.8. Pipes nomeados (FIFOs)

A named pipe is a file that acts like a pipe. You put something into the file, and it comes out the other end. Thus it's called a FIFO, or First-In-First-Out: the first thing you put in the pipe is the first thing to come out the other end.

If you write to a named pipe, the process which is writing to the pipe doesn't terminate until the information being written is read from the pipe. If you read from a named pipe, the reading process waits until there is nothing to read before terminating. The size of the pipe is always zero --- it does not store data, it just links two processes like the shell "|". However, since this pipe has a name, the two processes don't have to be on the same command line or even be run by the same user. Pipes were a very influential innovation of Unix.

Por exemplo, tente o seguinte

$ cd; mkfifo mypipe
$ echo "hello" >mypipe & # put into background
[1] 8022
$ ls -l mypipe
prw-r--r-- 1 penguin penguin 0 2007-04-29 08:25 mypipe
$ cat mypipe
hello
[1]+  Done                    echo "hello" >mypipe
$ ls mypipe
mypipe
$ rm mypipe

1.2.9. Sockets

Sockets are used extensively by all the Internet communication, databases, and the operating system itself. It is similar to the named pipe (FIFO) and allows processes to exchange information even between different computers. For the socket, those processes do not need to be running at the same time nor to be running as the children of the same ancestor process. This is the endpoint for the inter process communication (IPC). The exchange of information may occur over the network between different hosts. The two most common ones are the Internet socket and the Unix domain socket.

[Dica] Dica

"netstat -an" disponibiliza uma visão geral muito útil dos sockets que estão abertos num determinado sistema.

1.2.10. Ficheiros de dispositivo

Device files refer to physical or virtual devices on your system, such as your hard disk, video card, screen, or keyboard. An example of a virtual device is the console, represented by "/dev/console".

Existem 2 tipos de ficheiros de dispositivo

  • Dispositivo de Caractere

    • Acedido a um caractere de cada vez
    • 1 caractere = 1 byte
    • Ex, teclado, porta série, ...
  • Dispositivo de Bloco

    • acedido em unidades maiores chamadas blocos
    • 1 bloco > 1 byte
    • Ex, o disco rijo, ...

You can read and write device files, though the file may well contain binary data which may be an incomprehensible-to-humans gibberish. Writing data directly to these files is sometimes useful for the troubleshooting of hardware connections. For example, you can dump a text file to the printer device "/dev/lp0" or send modem commands to the appropriate serial port "/dev/ttyS0". But, unless this is done carefully, it may cause a major disaster. So be cautious.

[Nota] Nota

Para o acesso normal a uma impressora, use lp(1).

Os números de nós de dispositivo são mostrados ao executar ls(1) como se segue.

$ ls -l /dev/hda /dev/ttyS0 /dev/zero
brw-rw---- 1 root cdrom   3,  0 2007-04-29 07:00 /dev/hda
crw-rw---- 1 root dialout 4, 64 2007-04-29 07:00 /dev/ttyS0
crw-rw-rw- 1 root root    1,  5 2007-04-29 07:00 /dev/zero
  • "/dev/hda" has the major device number 3 and the minor device number 0. This is read/write accessible by the user who belongs to cdrom group.
  • "/dev/ttyS0" has the major device number 4 and the minor device number 64. This is read/write accessible by the user who belongs to dialout group.
  • "/dev/zero" tem o número 1 no maior dispositivo e o número 5 no menor dispositivo. Isto é acessível para leitura/escrita por todos.

No sistema Linux 2.6, o sistema de ficheiro sob "/dev/" é povoado automaticamente pelo mecanismo udev(7).

1.2.11. Ficheiros de dispositivos especiais

Existem alguns ficheiros de dispositivos especiais.

Tabela 1.10. Lista de ficheiros de dispositivos especiais

ficheiro de dispositivo acção descrição da resposta
/dev/null ler retorna o "caractere de fim-de-ficheiro (EOF)"
/dev/null escrever retorna nada (um poço de despejo de dados sem fundo)
/dev/zero ler return "the \0 (NUL) character" (not the same as the number zero ASCII)
/dev/random ler return random characters from a true random number generator, delivering real entropy (slow)
/dev/urandom ler return random characters from a cryptographically secure pseudorandom number generator
/dev/full escrever retorna o erro de disco cheiro (ENOSPC)

Estes são usados frequentemente em conjunto com o redireccionamento da shell (veja Secção 1.5.8, “Sequências de comandos típicas e redireccionamento da shell”).

1.2.12. procfs e sysfs

The procfs and sysfs mounted on "/proc" and "/sys" are the pseudo-filesystem and expose internal data structures of the kernel to the userspace. In other word, these entries are virtual, meaning that they act as a convenient window into the operation of the operating system.

The directory "/proc" contains (among other things) one subdirectory for each process running on the system, which is named after the process ID (PID). System utilities that access process information, such as ps(1), get their information from this directory structure.

The directories under "/proc/sys/" contain interface to change certain kernel parameters at run time. (You may do the same through specialized sysctl(8) command or its preload/configuration file "/etc/sysctrl.conf".)

[Nota] Nota

The Linux kernel may complain "Too many open files". You can fix this by increasing "file-max" value to a larger value from the root shell, e.g., "echo "65536" > /proc/sys/fs/file-max" (This was needed on older kernels).

As pessoas entram em pânico frequentemente quando notam num ficheiro em particular - "/proc/kcore" - o qual é geralmente enorme. Isto é (mais ou menos) uma cópia do conteúdo da memória do seu computador. É usado para depurar o kernel. É um ficheiro virtual que aponta para a memória do computador, portanto não se preocupe com o seu tamanho.

The directory under "/sys" contains exported kernel data structures, their attributes, and their linkages between them. It also contains interface to change certain kernel parameters at run time.

See "proc.txt(.gz)", "sysfs.txt(.gz)" and other related documents in the Linux kernel documentation ("/usr/share/doc/linux-doc-2.6.*/Documentation/filesystems/*") provided by the linux-doc-2.6.* package.

1.3. Midnight Commander (MC)

Midnight Commander (MC) é um "Canivete Suíço" do GNU para a consola Linux e outros ambientes de terminal. Isto oferece a novatos uma experiência de consola movida a menus o que é mais fácil de aprender que os comandos standard do Unix.

Você deve precisar de instalar o pacote Midnight Commander que é intitulado de "mc" com o seguinte.

$ sudo apt-get install mc

use o comando mc(1) para explorar o sistema Debian. Esta é a melhor maneira de aprender. Por favor explore algumas localizações interessantes apenas usando as teclas do cursor e Enter.

  • "/etc" e os seus sub-directórios
  • e"/var/log" os seus sub-directórios
  • "/usr/share/doc" os seus sub-directórios
  • "/sbin" e "/bin"

1.3.1. Personalização do MC

De modo a fazer o MC mudar o directório de trabalho ao sair e cd para o directório, eu sugiro modificar o "~/.bashrc" para incluir um script disponibilizado pelo pacote mc.

. /usr/share/mc/bin/mc.sh

Veja mc(1) (sob a opção "-P") para a razão. (Se você não compreende exactamente o que estou a falar aqui, pode fazer isto mais tarde.)

1.3.2. Arrancar o MC

O MC pode ser arrancado com o seguinte.

$ mc

MC takes care of all file operations through its menu, requiring minimal user effort. Just press F1 to get the help screen. You can play with MC just by pressing cursor-keys and function-keys.

[Nota] Nota

In some consoles such as gnome-terminal(1), key strokes of function-keys may be stolen by the console program. You can disable these features by "Edit" → "Keyboard Shortcuts" for gnome-terminal.

If you encounter character encoding problem which displays garbage characters, adding "-a" to MC's command line may help prevent problems.

Se isto não limpar os seus problemas de écran com o MC, veja Secção 9.6.6, “A configuração do terminal”.

1.3.3. Gestor de ficheiros em MC

The default is two directory panels containing file lists. Another useful mode is to set the right window to "information" to see file access privilege information, etc. Following are some essential keystrokes. With the gpm(8) daemon running, one can use a mouse on Linux character consoles, too. (Make sure to press the shift-key to obtain the normal behavior of cut and paste in MC.)

Tabela 1.11. As teclas de atalho do MC

tecla tecla de atalho
F1 menu de ajuda
F3 visualizador de ficheiros interno
F4 editor interno
F9 activa o menu de desenrolar
F10 sair do Midnight Commander
Tab mover entre duas janelas
Insert ou Ctrl-T marca o ficheiro para uma operação de múltiplos ficheiros como uma cópia
Del apaga o ficheiro (tenha cuidado -- configure o MC para modo de apagar seguro)
Teclas do cursor auto-explicativo

1.3.4. Truques de linha de comandos no MC

  • cd command changes the directory shown on the selected screen.
  • Ctrl-Enter or Alt-Enter copies a filename to the command line. Use this with cp(1) and mv(1) commands together with command-line editing.
  • Alt-Tab shows shell filename expansion choices.
  • One can specify the starting directory for both windows as arguments to MC; for example, "mc /etc /root".
  • Esc + n-keyFn (i.e., Esc + 1F1, etc.; Esc + 0F10)
  • Pressing Esc before the key has the same effect as pressing the Alt and the key together.; i.e., type Esc + c for Alt-C. Esc is called meta-key and sometimes noted as "M-".

1.3.5. O editor interno em MC

The internal editor has an interesting cut-and-paste scheme. Pressing F3 marks the start of a selection, a second F3 marks the end of selection and highlights the selection. Then you can move your cursor. If you press F6, the selected area is moved to the cursor location. If you press F5, the selected area is copied and inserted at the cursor location. F2 saves the file. F10 gets you out. Most cursor keys work intuitively.

Este editor pode ser iniciado directamente num ficheiro usando um dos seguintes comandos.

$ mc -e ficheiro_a_editar
$ mcedit ficheiro_a_editar

This is not a multi-window editor, but one can use multiple Linux consoles to achieve the same effect. To copy between windows, use Alt-F<n> keys to switch virtual consoles and use "File→Insert file" or "File→Copy to file" to move a portion of a file to another file.

Este editor interno pode ser substituído por qualquer editor externo à escolha.

Also, many programs use the environment variables "$EDITOR" or "$VISUAL" to decide which editor to use. If you are uncomfortable with vim(1) or nano(1) initially, you may set these to "mcedit" by adding the following lines to "~/.bashrc".

export EDITOR=mcedit
export VISUAL=mcedit

Eu recomendo definir isto para "vim" se possível.

Se você fica desconfortável com o vim(1), você pode continuar a usar o mcedit(1) para a maioria das tarefas de manutenção do sistema.

1.3.6. O visualizador interno no MC

MC is a very smart viewer. This is a great tool for searching words in documents. I always use this for files in the "/usr/share/doc" directory. This is the fastest way to browse through masses of Linux information. This viewer can be directly started using one of the following commands.

$ mc -v caminho/para/nome_de_ficheiro_a_visualizar
$ mcview caminho/para/nome_de_ficheiro_a_visualizar

1.3.7. Funcionalidades de auto-arranque do MC

Press Enter on a file, and the appropriate program handles the content of the file (see Secção 9.5.11, “Personalizar o programa a ser arrancado”). This is a very convenient MC feature.

Tabela 1.12. A reacção à tecla enter no MC

tipo de ficheiro reacção à tecla enter
ficheiro executável executa comando
ficheiro man canaliza o conteúdo para software de visualização
ficheiro html canaliza o conteúdo para explorador web
ficheiros "*.tar.gz" e "*.deb" explora o seu conteúdo como sendo um sub-directório

In order to allow these viewer and virtual file features to function, viewable files should not be set as executable. Change their status using chmod(1) or via the MC file menu.

1.3.8. Sistema de ficheiros FTP virtual no MC

MC can be used to access files over the Internet using FTP. Go to the menu by pressing F9, then type "p" to activate the FTP virtual filesystem. Enter a URL in the form "username:passwd@hostname.domainname", which retrieves a remote directory that appears like a local one.

Tente "[http.us.debian.org/debian]" como URL e explore o arquivo Debian.

1.4. O ambiente de trabalho estilo Unix básico

Apesar do MC lhe permitir fazer quase tudo, é muito importante aprender a usar as ferramentas de linha de comandos invocadas do aviso de shell e torna-se familiarizado com o ambiente de trabalho estilo Unix.

1.4.1. A shell de login

Você pode seleccionar a sua shell de login com chsh(1).

Tabela 1.13. Lista de programas da shell

pacote popcon tamanho Shell do POSIX descrição
bash * V:91, I:99 3536 Sim Bash: a GNU Bourne Again SHell (o standard de facto)
tcsh * V:4, I:27 768 Não Shell TENEX C: uma versão melhorada de Berkeley csh
dash * V:25, I:32 248 Sim Shell Alquimista da Debian. bom para script de shell
zsh * V:3, I:6 12784 Sim Z shell: the standard shell with many enhancements
pdksh * V:0.2, I:1.1 468 Sim versão de domínio público da Korn shell
csh * V:0.6, I:2 404 Não OpenBSD C Shell, a version of Berkeley csh
sash * V:0.2, I:1.0 856 Sim Stand-alone shell with builtin commands (Not meant for standard "/bin/sh")
ksh * V:0.5, I:1.6 2800 Sim the real, AT&T version of the Korn shell
rc * V:0.16, I:1.6 204 Não implementation of the AT&T Plan 9 rc shell
posh * V:0.01, I:0.11 228 Sim Policy-compliant Ordinary SHell (derivação da pdksh)

In this tutorial chapter, the interactive shell always means bash.

1.4.2. Personalizar o bash

Você pode personalizar o comportamento do bash(1) no "~/.bashrc".

por exemplo, tente o seguinte.

# CD upon exiting MC
. /usr/share/mc/bin/mc.sh

# set CDPATH to good one
CDPATH=.:/usr/share/doc:~:~/Desktop:~
export CDPATH

PATH="${PATH}":/usr/sbin:/sbin
# set PATH so it includes user's private bin if it exists
if [ -d ~/bin ] ; then
  PATH=~/bin:"${PATH}"
fi
export PATH

EDITOR=vim
export EDITOR
[Dica] Dica

Você pode encontrar mais dicas de personalização da bash, como os Secção 9.2.7, “Comandos coloridos”, em Capítulo 9, Dicas do sistema.

1.4.3. Teclas especiais

In the Unix-like environment, there are few key strokes which have special meanings. Please note that on a normal Linux character console, only the left-hand Ctrl and Alt keys work as expected. Here are few notable key strokes to remember.

Tabela 1.14. Lista de teclas de atalho para bash

tecla descrição do atalho da tecla
Ctrl-U apaga a linha antes do cursor
Ctrl-H apaga um caractere antes do cursor
Ctrl-D termina a entrada (sai da shell se estiver a usar uma shell)
Ctrl-C termina um programa em funcionamento
Ctrl-Z temporarily stop program by moving it to the background job
Ctrl-S pára a saída para o écran
Ctrl-Q reactiva a saída para o écran
Ctrl-Alt-Del reinicia/pára o sistema, veja inittab(5)
Left-Alt-key (opcionalmente, tecla-do-Windows) meta-tecla para o Emacs e a UI semelhante
Seta-para-cima inicia a busca no histórico de comandos sob bash
Ctrl-R start incremental command history search under bash
Tab complete input of the filename to the command line under bash
Ctrl-V Tab input Tab without expansion to the command line under bash

[Dica] Dica

The terminal feature of Ctrl-S can be disabled using stty(1).

1.4.4. Operações do rato ao estilo Unix

As operações do rato ao estilo Unix são baseadas em sistema de rato de 3 botões.

Tabela 1.15. Lista de operações de rato ao estilo Unix

acção resposta
Clique-esquerdo-e-arrastar do rato seleccionar e copiar para a área de transferência
Clique-esquerdo selecciona o início da selecção
Clique-direito selecciona o fim da selecção e copia para a área de transferência
Clique-central cola a área de transferência no cursor

The center wheel on the modern wheel mouse is considered middle mouse button and can be used for middle-click. Clicking left and right mouse buttons together serves as the middle-click under the 2 button mouse system situation. In order to use a mouse in Linux character consoles, you need to have gpm(8) running as daemon.

1.4.5. O pager

less(1) is the enhanced pager (file content browser). Hit "h" for help. It can do much more than more(1) and can be supercharged by executing "eval $(lesspipe)" or "eval $(lessfile)" in the shell startup script. See more in "/usr/share/doc/lessf/LESSOPEN". The "-R" option allows raw character output and enables ANSI color escape sequences. See less(1).

1.4.6. O editor de texto

You should become proficient in one of variants of Vim or Emacs programs which are popular in the Unix-like system.

I think getting used to Vim commands is the right thing to do, since Vi-editor is always there in the Linux/Unix world. (Actually, original vi or new nvi are programs you find everywhere. I chose Vim instead for newbie since it offers you help through F1 key while it is similar enough and more powerful.)

If you chose either Emacs or XEmacs instead as your choice of the editor, that is another good choice indeed, particularly for programming. Emacs has a plethora of other features as well, including functioning as a newsreader, directory editor, mail program, etc. When used for programming or editing shell scripts, it intelligently recognizes the format of what you are working on, and tries to provide assistance. Some people maintain that the only program they need on Linux is Emacs. Ten minutes learning Emacs now can save hours later. Having the GNU Emacs manual for reference when learning Emacs is highly recommended.

All these programs usually come with tutoring program for you to learn them by practice. Start Vim by typing "vim" and press F1-key. You should at least read the first 35 lines. Then do the online training course by moving cursor to "|tutor|" and pressing Ctrl-].

[Nota] Nota

Good editors, such as Vim and Emacs, can be used to handle UTF-8 and other exotic encoding texts correctly with proper option in the x-terminal-emulator on X under UTF-8 locale with proper font settings. Please refer to their documentation on multibyte text.

1.4.7. Definir um editor de texto predefinido

Debian comes with a number of different editors. We recommend to install the vim package, as mentioned above.

Debian provides unified access to the system default editor via command "/usr/bin/editor" so other programs (e.g., reportbug(1)) can invoke it. You can change it by the following.

$ sudo update-alternatives --config editor

The choice "/usr/bin/vim.basic" over "/usr/bin/vim.tiny" is my recommendation for newbies since it supports syntax highlighting.

[Dica] Dica

Many programs use the environment variables "$EDITOR" or "$VISUAL" to decide which editor to use (see Secção 1.3.5, “O editor interno em MC” and Secção 9.5.11, “Personalizar o programa a ser arrancado”). For the consistency on Debian system, set these to "/usr/bin/editor". (Historically, "$EDITOR" was "ed" and "$VISUAL" was "vi".)

1.4.8. Personalizar o vim

Você pode personalizar o comportamento do vim(1) em "~/.vimrc".

Por exemplo, tente o seguinte

" -------------------------------
" Local configuration
"
set nocompatible
set nopaste
set pastetoggle=<f2>
syn on
if $USER == "root"
 set nomodeline
 set noswapfile
else
 set modeline
 set swapfile
endif
" filler to avoid the line above being recognized as a modeline
" filler
" filler

1.4.9. Gravar as actividades da shell

The output of the shell command may roll off your screen and may be lost forever. It is good practice to log shell activities into the file for you to review them later. This kind of record is essential when you perform any system administration tasks.

The basic method of recording the shell activity is to run it under script(1).

Por exemplo, tente o seguinte

$ script
Script started, file is typescript

Faz quaisquer comandos de shell sob script.

Carregue em Ctrl-D para terminar o script.

$ vim typescript

Veja Secção 9.2.3, “Gravar as actividades da shell de modo limpo” .

1.4.10. Comandos Unix básicos

Let's learn basic Unix commands. Here I use "Unix" in its generic sense. Any Unix clone OSs usually offer equivalent commands. The Debian system is no exception. Do not worry if some commands do not work as you wish now. If alias is used in the shell, its corresponding command outputs are different. These examples are not meant to be executed in this order.

Tente os comandos seguintes a partir da conta de utilizador não-privilegiado.

Tabela 1.16. lista dos comandos Unix básicos

comando descrição
pwd mostra o nome do directório actual
whoami mostra o nome do utilizador actual
id mostra a identidade do utilizador actual (nome, uid, gid, e grupos associados)
file <foo> mostra o tipo de ficheiro para o ficheiro "<foo>"
type -p <nome_do_comando> mostra uma localização de ficheiro do comando "<nome_do_comando>"
which <nome_do_comando> , ,
type <nome_do_comando> mostra informação do comando "<nome_do_comando>"
apropos <palavra_chave> mostra comandos relacionados com a "<palavra_chave>"
man -k <palavra_chave> , ,
whatis <nome_do_comando> display one line explanation on command "<commandname>"
man -a <nome_do_comando> display explanation on command "<commandname>" (Unix style)
info <nome_do_comando> display rather long explanation on command "<commandname>" (GNU style)
ls lista o conteúdo do directório (ficheiros e directórios não escondidos)
ls -a lista o conteúdo do directório (todos os ficheiros e directórios)
ls -A list contents of directory (almost all files and directories, i.e., skip ".." and ".")
ls -la lista todo o conteúdo do directório com informação detalhada
ls -lai lista todo o conteúdo do directório com número de inode e informação detalhada
ls -d lista todos os directórios sob o directório actual
tree mostra o conteúdo da árvore de ficheiros
lsof <foo> lista o estado aberto do ficheiro "<foo>"
lsof -p <pid> lista ficheiros abertos pelo ID de processo: "<pid>"
mkdir <foo> make a new directory "<foo>" in the current directory
rmdir <foo> remove um directório "<foo>" no directório actual
cd <foo> change directory to the directory "<foo>" in the current directory or in the directory listed in the variable "$CDPATH"
cd / muda o directório para o directório raiz
cd muda o directório para o directório home do utilizador actual
cd /<foo> muda o directório para o directório de caminho absoluto "/<foo>"
cd .. muda o directório para o directório pai
cd ~<foo> muda o directório para o directório home do utilizador "<foo>"
cd - muda o directório para o directório anterior
</etc/motd pager mostra o conteúdo de "/etc/motd" usando o paginador predefinido
touch <junkfile> cria um ficheiro vazio "<junkfile>"
cp <foo> <bar> copia um ficheiro "<foo>" existente para um novo ficheiro "<bar>"
rm <junkfile> remove um ficheiro "<junkfile>"
mv <foo> <bar> rename an existing file "<foo>" to a new name "<bar>" ("<bar>" must not exist)
mv <foo> <bar> move an existing file "<foo>" to a new location "<bar>/<foo>" (the directory "<bar>" must exist)
mv <foo> <bar>/<baz> move an existing file "<foo>" to a new location with a new name "<bar>/<baz>" (the directory "<bar>" must exist but the directory "<bar>/<baz>" must not exist)
chmod 600 <foo> make an existing file "<foo>" to be non-readable and non-writable by the other people (non-executable for all)
chmod 644 <foo> make an existing file "<foo>" to be readable but non-writable by the other people (non-executable for all)
chmod 755 <foo> make an existing file "<foo>" to be readable but non-writable by the other people (executable for all)
find . -name <padrão> find matching filenames using shell "<pattern>" (slower)
locate -d . <padrão> find matching filenames using shell "<pattern>" (quicker using regularly generated database)
grep -e "<padrão>" *.html find a "<pattern>" in all files ending with ".html" in current directory and display them all
top display process information using full screen, type "q" to quit
ps aux | pager mostra informação dos processos a correr usando saída ao estilo BSD
ps -ef | pager mostra informação dos processos a correr usando saída ao estilo Unix system-V
ps aux | grep -e "[e]xim4*" mostra todos os processos que correm "exim" e "exim4"
ps axf | pager mostra informação de todos os processos a correr com saída em arte de ACSII
kill <1234> mata todos os processos identificados pelo ID de processo: "<1234>"
gzip <foo> descomprime "<foo>" para criar "<foo>.gz" usando a codificação Lempel-Ziv (LZ77)
gunzip <foo>.gz descomprime "<foo>.gz" para criar "<foo>"
bzip2 <foo> comprime "<foo>" para criar "<foo>.bz2" usando o algoritmo de compressão de texto organizado em blocos Burrows-Wheeler, e codificação Huffman (melhor compressão que gzip)
bunzip2 <foo>.bz2 descomprime "<foo>.bz2" para criar "<foo>"
xz <foo> compress "<foo>" to create "<foo>.xz" using the Lempel–Ziv–Markov chain algorithm (better compression than bzip2)
unxz <foo>.xz decompress "<foo>.xz" to create "<foo>"
tar -xvf <foo>.tar extrai ficheiros do arquivo "<foo>.tar"
tar -xvzf <foo>.tar.gz extrai ficheiros do arquivo gzipado "<foo>.tar.gz"
tar -xvjf <foo>.tar.bz2 extrai ficheiros do arquivo "<foo>.tar.bz2"
tar -xvJf <foo>.tar.xz extract files from "<foo>.tar.xz" archive
tar -cvf <foo>.tar <bar>/ arquiva o conteúdo da pasta "<bar>/" no arquivo "<foo>.tar"
tar -cvzf <foo>.tar.gz <bar>/ arquiva o conteúdo da pasta "<bar>/" no arquivo comprimido "<foo>.tar.gz"
tar -cvjf <foo>.tar.bz2 <bar>/ arquiva o conteúdo da pasta "<bar>/" no arquivo "<foo>.tar.bz2"
tar -cvJf <foo>.tar.xz <bar>/ archive contents of folder "<bar>/" in "<foo>.tar.xz" archive
zcat README.gz | pager mostra o conteúdo do "README.gz" comprimido usando o paginador predefinido
zcat README.gz > foo cria o ficheiro "foo" com o conteúdo descomprimido de "README.gz"
zcat README.gz >> foo append the decompressed content of "README.gz" to the end of the file "foo" (if it does not exist, create it first)

[Nota] Nota

Unix has a tradition to hide filenames which start with ".". They are traditionally files that contain configuration information and user preferences.

[Nota] Nota

Para o comando cd, veja builtins(7).

[Nota] Nota

The default pager of the bare bone Debian system is more(1) which cannot scroll back. By installing the less package using command line "apt-get install less", less(1) becomes default pager and you can scroll back with cursor keys.

[Nota] Nota

The "[" and "]" in the regular expression of the "ps aux | grep -e "[e]xim4*"" command above enable grep to avoid matching itself. The "4*" in the regular expression means 0 or more repeats of character "4" thus enables grep to match both "exim" and "exim4". Although "*" is used in the shell filename glob and the regular expression, their meanings are different. Learn the regular expression from grep(1).

Please traverse directories and peek into the system using the above commands as training. If you have questions on any of console commands, please make sure to read the manual page.

Por exemplo, tente o seguinte

$ man man
$ man bash
$ man builtins
$ man grep
$ man ls

The style of man pages may be a little hard to get used to, because they are rather terse, particularly the older, very traditional ones. But once you get used to it, you come to appreciate their succinctness.

Please note that many Unix-like commands including ones from GNU and BSD display brief help information if you invoke them in one of the following ways (or without any arguments in some cases).

$ <nome_do_comando> --help
$ <nome_do_comando> -h

1.5. O simples comando de shell

Now you have some feel on how to use the Debian system. Let's look deep into the mechanism of the command execution in the Debian system. Here, I have simplified reality for the newbie. See bash(1) for the exact explanation.

Um comando simples é uma sequência de componentes.

  1. Variable assignments (optional)
  2. Nome do comando
  3. Argumentos (opcional)
  4. Re-direcções (opcional: > , >> , < , << , etc.)
  5. Control operator (optional: && , || , <newline> , ; , & , ( , ) )

1.5.1. Execução de comando e variável de ambiente

Values of some environment variables change the behavior of some Unix commands.

Default values of environment variables are initially set by the PAM system and then some of them may be reset by some application programs.

  • The display manager such as gdm resets environment variables.
  • The shell in its start up codes resets environment variables in "~/bash_profile" and "~/.bashrc".

1.5.2. variável "$LANG"

The full locale value given to "$LANG" variable consists of 3 parts: "xx_YY.ZZZZ".


For language codes and country codes, see pertinent description in the "info gettext".

For the codeset on the modern Debian system, you should always set it to UTF-8 unless you specifically want to use the historic one with good reason and background knowledge.

Para mais detalhes sobre configuração do locale, veja Secção 8.3, “O locale”.

[Nota] Nota

The "LANG=en_US" is not "LANG=C" nor "LANG=en_US.UTF-8". It is "LANG=en_US.ISO-8859-1" (see Secção 8.3.1, “Bases de codificação”).

Tabela 1.18. Lista de recomendações de locale

recomendação de locale Linguagem (área)
en_US.UTF-8 Inglês(EUA)
en_GB.UTF-8 Inglês(Grã-Bretanha)
fr_FR.UTF-8 Francês(França)
de_DE.UTF-8 Alemão(Alemanha)
it_IT.UTF-8 Italiano(Itália)
es_ES.UTF-8 Espanhol(Espanha)
ca_ES.UTF-8 Catalão(Espanha)
sv_SE.UTF-8 Sueco(Suécia)
pt_BR.UTF-8 Português(Brasil)
ru_RU.UTF-8 Russo(Rússia)
zh_CN.UTF-8 Chinês(Rep._Popular_da_China)
zh_TW.UTF-8 Chinês(Taiwan_R.O.C.)
ja_JP.UTF-8 Japonês(Japão)
ko_KR.UTF-8 Coreano(República_da_Coreia)
vi_VN.UTF-8 Vietnamita(Vietname)

A execução de comando típica uma sequência de linha de shell como o seguinte.

$ date
Sun Jun  3 10:27:39 JST 2007
$ LANG=fr_FR.UTF-8 date
dimanche 3 juin 2007, 10:27:33 (UTC+0900)

Aqui, o programa date(1) é executado com diferentes valores da variável de ambiente "$LANG".

  • For the first command, "$LANG" is set to the system default locale value "en_US.UTF-8".
  • For the second command, "$LANG" is set to the French UTF-8 locale value "fr_FR.UTF-8".

Most command executions usually do not have preceding environment variable definition. For the above example, you can alternatively execute as the following.

$ LANG=fr_FR.UTF-8
$ date
dimanche 3 juin 2007, 10:27:33 (UTC+0900)

As you can see here, the output of command is affected by the environment variable to produce French output. If you want the environment variable to be inherited to subprocesses (e.g., when calling shell script), you need to export it instead by the following.

$ export LANG
[Dica] Dica

When filing a bug report, running and checking the command under "LANG=en_US.UTF-8" is good idea if you use non-English environment.

See locale(5) and locale(7) for "$LANG" and related environment variables.

[Nota] Nota

I recommend you to configure the system environment just by the "$LANG" variable and to stay away from "$LC_*" variables unless it is absolutely needed.

1.5.3. variável "$PATH"

When you type a command into the shell, the shell searches the command in the list of directories contained in the "$PATH" environment variable. The value of the "$PATH" environment variable is also called the shell's search path.

In the default Debian installation, the "$PATH" environment variable of user accounts may not include "/sbin" and "/usr/sbin". For example, the ifconfig command needs to be issued with full path as "/sbin/ifconfig". (Similar ip command is located in "/bin".)

You can change the "$PATH" environment variable of Bash shell by "~/.bash_profile" or "~/.bashrc" files.

1.5.4. variável "$HOME"

Many commands stores user specific configuration in the home directory and changes their behavior by their contents. The home directory is identified by the environment variable "$HOME".

Tabela 1.19. Lista de valores "$HOME"

valor de "$HOME" situação de execução do programa
/ programa executado pelo processo de init (daemon)
/root programa executado a partir da shell de root normal
/home/<utilizador_normal> programa executado a partir da shell de utilizador normal
/home/<utilizador_normal> program run from the normal user GUI desktop menu
/home/<utilizador_normal> program run as root with "sudo program"
/root program run as root with "sudo -H program"

[Dica] Dica

Shell expands "~/" to current user's home directory, i.e., "$HOME/". Shell expands "~foo/" to foo's home directory, i.e., "/home/foo/".

1.5.5. Opções de linha de comandos

Some commands take arguments. Arguments starting with "-" or "--" are called options and control the behavior of the command.

$ date
Mon Oct 27 23:02:09 CET 2003
$ date -R
Mon, 27 Oct 2003 23:02:40 +0100

Aqui o argumento de linha de comandos "-R" altera o comportamento de date(1) para gerar uma string da data compatível com RFC2822

1.5.6. Glob da shell

Often you want a command to work with a group of files without typing all of them. The filename expansion pattern using the shell glob, (sometimes referred as wildcards), facilitate this need.

Tabela 1.20. Padrões glob da shell

padrão glob da shell descrição de regra de correspondência
* nome de ficheiro (segmento) não iniciado com "."
.* nome de ficheiro (segmento) iniciado com "."
? exactamente um caractere
[…] exactamente um caractere com qualquer caractere envolvido em colchetes
[a-z] exactamente um caractere com qualquer caractere entre "a" e "z"
[^…] exactamente um caractere que não seja qualquer caractere envolvido em colchetes (excluindo "^")

Por exemplo, tente o seguinte

$ mkdir junk; cd junk; touch 1.txt 2.txt 3.c 4.h .5.txt ..6.txt
$ echo *.txt
1.txt 2.txt
$ echo *
1.txt 2.txt 3.c 4.h
$ echo *.[hc]
3.c 4.h
$ echo .*
. .. .5.txt ..6.txt
$ echo .*[^.]*
.5.txt ..6.txt
$ echo [^1-3]*
4.h
$ cd ..; rm -rf junk

Veja glob(7).

[Nota] Nota

Unlike normal filename expansion by the shell, the shell pattern "*" tested in find(1) with "-name" test etc., matches the initial "." of the filename. (New POSIX feature)

[Nota] Nota

BASH can be tweaked to change its glob behavior with its shopt builtin options such as "dotglob", "noglob", "nocaseglob", "nullglob", "nocaseglob", "extglob", etc. See bash(1).

1.5.7. Valor de retorno do comando

Each command returns its exit status (variable: "$?") as the return value.

Tabela 1.21. Códigos de saída do comando

estado de saída do comando valor de retorno numérico valor de retorno lógico
sucesso zero, 0 TRUE
erro não-zero, -1 FALSE

por exemplo, tente o seguinte.

$ [ 1 = 1 ] ; echo $?
0
$ [ 1 = 2 ] ; echo $?
1
[Nota] Nota

Please note that, in the logical context for the shell, success is treated as the logical TRUE which has 0 (zero) as its value. This is somewhat non-intuitive and needs to be reminded here.

1.5.8. Sequências de comandos típicas e redireccionamento da shell

Let's try to remember following shell command idioms typed in one line as a part of shell command.

Tabela 1.22. Idiomas de comandos de shell

idioma do comando descrição
command & background execution of command in the subshell
comando1 | comando2 pipe the standard output of command1 to the standard input of command2 (concurrent execution)
comando1 2>&1 | comando2 pipe both standard output and standard error of command1 to the standard input of command2 (concurrent execution)
comando1 ; comando2 execute command1 and command2 sequentially
comando1 && comando2 execute command1; if successful, execute command2 sequentially (return success if both command1 and command2 are successful)
comando1 || comando2 execute command1; if not successful, execute command2 sequentially (return success if command1 or command2 are successful)
comando > foo redirect standard output of command to a file foo (overwrite)
command 2> foo redirect standard error of command to a file foo (overwrite)
command >> foo redirect standard output of command to a file foo (append)
command 2>> foo redirect standard error of command to a file foo (append)
command > foo 2>&1 redirect both standard output and standard error of command to a file "foo"
comando < foo redirect standard input of command to a file foo
command << delimiter redirect standard input of command to the following lines until "delimiter" is met (here document)
command <<- delimiter redirect standard input of command to the following lines until "delimiter" is met (here document, the leading tab characters are stripped from input lines)

The Debian system is a multi-tasking system. Background jobs allow users to run multiple programs in a single shell. The management of the background process involves the shell builtins: jobs, fg, bg, and kill. Please read sections of bash(1) under "SIGNALS", and "JOB CONTROL", and builtins(1).

Por exemplo, tente o seguinte

$ </etc/motd pager
$ pager </etc/motd
$ pager /etc/motd
$ cat /etc/motd | pager

Although all 4 examples of shell redirections display the same thing, the last example runs an extra cat command and wastes resources with no reason.

The shell allows you to open files using the exec builtin with an arbitrary file descriptor.

$ echo Hello >foo
$ exec 3<foo 4>bar  # abrir ficheiros
$ cat <&3 >&4       # redireccionar stdin para 3, stdout para 4
$ exec 3<&- 4>&-    # fechar ficheiros
$ cat bar
Hello

Here, "n<&-" and "n>&-" mean to close the file descriptor "n".

The file descriptor 0-2 are predefined.

Tabela 1.23. Predefined file descriptors

dispositivo descrição descritor de ficheiro
stdin entrada standard 0
stdout saída standard 1
stderr erro standard 2

1.5.9. Command alias

You can set an alias for the frequently used command.

Por exemplo, tente o seguinte

$ alias la='ls -la'

Now, "la" works as a short hand for "ls -la" which lists all files in the long listing format.

You can list any existing aliases by alias (see bash(1) under "SHELL BUILTIN COMMANDS").

$ alias
...
alias la='ls -la'

You can identity exact path or identity of the command by type (see bash(1) under "SHELL BUILTIN COMMANDS").

Por exemplo, tente o seguinte

$ type ls
ls is hashed (/bin/ls)
$ type la
la is aliased to ls -la
$ type echo
echo is a shell builtin
$ type file
file is /usr/bin/file

Here ls was recently searched while "file" was not, thus "ls" is "hashed", i.e., the shell has an internal record for the quick access to the location of the "ls" command.

1.6. Processamento de texto estilo Unix

In Unix-like work environment, text processing is done by piping text through chains of standard text processing tools. This was another crucial Unix innovation.

1.6.1. Ferramentas de texto de Unix

Existem algumas ferramentas standard de processamento de texto que são muito usadas nos sistemas tipo Unix.

  • Nenhuma expressão regular é usada:

    • cat(1) concatena ficheiros e escreve o conteúdo inteiro.
    • tac(1) concatena ficheiros e escreve-os em reverso.
    • cut(1) selecciona partes de linhas e escreve-as.
    • head(1) escreve a parte inicial de ficheiros.
    • tail(1) escreve a parte final de ficheiros.
    • sort(1) organiza as linhas de ficheiros de texto.
    • uniq(1) remove linhas duplicadas de um ficheiro organizado.
    • tr(1) traduz ou apaga caracteres.
    • diff(1) compara ficheiros linha a linha.
  • É usada uma expressão regular básica (BRE):

    • grep(1) faz coincidir texto com padrões.
    • ed(1) é um editor de linhas primitivo.
    • sed(1) é um editor de fluxos.
    • vim(1) é um editor de écran.
    • emacs(1) é um editor de écran. (BRE de certo modo extensa)
  • É usada expressão regular extensa (ERE):

    • egrep(1) faz coincidir texto com padrões.
    • awk(1) faz processamento de texto simples.
    • tcl(3tcl) pode fazer todo o processamento de texto concebível: re_syntax(3). Bastante usado com tk(3tk).
    • perl(1) pode fazer todo o processamento de texto concebível. perlre(1).
    • pcregrep(1) from the pcregrep package matches text with Perl Compatible Regular Expressions (PCRE) pattern.
    • python(1) com o módulo re pode fazer todo o processamento de texto concebível. Veja "/usr/share/doc/python/html/index.html".

Se você não ter a certeza do que estes comandos fazem, por favor use "man comando" para descobri-lo por si.

[Nota] Nota

Sort order and range expression are locale dependent. If you wish to obtain traditional behavior for a command, use C locale instead of UTF-8 ones by prepnding command with "LANG=C" (see Secção 1.5.2, “variável "$LANG"” and Secção 8.3, “O locale”).

[Nota] Nota

Perl regular expressions (perlre(1)), Perl Compatible Regular Expressions (PCRE), and Python regular expressions offered by the re module have many common extensions to the normal ERE.

1.6.2. Expressões regulares

Regular expressions are used in many text processing tools. They are analogous to the shell globs, but they are more complicated and powerful.

The regular expression describes the matching pattern and is made up of text characters and metacharacters.

The metacharacter is just a character with a special meaning. There are 2 major styles, BRE and ERE, depending on the text tools as described above.

Tabela 1.24. Meta-caracteres para BRE e ERE

BRE ERE descrição da expressão regular
\ . [ ] ^ $ * \ . [ ] ^ $ * common metacharacters
\+ \? \( \) \{ \} \|   BRE only "\" escaped metacharacters
  + ? ( ) { } | ERE only non-"\" escaped metacharacters
c c match non-metacharacter "c"
\c \c match a literal character "c" even if "c" is metacharacter by itself
. . match any character including newline
^ ^ posição no início de uma string
$ $ posição no fim de uma string
\< \< position at the beginning of a word
\> \> position at the end of a word
\[abc…\] [abc…] match any characters in "abc…"
\[^abc…\] [^abc…] match any characters except in "abc…"
r* r* match zero or more regular expressions identified by "r"
r\+ r+ match one or more regular expressions identified by "r"
r\? r? match zero or one regular expressions identified by "r"
r1\|r2 r1|r2 match one of the regular expressions identified by "r1" or "r2"
\(r1\|r2\) (r1|r2) match one of the regular expressions identified by "r1" or "r2" and treat it as a bracketed regular expression

The regular expression of emacs is basically BRE but has been extended to treat "+"and "?" as the metacharacters as in ERE. Thus, there are no needs to escape them with "\" in the regular expression of emacs.

grep(1) pode ser usado para executar a pesquisa de texto usando a expressão regular.

Por exemplo, tente o seguinte

$ egrep 'GNU.*LICENSE|Yoyodyne' /usr/share/common-licenses/GPL
GNU GENERAL PUBLIC LICENSE
GNU GENERAL PUBLIC LICENSE
Yoyodyne, Inc., hereby disclaims all copyright interest in the program

1.6.3. Expressões de substituição

Para a expressão de substituição, alguns caracteres têm significados especiais.

Tabela 1.25. A expressão de substituição

expressão de substituição descrição do texto para substituir a expressão de substituição
& what the regular expression matched (use \& in emacs)
\n what the n-th bracketed regular expression matched ("n" being number)

For Perl replacement string, "$n" is used instead of "\n" and "&" has no special meaning.

Por exemplo, tente o seguinte

$ echo zzz1abc2efg3hij4 | \
sed -e 's/\(1[a-z]*\)[0-9]*\(.*\)$/=&=/'
zzz=1abc2efg3hij4=
$ echo zzz1abc2efg3hij4 | \
sed -e 's/\(1[a-z]*\)[0-9]*\(.*\)$/\2===\1/'
zzzefg3hij4===1abc
$ echo zzz1abc2efg3hij4 | \
perl -pe 's/(1[a-z]*)[0-9]*(.*)$/$2===$1/'
zzzefg3hij4===1abc
$ echo zzz1abc2efg3hij4 | \
perl -pe 's/(1[a-z]*)[0-9]*(.*)$/=&=/'
zzz=&=

Here please pay extra attention to the style of the bracketed regular expression and how the matched strings are used in the text replacement process on different tools.

These regular expressions can be used for cursor movements and text replacement actions in some editors too.

The back slash "\" at the end of line in the shell commandline escapes newline as a white space character and continues shell command line input to the next line.

Por favor leia todos os manuais relacionados para aprender estes comandos.

1.6.4. Substituição global com expressões regulares

The ed(1) command can replace all instances of "FROM_REGEX" with "TO_TEXT" in "file".

$ ed file <<EOF
,s/FROM_REGEX/TO_TEXT/g
w
q
EOF

The sed(1) command can replace all instances of "FROM_REGEX" with "TO_TEXT" in "file".

$ sed file 's/FROM_REGEX/TO_TEXT/g' | sponge file
[Dica] Dica

The sponge(8) command is a non-standard Unix tool offered by the moreutils package. This is quite useful when you wish to overwrite original file.

The vim(1) command can replace all instances of "FROM_REGEX" with "TO_TEXT" in "file" by using ex(1) commands.

$ vim '+%s/FROM_REGEX/TO_TEXT/gc' '+w' '+q' ficheiro
[Dica] Dica

The "c" flag in the above ensures interactive confirmation for each substitution.

Múltiplos ficheiros ("ficheiro1", "ficheiro2",e "ficheiro3") podem ser processados com expressões regulares à semelhança com vim(1) ou perl(1).

$ vim '+argdo %s/FROM_REGEX/TO_TEXT/ge|update' '+q' ficheiro1 ficheiro2 ficheiro3
[Dica] Dica

The "e" flag in the above prevents the "No match" error from breaking a mapping.

$ perl -i -p -e 's/FROM_REGEX/TO_TEXT/g;' ficheiro1 ficheiro2 ficheiro3

In the perl(1) example, "-i" is for in-place editing, "-p" is for implicit loop over files.

[Dica] Dica

Use of argument "-i.bak" instead of "-i" keeps each original file by adding ".bak" to its filename. This makes recovery from errors easier for complex substitutions.

[Nota] Nota

ed(1) and vim(1) are BRE; perl(1) is ERE.

1.6.5. Extrair dados de tabela de ficheiro de texto

Let's consider a text file called "DPL" in which some pre-2004 Debian project leader's names and their initiation days are listed in a space-separated format.

Ian     Murdock   August  1993
Bruce   Perens    April   1996
Ian     Jackson   January 1998
Wichert Akkerman  January 1999
Ben     Collins   April   2001
Bdale   Garbee    April   2002
Martin  Michlmayr March   2003

O awk é frequentemente usado para extrair dados deste tipo de ficheiros.

Por exemplo, tente o seguinte

$ awk '{ print $3 }' <DPL                   # month started
August
April
January
January
April
April
March
$ awk '($1=="Ian") { print }' <DPL          # DPL called Ian
Ian     Murdock   August  1993
Ian     Jackson   January 1998
$ awk '($2=="Perens") { print $3,$4 }' <DPL # When Perens started
April 1996

Shells such as Bash can be also used to parse this kind of file.

Por exemplo, tente o seguinte

$ while read first last month year; do
    echo $month
  done <DPL
... same output as the first Awk example

Here, the read builtin command uses characters in "$IFS" (internal field separators) to split lines into words.

If you change "$IFS" to ":", you can parse "/etc/passwd" with shell nicely.

$ oldIFS="$IFS"   # guarda o valor antigo
$ IFS=':'
$ while read user password uid gid rest_of_line; do
    if [ "$user" = "bozo" ]; then
      echo "$user's ID is $uid"
    fi
  done < /etc/passwd
bozo's ID is 1000
$ IFS="$oldIFS"   # restaura o valor antigo

(If Awk is used to do the equivalent, use "FS=':'" to set the field separator.)

IFS is also used by the shell to split results of parameter expansion, command substitution, and arithmetic expansion. These do not occur within double or single quoted words. The default value of IFS is <space>, <tab>, and <newline> combined.

Be careful about using this shell IFS tricks. Strange things may happen, when shell interprets some parts of the script as its input.

$ IFS=":,"                        # use ":" and "," as IFS
$ echo IFS=$IFS,   IFS="$IFS"     # echo is a Bash builtin
IFS=  , IFS=:,
$ date -R                         # just a command output
Sat, 23 Aug 2003 08:30:15 +0200
$ echo $(date -R)                 # sub shell --> input to main shell
Sat  23 Aug 2003 08 30 36 +0200
$ unset IFS                       # reset IFS to the default
$ echo $(date -R)
Sat, 23 Aug 2003 08:30:50 +0200

1.6.6. Script snippets for piping commands

The following scripts do nice things as a part of a pipe.

Tabela 1.26. List of script snippets for piping commands

script snippet (type in one line) efeito do comando
find /usr -print encontra todos os sob "/usr"
seq 1 100 escreve 1 até 100
| xargs -n 1 <command> run command repeatedly with each item from pipe as its argument
| xargs -n 1 echo split white-space-separated items from pipe into lines
| xargs echo merge all lines from pipe into a line
| grep -e <regex_pattern> extract lines from pipe containing <regex_pattern>
| grep -v -e <regex_pattern> extract lines from pipe not containing <regex_pattern>
| cut -d: -f3 - extract third field from pipe separated by ":" (passwd file etc.)
| awk '{ print $3 }' extract third field from pipe separated by whitespaces
| awk -F'\t' '{ print $3 }' extract third field from pipe separated by tab
| col -bx remove backspace and expand tabs to spaces
| expand - expande separadores
| sort| uniq organiza e remove duplicados
| tr 'A-Z' 'a-z' converte maiúsculas para minúsculas
| tr -d '\n' concatena linhas em uma linha
| tr -d '\r' remove CR
| sed 's/^/# /' adiciona "#" ao inicio de cada linha
| sed 's/\.ext//g' remove ".ext"
| sed -n -e 2p escreve a segunda linha
| head -n 2 - escreve as primeiras duas linhas
| tail -n 2 - escreve as últimas duas linhas

One-line shell script can loop over many files using find(1) and xargs(1) to perform quite complicated tasks. See Secção 10.1.5, “Idiomas para a selecção de ficheiros” and Secção 9.5.9, “Repeating a command looping over files”.

Quando a utilização dos modos interactivos da shell se torna muito complicada, por favor considere escrever um script de shell (veja Secção 12.1, “O script shell”).