Adding a slash at the end of the destination path /opt/alfresco/archived/2020-01-07
would have made the mv
command error out, as the 2020-01-07
directory evidently does not exist. This would have saved your files.
They would also have been saved if /opt/alfresco/archived/2020-01-07
had been an existing directory (regardless of whether the destination path had a slash at the end or not), and your files would have been moved into that directory (filename collisions may still have been an issue though, as you move files from several directories into a single directory). This is what you wanted to do. What you forgot to do was to create that directory first.
Now, since the directory did not exist, what the find
command did was to take each individual XML and PDF file, rename it to /opt/alfresco/archived/2020-01-07
, and then continue doing the same with the next file, overwriting the previous.
The file /opt/alfresco/archived/2020-01-07
is now the last XML or PDF file found by find
.
Also note that since you ran your find
command across /opt/alfresco
, any PDF or XML file below that path, for example in any directory beneath /opt/alfresco/archived
, would have met the same fate.
This is such an easy error to make.
There is no convenient way to recover the lost files other than restoring them from your backups.
If you do not take hourly backups of your data, this may be a good point in time to start looking into doing that. I would recommend restic
or borgbackup
for doing backups of personal files, preferably against some sort of off-site or at least external storage.
The following questions and answers may be of some help:
In your next rewrite of this script, you may want to ignore the archived
subdirectory, and use mv -n -t
. You also need to explicitly -print
the found files (or use mv -v
) as find
will otherwise not output their location:
find /opt/alfresco \
-path /opt/alfresco/archived -prune -o \
-type f \( -iname '*.pdf' -o -iname '*.xml' \) \
-exec mv -n -t /opt/alfresco/archived/2020-01-07 {} + \
-print >/opt/alfresco/scripts/move.log
A few things from the comments (below) that may be useful to know:
If GNU mv
is used with -t target
, it will fail if target
is not a directory. You would use -exec mv -t /opt/alfresco/archived/2020-01-07 {} +
to move multiple files at once with find
(which would also speed up the operation).
If GNU mv
is used with -n
, it will refuse to overwrite existing files.
Neither -t
nor -n
are standard (macOS and FreeBSD have -n
too though), but that shouldn't stop you from using them in scripts that don't need to be portable between systems.
scp
,rsync
,ftp
, etc...), or sometar.gz
archive stored elsewhere on your computer – Basile Starynkevitch Jan 07 '20 at 13:50*.pdf
and*.xml
files quite efficiently. I really suggest taking an hour to learn more about them. In 2020 a few gigabytes of data is not much. – Basile Starynkevitch Jan 07 '20 at 13:55mv *
?") is a question I've asked in interviews in the past. Understanding how globs are expanded by the shell, rather than individual commands, and reasoning about the behavior in the way Kusalananda describes are good skills to have if you're working with Linux regularly. – Xiong Chiamiov Jan 07 '20 at 21:10mv
(and alsocp
) with options-i
and-v
(short-iv
) has saved me lots of headaches. The option-i
instructsmv
to prompt for confirmation before overwriting existing files and-v
increases the verbosity by printing the source and the destination paths. Using-iv
in your case would have paused the process at the moving of the second file over the first moved and because of the verbosity you would have known which file got moved first. – woodengod Jan 08 '20 at 00:26ln
to avoid actually copying the data. Of course I would already have been using-exec mv -t /dest/dir {} +
so find could pass multiple files to one invocation ofmv
. It seems-exec command {} more args +
doesn't work, but apparently does with{} \;
– Peter Cordes Jan 08 '20 at 17:33