Mostrando entradas con la etiqueta bash. Mostrar todas las entradas
Mostrando entradas con la etiqueta bash. Mostrar todas las entradas

20120321

Script para generar conjuntos de archivos - Script to generate sets of files

Después de un tiempo sin postear, he vuelto, al menos por ahora!!
Estos días he necesitado varios conjuntos de archivos para realizar pruebas de rendimiento, y para generar estos, he creado el siguiente script, que ahora comparto con vosotros.
Espero que os sea de utilidad.


After some time without posting, I'm back!
This days I needed some sets of files to make some performance tests. To generate this files I wrote the following script that I'm going to share with all of you.
I hope you find it useful.

PS. I know, I know. My English is not good enought but, the more you help me, the more I learn.

#!/bin/bash
#
FILES_PATH="$PWD/files"                    # path to create de files
declare -i DIR_NUM=13                      # num. of dirs to create
declare -i FILE_NUM=1000                   # num. of files to create
declare -i FILE_MIN_SIZE=10                # in kB
declare -i FILE_MAX_SIZE=200               # in kB
declare -i FILE_SIZE=$FILE_MAX_SIZE

if [ ! -d $FILES_PATH ]; then
  mkdir $FILES_PATH
fi

for (( dir=1;dir<=$DIR_NUM;dir++ )); do
  mkdir "$FILES_PATH/dir_$dir"
  for (( file=1;file<=$FILE_NUM;file++ )); do
    FILE_SIZE=$(($RANDOM%(FILE_MAX_SIZE)-$FILE_MIN_SIZE))+$FILE_MIN_SIZE
    FILE_SIZE=$(($RANDOM%FILE_MAX_SIZE))
    FILE_SIZE=$FILE_SIZE+$FILE_MIN_SIZE
    dd count=$FILE_SIZE if=/dev/zero of="$FILES_PATH/dir_$dir/file_$file" >> /dev/null 2>&1
  done
done

20111105

Modo VI en la línea de comandos - VI mode on the command line

Desde los primeros días de mi primer trabajo en el que tuve que enfrentarme con la consola de equipos GNU/Linux me acostumbré, por consejo de un ex-jefe y amigo, a utilizarla en modo de comandos vi.
Para conseguir esto, basta con introducir lo siguiente:
set -o vi

A partir de ese momento, la consola funciona como el editor vi, así tienes un modo de inserción y un modo de ejecución de comandos. Desde este, por ejemplo, puedes realizar búsquedas en tu historial de comandos, como si buscases dentro de un documento de texto con vi.
Para volver al modo habitual, introduce lo siguiente:
set -o emacs

Por defecto los sistemas suelen venir en modo emacs, por lo que si como yo, prefieres el modo vi, puedes hacer que sea el predeterminado añadiendo la siguiente entrada al fichero /etc/inputrc:
set editting-mode vi

A disfrutarlo (o sufrirlo según el caso ;P)

Since first days I start working with GNU/Linux I got used to type and work with the vi mode enabled on the command line. To achieve this, just type the following:
set -o vi

Once you have change into the vi mode, the command line works as the vi editor, so it has the insert and the commands modes to work with. So if you, for instance, wants to search for a recently typed command, you can look for it on the same way you'll do on the vi editor.
To return to the previos configuration, just type:
set -o emacs

By default, the systems use to be configured on the emacs mode, but if you like the vi mode, you can do it the the default mode just adding to the /etc/inputrc the following line:
set editting-mode vi

Enjoy it (or suffer with it ;P)

PS. I know, I know. My English is not good enought but, the more you help me, the more I learn.

20110829

Renombrado de ficheros por lotes - Batch renaming files

Me ha vuelto a pasar!!! He ejecutado el siguiente script desde la línea de comandos para el renombrado de archivos en lotes:
for i in `ls`; do mv $i BKP_$i; done

mediante el cual, añado el prefijo BKP_ a todos los ficheros del directorio de trabajo, peeeeero lo he hecho en la ruta equivocada!!! Así que tengo un montón de archivos a los que les he añadido un prefijo que tengo que quitar.
La solución (o una de ellas) me la ofrece el comando cut. Con él, podemos seleccionar de una cadena de texto, los caracteres que queramos mostrar o quitar (--complement).
El nuevo script para reparar el entuerto sería:
for i in `ls`; do mv $i `echo $i| cut -c 1-4 --complement`; done

Y problema resuelto.

Ups, I did it again!!! I have run the following script from the command line to rename some files:
for i in `ls`; do mv $i BKP_$i; done

to add the prefix BKP_ to all files in the current directory, but I did it on the wrong path!!! So I have lots of files with a prefix to remove.
The solution (or at least, one of them) is to use the command cut. We can use it to choose some characters from a string.
The script to repair my mistake and remove (--complement) this prefix is:
for i in `ls`; do mv $i `echo $i| cut -c 1-4 --complement`; done

And its done!!!

PS. I know, I know. My English is not good enought but, the more you help me, the more I learn.