I've just lost a small part of my audio collection, by a stupid mistake I made. :-(
GLADLY I had a fairly recent backup, but it was still irritating. Apart from yours truly, the other culprit doing the mischief was mv
, which will show as follows:
The audio files had a certain scheme:
ARTIST - Some Title YY.mp3
where YY
is the 2-digit year specification.
mkdir 90<invisible control character>
(Up to this moment, I did not know that I had actually typed one third excess character which was invisible ...!)
Instead of having all in one directory, I wanted to have all 1990s music in one directory. So I typed:
find . -name '* 9?.mp3' -exec mv {} 90 \;
Not so hard to get the idea what happened eh? :->
The (disastrous) result was a virgin empty directory called '90 something' (with something being the "invisible" control character) and one single file called '90', overwritten n times.
ALL FILES WERE GONE. :-(( (obviously)
Wish mv
would've checked in time whether the signature of the destination "file" (remember on *NIX: Everything Is A File) starts with a d------
(e. g. drwxr-xr-x
). And, of course, whether the destination exists at all. There is a variant of the aforementioned scenario, when you simply forgot to mkdir
the directory first. (but of course, you assumed that it's there...)
Even our pet-hate OS starting with the capital W DOES DO THIS. You get even prompted to specify the type of destination (file? directory?) if you ask for it.
Hence, I'm wondering if we *NIXers still have to write ourselves a "mv
scriptlet" just to avoid these kinds of most unwanted surprises.
.mp3
should be there with the name90
, it could have been one for which you did not have a backup.mv
isn't the problem here, technically, it doesn't know that you are moving a series of files. You are runningmv
one time for each file. That's howfind -exec ;
works. If you had usedfind -exec +
(as in some of the comments)mv
would have screamed as soon as it got more than one argument.mv
for each single file might seem a bit less thought-out at first, it will (as I had said previously) be the only sane solution once source files are scattered among various subdirectories. That in my test-case, source files were all in one directory does not mean that it's my actual test case. It's in fact just a simplification, because I may easily elaborate on that on my own later. Plus, it makes questions less time-consuming to read due to their reduced length. :)mv
to require that the destination exists?mv oldfile newfile
is the way to rename a file, and it's silly to expectnewfile
to exist already and be a directory.