41

Aula - Comandos Linux - Parte 2

Embed Size (px)

Citation preview

Page 1: Aula - Comandos Linux - Parte 2
Page 2: Aula - Comandos Linux - Parte 2

Objetivos

● Redirecionamento de entrada e saída (continuação);

● Links simbólicos x hard links;● Documentação no GNU/Linux;● Comandos de busca no sistema de arquivos;● Comandos para manipulação de arquivos de

texto;● Introdução a metacaracteres;

Page 3: Aula - Comandos Linux - Parte 2

Introdução ao Linux

Page 4: Aula - Comandos Linux - Parte 2

Introdução ao LinuxPipe

# ls / | more

# ls / | less

# ls / | xargs -n 2

# ls / | xargs -n 3

# echo “linux:macos:freebsd:openbsd” >

/tmp/teste.txt

# cat /tmp/teste.txt | xargs -d: -n 2

Page 5: Aula - Comandos Linux - Parte 2

Introdução ao Linux

Pipe

# echo 1 2 3 4 | xargs -n 2

# find /tmp -name "*.tmp" | xargs rm

# find /tmp -name "*.tmp" -print0 | xargs -0 rm

# ls / | xargs -n 4 > /tmp/teste3.txt

# cat /tmp/teste3.txt

Page 6: Aula - Comandos Linux - Parte 2

Introdução ao Linux

Pipe

# ls / | tee /tmp/teste4.txt

# cat /tmp/teste4.txt

# echo “egeekacademy” | tr [a-z] [A-Z]

Page 7: Aula - Comandos Linux - Parte 2

Introdução ao Linux

Pipe

# cat /etc/passwd | grep root

# cat /etc/passwd | grep -v root

# grep -v root /etc/passwd | tee /root/root.txt

Page 8: Aula - Comandos Linux - Parte 2

Introdução ao Linux

Links simbólicos (sym links)

● Pode apontar para um arquivo ou diretório em qualquer lugar:● Funciona em vários sistemas de arquivos;● Funciona para arquivos ou diretórios que estão em outras

partições ou discos;● Funciona em sistemas de arquivos remotos: NFS;● Ocupa pouco espaço;● Não funciona se o arquivo original for removido (link quebrado);

Page 9: Aula - Comandos Linux - Parte 2

Introdução ao LinuxLinks simbólicos (sym links)

# cp /etc/passwd /tmp

# cd /tmp

# ln -s passwd link.simbolico

# cp passwd passwd2

# stat passwd

# stat link.simbolico

# ls -i passwd

# ls -i link.simbolico

Page 10: Aula - Comandos Linux - Parte 2

Introdução ao Linux

Hard Link

● Não é um link;

● Aponta para o mesmo endereço físico (conhecido como inode);

● Continua funcionando caso o “arquivo original” seja removido;

● Possui as seguintes limitações:

– o “arquivo original” e o hard link devem estar localizados no mesmo sistema de arquivos;

– Não pode apontar para diretórios;

Page 11: Aula - Comandos Linux - Parte 2

Introdução ao Linux

Hard Link

# cd /tmp

# ln passwd2 link.hard

# stat passwd2

# stat link.hard

# ls -i passwd2

# ls -i link.hard

Page 12: Aula - Comandos Linux - Parte 2

Introdução ao Linux

Page 13: Aula - Comandos Linux - Parte 2

Introdução ao Linux

Seções do man:● man 1 – Programas e executáveis disponíveis ao usuário;● man 2 – Rotinas de sistema Unix e C;● man 3 – Rotinas de bibliotecas da linguagem C;● man 4 - Arquivos especiais (dispositivos em /dev);● man 5 - Arquivos de configuração e convenções; ● man 6 – Games;● man 7 – Diversos (macros textuais, por exemplo, regex);● man 8 - Comandos administrativos;● man 9 - Rotinas de Kernel.

Page 14: Aula - Comandos Linux - Parte 2

Introdução ao Linux

Comando man

# man ls

# man 1 passwd

# man 5 passwd

# man 7 regex

# man -L pt_BR ls

