我使用的是 macOS 10.12.1 和 bash 3.2.57(1)-release。这个 bash 脚本完全符合我的要求,除了一件事:我不想打印所有那些“没有这样的文件或目录”消息。以下是脚本:
#!/bin/bash
#set -x
cd "/volumes/Mac HD Ext/iTunes/Audiobooks"
pwd
for dir in *
do
#echo "$dir"
[ "$(ls ./"${dir}"/*.m4a)" ] && cd "${dir}" || continue
#ls *.m4a
pwd
for nameext in *.m4a
do
#echo $nameext
name=${nameext%.*} # name has the .m4a removed
#echo ${name}
newnameext=${name}.m4b # Add .m4b onto name
#echo ${newnameext}
#echo ${nameext}-${newnameext}
#mv "${nameext}" "${newnameext}" # Activate when ready
echo "${nameext}" "${newnameext}"
echo
done
cd .. # Move back to Audiobooks directory
done
exit
以下是部分输出:
ls: ./Eugenia Cheng/*.m4a: No such file or directory
ls: ./Eustace Mullins/*.m4a: No such file or directory
ls: ./F. Scott Fitzgerald/*.m4a: No such file or directory
ls: ./Frank H. T. Rhodes/*.m4a: No such file or directory
/volumes/Mac HD Ext/iTunes/Audiobooks/Frans de Waal
Our Inner Ape_ A Leading Primatologist Explains Why We Are Who We Are.m4a Our Inner Ape_ A Leading Primatologist Explains Why We Are Who We Are.m4b
/volumes/Mac HD Ext/iTunes/Audiobooks/Frans de Waal_Sean Runnette
Are We Smart Enough to Know How Smart Animals Are_ - Part 01.m4a Are We Smart Enough to Know How Smart Animals Are_ - Part 01.m4b
Are We Smart Enough to Know How Smart Animals Are_ - Part 02.m4a Are We Smart Enough to Know How Smart Animals Are_ - Part 02.m4b
Are We Smart Enough to Know How Smart Animals Are_ - Part 03.m4a Are We Smart Enough to Know How Smart Animals Are_ - Part 03.m4b
Are We Smart Enough to Know How Smart Animals Are_ - Part 04.m4a Are We Smart Enough to Know How Smart Animals Are_ - Part 04.m4b
Are We Smart Enough to Know How Smart Animals Are_ - Part 05.m4a Are We Smart Enough to Know How Smart Animals Are_ - Part 05.m4b
Are We Smart Enough to Know How Smart Animals Are_ - Part 06.m4a Are We Smart Enough to Know How Smart Animals Are_ - Part 06.m4b
Are We Smart Enough to Know How Smart Animals Are_ - Part 07.m4a Are We Smart Enough to Know How Smart Animals Are_ - Part 07.m4b
Are We Smart Enough to Know How Smart Animals Are_ - Part 08.m4a Are We Smart Enough to Know How Smart Animals Are_ - Part 08.m4b
Are We Smart Enough to Know How Smart Animals Are_ - Part 09.m4a Are We Smart Enough to Know How Smart Animals Are_ - Part 09.m4b
Are We Smart Enough to Know How Smart Animals Are_ - Part 10.m4a Are We Smart Enough to Know How Smart Animals Are_ - Part 10.m4b
Are We Smart Enough to Know How Smart Animals Are_ - Part 11.m4a Are We Smart Enough to Know How Smart Animals Are_ - Part 11.m4b
Are We Smart Enough to Know How Smart Animals Are_ - Part 12.m4a Are We Smart Enough to Know How Smart Animals Are_ - Part 12.m4b
ls: ./Frederick Forsyth/*.m4a: No such file or directory
ls: ./Gabor Boritt/*.m4a: No such file or directory
ls: ./Gaius Julius Caesar/*.m4a: No such file or directory
ls: ./Genevieve von Petzinger/*.m4a: No such file or directory
答案1
您可以将 ls 的标准错误重定向到位存储桶,即ls blah 2> /dev/null
。我应该顺便指出,使用find
可能是此用例的更优雅的解决方案,并且根据定义避免了此问题。