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

avatar
(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



0
0
0.000
1 comments
avatar

Keep up the good work. 👏🎵

Dear beloved Hive creator,

Coding poet Gudasol here to support you sharing your art + life on Hive.

As a fellow creator, I know how hard it is to get the word out there.

I built cXc.world to help creators like us get more support from the blockchain community + beyond.

Share your music on cXc.world, and copy the Markdown for a easy post includes embedded players for Spotify, Youtube, Soundcloud.

That way, you can earn HIVE + stack streams on centralized platforms, as they do still matter.

Not a music creator? No problem. You can still use cXc.world to find + share music you love.

What's next?

Preview the next evolution of cXc, Tetra.earth.

Expose local music from your area!

We're helping grassroots musicians, and you can too by adding their music (no sign up or WAX account required).

how to add music on cXc.world

Join our community 🐬

Find fellow music lovers in cXc's Discord

Bad news: Saying see you later to Hive! 👋

We didn't get the needed support to continue cXc.world on Hive, as our DHF proposal lacked votes, but [Good News Everyone] cXc.world will add a Markdown copy button, allowing you to easily share your music + music you find on Hive.

For now, we're on WAX, with tools you can use to mint your own Music/Media NFT collection.

Curious about the future of Earth + ET relations? New economic systems?

Find more apps + art from Gudasol

Want to build tools like I used to share this?

I'd love to show you some tips on AI Code generation

0
0
0.000