Español | English |
Es normal encontrarse con archivos pdf con nombres de archivo tan descriptivos como 1606152010.pdf ...
Si el autor asignó un título más apropiado en el campo Title de los metadatos del documento pdf , es posible renombrar éste de forma automática, vamos a ver cómo.
En Debian puedes encontrar diversas herramientas | |
---|---|
|
Un título | ||
---|---|---|
|
Saneando Es necesario asegurarse de que el nombre de archivo es seguro, | |
---|---|
|
ya terminamos... | ||
---|---|---|
# Rename |
📜
titlepdf.sh
Juntando todas las piezas,
#!/bin/bash ### # titlepdf # # Rename pdf file as the title metadata. # Makes or not the renaming without error code. # # Dependencies, # file # pdfinfo # # (c) 2016, xae. # # 20160221 ### declare -r SCRIPT=${0##*/} # depends # Verify if a dependencie is satisfied, # if not returns 0. function depends { [ -e $1 ] || { printf "%s\n" "$SCRIPT:$LINENO: $1. Not avalaible." >&2 ; return 0; } return 1 } # Usage # Exits from script with error code 1 function usage { printf "$1\nUsage:\n\ttitlepdf pdf_file\n\n" >&2 exit 1 } # Dependencies depends "/usr/bin/file" || depends "/usr/bin/pdfinfo" && exit 1 [ $# -eq 0 ] && usage shopt -s extglob for FILE in "$@"; do # Test if input is valid [[ "$(file -b "$FILE")" =~ 'PDF' ]] || continue # Extract metadata METADATA=$(pdfinfo "$FILE" 2> /dev/null) || continue # Title # No title tag [ -z "${METADATA##*Title:*}" ] || continue # Extract TITLE="${METADATA#Title:*}" TITLE="${TITLE%%[[.newline.]]*}" # Trim TITLE="${TITLE#${TITLE%%[![.space.]]*}}" # No title [ -z "${TITLE}" ] && continue # Make path safe REPLACE_CHAR=_ TITLE="${TITLE//[![:alnum:]]/"$REPLACE_CHAR"}" # Remove replace char runs TITLE="${TITLE//+("$REPLACE_CHAR")/"$REPLACE_CHAR"}" # Rename mv -i -- "$FILE" "$TITLE".pdf done exit 0
∎
English | Español |
Often you get pdf files named like 1606152010.pdf ,
a very illustrative title ...
If the author gave a better name in the Title metadata field of the pdf document, renaming the file would be possible , let's do it...
Debian provides several tools to manage | |
---|---|
|
A title | ||
---|---|---|
|
Sanity check It is a must to test that filenames are secure, | |
---|---|
|
finishing up... | ||
---|---|---|
# Rename |
📜
titlepdf.sh
All pieces together,
#!/bin/bash ### # titlepdf # # Rename pdf file as the title metadata. # Makes or not the renaming without error code. # # Dependencies, # file # pdfinfo # # (c) 2016, xae. # # 20160221 ### declare -r SCRIPT=${0##*/} # depends # Verify if a dependencie is satisfied, # if not returns 0. function depends { [ -e $1 ] || { printf "%s\n" "$SCRIPT:$LINENO: $1. Not avalaible." >&2 ; return 0; } return 1 } # Usage # Exits from script with error code 1 function usage { printf "$1\nUsage:\n\ttitlepdf pdf_file\n\n" >&2 exit 1 } # Dependencies depends "/usr/bin/file" || depends "/usr/bin/pdfinfo" && exit 1 [ $# -eq 0 ] && usage shopt -s extglob for FILE in "$@"; do # Test if input is valid [[ "$(file -b "$FILE")" =~ 'PDF' ]] || continue # Extract metadata METADATA=$(pdfinfo "$FILE" 2> /dev/null) || continue # Title # No title tag [ -z "${METADATA##*Title:*}" ] || continue # Extract TITLE="${METADATA#Title:*}" TITLE="${TITLE%%[[.newline.]]*}" # Trim TITLE="${TITLE#${TITLE%%[![.space.]]*}}" # No title [ -z "${TITLE}" ] && continue # Make path safe REPLACE_CHAR=_ TITLE="${TITLE//[![:alnum:]]/"$REPLACE_CHAR"}" # Remove replace char runs TITLE="${TITLE//+("$REPLACE_CHAR")/"$REPLACE_CHAR"}" # Rename mv -i -- "$FILE" "$TITLE".pdf done exit 0
∎
Media
Free Software Foundation, FAL, via Wikimedia Commons | ||
---|---|---|
VectorVoyager, via Wikimedia Commons | VectorVoyager, via Wikimedia Commons |