使用title标签重命名并替换所有文件夹中的url

时间:2016-06-03 14:13:50

标签: php bash sed

我需要使用bash脚本来执行此操作: 用文件的标题标签替换原始文件名(而不是我想要添加“_”的单词之间的空格。 然后将包含原始名称的所有文件夹中的所有链接替换为新的标题名称

我从这开始:

#!/bin/bash
for f in *.html ; do 

mv -v "$f" "$(grep -oP '<title>\K.+?</title>' $f | sed 's#</title>##').html"
mv "$file" `echo $file | tr ' ' '_'` ;  
done

但空格仍然存在,我不知道如何用新标题名称替换整个文件夹中的url 谢谢

3 个答案:

答案 0 :(得分:1)

为了在子文件夹中执行此操作,您应该使用“find”和“while循环”。

类似的东西:

#!/bin/bash
find . -name "*.html" > listing.txt
while read filepath  
do  
    # Considering the subfolder can have spaces,
    # We will start by separating the filename
    # And dirname
    folderpath=`dirname "$filepath"`
    filename=`basename "$filepath"`  

    # With GNU GREP
    new_name=$(grep -oP '<title>\K.+?</title>' "$filepath" | sed 's#</title>##' | tr ' ' '_')
    # With BSD GREP (for who need the info)
    # new_name=$(grep -o '<title>.*</title>' $filepath | sed -e 's/<title>\(.*\)<\/title>/\1/g' | tr ' ' '_')
    mv -v "$filepath" "$folderpath/`basename $new_name`.html"  
done < listing.txt

答案 1 :(得分:0)

我不确定你为什么要使用mv两次,尝试使用变量存储新文件名&amp;在那里重写所有内容。

#!/bin/bash
for f in *.html ; do 
  new_name=$(grep -oP '<title>\K.+?</title>' "$f" | sed 's#</title>##' | tr ' ' '_')
  mv -v "$f" "${new_name}.html"  
done

答案 2 :(得分:0)

这是我对这项任务的最终讽刺: 脚本这样做:

#!/bin/bash

cd /some/folder

###############################################################################
## DEFINITIONS                                                               ##
###############################################################################
## Work directory:
DIR=${1}

## Log file to put the files without title:
LOG="log.txt"

###############################################################################
## MAIN                                                                      ##
###############################################################################
for FILE in $(find ${DIR} -name "*.html"); do

    ## Get the string that will be the file new name.
    PATTERN=$(cat ${FILE} |grep -o -P '(?<=<title>).*(?=</title>)')

    ## Some files dont have the <title>some string</title> tag, so dont do any-
    ## thing.
    if [ ! -z "${PATTERN}" ]; then
        ## Get the file directory base and the basename. Both are necessaries
        ## to replace the file indications and rename files.
        BAS_FILE_NAME=$(basename ${FILE})
        DIR_FILE_NAME=$(dirname  ${FILE})

        ## Prepare the string that will be the file new name. Change the space
        ## by the underscore (_).
        STRING=$(basename "${PATTERN}" |tr -s ' ' '_')

        ## Replace all entries:
        for TMP in $(find ${DIR} -name "*.html"); do
           sed -i -e "s/${BAS_FILE_NAME}/${STRING}.html/g" ${TMP} 2> /dev/null
        done

        ## Move the file to new name:
        mv "${FILE}" "${DIR_FILE_NAME}/${STRING}.html"

    else
        ##
        echo "${FILE} : Without <title> tag!" >> ${LOG}
    fi
done

希望有人会喜欢它,很高兴来到这里,谢谢大家......