Example rename patterned files in subfolders: Difference between revisions
From WickyWiki
mNo edit summary |
mNo edit summary |
||
| Line 5: | Line 5: | ||
Example-script to rename multiple files in subfolders that have a pattern, include the top subfolder in the new pattern. | Example-script to rename multiple files in subfolders that have a pattern, include the top subfolder in the new pattern. | ||
Filename format: | Filename location and format: | ||
* ". | * "./${year}/${title} by ${author}.${ext}" | ||
* "./1992/Greatest Story by A.Uthor.txt" | |||
New name and location: | New name and location: | ||
* "./${author} - Stories [${year}] - ${title}.${ext}" | * "./${author} - Stories [${year}] - ${title}.${ext}" | ||
* "./A.Uthor - Stories [1992] - Greatest Story.txt" | |||
<syntaxhighlight lang=bash> | <syntaxhighlight lang=bash> | ||
| Line 46: | Line 49: | ||
done | done | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Note: the resulting script 'aaa_copy.sh' can not be executed as it is not executable. You should check a few lines first and then copy these to a terminal to test the results. Then the whole contents of the script can be done the same way. | |||
=See also= | =See also= | ||
Latest revision as of 11:47, 16 February 2019
Example-script to rename multiple files in subfolders that have a pattern, include the top subfolder in the new pattern.
Filename location and format:
- "./${year}/${title} by ${author}.${ext}"
- "./1992/Greatest Story by A.Uthor.txt"
New name and location:
- "./${author} - Stories [${year}] - ${title}.${ext}"
- "./A.Uthor - Stories [1992] - Greatest Story.txt"
#go to the folder
cd "/some/folder/on/a/disk"
echo "#!/bin/bash" > ./aaa_copy.sh
# index files .txt and .rtf in subfolders
unset files i
while IFS= read -r -u3 -d $'\0' file; do
files[i++]="$file"
done 3< <( find "./" -type f \( -iname "*.txt" -o -iname "*.rtf" \) -print0 )
echo "${#files[@]} files found"
# loop over found files
i=0
while [ $i -lt ${#files[@]} ] ; do
file="${files[$i]}"
echo $file
#get path, name, ext
path=$(dirname "${file}")
nameext="${file##*/}"
name="${nameext%.*}"
ext="${nameext##*.}"
#get year,title,author
year=$( echo $path | rev | cut --delimiter=/ -f1 | rev )
title=$( echo $name | sed 's/ [Bb]y .*//' )
author=$( echo $name | sed 's/.* [Bb]y //' )
#add new name to rename script
echo "cp \"${file}\" \"./${author} - Stories [${year}] - ${title}.${ext}\"" >> ./aaa_copy.sh
i=$(( $i + 1 ))
done
Note: the resulting script 'aaa_copy.sh' can not be executed as it is not executable. You should check a few lines first and then copy these to a terminal to test the results. Then the whole contents of the script can be done the same way.