0

I'm trying to write a command that uses mv to move files two directory levels up.

So if the folder order goes like this: ~/Test/2020-08-01/001/002/file.txt, I want to move file.txt from directory 002 to directory 2020-08-01.

When I type out this command from my home directory mv ~/Test/2020-08-01/001/002/* ../.. I get an error that says: mv: cannot move '/home/user/Test/2020-08-01/001/002/file1.txt' to '../../file1.txt': Permission denied

I don't understand why I'm getting a "Permission denied" error and I don't think it's sudo-related. I also don't want to try sudo in case I mess something up.

If anyone has any insight please let me know. Thank you.

2
  • In your case ../.. actually points to / (try running: realpath ../..) and for that reason you get Permission denied. The command should be: mv ~/Test/2020-08-01/001/002/* ~/Test/2020-08-01 Commented Dec 12, 2022 at 5:32
  • Maybe show the output of ls -ld ../.., or change your directory to ~/Test/2020-08-01/001/002 before running the command mv * ../... Commented Dec 12, 2022 at 7:26

1 Answer 1

0

The issue is that ../.. in your command is relative to your current directory. If your current directory is /home/user, then ../.. refers to the root of the directory hierarchy (where your non-privileged user can't write).

To move file.txt from ~/Test/2020-08-01/001/002 to ~/Test/2020-08-01, use

mv ~/Test/2020-08-01/001/002/file.txt ~/Test/2020-08-01

If you want to use relative directory paths, then ensure that you are first in the correct directory, then perform the move:

cd ~/Test/2020-08-01/001/002
mv file.txt ../..

or,

cd ~/Test/2020-08-01
mv 001/002/file.txt .

... or some combination thereof.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.