从文件名中删除双前缀
Remove double prefix from filenames
我的文件嵌套在文件夹中。如果文件名以重复两次的字符串开头,我想将文件名更改为使该字符串只出现一次的名称。
例如:
- 重命名"Dirty Projectors - Dirty Projectors - Search For Life.mp3"
至 "Dirty Projectors - Search For Life.mp3"
- 将 "Eefje de Visser - Eefje de Visser - Bitterzoet.mp3" 重命名为 "Eefje de Visser - Bitterzoet.mp3"
- 保持 "Feng Suave - Maybe Another Time.mp3" 不变。
如何在 Linux 中使用命令行执行此操作?
How can I do this using command-line in Linux?
经过一些研究,它似乎有点复杂。因此,它可能最终会出现在脚本中。
renameMusic.sh
#!/bin/bash
DIRECTORY=
cd ${DIRECTORY}
echo "Normalize filenames in $(pwd)"
for FILENAME in *; do
echo
echo "Checking file ${FILENAME}"
# Count number of dash (-)
NUMBER=$(grep -o '-' <<< ${FILENAME} | wc -l)
echo "Found ${NUMBER} of dash (-)"
# Check if the artist name seems to be there twice
if [[ "${NUMBER}" -eq 2 ]]; then
# If so, remove the double part
NEWFILENAME=$(echo "${FILENAME}" | cut -d "-" -f 2- | sed -e 's/^[[:space:]]*//')
echo "Going to move to new name ${NEWFILENAME}"
# mv ${FILENAME} ${NEWFILENAME}
fi
done
感谢
- Looping through
ls
results in Bash shell script
- How to loop through file names returned by find?
- Count total number of occurrences using
grep
- Comparing numbers in Bash
- How to trim whitespace from a Bash variable?
- Whats the difference between
rename
and mv
- When to wrap quotes around a shell variable?
我结合了@Barmar 的正则表达式和@U880D 的shell 脚本以递归地删除文件名中的任何重复前缀。使用 find
和 perl-rename
时出现问题,所以我不得不这样做:
#!/bin/bash
shopt -s globstar;
for DIR in ${PWD}/**/; do
cd "${DIR}"
for FILENAME in *.mp3 ; do
NEWFILENAME=$(echo "${FILENAME}" | perl -pe 's/^(.*)//')
if [ "${FILENAME}" != "${NEWFILENAME}" ] ; then
mv "${FILENAME}" "${NEWFILENAME}"
fi
done
done
我的文件嵌套在文件夹中。如果文件名以重复两次的字符串开头,我想将文件名更改为使该字符串只出现一次的名称。
例如:
- 重命名"Dirty Projectors - Dirty Projectors - Search For Life.mp3" 至 "Dirty Projectors - Search For Life.mp3"
- 将 "Eefje de Visser - Eefje de Visser - Bitterzoet.mp3" 重命名为 "Eefje de Visser - Bitterzoet.mp3"
- 保持 "Feng Suave - Maybe Another Time.mp3" 不变。
如何在 Linux 中使用命令行执行此操作?
How can I do this using command-line in Linux?
经过一些研究,它似乎有点复杂。因此,它可能最终会出现在脚本中。
renameMusic.sh
#!/bin/bash
DIRECTORY=
cd ${DIRECTORY}
echo "Normalize filenames in $(pwd)"
for FILENAME in *; do
echo
echo "Checking file ${FILENAME}"
# Count number of dash (-)
NUMBER=$(grep -o '-' <<< ${FILENAME} | wc -l)
echo "Found ${NUMBER} of dash (-)"
# Check if the artist name seems to be there twice
if [[ "${NUMBER}" -eq 2 ]]; then
# If so, remove the double part
NEWFILENAME=$(echo "${FILENAME}" | cut -d "-" -f 2- | sed -e 's/^[[:space:]]*//')
echo "Going to move to new name ${NEWFILENAME}"
# mv ${FILENAME} ${NEWFILENAME}
fi
done
感谢
- Looping through
ls
results in Bash shell script - How to loop through file names returned by find?
- Count total number of occurrences using
grep
- Comparing numbers in Bash
- How to trim whitespace from a Bash variable?
- Whats the difference between
rename
andmv
- When to wrap quotes around a shell variable?
我结合了@Barmar 的正则表达式和@U880D 的shell 脚本以递归地删除文件名中的任何重复前缀。使用 find
和 perl-rename
时出现问题,所以我不得不这样做:
#!/bin/bash
shopt -s globstar;
for DIR in ${PWD}/**/; do
cd "${DIR}"
for FILENAME in *.mp3 ; do
NEWFILENAME=$(echo "${FILENAME}" | perl -pe 's/^(.*)//')
if [ "${FILENAME}" != "${NEWFILENAME}" ] ; then
mv "${FILENAME}" "${NEWFILENAME}"
fi
done
done