Can’t Find a File You’ve Moved on Linux? The Culprit Might be Slash


Typically, mv is one of the first Linux commands you learn. But one little slip, and things can get surprisingly confusing for the user. Sometimes it looks like moved files have simply vanished.




How This Particular Problem Foxes People

Moving a file on the command line is pretty simple. You specify the file you want to move, and where you want to move it to. But still, it’s easy for a new user to find themselves exasperated and frustrated over mv.

They try to move a file into a directory, and it seems to work. They don’t get a confirmation message, but neither do they get an error message. Linux often takes the no news is good news approach. If you don’t get an error message, it must have worked.

Or did it? When they cd into the target directory, the file isn’t there. And of course, because mv moves the file from the original directory, it’s not there either. It’s starting to look like the file has been lost in the ether. This is usually the point where the frustrated user reaches out for help.


The Part Slash Plays in the mv Command

Let’s say you’re moving a file to a different directory. You’re going to keep the same filename. In theory, this makes things easy because you don’t need to specify a target filename on the command line. By default, mv uses the original filename.

We’ve got a file in the ~/Downloads/src directory. We move it to the ~/Documents/backup directory. As expected, it’s now in the backup directory, and it has been removed from the src directory.

ls ~/Downloads/src
mv ~/Downloads/src/important-file.dat ~/Documents/backup
ls ~/Documents/backup/
ls ~/Downloads/src
Moving a file with the mv command

That’s nice and simple, and everything works as expected. If we look into our target directory, we find the moved file, and we get on with the rest of our work.


A moved file in its new directory.

But let’s say our Linux newcomer isn’t using tab completion. They’re typing the directory paths by hand. If they misspell the name of the final directory, we get a very different behavior.

mv ~/Downloads/src/important-file.dat ~/Documents/backpu

Trying to use the mv command with a typo in the final directory name.

They’ve made a typo with the target directory name, but mv exits silently back to the command prompt. On the face of it, it looks like the file move worked.

Let’s check.


ls ~/Downloads/src
ls ~/Documents/backup/
Using ls to look for the file in the original and new directories.

The original and target directories are both empty. Where did the file go?

Bash tries to find a directory called backpu, but can’t find one. It concludes you want to rename your moved file to backpu. You’ll find a file called backpu one directory level higher than your target directory.

ls -l ~/Documents

The location of the missing and misspelled file.

To fix this, you can move your file to where it should have gone, and specify its proper name on the command line.


mv ~/Documents/backpu ~/Documents/backup/important-file.dat 
ls ~/Documents/backup
Moving and renaming the misspelled file

The confusion and the hunt for the missing, badly-named file could have been avoided by adding a trailing slash to the target directory on the command line. That way, if you make a typo, Bash reports an error.

mv ~/Downloads/src/important-file.dat ~/Documents/backpu/

The trailing slash on the mv command line flags errors with the final directory name

The trailing slash explicitly tells Bash that this is a directory name, not a filename. Because Bash can’t find the misspelled directory, it reports the error to you and doesn’t move anything.


This is a better outcome for failures. You’re alerted to the error, and the original file remains touched.

Using the Bash tab completion feature not only speeds up the entry of directory paths, it gives a few extra bonuses. All the directory names are automatically spelled correctly, and a trailing slash is added to the final directory.

It’s Not Exactly Slash’s Fault

Slash didn’t make the typo, after all. But if slash is present and on duty, you’re told about the error before mv does anything questionable with your file.

As is often the case, a good diagnostic first step is to check the command history and see what command was actually issued, not what you think you typed. If you spot a typo, look for a file with that misspelled name, one directory level higher than where you expect it to be.



Source link

Previous articleAre Macs affected by the Crowdstrike outage?