Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I am facing a very weird issue.
Consider an example I have these directories "/ten" and "/one/two/three/four" I have a few files in these directories.
When i execute the following command
mv /ten/ /one/two/three/four/five/six
it gives the output as
mv: cannot move '/ten/' to '/one/two/three/four/five/six' : No such file or directory. Which looks fine as it doesn't create directories.
But if I execute the following command
mv /one/two/three/four/ /one/two/five/six
the directories five/six get created inside /one/two. i.e. the mv command succeeds.
Can anyone please explain what is happening here ? Why doesn't it give an error No such file or directory ?
EDIT : Further Observation ..
Directories /one/two/three/four exists also directories /one/two/five exists.
Executing mv /one/two/three/four/ /one/two/five/six will succeed. Here directory six will get created even though it is not present.
This doesn't happen in the case when I execute mv /one/two/three/four /one/two/five/six and the "five" directory doesn't exists. In this case it will give error.
I thought mv will never create any directories.
Please let me know if I have missed something obvious.
Either you're executing another mv binary, executing another version of mv, or something is wrapping it up like a function, a script or perhaps an alias.
To know if you're really running the real mv or not, run
type mv
You should get
mv is /bin/mv
As suggested by Etan Reisner, you can also add -a to have more information:
type -a mv
UPDATE
Directories /one/two/three/four exists also directories /one/two/five
exists. Executing mv /one/two/three/four/ /one/two/five/six will
succeed. Here directory six will get created even though it is not
present. This doesn't happen in the case when I execute mv
/one/two/three/four /one/two/five/six and the "five" directory doesn't
exists. In this case it will give error.
Since /one/two/five existed it simply moved your directory /one/two/three/four as /one/two/five/six. That means /one/two/five/six is now the new name or pathname of the directory which was previously /one/two/three/four.
The problem in understanding you are having can be helped with a reference to the man page for mv and a few examples. From man 1 mv Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY. What is not apparent is what is SOURCE and what is DEST and this is where your confusion arises. For example:
mv /ten/ /one/two/three/four/five/six
it gives the output as mv: cannot move '/ten/' to '/one/two/three/four/five/six' :
No such file or directory. Which looks fine as it doesn't create directories.
It doesn't. In you example, the SOURCE is /ten and, your DEST depends on whether /one/two/three/four/five exists and also whether /one/two/three/four/five/six exists.
If /one/two/three/four/five exists, then mv /ten /one/two/three/four/five will cause /ten to be moved and become a new subdirectory of /one/two/three/four/five. e.g. /one/two/three/four/five/ten.
If /one/two/three/four/five exists (but not ../six), then mv /ten /one/two/three/four/five/six will cause /ten to be moved and become six new subdirectory of /one/two/three/four/five. e.g. /one/two/three/four/five/six.
if however /one/two/three/four/five do not exist, then mv will fail because you have not provided a valid DEST.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 11 months ago.
Improve this question
I was following the SocketXP Agent Download & Setup, and on the very first step I am asked to run the following command:
curl -O https://portal.socketxp.com/download/linux/socketxp && chmod +wx socketxp && sudo mv socketxp /usr/local/bin
This was followed by some confusion, becuase the bin-folder now appeared to be an executable. It turns out that rather than inserting socketxp into the /usr/local/bin folder, I actually deleted the whole folder and replaced it with the socketxp file, now renamed to a file called bin.
However, after recreating the folder, I see I can transfer test files into it without issues with
~touch test
~sudo mv test /usr/local/bin
So after seeing this, I re-ran the same socketxp installation command, and this time around it worked fine.
I'm at a loss as to what the original problem was, but I am very interested in not having this happen again. I suspect I am missing some basic mv knowledge. Grateful for any tips and pointers that can explain for me what caused the issue
I was doing this on a 64-bit Linux SIMATIC controller from Siemens, which runs an OS based on Debian.
I am asked to run the following command:
curl -O https://portal.socketxp.com/download/linux/socketxp && chmod +wx socketxp && sudo mv socketxp /usr/local/bin
This was followed by some confusion, becuase the bin-folder now
appeared to be an executable. It turns out that rather than inserting
socketxp into the /usr/local/bin folder, I actually deleted the whole
folder and replaced it with the socketxp file, now renamed to a file
called bin.
Not plausible. When the destination path in a mv command is a directory, the source file(s) are moved into that directory (unless that is overridden by a command-line option). This is consistent with what you observed in subsequent experiments.
However, if only one source is given and the destination path either does not exist (but its parent directory does) or designates a regular file, then mv renames the source to the destination. We can only guess about what actually happened, but my first guess would be that /usr/local/bin did not initially exist. That might have arisen because of an earlier error, such as executing rm -rf /usr/local/bin when you really meant rm -rf /usr/local/bin/*, or perhaps the machine just came that way.
In Linux you can use mv also to rename files. To prevent a mistake like yours, always add a / at the end of a path that you want to move something into:
sudo mv test /usr/local/bin/
This would have behaved as you expected and put the file test into the folder /usr/local/bin.
As #JohnBollinger stated, the behaviour to expect would be that mv moves files into an existing folder if you do not explicitly tell it that the last parameter is NOT a directory.
In your case it could have been that /usr/local/bin simply didn't exist when you executed your command. In this case the variant with the appended / would have emitted an error message. Or you accidentally specified the option -T (maybe intended for some other command or a copy-paste-mistake?)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
I had a problem with my Ubuntu install. I was able to boot from liveCD and connect an external hard drive. I want to backup my files now.
I tried cp -r /home destination, but I get problem with spaces in filenames, symlinks, errors "Cannot create fifo: Operation not permitted" "Permission denied" "Invalid argument" and plenty more. What is the best way to do it? Will cp -a fix these issues or should I do something more clever?
I found out that rsync doesn't have problems with filenames. But it doesn't copy .so and .a files. Also it is running extremely slow comparing to cp.
EDIT:
I followed the advice of John Bollinger and created an archive, because my external drive wasn't ext4 formatted, so is not able to preserve all file attributes.
From a liveCD home refers to liveCD home, so one has to use:
tar -c -z -f /my/backup/disk/home.tar.gz -C / media/ubuntu/longDeviceName/home
Despite sudo, I still received some "Cannot open: Permission denied" and "socket ignored" errors creating a tar for several .png files in .cache/software-center/icons/blabla. I wonder whether it is normal.
If you do not want to reformat your backup disk with a filesystem that has enough capabilities to represent all of the attributes of your files (e.g. ext4) then preserving them across the backup requires putting them into some sort of container. The traditional container for this sort of thing is a [compressed] tarball. You might therefore try
tar -c -z -f /my/backup/disk/home.tar.gz -C / home
You would recover the contents of that tarball via
tar -x -z -f /my/backup/disk/home.tar.gz -C /
Either or both might need to be run with privilege, obtained by being root or by using sudo.
That will handle symlinks, executable files, and any filename just fine, but it may still have trouble if the data you are trying to back up include any special files, such as device nodes or FIFOs. In that event, you may simply need to remove such files first, and recreate them after restoring the other files. You can identify such files via find:
find /home -not -type f -not -type d -not -type l
The accepted answer does not backup / recover file permission.
You should use parameter "p" while backing up and while recovering.
Also you might want to recover to specific folder and then move things around to not overwrite files you might want to keep.
"/" on the end of the command stands for backing up entire system:
sudo tar -cvpzf /backupfolder/backup.tar.gz --exclude=/mnt /
sudo mkdir /recover_v1.1
sudo tar -xvpzf backup.tar.gz -C /recover_v1.1
... // replacing whatever you need manually
Manually replace files you need to recover and keep those you want to keep.
-x extract
-p include permissions
-v verbose will show you the files name while working
-z compression
-f name the file
You might want to setup cron jobs to run backup automatically.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
Operating system: SLES12 VM
So I start off in a directory:
DirA: /home/user/testA/testB
My goal is to move a file from this directory to a directory given by
DirB_rel: /home/user/testA/testB/../../deliverables/rpm/SOURCE
Note: testA is a symlink which is not included in DirB_abs
Which, when I cd to it, gives a pwd of
DirB_abs:/home/user/deliverables/rpm/SOURCE
The problem is, when I try move a file using mv (have tried tar.gz and .txt) from DirA to DirB_rel, the file is deleted from original location as expected, but it does not appear at new location and is therefore lost.
E.g. mv testFile.txt DirB_rel -> File disappears
However, when I use the absolute path for directory B, mv works correctly.
E.g. mv testFile.txt DirB_abs -> Success
Any idea whats going on here?
Thanks!
The problem is with the symlink. When you do user/testA/testB/../../ and testA is asymlink, you wont go back to user, but to the parent directory of the directory testA links to
the mv command will reference the directory you are currently in and not from where the file is. So if we are in home ~/ and want to move ~/A/file to ~/B/file you use mv as follows:
mv A/file B/
Note that if you use this
mv A/file ../B/
the command will look for B in /home/B and not ~/B since we are in the ~/ directory issuing the command.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
when i copy files, execute the file in linux, I am Not quite understand the difference and how to use them.
please help. thanks.
. means current directory
./ means current directory, too
./* means all files in current directory
. means the current directory, and ./ is the same but more explicit, saying "Hey, I'm a directory!" It's like any other folder: TheFolder and TheFolder/ refer to the same object. One case where the meaning is different is when looking at a symlink pointing at a directory: TheLink can refer either to the link object or the directory that you pointed at, depending on the situation, while TheLink/ will always refer to the directory. Also, when you run the rsync command, it will treat TheFolder and TheFolder/ differently.
./* just means all the files in the current directory, same as *. Bash expands that asterisk before the command is run, so the program doesn't see the asterisk, instead seeing all the files as arguments. The difference between these two is that for the former, the command will see "./" prepended to each filename.
To see how these differ, echo is a safe command to run. It will just print the value of what you send it:
echo *
echo ./*
. and ./ both mean current working directory
../ means parent directory of the current working directory
./* means all files and directories in current working directory plus all files and directories in all directories in the current working directory.
./* is all files in current directory
./ is current directory
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I have two copies of the same directory tree. They almost have the same files in both (one version may have a couple extra or missing files). However, most of the files are in common to both directories (have the same relative paths and everything).
Assume these are in directories:
version1/
version2/
The problem is that the permissions in version1/ got messed up, and I would like to copy over the permissions from version2/, but do it without replacing the files in version1/ which are newer.
Is there an automated way to do this via bash? (It doesn't have to be bash, it could be some other method/programming language as well).
You should have a look at the --reference option for chmod:
chmod --reference version2/somefile version1/somefile
Apply find and xargs in a fitting manner and you should be fine, i.e. something like
~/version2$ find . -type f | xargs -I {} chmod --reference {} ../version1/{}
This even works recursively, and is robust against missing files in the target directory (bar the No such file ... errors, which can be ignored). Of course it won't do anything to files that only exist in the target directory.
Cheers,
You could use this script (it changes the permissions recursively but individually for each file/directory)
#!/bin/sh
chmod --reference $1 $2
if [ -d $1 ]
then
if [ "x`ls $1`" != "x" ]
then
for f in `ls $1`
do
$0 $1/$f $2/$f
done
fi
fi
Run the script with arguments version2 version1
You could try:
chmod owner-group-other ./dir or ./file
Unless permissions are fine grained and different from one file to another, you could do a recursive chmod on the directory and unify the permissions.
See man chmod for references on the options that might be useful