Con esta práctica se proporciona al alumno una batería de
operaciones para realizar únicamente con el comando find. En
cada enunciado aparece un apartado Keywords que deja ver los
elementos a incorporar en la conjugación del comando y que,
por supuesto, tienen que ser consultados en la página de
manual antes de utilizarlos correctamente.
1) Localiza
recursivamente (todas las
ramificaciones de directorios a partir del punto que se indique)
los ficheros regulares cuyo nombre sea core contenidos
en /tmp y bórralos.
Keywords: -name
-exec
NOTA: En caso de no existir ninguno, producir material
para obtener resultados creando directorios por debajo de /tmp en
varios niveles de profundidad con el comando mkdir y
poblándolos con archivos de nombre core con el comando touch.
2) Posiciónate en el
directorio /usr/bin y ejecuta el comando file
para cada uno de los ficheros regulares existentes en él.
Keywords: -exec
3) Se desea conocer el
número de ficheros regulares que existen en el sistema (desde /)
que cumplan con los siguientes atributos de permisos de acceso y
sólo con ellos (cualquier otro permiso activo además de los
buscados debe excluir el archivo): Lectura y escritura para el
propietario y el grupo propietario, sólo lectura para otros
usuarios. Antes de proceder, el alumno generará un espacio (mkdir)
donde se inserten ficheros (touch) con estas
caracteristicas y otras diferentes (chmod) y pruebe
prototipos del comando find hasta encontrar la
conjugación correcta:
Keywords: -perm
4) Se desea conocer el
número de ficheros regulares que existen en el sistema (desde /)
que cumplan con los siguientes atributos de permisos de acceso
además de cualquier otro: Lectura y escritura para el
propietario y el grupo propietario, lectura para otros usuarios.
Antes de proceder, el alumno generará un espacio (mkdir)
donde se inserten ficheros (touch) con estas
caracteristicas y otras diferentes (chmod) y pruebe
prototipos del comando find hasta encontrar la
conjugación correcta:
Search for files which are
writable by somebody (their owner, or their group, or anybody
else).
find . -perm /220
find . -perm /u+w,g+w
find . -perm /u=w,g=w
All three
of these commands do the same thing, but the first one uses
the octal representation of the file mode, and the other two
use the symbolic form.
These commands all search for files which are writable by either
their owner or their group. The files don't
have to be writable by both
the owner and group to be matched; either will do.
find . -perm -220
find . -perm -g+w,u+w
Both these commands do the
same thing; search for files which are writable by both their
owner and their group.
find . -perm -444 -perm /222
\! -perm /111
find . -perm -a+r -perm /a+w
\! -perm /a+x
These two commands both
search for files that are readable for everybody ( -perm -444 or
-perm -a+r), have at least one write bit set
( -perm /222 or -perm /a+w)
but are not executable for anybody ( ! -perm /111 and ! -perm /a+x
respectively).
cd /source-dir
find . -name .snapshot -prune
-o \( \! -name '*~' -print0 \)|
cpio -pmd0 /dest-dir
This command copies the
contents of /source-dir to /dest-dir, but omits files and
directories named .snapshot (and anything in them).
It also omits files or
directories whose name ends in ~, but not their contents.
The construct -prune -o \( ... -print0 \) is quite
common. The idea here
is that the expression before -prune matches things which are to
be pruned. However, the -prune action itself
returns true,
so the following -o ensures that the right hand side is
evaluated only for those directories which didn't get pruned
(the contents of the pruned
directories are not even visited, so their contents are
irrelevant). The expression on the right
hand
side of the
-o is in parentheses only for clarity. It emphasises
that the -print0 action takes place only for things that didn't
have -prune applied to
them. Because the default `and' condition between tests
binds more tightly than -o, this is the default any‐
way, but the parentheses help
to show what is going on.
find repo/ \( -exec test -d
'{}'/.svn \; -or \
-exec test -d {}/.git \; -or
-exec test -d {}/CVS \; \) \
-print -prune
Given the
following directory of projects and their associated SCM
administrative directories, perform an efficient search for the
projects' roots:
repo/project1/CVS
repo/gnu/project2/.svn
repo/gnu/project3/.svn
repo/gnu/project3/src/.svn
repo/project4/.git
In this example, -prune
prevents unnecessary descent into directories that have already
been discovered (for example we do not search
project3/src because we
already found project3/.svn), but ensures sibling directories
(project2 and project3) are found.
find /tmp -type f,d,l
Search for
files, directories, and symbolic links in the
directory /tmp passing these types as a comma-separated list (GNU
exten‐
sion), which is otherwise
equivalent to the longer, yet more portable:
find /tmp \( -type f -o -type
d -o -type l \)
(1) Recursivamente significa en
todas las ramificaciones de directorios a partir del punto que se
indique.