How to get rid of permision denied while using mv command? - terminal

I am trying to move a file from one directory to other by command line.I used this command
raghul#raghul-Aspire-5750Z:~/temp/newfolder$ mv copy.txt /temp/
I got error like this
cannot create regular file '/temp': Permission denied
Can someone help me to figure this out? I need to move a file from one directory to other.

First of all you are using the copy command cp, not the move command mv.
Secondly you are trying to copy the file to a new file named /temp, ie. a file named temp in the / directory. This resides in the filesystem's root directory, which is mostly likely owned by root. Unless you have root permissions you can not write to the root directory.
Given that you are naming the file temp, I assume that you want to move the file to the /tmp directory, for which you will have permission to write to. Do this:
$ mv copy.txt /tmp
This will work only if you also have write permission on the file copy.txt because you need to be able to remove it. If you just wanted to copy the file, just read permission is required.
Otherwise, if you really do wish to move the file to a /temp directory, you can use sudo to do that, provided that you are set up as a sudo user:
$ sudo mv copy.txt /temp
[sudo] password for raghul

I just noticed that you're in a personal directory called ~/temp/newfolder. Is that the temp you're trying to move the file to: your personal one, in which onefolder is in? So you want to move the file up one directory?
Then the problem is that your command is missing the 'personal' tag ~. The command should be:
mv copy.txt ~/temp/

Try moving it with sudo command as it seems you don't have permission to move the file.
If you are requested for a password enter the root's password.
Try this:
sudo cp copy.txt /temp/

Try this: change /temp to
mv index.text temp

Related

Error copying directories at command line using cp