# man 7 undocumented

Page 15: Aula - Comandos Linux - Parte 2

Introdução ao Linux

Comando apropos / catman / whatis

# apropos copy

# man -k copy

# catman

# whatis ls

Page 16: Aula - Comandos Linux - Parte 2

Introdução ao Linux

Comando info / whereis / which

# info ls

# whereis passwd

# which ls

Page 17: Aula - Comandos Linux - Parte 2

Introdução ao Linux

Comandos de localização

# updatedb

# locate passwd

Page 18: Aula - Comandos Linux - Parte 2

Introdução ao LinuxComandos de localização

# ls -lh /etc/passwd

# find / -iname passwd

# find / -perm 777

# find /root -type f -perm 777

# find / -size +10M

# find / -perm 600 -exec ls -l {} \;

# find / -perm 600 -print0 | xargs -0 ls -l

Page 19: Aula - Comandos Linux - Parte 2

Introdução ao LinuxComandos de localização

# find / -type f -empty

# find / -type d -empty

# find / -mtime 50

# find / -mtime +50 -mtime -100

# find / -atime 50

# find . -type f -atime -1 -exec ls -l {} \;

$ find ~ -cmin -60

# find / -name "*.txt" -exec chmod -x {} ";"

Page 20: Aula - Comandos Linux - Parte 2

Introdução ao Linux

Comandos de localização

# ls -l teste.txt

# find / -perm -4000

# find / -perm 4000

# find / -maxdepth 2 -name passwd

# find . -type d -empty -exec rmdir {} \;

Page 21: Aula - Comandos Linux - Parte 2

Introdução ao Linux

Encerrando sessão

# logout

# exit

Page 22: Aula - Comandos Linux - Parte 2

Introdução ao Linux

Desligando e reiniciando a máquina

# halt

# shutdown -h 10

# shutdown -c

# shutdown -r 10

# shutdown -r now

# reboot

Page 23: Aula - Comandos Linux - Parte 2

Introdução ao Linuxjoin

$ cat hardwares.txt1 Processador Core i72 Memória RAM 8GB Kingston3 HD Seagate Sata II 500 GB

$ cat precos.txt1 R$ 1000,002 R$ 200,003 R$ 250,00

$ join hardwares.txt precos.txt > tabela.txt$ cat tabela.txt

Page 24: Aula - Comandos Linux - Parte 2

Introdução ao Linuxjoin

$ cat arq1.txt1 x1 12 y1 23 z1 3

$ cat arq2.txt1 x2 22 y2 43 z2 6

$ join -1 1 -2 1 -o '1.2 2.3' arq1.txt arq2.txt

Page 25: Aula - Comandos Linux - Parte 2

Introdução ao Linuxfmt$ fmt -w 33 tabela.txt

uniq$ cat frutas.txtMaçã BananaLaranjaMaracujáLaranja$ cat frutas.txt | sort | uniq $ cat frutas.txt | sort | uniq -d

Page 26: Aula - Comandos Linux - Parte 2

Introdução ao Linuxpr

Divide o arquivo para impressão. O padrão é 66 linhas por 72 caracteres de largura.

$ cat frutas.txt$ cat verduras.txt$ cat -n frutas.txt | head$ nl verduras.txt | pr -m – frutas.txt | head$ cat /etc/passwd > /tmp/teste.txt$ cat /etc/passwd >> /tmp/teste.txt$ cat -n /tmp/teste.txt$ cat /tmp/teste.txt | pr -l 47 -w 72

Page 27: Aula - Comandos Linux - Parte 2

Introdução ao Linux

paste

$ paste frutas.txt verduras.txt

uniq

$ sort frutas.txt | uniq | nl

tr

$ echo “Frase com espaços” | tr ' ' '$'

$ echo “Frase com espaços” | tr ' ' '\t'

$ cat verduras.txt | tr ' ' '\n'

Page 28: Aula - Comandos Linux - Parte 2

Introdução ao Linuxdd e du

# dd if=/dev/random bs=2 count=6 | base64 - >

/tmp/senhas.txt

