Bash, Ficheiros e Extensões

Alguns truques de bash úteis (e post para eu me lembrar deles):

FICH=”/home/bruno/abc.txt”

FICH_SEM_DIR=`basename $FICH`
echo “O ficheiro sem o caminho absoluto e’ $FICH_SEM_DIR “#abc.txt

NOME_FICH=${FICH_SEM_DIR%.*}
echo “O ficheiro sem o caminho absoluto e sem extensao e’ $NOME_FICH ” #abc

EXTENSAO_DO_FICH=${FICH_SEM_DIR#*.}
echo “A extensao do ficheiro e’ $EXTENSAO_DO_FICH “#txt

#se soubermos a priori a extensão do ficheiro é mais simples

FICH_SEM_DIR_SEM_EXTENSAO=`basename $FICH .txt`
echo “O ficheiro sem o caminho absoluto e sem extensao e’ $FICH_SEM_DIR_SEM_EXTENSAO “#abc

facebooktwittergoogle_plusredditpinterestlinkedinmail
Posted in Linux. Tags: , , . No Comments »
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Pub.

Detach a running process – Linux

Sometimes I start a job remotely and only after I remember that I may need to log-off, but the process must continue to run afterwards. This can be done with command disown. On the same shell where your process is running type Ctrl+Z, to pause the program, you’ll see the job id, something like [1], you need it for the disown command, now restart the job in background with the command bg and you’re ready to issue the disown command:

disown -h %1

you can also use the pid1 of the process (use jobs -l on that same shell and you’ll get a longer number that is the pid of the process you want to detach):

disown -h pid

after that you can close the shell with exit or Ctrl+D.

If you start your program with nohup2 or screen you don’t have to worry about log-off and running processes.

This command can also be used in Unix (such as BSD/MacOS X/etc.) but their might be implementation differences.

1 After some interaction on Google+ with CNF we found out that the pid is accepted but it is not documented. I’ve changed the post to reflect a more appropriated use of the disown command (according to the manual).

2 disown -h is is basically a nohup a posteriori.

Solution found on serverFault.

facebooktwittergoogle_plusredditpinterestlinkedinmail
Posted in Linux. Tags: , . 2 Comments »
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Pub.

Download Youtube Playlist with youtube-dl

To download a full playlist from Youtube with youtube-dl do the following:

youtube-dl.py -A -t -c “http://www.youtube.com/p/1234567890ABCDEF”

-A makes youtube-dl auto-number the files downloaded in order (0001, 0002, …)

-t uses the title page, otherwise the file name will be non-understandable

-c continues in case of failure

In case your playlist url is something like: http://www.youtube.com/playlist?list=1234567890ABCDEF transform it into the format: http://www.youtube.com/p/1234567890ABCDEF otherwise you’ll get an error.

One can specific also the quality of the video by using the -fmt FMT or –max-quality=FMT where FMT is number as can be seen here, 34 is 360p, 35 480p, 37 is 1080p …

youtube-dl can be installed in your Linux distro using the normal package managers or you can download it by hand to your favorite directory from here. On this directory executing youtube-dl -U makes the script get auto-updated to the latest version – important because sometimes Youtube changes things that make elder versions of the script not work.

facebooktwittergoogle_plusredditpinterestlinkedinmail
Posted in Linux, Tech. Tags: , , . 1 Comment »
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Pub.

Saber detalhes da RAM instalada a partir da linha de comandos

Já mais do que uma vez que precisei de saber detalhes sobre a memória RAM que tenho instalada no sistema (velocidade, tipo, tamanho, etc.), mas não me apetece/posso abrir a caixa do PC.
Em Linux podem usar o comando dmidecode para obter estas e muitas outras informações do vosso hardware. A sequência de comandos seguinte mostra somente a informação (relevante) sobre a memória RAM instalada no computador.

sudo dmidecode | grep -A20 ‘Memory Device’ | grep -v ‘Range’ |grep ‘Speed:\|Part Number\|Size:\|Type:’

No sistema onde estou a testar isto obtenho como resultado:

Size: 2048 MB
 Type: DDR2
 Speed: 800 MHz (1.2 ns)
 Part Number: EBEXXXX8AXXX-XX-X
 Size: No Module Installed
 Type: DDR2
 Speed: Unknown
 Part Number:

Nota:O dmidecode pode estar fora do vosso PATH, só acessível ao root ou não estar instalado no sistema.

facebooktwittergoogle_plusredditpinterestlinkedinmail
Posted in Linux. Tags: , , , . No Comments »
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Como procurar vários padrões no mesmo comando ‘grep’

Para procurar vários padrões no mesmo comando grep basta usar \| como separador entre os diversos padrões.

grep “padrao1\|padrao2\|padrao3” fich.txt

facebooktwittergoogle_plusredditpinterestlinkedinmail
Posted in Linux. Tags: , , . No Comments »
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Como montar uma imagem ISO em Linux

Porque me estou sempre a esquecer disto, aqui vai:

mount -o loop /path/to/disk.iso /path/to/mount-dir

Por omissão é assumido o file system ISO9660, se for preciso forçar deve-se adicionar -t iso9660 ao comando anterior. Normalmente é necessário ter-se permissões de administração para invocar o mount.

facebooktwittergoogle_plusredditpinterestlinkedinmail
Posted in Linux. Tags: , . No Comments »
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...