Un mundo libre

Blog sobre cultura libre en internet, linux, etc

El comando cat

Jun-11-2010 By Libre

Pocas veces un comando parece que no hace demasiadas cosas, pero “en la sombra”, tiene grandes usos. Este es el caso del comando “cat“.

Su definición formal dice que “concatena ficheros e imprime por la salida estándar”. Por supuesto esto es cierto, pero podemos hacer más cosas gracias a la potencia de cat y al shell.

En primer lugar es posible que veamos los caracteres no imprimibles; vale…, esto se puede hacer con otros programas como por ejemplo “vi”, pero gracias a esta funcionalidad y a la redirección de linux podemos hacer cosas como por ejemplo filtrar la salida de un “ls” para comprobar si hay caracteres no imprimibles en el nombre de un fichero.

# ls -l |cat -vettotal 124$drwxr-xr-x   2 root root  4096 2010-05-23 08:46 bin$drwxr-xr-x   4 root root  4096 2010-02-02 05:31 boot$lrwxrwxrwx   1 root root    11 2009-09-27 22:52 cdrom -> media/cdrom$drwxr-xr-x  18 root root  4280 2010-06-07 06:04 dev$drwxr-xr-x 151 root root 12288 2010-06-07 06:05 etc$drwxr-xr-x   6 root root  4096 2010-06-04 23:48 home$lrwxrwxrwx   1 root root    33 2009-09-27 23:06 initrd.img -> boot/initrd.img-2.6.28-11-generic$drwxr-xr-x  16 root root 12288 2010-06-04 16:39 lib$drwxr-xr-x   7 root root 12288 2010-06-04 16:39 lib32$lrwxrwxrwx   1 root root     4 2009-09-27 22:52 lib64 -> /lib$drwx------   2 root root 16384 2009-09-27 22:52 lost+found$drwxr-xr-x   3 root root  4096 2010-06-07 06:04 media$drwxr-xr-x   2 root root  4096 2009-04-13 11:33 mnt$drwxr-xr-x   7 root root  4096 2010-01-20 05:58 opt$dr-xr-xr-x 169 root root     0 2010-06-07 08:03 proc$drwx------  18 root root  4096 2010-06-04 23:48 root$drwxr-xr-x   2 root root 12288 2010-06-04 16:39 sbin$drwxr-xr-x   2 root root  4096 2009-03-06 18:16 selinux$drwxr-xr-x   2 root root  4096 2009-04-20 15:59 srv$drwxr-xr-x  12 root root     0 2010-06-07 08:03 sys$drwxrwxrwt  14 root root 12288 2010-06-07 06:33 tmp$drwxr-xr-x  13 root root  4096 2009-11-26 08:39 usr$

En este caso no tenemos nombres con caracteres no imprimibles ni tabuladores en el listado, pero sí podemos ver los saltos de línea.

Por supuesto podemos mostrar la salida de cat con número de línea, por si tenemos la necesidad de saber exactamente en qué línea estamos viendo algo:
# ls -l |cat -n  1    total 124  2    drwxr-xr-x   2 root root  4096 2010-05-23 08:46 bin  3    drwxr-xr-x   4 root root  4096 2010-02-02 05:31 boot  4    lrwxrwxrwx   1 root root    11 2009-09-27 22:52 cdrom -> media/cdrom  5    drwxr-xr-x  18 root root  4280 2010-06-07 06:04 dev  6    drwxr-xr-x 151 root root 12288 2010-06-07 06:05 etc  7    drwxr-xr-x   6 root root  4096 2010-06-04 23:48 home  8    lrwxrwxrwx   1 root root    33 2009-09-27 23:06 initrd.img -> boot/initrd.img-2.6.28-11-generic  9    drwxr-xr-x  16 root root 12288 2010-06-04 16:39 lib 10    drwxr-xr-x   7 root root 12288 2010-06-04 16:39 lib32 11    lrwxrwxrwx   1 root root     4 2009-09-27 22:52 lib64 -> /lib 12    drwx------   2 root root 16384 2009-09-27 22:52 lost+found 13    drwxr-xr-x   3 root root  4096 2010-06-07 06:04 media 14    drwxr-xr-x   2 root root  4096 2009-04-13 11:33 mnt 15    drwxr-xr-x   7 root root  4096 2010-01-20 05:58 opt 16    dr-xr-xr-x 169 root root     0 2010-06-07 08:03 proc 17    drwx------  18 root root  4096 2010-06-04 23:48 root 18    drwxr-xr-x   2 root root 12288 2010-06-04 16:39 sbin 19    drwxr-xr-x   2 root root  4096 2009-03-06 18:16 selinux 20    drwxr-xr-x   2 root root  4096 2009-04-20 15:59 srv 21    drwxr-xr-x  12 root root     0 2010-06-07 08:03 sys 22    drwxrwxrwt  14 root root 12288 2010-06-07 06:33 tmp 23    drwxr-xr-x  13 root root  4096 2009-11-26 08:39 usr 24    drwxr-xr-x  15 root root  4096 2010-04-24 22:07 var 25    lrwxrwxrwx   1 root root    30 2009-09-27 23:06 vmlinuz -> boot/vmlinuz-2.6.28-11-generic

También lo podemos utilizar como editor rápido. Esto es llamado en bash “here document“

# cat > documento_nuevo << EOFprimerasegundaterceraEOF
Con la sentencias anteriores tendremos un fichero llamado documento_nuevo y el contenido será “primera, sgunda, tercera”, tal cual lo hemos escrito en el documento

Comments are closed.