Linux Básico

Introdução

Sobre

Este repositório apresenta um conceito básico e comandos do Linux.

Diretório principal do Linux

  • boot: arquivo necessário para iniciar o sistema como o kernel Linux
  • bin: comando para usuários em geral
  • dev: arquivos do dispositivo
  • etc: arquivos de configuração do sistema
  • home: diretório inicial para usuários
  • lib: biblioteca
  • mídia: ponto de montagem para mídia externa
  • proc: informações de processo do sistema
  • root: diretório inicial para usuários root
  • sbin: comandos para usuários administradores do sistema
  • tmp: arquivos temporários
  • usr: programas, bibliotecas, documentos, etc.
  • var: arquivos atualizados com frequência, como arquivos de log

Listas de Comando

Arquivo

# cd
$ cd
#changes directory to home
$ cd
- #goes back to previous files

# less
$ less test
.md
# f/b : show next/previous page
# q : quits
# = : show file name and current line number
# / : search in forward order
# ? : search in backward order
# v : launch vi editor

#head/tail
$ head
-5 test.md # shows 5 heading lines of test.md
$ tail
-3 test.md # shows 3 bottom lines of test.md

#copy
$ cp
-i test.md tmp # overwrites with confirmation
$ cp
-i test.md tmp # overwrites without confirmation
$ cp
-r tmp tmp2 # copy directory

# move/rename
$ mv
-i data.txt tmp # overwrites with confirmation
$ mv
-f data.txt tmp # overwrites without confirmation
$ mv tmpdir mydir
# -r is not necessary for moving directory like `rm` or `cp` commands

# remove
$ rm
-i data.txt # removes with confirmation
$ rm
-f data.txt # removes without confirmation
$ rm
-r tmp # removes directory(even when the directory is NOT empty)
$ rm
-R tmp # removes directory(same with -r option)
$ rmdir tmp
# `rmdir` also removes EMPTY files

# create directory
$ mkdir
-p tmp/tmp2 # parents directory will be also created
$ mkdir
-m 705 mydir # creates directory with set permissions

Contar / classificar


# count
$ ls
-l | wc # shows bytes/lines/words count
$ wc
-c count.txt # shows only bytes count
$ wc
-l count.txt # shows only lines count
$ wc
-w count.txt # shows only words count

# sort
$ ls
-l | sort # sorts by alphabetic order
$ ls
-l | sort -b # sorts ignoring spaces in the opening sentence
$ ls
-l | sort -f # sorts ignoring Small/Capital
$ ls
-l | sort -r # sorts in reverse way
$ ls
-l | sort -n # sorts number not as a string but as a number

Pesquisa

Localizar


# `locate` commands search based on database
# therefore it is faster than `find` command
$ locate sort


# to update database
# * this is usually executed regularly
$ updatedb

Encontrar

Opção


# `maxdepth` otption defines the depth of search target
$ find
. -name "README.md" -maxdepth 1

Doença


# Basic
# find [targetDirectory] [option]

# By name
$ find
. -name "README.md"

# By size
$ find
. -size 1k # file size = 1k byte
$ find
. -size +100c # file size > 100 byte
$ find
. -size +1k # file size > 1k byte
$ find
. -size -100c # file size < 100 byte
$ find
. -size -1k # file size < 1k byte

# By types
$ find
. -type f # files
$ find
. -type d # directories
$ find
. -type l # symlinks

# By modified time
$ find
. -mtime -1 # files modified in a day
$ find
. -mmin -60 # files modified in last 60 mitnues
$ find
. -mtime 7 # files which have not modified over a week
$ find
. -mmin 60 # files which have not modified over an hour

# By accessed time
$ find
. -atime -1 # files accessed in a day
$ find
. -amin -60 # files accessed in last 60 minutes
$ find
. -atime 7 # files which have not accessed over a week
$ find
. -amin 60 # files which have not accessed over an hour

# By permission
$ find
. -perm a+r # all read-permission is allowed
$ find
. -perm 644 # permission code is 644
$ find
. -perm -444 # read-permission is allowed for all users
$ find
. -perm +444 # read-permission is allowed for one of owner, owner group or other users

# By users
$ find
. -user david # files owned by david
$ find
. -uid XXXXXX # files owned by declared uid

# By regexp
# @see http://www.gnu.org/software/findutils/manual/html_mono/find.html#Regular-Expressions
$ find
. regex ".*git*"

Açao


# print
$ find
. -name "*git*" # shows matched files(default)
$ find
. -name "*git*" -print # shows matched files(default)
$ find
. -name "*git*" -ls # shows file's information also

# `-exec` enables you to execute command to the result
$ find
. -name "README.md" -exec rm {} ¥; # without confirmation
$ find
. -name "README.md" -ok rm {} ¥; # with confirmation

Exemplo