# dd if=/dev/sda1 of=/dev/sdb2

# dd if=/dev/zero of=/tmp/arquivo.iso bs=1M count=10

# du -h /tmp/arquivo.iso

# du -hs /home/leo

Page 29: Aula - Comandos Linux - Parte 2

Introdução ao Linux

split

$ split -b 1MB /tmp/arquivo.iso

$ ls -lh /tmp

$ cat /etc/passwd > /tmp/teste1.txt

$ split -l 2 /tmp/teste1.txt

$ ls -lh /tmp

Page 30: Aula - Comandos Linux - Parte 2

Introdução ao Linux

expand e unexpand

$ nano url.txt

<ESTAÇO> egeeked <ESTAÇO> .com <TAB> .br

$ cat url.txt

$ expand -t1 url.txt

$ unexpand -a1 url.txt

Page 31: Aula - Comandos Linux - Parte 2

Introdução ao Linux

od | hexdump | xxd

$ cat arq1.txt

$ od -x arq1.txt

$ od -xc arq1.txt

$ hexdump arq1.txt

$ hexdump -C arq1.txt

Page 32: Aula - Comandos Linux - Parte 2

Introdução ao Linux

od | hexdump | xxd

$ echo “Teste” | hexdump -v -e '/1 “%02X\n”'

$ xxd arq1.txt > arq2.txt

$ cat arq2.txt

$ xxd -r arq2.txt

$ od -t x1 -An arq1.txt | tr -d '\n'

Page 33: Aula - Comandos Linux - Parte 2

Introdução ao LinuxComandos sequenciais

Executar 3 comandos em sequência independente do resultado de cada um:$ Comando 1 ; Comando 2 ; Comando 3

Executar o próximo comando apenas se o anterior executar com sucesso:$ Comando 1 && Comando 2 && Comando 3

Executar o próximo comando apenas se o anterior executar sem sucesso:$ Comando 1 || Comando 2 || Comando 3

Page 34: Aula - Comandos Linux - Parte 2

Introdução ao Linux

Comandos sequenciais – Exemplos

$ ls arquivonaoexistente ; cat /etc/issue

$ ls arquivonaoexistente && cat /etc/issue

$ ls arquivonaoexistente || cat /etc/issue

Page 35: Aula - Comandos Linux - Parte 2

Introdução ao Linux

df

$ df

$ df -h

$ df -T

$ df -Th

Page 36: Aula - Comandos Linux - Parte 2

Introdução ao Linux

ls

$ cat /etc/ld.so.conf

$ ls -dl `cat /etc/ld.so.conf`

$ ls -dl $(cat /etc/ld.so.conf)

$ ls --color

$ ls -a

$ ls /dev/sd*

Page 37: Aula - Comandos Linux - Parte 2

Introdução ao Linux

ls

$ ls /etc/host*

$ ls /dev/s??

$ ls /dev/sd[abc]

$ ls /dev/sda[!01]

Page 38: Aula - Comandos Linux - Parte 2

Introdução ao Linuxdiff

Arquivo Teste1.java

1.public class Teste1 {2.3. public static void main(String args[]) {4.5. System.out.println("Hello World!");6.7. }8.9.}

Page 39: Aula - Comandos Linux - Parte 2

Introdução ao Linuxdiff

Arquivo Teste2.java

1.public class Teste2 {2.3. public static void main(String args[]) {4.5. System.out.println("Alo Mundo!");6.7. }8.9.}

Page 40: Aula - Comandos Linux - Parte 2

Introdução ao Linux

diff

$ diff Teste1.java Teste2.java 1c1< public class Teste1 {---> public class Teste2 {5c5< System.out.println("Hello World!");---> System.out.println("Alo Mundo!");

Page 41: Aula - Comandos Linux - Parte 2

Introdução ao Linux

sed

# echo "O Windows é um sistema operacional muito seguro. O Windows é um sistema operacional multitarefa" | sed 's/Windows/Linux/'

# echo "O Windows é um sistema operacional muito seguro. O Windows é um sistema operacional multitarefa" | sed 's/Windows/Linux/g'