0

I am using macOS 10.12.1 and bash 3.2.57(1)-release. This bash script does just what I want except for one thing: I don't want all those "No such file or directory" messages to print. Here is the script:

#!/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

and here is part of the output:

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 Answer 1

1

You could redirect the standard error of ls to the bit bucket, ie ls blah 2> /dev/null. I should point out in passing that using find may be a more elegant solution to this use case, and avoids this problem by definition.

2
  • Thanks, King. I used ls blah 2> /dev/null and it did the trick.
    – grok12
    Dec 6, 2016 at 16:55
  • Note that if you encounter a legitimate error, that will be suppressed too.
    – MMB
    Dec 26, 2016 at 2:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .