Snippets

Giuliano Ribeiro Linux Stuffs/Tips

Created by Giuliano Ribeiro last modified

View the Beginning of Text Files with head

The head command is a core Linux utility used to view the very beginning of a text file. Despite its narrow functionality, head is useful in many systems administration and scripting tasks. For similar functionality that address the end of a file, consider the tail utility.

Using head

Consider the following invocation:

head /etc/rc.conf

This will print the first 10 lines of the /etc/rc.conf file to standard output on the terminal. Thus, head is useful for a number of different situations such as determining the contents of a file if the file names are ambiguous.

If a file has fewer than 10 lines, head will print the entire file.

Control the Length of Output with head

With the -n option, the number of lines that head outputs can be modified. For example:

head -n 24 /etc/logrotate.conf

This prints the first 24 lines of the /etc/logrotate.conf file to the terminal. You can specify the number of lines before or after you declare the file. Therefore, the following command is equivalent to the previous command:

head /etc/logrotate.conf -n 24

If a file is smaller than the specified number of lines, head will print the entire file.

View the Beginning of Multiple Files with head

head can process multiple files at once. Consider the following:

$ ls 
example  roster

$ head * 
==> example <==
lollipop
The Joke
Jockey to the Fair
Simon's Fancy
Truckles

==> roster <==
John
Susan
Michael 
Robert
Justin

Herbert
Marissa
George
Jacob

head outputs the first ten lines of each file by default. If you are using head to read more than one file, you may also use the -n option to control the number of lines printed.

Combine head with Other Commands

head can be used to filter the output of commands as well as files. For instance:

% cat --help | head -n 2
Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s), or standard input, to standard output.

$ ls /usr/lib | head
alsa-lib
ao
apr.exp
apr-util-1
aprutil.exp
aspell
aspell-0.60
avahi
awk
bmp

In the first example, head filters the full output of cat --help to generate only the first two lines of the output of the command. In the second example, head prints the first ten lines of the output of the ls command.

Sintaxe Padrão

A sintaxe padrão do cURL é a seguinte

curl [parâmetros] [url]

Para requisições simples, não é necessário nenhum parâmetro, somente a url de destino.

curl http://localhost/

Definindo o verbo HTTP

Por padrão, uma requisição simples no cURL é feita implicitamente através do verbo GET, mas é possível alterar o verbo HTTP da requisição através da opção -X. Sendo assim, para realizar uma chamada com o verbo DELETE por exemplo, basta fazer desta forma:

curl -X DELETE http://localhost/person/foo

Enviando corpo na requisição

Principalmente em métodos POST e PUT é natural transmitir na requisição algum conteúdo no corpo, para fazer isso no cURL basta utilizar a opção -d.

curl -X POST -d 'name=Fulano&email=fulano@foo.bar' http://localhost:8888/person/

Alterando os cabeçalhos da requisição

Geralmente é necessário especificar alguma informação no cabeçalho da requisição, como por exemplo o Authorization, Content-Type, etc. Para isso, utilize a opção -H em cada item de cabeçalho adicionado.

curl -X POST -H 'Content-Type: application/json' -d '{"name": "Fulano", "email": "fulano@foo.bar"}' http://localhost:8888/person/

Salvando a resposta em arquivo

É muito comum realizar requisições para pegar um conteúdo que deverá ser salvo em um arquivo, por exemplo, baixar configurações, credenciais e os famosos dotfiles. Existem duas formas de se fazer isso, uma é direcionando a saída do curl para um arquivo através do >:

curl http://mydotfiles.net/vimrc > ~/.vimrc

Ou através da opção -o:

curl -o ~/.vimrc http://mydotfiles.net/vimrc

Exibindo o cabeçalho de resposta

O cURL por padrão exibe somente o conteúdo do corpo da resposta, porém, muitas vezes (para não dizer todas), é necessário visualizar os cabeçalhos de retorno, como por exemplo o status code. Nesses casos utilize a opção -i.

curl -i http://localhost:8888/person
telnet IPDOSERVIDOR smtp
HELO DOMINIOQUEVAIENVIAROEMAIL.com
mail from: EMAIL@DOMINIOQUEVAISAIROEMAIL.com
rcpt to: EMAIL@DOMINIOQUEVAIENVIAROEMAIL.com
data
TESTEEEEE
.
quit

If you put your keys to ~/.ssh/authorized_keys but first you needed to create this folder and files, make sure to set the right permission on these files.

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.