Skip to content
Advertisement

How to undo “mv file -” command in Ubuntu [closed]

As far as you know, - has different usages for in combination with different commands in Linux. For example in combination with cd command, it means “Change directory to the previous location” and in combination mv or cat command it means stdin or stdout.

Unfortunately I wrongly used this character with mv command. I wanted to move my file to the previous location which I have been before the change directory but I moved it to stdin instead.

Is there any way to recover my file?

I run this command:

# mv myfile -

Advertisement

Answer

I moved it to stdin instead.

No, you moved to a file literally named by a dash (you’ll use /dev/stdin or /proc/self/fd/0 to refer to the stdin, i.e. the file descriptor 0).

You want to

mv -i ./- myfile

this is usual practice (to prefix by ./ a strange path). The -i interactively asks for confirmation.

Sometimes it is not even easy to type a path for a weird file (e.g. for a file name containing a single newline character). You could use globbing (read about shell expansion), e.g. mv -i ./? file.

See mv(1) and path_resolution(7) and glob(7) and proc(5)

The good habit is to avoid strange characters in file paths.

Advertisement