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 ?


Solution 1:

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.

Solution 2:

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.)