Score:-1

File is gone on mv command :

us flag

I was trying to move file.py to the existing directory Modules ...

wm@my-laptop:~/Desktop/1linuxandpython$ mv Modules_and_packages.py /Modules_and_packages_Game

it said permission denied , so i did this:

wm@my-laptop:~/Desktop/1linuxandpython$ sudo mv Modules_and_packages.py /Modules_and_packages_Game

then it disappeared, i searched for the answer and found that's the file's name is /Modules_and_packages_Game now but can't find it anywher. And how could be possible for .py to be names as folder name ?

edit:

enter image description here

ls -l show the folder name and when i execute it ,it opens as a file.py knowing that before executing the command:

$ sudo mv Modules_and_packages.py /Modules_and_packages_Game

i was sure that folder Modules_and_packages_Game is there ?

Artur Meinild avatar
vn flag
Does it appear if you do `ls /`?
guiverc avatar
cn flag
You used the `mv` to move and rename the file; the command executed was exactly what you told it to do, even if that wasn't your intention. It's located in the `/` directory just as you reported
Score:10
in flag

Target of your mv command is /Modules_and_packages_Game. That is an absolute path.

  • If the path existed and is a directory, mv will move the source files to that directory.
  • If the path does NOT exist, the source file will be renamed to the target name.

Compare man mv and see the three possible forms of executing mv:

SYNOPSIS
  mv [OPTION]... [-T] SOURCE DEST
  mv [OPTION]... SOURCE... DIRECTORY
  mv [OPTION]... -t DIRECTORY SOURCE...
DESCRIPTION
  Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

Most likely, you used first form (rename SOURCE to DEST) while you wanted the second form (move SOURCE to DIRECTORY).
Your file is now a file in / (root of your file system) named Modules_and_packages_Game.

Check using ls -l /Modules_and_packages_Game.

To move back, run:

sudo mv /Modules_and_packages_Game Modules_and_packages.py

Now do it correctly. I assume your target directory is the relative path Modules_and_packages_Game, and not the absolute path /Modules_and_packages_Game.

Either (using second form):

mv Modules_and_packages.py Modules_and_packages_Game

or (using third form):

mv -t Modules_and_packages_Game Modules_and_packages.py

The latter is more secure, as it will give an error if the target directory does not exist.

Marinos TBH avatar
us flag
Actually the file now is a folder . when i : ls or ls -l , it appears blue folder , and to acces it i just need to /Modules_and_packages_Game so it compiles a ./file.py
pLumo avatar
in flag
A file cannot be a directory after `mv`. Then the directory existed before, and you moved your file to that directory.
Marinos TBH avatar
us flag
See screenshot below please .
pLumo avatar
in flag
You're wrong, you execute the moved file with `/Modules_and_packages_Game`. It is exactly as I said. What you see (the blue directory) is the relative path `Modules_and_packages_Game` which is **not the same** as `/Modules_and_packages_Game` .
Score:1
jp flag

As others are pointing to you, you're confusing relative paths vs. absolute paths. Put in mind the following considerations:

  1. A lone slash / is the root directory. Think of it as the folder that is on top of all other folders, that is, every folder and file in your system is somewhere inside the / directory.
  2. An absolute path is a path from the root directory to a file or folder. Thus, it always begins with /, which is the root directory, and ends with the file or folder you want to reach. An example of an absolute path for a file would be:
/home/username/Desktop/myFile.txt
  1. The tilde ~ is a shortcut that represents your home directory. Thus, a file somewhere inside your home folder can be accessed by using this shortcut, and the above example could be:
~/Desktop/myFile.txt
  1. The current directory or working directory is the one you're currently working on. It is normally displayed on the Bash prompt in blue, between the semicolon : and the dollar sign $. For example, if you're currently working on your Desktop folder, the Bash prompt on the Terminal would be like this:
username@hostname:~/Desktop$
  1. A relative path can be used when the file or folder you want to access is inside your current working directory. In that case, you must not begin the path with /, because that would represent that the path is an absolute path starting in the root directory. As an example, if you're already working on your Desktop folder, you could read the contents of the myFile.txt file by just typing its name.
username@hostname:~/Desktop$ cat myFile.txt
  1. The current directory can be represented by a single dot .. Thus, to prevent mistakes and make sure you're referencing a relative path, you may begin relative paths with ./ to represent something inside the current directory, e.g.:
username@hostname:~/Desktop$ cat ./myFile.txt
  1. The directory above your working directory is represented by two dots ... Thus, if you're in your Downloads folder but want to access your file on the Desktop, you could use the following relative path:
username@hostname:~/Downloads$ cat ../Desktop/myFile.txt

As a further example, if the myFile.txt is inside the someFolder directory on your Desktop, then you could read its contents using either:

  • A relative path (no slash at the beginning):
username@hostname:~/Desktop$ cat someFolder/myFile.txt
  • A relative path with the dot, to avoid confusion:
username@hostname:~/Desktop$ cat ./someFolder/myFile.txt
  • The absolute path (with a slash at the beginning):
username@hostname:~/Desktop$ cat /home/username/Desktop/someFolder/myFile.txt
  • The absolute path with the home directory shortcut:
username@hostname:~/Desktop$ cat ~/Desktop/someFolder/myFile.txt

Please, make some practice with this, then come back and you will understand the other answers.

In short, your file is now inside your root directory and its name is the same as the name of the folder you were trying to put it into. Thus the absolute path to the file is now /Modules_and_packages_Game (it is still a python file, it's just missing the .py extension because you inadvertently changed its name). This is different from the folder you have in ~/Desktop/1 linux and python steps/Modules_and_packages_Game. To put the python file into that folder and change its name back, use:

mv /Modules_and_packages_Game "~/Desktop/1 linux and python steps/Modules_and_packages_Game/Modules_and_packages.py"

(Note the double quotes " are needed because you have whitespaces in the path.)

Marinos TBH avatar
us flag
Grateful for such an answer, i' fully understand how the directory navigation works. Otherwise i got this : mw@my-laptop:~$ mv /Modules_and_packages_Game "~/Desktop/1 linux and python steps/Modules_and_packages_Game/Modules_and_packages.py" mv: cannot move '/Modules_and_packages_Game' to '~/Desktop/1 linux and python steps/Modules_and_packages_Game/Modules_and_packages.py': No such file or directory
Alejandro avatar
jp flag
Double check both absolute paths to see which one is not correct, and adjust to your needs. You may need to use `sudo` before `mv`. or use `cp` instead of `mv`. Maybe the folder is `1linuxandpython` and not `"1 linux and python steps"`, there are both in your question. You can check with `ls` and autocomplete valid paths in Terminal with the <kbd>Tab</kbd> key.
Marinos TBH avatar
us flag
OK finally it worked (i deleted the double quotes and made it to '1 linux and python steps' and used sudo ) sudo mv /Modules_and_packages_Game ~/Desktop/'1 linux and python steps'/Modules_and_packages_Game/Modules_and_packages.py. Modules_and_packages.py is there and /Modules_and_packages_Game is no longer executable. Thanks for help may everyone find such explanations.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.