I'm working with a CentOS server. I have a folder named test located in /home/server/folder/test. I need to copy the directory test to /home/server/. How can I do it?
cp -r /home/server/folder/test /home/server/
To copy all files, including hidden files use:
cp -r /home/server/folder/test/. /home/server/
As I understand, you want to recursively copy test directory into /home/server/ path...
This can be done as:
-cp -rf /home/server/folder/test/* /home/server/
Hope this helps
This works for me.
cp -r /home/server/folder/test/. /home/server
For copy directory use following command
cp -r source Destination
For example
cp -r /home/hasan /opt
For copy file use command without -r
cp /home/file /home/hasan/
Related
I have this command:
ln -sf src/* lang/golang/src/genericc/
I want to symlink all the files in src to the existing genericc directory, but when I run the above command I get broken symlinks in the destination. Anyone know how to do this?
Symlinks created with relative paths (i.e. where the source path doesn't start with "/") get resolved relative to the directory the link is in. That means a link to "src/foo.c" in the lang/golang/src/genericc/ directory would try to resolve to lang/golang/src/genericc/src/foo.c which probably doesn't exist.
Solution: either use an absolute path to the source files, like this:
ln -sf /path/to/src/* lang/golang/src/genericc/
or, to get the * wildcard to work right with a correct command, cd to the target directory so the relative paths will work the same way during creation that they will during resolution:
cd lang/golang/src/genericc
ln -sf ../../../../src/* ./
First of all, you can try ln -s $PATH_TO_SRC/* $PATH_TO_TARGET/.
However, it might have the "Argument list too long error".
Then you can use:
find $PATH_TO_SRC/ -type f -name "*.jpg" -exec cp -s {} . \;
Because if you use ln -s with find or bash loop, it will only create an empty link. Instead, we can use cp -sto create a smylink as well.
With the -r option, ln creates a link to the actual files wherever they are:
ln -srf src/* lang/golang/src/genericc/
This is probably a simple fix but I wrote a bash script to create two directories with one of those being a sub-directory of the other. I will link the script below. It creates the "/usr/local/sites" just fine but it won't create the A-upgrade below that directory for some reason. Any thoughts?
#!/bin/bash
DIRECTORY=/usr/local/sites/
SITE=A
sudo mkdir -p "$DIRECTORY"
sudo mkdir -p "$DIRECTORY/$SITE-upgrade/"
cd "$DIRECTORY/$SITE-upgrade/"
After help from the others in the comments, I stupidly realized that I had a cleanup function in my script that was deleting my directory, which is what it was suppose to do. Thanks again for the help guys. Sometimes it helps to add a "-x". The cleanup directory did the following and was deleting the directory I was searching for.
log "cleaning up folder"
log "cd up a directory"
cd ..
log "remove folder $SITE-upgrade"
find "$SITE-upgrade" -type d | xargs rm -rf
You have $SITE in the sudo statement instead of $SITES, which is the variable you assigned to above the sudo statement.
I would like to copy or move some file a.xml from /tmp to another folder in the same server /var/lib/myfolder.
I tried the following with putty:
user#server:/tmp$ mv a.xml /var/lib/myfolder
I get the error message, that this operation is not allowed.
How can I copy or move this file to another folder?
Update:
Error message:
mv a.xml is not possible: The operation is not allowed
Try cp and then rm the source file.
cp a.xml /var/lib/myfolder
rm -f a.xml
The solution is to, as my comment, write this command:
sudo mv a.xml /var/lib/myfolder
I have a directory that I want to copy all of it, but to a directory with a different name.
Example:
/Home/user/DirA-Web
copy its contents to (but it needs to be created)
/Home/user/version1/DirB-Img
/Home/user/version2/DirB-Img
I could always copy it and the rename it, I suppose.
Edit: I currently rsync the directories to the desired location and them mv in a for loop to rename them. I am looking for something cleaner.
If the directory
/Home/user/version1/
exists, a simple cp will do:
cp -r /Home/user/DirA-Web /Home/user/version1/DirB-Img
If not, you need to use mkdir beforehand, because cp has no option to
create your target directory recursively:
mkdir -p /Home/user/version1/DirB-Img && cp -r /Home/user/DirA-Web /Home/user/version1/DirB-Img
This question already has answers here:
Linux: copy and create destination dir if it does not exist
(27 answers)
Closed 7 years ago.
When copying a file using cp to a folder that may or may not exist, how do I get cp to create the folder if necessary? Here is what I have tried:
[root#file nutch-0.9]# cp -f urls-resume /nosuchdirectory/hi.txt
cp: cannot create regular file `/nosuchdirectory/hi.txt': No such file or directory
To expand upon Christian's answer, the only reliable way to do this would be to combine mkdir and cp:
mkdir -p /foo/bar && cp myfile "$_"
As an aside, when you only need to create a single directory in an existing hierarchy, rsync can do it in one operation. I'm quite a fan of rsync as a much more versatile cp replacement, in fact:
rsync -a myfile /foo/bar/ # works if /foo exists but /foo/bar doesn't. bar is created.
I didn't know you could do that with cp.
You can do it with mkdir ..
mkdir -p /var/path/to/your/dir
EDIT
See lhunath's answer for incorporating cp.
One can also use the command find:
find ./ -depth -print | cpio -pvd newdirpathname
mkdir -p `dirname /nosuchdirectory/hi.txt` && cp -r urls-resume /nosuchdirectory/hi.txt
There is no such option. What you can do is to run mkdir -p before copying the file
I made a very cool script you can use to copy files in locations that doesn't exist
#!/bin/bash
if [ ! -d "$2" ]; then
mkdir -p "$2"
fi
cp -R "$1" "$2"
Now just save it, give it permissions and run it using
./cp-improved SOURCE DEST
I put -R option but it's just a draft, I know it can be and you will improve it in many ways. Hope it helps you
rsync is work!
#file:
rsync -aqz _vimrc ~/.vimrc
#directory:
rsync -aqz _vim/ ~/.vim
cp -Rvn /source/path/* /destination/path/
cp: /destination/path/any.zip: No such file or directory
It will create no existing paths in destination, if path have a source file inside.
This dont create empty directories.
A moment ago i've seen xxxxxxxx: No such file or directory, because i run out of free space. without error message.
with ditto:
ditto -V /source/path/* /destination/path
ditto: /destination/path/any.zip: No space left on device
once freed space cp -Rvn /source/path/* /destination/path/ works as expected