I have a file which I want to copy from one directory to another directory.
Suppose that's the file some.txt
to copy from folder1 to folder2
cp -r some.txt /folder2
I am able to copy this file but if the filename start with $
like $somefile.class
then I am not able to copy the file, just getting file not exist error
cp -r $somefile.class /folder2
Best Answer
First of all, you don't need the
-r
flag which is (fromman cp
):That is only useful when copying entire directories and their contents. Then, whenever your file names contain strange characters, you need to either escape them or protect them with single quotes as the other answers have already suggested:
Alternatively, you can use the shell's glob expansion feature to copy the file:
The
?
matches "any single character" and*
matches "0 or more characters". So, using these globs will allow you to copy the target file without worrying about the name. However, bear in mind that you should use this carefully and make sure that the globs only match the file you want to copy. For example, the ones I used would also matchLsomefile.class
.