Ignoring mv commands "cannot stat" error ?

So, my third thread here ^^ and still asking questions.

Thanks for you patience and help, I really appreciated it :wink:

I currently use a shell script to move folders of songs from one to another location on my harddrive.
I use something like this:

sudo mv /var/mobile/Media/"My Music"/"Vasco Rossi" /var/mobile/Media/0tempmusic/"Vasco Rossi"
sudo mv /var/mobile/Media/"My Music"/"Yui" /var/mobile/Media/0tempmusic/"Yui"
sudo mv /var/mobile/Media/"My Music"/"ZARD" /var/mobile/Media/0tempmusic/"ZARD"
sudo mv /var/mobile/Media/"My Music"/"8 Mile[Soundtrack][2002][MP3@320kbps]-FLAWL3SS" /var/mobile/Media/0tempmusic/"8 Mile[Soundtrack][2002][MP3@320kbps]-FLAWL3SS"
sudo mv /var/mobile/Media/"My Music"/"Berlin Calling OST" /var/mobile/Media/0tempmusic/"Berlin Calling OST"
sudo mv /var/mobile/Media/"My Music"/"Hangover OST" /var/mobile/Media/0tempmusic/"Hangover OST"
sudo mv /var/mobile/Media/"My Music"/"Inception OST - 2010 - (Hans Zimmer)" /var/mobile/Media/0tempmusic/"Inception OST - 2010 - (Hans Zimmer)"

The problem however is that I don't have all the folders in the place the script works with.
This naturally results in cannot stat errors.

What I would be interested in now is a way to have the script just ignore any "cannot stat error message".

I know that using a "if-else" would solve the job, but since the code up there is only a small portion of the real thing it simply would be alot of work and writing (or copy and paste if you want) to do.

I hope there is something the mv command has that will help me with that.

As always:

Thanks in advance you guys are awesome :smiley:

You can shut up the error messages by sending them to the black hole sink /dev/null:

mv src dest 2> /dev/null
1 Like

Awesome ! Just what I needed thanks for that :slight_smile:

On a side not: is sending the errormessages to the black hole known as "/dev/null" the only ways besides else if to do this ?
Just curious xD

Well, the message you're getting is printed on stderr (as opposed to stdout, which is where normal output goes). If you want to just get rid of that stream, this is the easiest way.

Many times it can be useful to ignore all output and just check the return value of command to see whether it exited normally or not:

grep string someFile &>/dev/null 
case $? in 
  0) echo "found it";;
  1) echo "not found, someFile exists";;
  2) echo "someFile does not exist";;
esac

Redirection &> will redirect both stdout and stderr.

You can specify where does this message go, by manipulating the stderr stream. Stderr is associated by a file descriptor 2 (/dev/fd/2 in bash).

Other than redirecting it to /dev/null, you could redirect it to a file, to capture the stderr, like

command 2> myErrs.log

or '2>>' to append.

You can also process the stderr by piping it to another command. Reply by alister in this post explains this and might be of interest to you:

Other than processing what goes OUT of stderr, you might wanna write INTO stderr. Like this:

$echo "This goes to stderr" >&2

good luck!
mirni