2

I tried moving a file in Linux from one directory to another and accidentally had an extra > character.

mv /u01/app/oracle/product/12.1.0.2/db_1/dbs/>test2.dbf /u01/shared_data/oradata/TEST/test.dbf

Now, my entire dbs folder is missing. However when I locate the dbs folder /u01/app/oracle/product/12.1.0.2/db_1/dbs it appears to be there but I can't ll or cd to it:

-bash: cd: /u01/app/oracle/product/12.1.0.2/db_1/dbs: No such file or directory

How do I get the original back to where it was?

Bart
  • 2,221
rman
  • 105
  • 1
    I suspect you renamed the dbs directory to test.dbf. – stolenmoment Aug 07 '19 at 17:57
  • I can't tell you where the directory has gone to, but the reason you still see it as being present, is because you use "locate" to search for it. "locate" searches in a database which is updated daily. You should probably use "find". – twan163 Aug 07 '19 at 18:09
  • DOH! You are right stolenmoment. I was able to locate the file and move it back to the original name. I guess the > character moves all items into a new directory? Can't find it on the mv man pages. Thanks a bunch! – rman Aug 07 '19 at 18:19

1 Answers1

7

The > here redirects stdout to a file, like it would in a more normal use case:

printf "%s\n" "hello world" > filename

The spaces around > are optional, and it doesn't have to go at the end. This does the same:

printf "%s\n">filename "hello world" 

So your mv line would more conventionally be written:

mv /u01/app/oracle/product/12.1.0.2/db_1/dbs/ /u01/shared_data/oradata/TEST/test.dbf > test2.dbf

which has renamed your folder to test.dbf (in a different directory) and written mv's stdout (probably nothing) to test2.dbf in your current directory. Hopefully that didn't accidentally overwrite an Oracle datafile.

PS: If extra > are a frequent problem, bash's set -o noclobber/set -C option can at least help prevent overwriting files. The bash manpage describes it as: If set, bash does not overwrite an existing file with the >, >&, and <> redirection operators. This may be overridden when creating output files by using the redirection operator >| instead of >.

derobert
  • 109,670
  • Excellent explanation derobert +1. Everything is working normal again after moving all items back to their original location. – rman Aug 07 '19 at 18:46
  • @rman Glad to hear that. Worst case, considering your username, I figured you'd just restore a backup ☺. – derobert Aug 07 '19 at 18:48