Titlepdf. Bash  script .⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

in #bash5 months ago (edited)
EspañolEnglish

 

 

   

 

 

       

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
que permiten visualizar y/o alterar los metadatos de un
documento pdf, de forma arbitraria,
he elegido la utilidad pdfinfo

 
   $ pdfinfo 1234567890.pdf
   Title: titlepdf
   Author: xae
   Creator: Writer
   Producer: PDFsharp 1.31.178 ...
   ...
 

Un título


                                   

 
   METADATA=$(pdfinfo "$FILE" 2> /dev/null)
   # Title
   # Extract
   TITLE="${METADATA#Title:*}"
   TITLE="${TITLE%%[[.newline.]]*}"
   # Trim
   TITLE="${TITLE#${TITLE%%[![.space.]]*}}"
 

Saneando

   Es necesario asegurarse de que el nombre de archivo es seguro,
considero que un nombre de archivo es seguro si sólo contiene
caracteres alfanuméricos del idioma en uso.
Como carácter de reemplazo utilizo el guión bajo.

 
   # Make path safe
   REPLACE_CHAR=_
   TITLE="${TITLE//[![:alnum:]]/"$REPLACE_CHAR"}"
   # Remove replace char runs
   TITLE="${TITLE//+("$REPLACE_CHAR")/"$REPLACE_CHAR"}"
 

ya terminamos...


                                    

   # Rename
   mv -- "$FILE" "$TITLE".pdf
 

 

 

📜

   

 

 

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


EnglishEspañ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
the metadata of a pdf  file,
today we will be using pdfinfo

 
   $ pdfinfo 1234567890.pdf
   Title: titlepdf
   Author: xae
   Creator: Writer
   Producer: PDFsharp 1.31.178 ...
   ...
 

A title


                                   

 
   METADATA=$(pdfinfo "$FILE" 2> /dev/null)
   # Title
   # Extract
   TITLE="${METADATA#Title:*}"
   TITLE="${TITLE%%[[.newline.]]*}"
   # Trim
   TITLE="${TITLE#${TITLE%%[![.space.]]*}}"
 

Sanity check

   It is a must to test that filenames are secure,
by secure is meant a name made of only alphanumeric
characters of the current locale.
As replace char we are using the underscore.

 
   # Make path safe
   REPLACE_CHAR=_
   TITLE="${TITLE//[![:alnum:]]/"$REPLACE_CHAR"}"
   # Remove replace char runs
   TITLE="${TITLE//+("$REPLACE_CHAR")/"$REPLACE_CHAR"}"
 

finishing up...


                                    

   # Rename
   mv -- "$FILE" "$TITLE".pdf
 

 

 

📜

   

 

 

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

Sort:  
Loading...