# finds files updated within a day
$ find
. -type f -mtime -1

# finds files updated within past 30 minutes
$ find
. -type f -mmin -60

# find and delete files which have not been accessed in 200 days
$ find
. -atime +200 -exec rm {} ¥;

Fecho eclair


#############
# gzip
#############
# zip file
$ cal
> cal.txt
$ gzip cal
.txt # this creates cal.txt.gz but cal.txt would be deleted
$ gzip
-c cal.txt > cal.txt.gz # if original files should not be deleted

# zip directory
$ mkdir tmp

$ cp cal
.txt tmp
$ gzip
-r tmp # be sure that `gzip -r` will not zip directory itself

# let's see how different
$ ls
-l # see the difference of sizes
$ cat cal
.txt # you can see the calander
$ cat cal
.txt.gp # but this shows misterious results

# unzip
$ gzip
-d cal.txt.gp # unzip file
$ gzip
-dr tmp # unzip files in tmp directory
$ gunzip cal
.txt.gp # equals with `gzip -d` command

#############
# bzip2
#############
$ bzip2 sample
.dat # this creates sample.dat.bz2
$ bzip2
-d sample.dat.bz2 # unzip


#############
# tar (and then gzip)
#############
# tar [options] archivefile.tar targetDirectory
#
# options
# -c : creates archive
# -x : unzip archive
# -t : show files in archived file
# -f : select archive file by file name
# -r : add files to archived file
# -v : show info in details
# -z : gzip
# -j : bzip2

# example
$ mkdir tmp

$ ls
-l > tmp/ls.txt
$ cal
> tmp/cal.txt
# creates archived files
$ tar cvf tmp
.tar tmp # just archived and not compressed
$ tar cvfz tmp
.tar.gz tmp # archived and compressed with gzip
$ tar cvfj tmp
.tar.bz2 tmp # archived and compressed with bzip2
# see the dirrerences
$ ls
-l

gzip vs bzip2 vs tar?

gzip / bzip2 vs tar?

tar não é um formato de compressão – é uma maneira de combinar vários arquivos em um arquivo.

bz2 e gz são formatos de compressão – variações de um tema – taxas de compressão semelhantes, eu acho. O gerenciador de arquivos padrão com o ubuntu pode abrir a maioria, senão todos esses tipos de arquivos.

Então gzip vs bzip2?

bz2 geralmente compacta melhor do que gz, mas é mais lento. gz é o padrão de fato porque é mais antigo, e bz2 não está disponível por padrão em alguns sistemas operacionais (é por isso que você encontra principalmente arquivos .gz).

@see
http://ubuntuforums.org/showthread.php?t=1538026

Link físico / link simbólico


# shows hardlinks
$ ls
-i1 # with `-i` options, `ls` shows index node(i node)

# creates hard links
$ touch test
.txt # creates test.txt
$ ln test
.txt test.hard # creates hardlink of test.txt
$ ls
-i1 # check that i node of test.txt equals with test.hard

# creates symbol links
$ touch symbol
.txt
$ ln
-s symbol.txt symbol.sym
$ ls
-i1

# let's have a look at the differene between hard and symbol
$ touch
{hard,symbol}.txt # creates hard.txt and symbol.txt
$ ln hard
.txt hard.hard # creates hard link
$ ln
-s symbol.txt symbol.sym # creates symbol link
$ rm
{hard,symbol}.txt # removes original .txt files
$ cat hard
.hard # see how it works
$ cat symbol
.sym # see how it works

Hardlink vs Symlink?

Um hardlink não é um ponteiro para um arquivo, é uma entrada de diretório (um arquivo) apontando para o mesmo inode. Mesmo se você alterar o nome do outro arquivo, um hardlink ainda aponta para o arquivo …

Por outro lado, um link simbólico está na verdade apontando para outro caminho (um nome de arquivo); ele resolve o nome do arquivo cada vez que você o acessa por meio do link simbólico.

@Vejo

Redirecionar

# redirect result
$ ls
> redirect.txt # the result of `ls` command would be written to redirect2.txt
$ cat redirect
.txt
$ ls
-i1 >> redirect.txt # adds the result to the existing file

# redirect error
$ ls
-222 2> error.txt # 2> writes error log
$ ls
-333 2>> error.txt # 2>> adds error log to the existing file

# redirect both result and error
# . = current file
# aaa = not-existring file
$ ls
. aaa > ls.log 2>&1 # wrtites both the result and error log

# tips
# by combining `cat` and redirect, you can join the result of
# multiple files contents
$ cat apple
.txt banana.txt > banapple.txt

# input redirect
# `tr a-z A-Z` replaces all small caps to Capital caps, but
# this command cannot have file as an argument.
# By usring input `<`, a content of file can be sent to commands.
$ tr a
-z A-Z < error.txt