Mac OS X Yosemite v.10.10.5.
I am trying to use the cp command to copy one Git directory to another.
This command-line statement:
cp -r /path/to/dir/from/ /path/to/dir/to/
Returns this error:
cp: /path/to/dir/to/.git/objects/00/00ad2afeb304e18870d4509efc89fedcb3f128: Permission denied
This error is returned one time each for (what I believe, but haven't verified, is) every file in the directory.
The first time I ran the command it worked properly, as expected, without error. But, without making any changes to any files, the second (and subsequent) times I ran the command, I got the error.
What's going on? And how can I fix this?
Edit:
In response to a question in the comment:
What does ls -l /path/to/dir/to/.git/objects/00/00ad2afeb304e18870d4509efc89fedcb3f128 show?
The answer is it shows:
-r--r--r-- 1 myusername staff 6151 May 6 00:45 /path/to/dir/to/.git/objects/00/00ad2afeb304e18870d4509efc89fedcb3f128
The reason you are getting Permission Denied is because you are trying to overwrite a file that already exists in the destination directory that has read only permissions set on it. Since it appears you're trying to overwrite it you could just remove the destination directory if it exists before the copy operation. Also you should use -R, not -r ...
Historic versions of the cp utility had a -r option. This
implementation
supports that option; however, its use is strongly discouraged, as it
does not correctly copy special files, symbolic links, or fifo's.
Using a command such as this should resolve your issue:
[[ ! -d dest ]] || rm -rf dest ; cp -R src dest
The above checks if dest exists; if it does recursively remove it, then copy the source to dest,
You may want cp -rp for this operation. -p preserves the user and group IDs associated with the file. Try starting over using -p and see if that solves the issue.
Anther reason you might be seeing this issue is if the permission really is denied. That is, if you're trying to copy into a folder owned by another user without superuser privileges.

make a file removable just by root in mac

i'm trying to make a file removable just by root user In mac 10.10.
i was try this :
chown root <fileName>
but other user can remove it;
any idea?
As an alternative to changing the permissions on the containing directory, you can set the uimmutable flag on the file:
sudo chown root foo
sudo chflags uimmutable foo
Now only root will be able to delete foo. Note, though, that nobody will be able to modify the file, either. Root could remove the uimmutable flag and then modify it, of course, but that opens a window for others to delete it.
The act of removing an entry in a directory modifies the directory, but not the file. (When you remove a file, you are unlinking the name from the file and the link count on the file is decremented. The file itself may not be deleted, but will no longer be accessible by the name that was removed.) In order to ensure that only some process with root privilege can unlink a file, you need to modify the permissions on the directory. So to ensure that no-one but root can delete the file /p/a/t/h/file:
sudo chown root /p/a/t/h # make root the owner of the directory
sudo chmod og-w /p/a/t/h # remove write permissions from other and group
Note that this is less fine grained that you might like and will prevent non-root users from removing or creating any files in /p/a/t/h.

SCP says file has downloaded, but the file does not appear

I am using ssh to work on a remote server, however when I try to download a file using scp in this format:
scp name#website.com:somefile.zip ~/Desktop
It asks me for my password, and shows this:
somefile.zip 100% 6491 6.3KB/s 00:00
however, this file never appears on my desktop. Any help
I think that you are logging into the remote machine using ssh and then running the command on the remote machine. You should actually be running the command without logging into your remote server first.
You need to specify the file path
scp name#website.com:/path/to/somefile.zip ~/Desktop
~/Desktop should actually be a directory, not a file. I suggest that you do the following:
Remove the ~/Desktop file with rm ~/Desktop (or move it with mv if you want to keep its contents).
Create the directory with mkdir ~/Desktop.
Try again to scp the zip file.
BTW, when I need to copy files into directories, I usually put a slash after the directory to avoid such problems (in case I make a mistake), e.g. scp server:file ~/Desktop/; if the directory doesn't exist, I get an error instead of unwanted file creation.
You are doing this from a command line, and you have a working directory for that command line (on your local machine), this is the directory that your file will be downloaded to. The final argument in your command is only what you want the name of the file to be. So, first, change directory to where you want the file to land. I'm doing this from git bash on a Windows machine, so it looks like this:
cd C:\Users\myUserName\Downloads
Now that I have my working directory where I want the file to go:
scp -i 'c:\Users\myUserName\.ssh\AWSkeyfile.pem' ec2-user#xx.xxx.xxx.xxx:/home/ec2-user/IwantThisFile.tar IgotThisFile.tar
Or, in your case, (that is with the VERY strong password you must be using):
cd ~/Desktop
scp name#website.com:/path/to/somefile.zip somefile.zip

Copying from mount share drive to local folder through script

This is my first time trying to work with Linux Scripts so this may be something obvious.
Here is what I am trying to do:
Remove all contents from local folder - rm /home/user/Documents/Exercise/
Copy files from a shared windows network drive - cp smb://server/arc/Exercise%20Files/Word/
So from my understanding my command should look like this
rm /home/user/Documents/Exercise/
cp smb://server/arc/Exercise%20Files/Word/ /home/user/Documents/Exercise/
But anytime I try and run either of the above commands I get the following error:
"rm: cannot remove `/home/user/Documents/Exercise/': Is a directory"
"cp: cannot stat `smb://server/arc/Exercise%20Files/Word/': No such file or directory"
What am I doing wrong?
Kind Regards,
M
Based on your request and your test, let me point what is not written properly:
Remove all contents from local folder
rm /home/user/Documents/Exercise/
Error says rm: cannot remove /home/user/Documents/Exercise/': Is a directory
You should
rm /home/user/Documents/Exercise/*
which will delete everything inside the directory, but not the directory.
Copy files from a shared windows network drive
cp smb://server/arc/Exercise%20Files/Word/ /home/user/Documents/Exercise/
Error says cp: cannot stat smb://server/arc/Exercise%20Files/Word/': No such file or directory
You should check if route smb://server/arc/Exercise%20Files/Word/ is correct. Then, use the following:
cp smb://server/arc/Exercise%20Files/Word/* /home/user/Documents/Exercise/
You can't delete a directory if it has content within it.
To delete the content and the directory at the same time, use the following command:
rm -r /home/user/Documents/Exercise/
This recursively deletes the directory and any content within it.
To copy the file, I believe you have to mount the directory beforehand, like so:
mount -t cifs //server/share /mnt/mount_directory -o user=username
Can you confirm if that works?
Remove / Delete Command:
rm -rfv /home/user/Documents/Exercise/*
Copy Command:
cp -rfv /home/user/Documents/ExerciseShare/ExerciseFiles/Word/ /home/user/Documents/Exercise/

Changing folder permissions from command line (on mac)

I'm trying to write a script that will let me add to an existing directory structure and copy a bunch of files into various places within this. However, using mkdir ... and cp... commands alone wont work since I do not have permission to do so. I understand that this can be changed manually in the 'Get Info' window, but this script will be run by others and its whole point is to save time and hassle.
Is there a way of adding to this script to give me permission to copy files to BASEDIR/SUBDIRS?
A bit more detail on what I'm doing:
I want to add to the directory BASEDIR with a bunch of SUBDIRS then copy files into these subdirectories. The problem is that I am receiving these 'permission denied' errors right after the mkdir BASEDIR/SUBDIR1/SUBDIR2 command.
Thanks
The command
sudo chmod -R ugo=rwx BASEDIR/
gives all folder permissions to all users to BASEDIR and all its subdirectories

Resources