How to copy file in the server to another folder with putty - linux

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

Related

Remove a file has the same name with folder after using tar

I created a script to find files, move them to a folder, compress this folder using tar. Then delete original folder. But after running the script, the folder was removed but a file having same name with the folder was created. I tried to run one by one command, it's OK. There is not this file. I added rm command in the script to remove it but not working.
I don't know why this file was create.
My script is below:
#!/bin/bash
cd /home/tuan/testrm/9/
mkdir compressdir
sudo find . -type f -name "*.txt" -print | xargs -I {} mv {} compressdir > /dev/null 2>&1 &
tar -cvzf compresslog.tar.gz --remove-files compressdir
mv compresslog.tar.gz compresslog-`date +"%d%m%Y"`.tar.gz
rm -rf compressdir
I want to know why this file was create and how to prevent this happen.
You should remove & at the end of sudo find line, and it will works.
Because of The & makes the command run in the background.
Root cause: the sudo find command line and tar -> mv -> rm run at synchronized.
If you had checked the file compresslog.tar.gz which the script generated, you will found that it was null or error, and the compress file contain the same content with someone file which you find.

Symlink multiple files to an existing folder

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/

Download, extract and copy file from a folder that has a version in its name

I'm writing a bash script that downloads an compressed archive from an universal URL (in which new release of the software will automatically be presented), extracts it and copies a file called wimboot to a folder.
This is what I currently have:
sudo curl http://git.ipxe.org/releases/wimboot/wimboot-latest.tar.gz -o ./wimboot.tar.gz
sudo tar -zxvf ./wimboot.tar.gz #Extracts into a folder called "wimboot-2.5.2-signed", in it is the file I need ("wimboot").
cd ./wimboot*/
sudo cp wimboot /my-folder/
But this doesn't work. Is there a method that will allow me to do this?
You can ask tar for a file listing (-t option), which you can then grep for wimboot -- that should give you the relative path to the file also. A naive first try would be something like:
src_file=$(tar tf wimboot.tar.gz | grep wimboot)
cp "$src_file" my_folder/
But you will probably want to add some error checking and stuff to that. And probably a more complicated grep expression to ensure you get the one thing you're after.
There's also no need to extract the entire archive. You can just ask tar to extract the file you're interested in:
tar zxf wimboot.tar.gz "$src_file"
I've built on top of the other answers and this worked for me:
#Create a temporary folder, this folder must be empty!
sudo mkdir temp
#Download the archive and save it in the temporary folder.
sudo curl http://git.ipxe.org/releases/wimboot/wimboot-latest.tar.gz -o ./temp/wimboot-latest.tar.gz
#Extract the downloaded archive to the temporary folder.
sudo tar xvf ./temp/wimboot-latest.tar.gz -C ./temp
#Search for and copy files with the name "wimboot" to the web directory.
sudo find ./temp/ -name 'wimboot' -exec cp {} /var/www/ \;
#Delete the temporary folder.
sudo rm -Rf temp
I do not recommend this for large archives.

CentOS: Copy directory to another directory

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/

How to have the cp command create any necessary folders for copying a file to a destination [duplicate]

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

Resources