Tubo


$ ls
-l > ls.txt
$ tr a
-z A-Z < ls.txt

# By using pipe, above process can be written in one line
$ ls
-l | tr a-z A-Z

# If you want to send the result both to a file and other command
# then use `tee`
$ ls
-l | tee result.log | less

Do utilizador


#############
# basic
#############

# shows all users
$ id


# shows all groups
$ groups


# see User Database
$ less
/etc/passwd

# see Group Database
$ less
/etc/group

# adds user
$ useradd david

$ useradd
-u 1000 david # with udi
$ useradd
-d /home/dev # with custome home directory path

# changes password
$ passwd david


# deletes user
$ userdel david

$ userdel
-r david # deletes with home directory

# adds group
$ groupadd designer


# deletes group
$ groupdel designer


#############
# usermod
#############

# locks user
# (when users would not be used for a long time)
$ usermod
-L david

# unlocks user
$ usermod
-U david

# changes primary group
$ usermod
-g develop david

# changes sub group
$ usermod
-G develop david

# changes uid
$ usermod
-u 1000 david

# changes home directory path
$ usermod
-d /home/dev/ david

Permissão

Proprietário (usuário / grupo)


# changes owner
$ chown alice sample

$ chown
-R alice sample # changes all files in the directory

# changes group
$ chgrp develp tmp

$ chgrop
-R develop tmp

# changes owner and group at the same time
$ chown alice
:develop tmp

Tipos Acessíveis

  • Permissão
    • r: ler (4)
    • w: escrever (2)
    • x: executar (1)
  • Segmento alvo
    • proprietário
    • grupo ao qual o proprietário pertence
    • outros usuários
  • Contadores de permissão
    • 7: 4 + 2 + 1 = r + w + x
    • 5: 4 + 0 + 1 = r + x
    • 4: 4 + 0 + 0 + r

$ touch sample
.txt
$ ls
-l sample.txt
-rw-r--r-- 1 username group 0 1 10 10:11 sample.txt
# user = can read and write
# group = can read only
# others = can read only

# changes permission of sample.txt
$ chmod
755 sample.txt
$ ls
-l sample.txt
-rwxr-xr-x 1 username group 0 1 10 10:11 sample.txt

# other options
# - target : u, g, o
# - control : +, -, =
# - type : r, w, x
#
$ chmod ug
+rx sample.txt
$ ls
-l sample.txt
-rwxr--r-- 1 username group 0 1 10 10:11 sample.txt

raiz


# changes user to root user
$ su
- # you need `-` option to use some restricted commands
$ exit

Concha

Aqui, usamos principalmente bashcomo exemplo.

Uso Básico


# shows all shells list which you can use
$ cat
/etc/shells
/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

# shows PATH
$ echo $PATH


# `;`
# commands would be continually executed
$ git init
; git add -A; git commit -m "first commit"

# `&&`
# means "AND"
$ touch README
.md && git add README.md

# `||`
# means "OR"
$ cd
/tmp || echo "there is no such directory"

# shows executed commands history
$ history

$ history
-c # deletes history
$ history
5 # shows the 5 latest history
$ echo $HISTSIZE
# shows the size of saved history
$
!cat # executes the latest command begin with `cat`
$
!732 # executes the history with the number
$
!! # executes the last command in history
$
!-3 # executes the 3 before command in history
$
^cat^ls # executes the latest command, after replacing `ls` command with `cat`

Atalhos do teclado

Existem alguns atalhos de teclado úteis no bash.

  • tab : complementa comandos ou caminhos
  • ctrl + a : move o cursor para o início da linha
  • ctrl + e : move o cursor para o final da linha
  • crll + l : display claro
  • ctrl + c : parar o processo atualmente em execução
  • ctrl + s : para de mostrar o resultado no display
  • ctrl + q : reinicie mostrando o resultado no display
  • ctrl + z : para o processo atualmente em execução temporariamente
  • ctrl + d : sair
  • ctrl + r : inicia a pesquisa incremental (histórico de comandos)

Variáveis ​​Shell


# history
$ echo $HISTFILE

$ echo $HISTFILESIZE

$ echo $HISTSIZE


# home directory path
$ echo $HOME


# hostname
$ echo $HOSTNAME


# language
$ echo $LANG


# command search path
$ echo $PATH


# prompt string
# @see http://www.unix.com/shell-programming-and-scripting/131510-need-explanation-ps1-ps2-ps3-ps4.html
$ echo $PS1

$ echo $PS2
# subshell prompt string, by default ">"

# current directory path
$ echo $PWD


# current shell
$ echo $SHELL


# terminal type
$ echo $TERM


# user
$ echo $UID

$ echo $USER

Definir Variáveis ​​Shell


# defines variables
$ name
="David Brown"

# use defined variables