How to Fix Error Moving File Permission Denied in Ubuntu?

Are you trying to move files in your Ubuntu machine and are met with the Permission denied error? If that is the case, then this guide is made for you.

File management is an important task for a System Administrator and even for an average user. However, when working with files in Ubuntu, you need to consider many permissions and ownership rights. Each user has certain rights that are defined within Ubuntu.

The “permission denied” error while trying to move files within Ubuntu occurs because the user trying to perform the move operation doesn’t have ownership over the file being moved. This guide will explain the reasons for encountering the error and provide its solution.

Let’s get started!

How to Fix Error Moving File Permission Denied in Ubuntu?

To fix the error “Permission Denied while moving files,” you need to execute the command as the root user, which can be done by either using the keyword “sudo” before the command or by changing the mod of the user that is executing the command. Otherwise, you can change the ownership of the file.

However, in rare cases, you can encounter the same error even when executing the move command as a root user, which happens when you over-use the “sudo” command to do everything, as this changes the ownership of the files to “root.”

Let’s look at the different solutions for this error in detail!

Use the “sudo” Command

The best solution to move files within the Ubuntu Filesystem without encountering any “permission denied” error is to use the “sudo” keyword at the start of the command. With this method, the commands are executed as a root user, which has all the permissions within the Ubuntu Filesystem, and that’s why no permission error occurs.

Let’s demonstrate this through an example. Suppose you have a file that you want to move that is placed on the Desktop, and you want to move it to a folder named “LinuxJava,” as you can see in the following screenshot.

How to Fix Error Moving File Permission Denied in Ubuntu?

To move this file “123.rar” into the folder, normally, you would use the following command.

mv 123 /LinuxJava

However, this command returns the following error statement on the terminal window.

How to Fix Error Moving File Permission Denied in Ubuntu?

Retry the same move command, but this time around, the command starts with a “sudo” keyword. Therefore, the command becomes.

sudo mv 123 /LinuxJava

However, remember that you will require the root user’s password to execute this command.

How to Fix Error Moving File Permission Denied in Ubuntu?

After typing the password to the root user, you can execute the “mv” command without encountering the “permission denied” error. The next step is to head inside the folder and verify that the file has been moved.

How to Fix Error Moving File Permission Denied in Ubuntu?

Use the Superuser Shell

Another way to achieve the same results as the above method without typing the “sudo” before every command is to use the super user or root shell. This allows a user to access the root command shell and execute commands with the highest permissions.

To do this, you need to open a terminal on your Ubuntu machine (CTRL + ALT + T) and then type the following command to convert it into a superuser shell.

sudo su

When you type this command and execute it, you will be prompted to provide the password of the root user of the Ubuntu machine.

How to Fix Error Moving File Permission Denied in Ubuntu?

Therefore, type in the password for the root user and press the “Enter” key. Once you have done that, you will observe that the username and the hostname within the terminal have changed to root.

How to Fix Error Moving File Permission Denied in Ubuntu?

Let’s try moving the file named “76.rar” from the desktop to the folder “LinuxJava” also on the desktop.

How to Fix Error Moving File Permission Denied in Ubuntu?

To do this, you will have to type the following commands in the terminal

cd Desktop
mv 76.rar LinuxJava/

The first command changes the pwd to “Desktop,” and the second is the move (mv) command. Once you execute the following commands, your file will be copied into the LinuxJava folder without errors.

How to Fix Error Moving File Permission Denied in Ubuntu?

Use the “-f” Flag in the “mv” Command

If you are running the mv command and are still experiencing permission denied error or any other error like “overwriting,” you can include the “-f” option within the move (mv) command. The “-f” stands for “force,” and it will force the file into the destination folder even if there is an overwriting issue.

Therefore, instead of using the move command, use the normal syntax for moving a file from one location to another, like the following.

sudo mv <source_path> <destination_path>

Use the following command with the addition of the “force” option.

sudo mv -f <source_path> <destination_path>

However, be careful when using this method because this method will overwrite the existing files with the same names in the destination without any confirmation or prompts. If you want to avoid accidentally overwriting important files or documents, you can add the “-i” flag, which will prompt you on the terminal in case of overwriting.

Therefore, the command to show the prompts with the force option becomes.

sudo mv -f -i <source_path> <destination_path>

Change File Permissions Correctly

If you do not want to use the “sudo” command for everything, you do not want users to have sudo access to everything. Then, you can simply change the permission of the file correctly, which can be done by using the chmod command to change the permissions of the file/folder that needs to be moved.

To demonstrate this method, let’s take a file named “myData.zip” placed on the desktop, which needs to be moved to the folder “LinuxJava” on the desktop. The first thing to do is find the file permissions by running the following command on the terminal with “Desktop as its present working directory.

ls -al

Running this command will list all the directories and files available on the present working directories.

How to Fix Error Moving File Permission Denied in Ubuntu?

As you can see in the screenshot above, the user “Linuxuser” only has read access permissions to the file “myData.zip,” which causes the permission denied error. To fix this issue, let’s change the permissions to “755” using the chmod command.

chmod
chmod 755 myData.zip

Once that is done, you can re-run the “ls -al” command to view the access permissions of the files again and verify that changes are made through the chmod command.

How to Fix Error Moving File Permission Denied in Ubuntu?

Using the following command, let’s try moving this file into the “LinuxJava” folder without the sudo keyword.

mv myData.zip LinuxJava/

After successfully executing the command, head inside the folder and verify that the files have been moved.

How to Fix Error Moving File Permission Denied in Ubuntu?

Change Default Ownership

If you have often used the “sudo” keyword to move and copy files. A rare bug can cause the ownership of the files to change by itself. This causes the error of permission being denied while moving files, even when using the “sudo” keyword.

When this happens, you must change the default ownership of the directories you work with. For example, if you work within the home directories, you can simply run the following command in the terminal.

sudo chown -R $USER:$USER $HOME

After this, re-run the move operation, which will work without encountering the permission denied error.

Affordable VPS Hosting With Dracula Servers

Looking for reliable and budget-friendly Virtual Private Server (VPS) hosting? Look no further than Dracula Servers. Dracula Servers offers a range of VPS hosting plans tailored to meet diverse needs. With competitive pricing, robust performance, and a user-friendly interface, it’s an excellent choice for individuals and businesses alike.

Explore the Dracula Servers website to discover hosting solutions that align with your requirements and take your online presence to new heights with their affordable and efficient VPS hosting services.

Visit Dracula Servers and experience reliable VPS hosting without breaking the bank.

Conclusion

The error “permission denied” occurs while trying to move files/folders from one location to another within the Ubuntu Files system because the user trying to operate does not have the rights to move these files.

To tackle this issue, the easiest method is to include the keyword “sudo” at the start of the move command. Through this method, the move command will be executed with superuser privileges and will not encounter the “permission denied” error. However, to use the “sudo” keyword, you will be prompted to provide the root user’s password.

Alternatively, if you do not want to include the sudo keyword at the start of every command you execute, you can head to a super user shell. Within the superuser shell, each command will be executed with superuser privileges.

Otherwise, you can use the “-f” option in the move command if the error is due to an overwrite issue or change the file permissions.

Lastly, if the error is due to the overuse of the sudo keyword that causes the owner to be changed for every file, then simply change the ownership of the directory using the “chown” common.

Check out More Linux Tutorials Here!

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
× Dracula Servers

Subscribe to DraculaHosting and get exclusive content and discounts on VPS services.