I want to copy all of the files from a subdirectory into another directory without copying the original folder. In a terminal I would just do this:
我想将所有文件从子目录复制到另一个目录而不复制原始文件夹。在终端我会这样做:
cp -r dir1/* dir2
and then dir2 will contain all of the files from dir1 without containing dir1 itself. I am trying to replicate this in a bash script and I am getting an error. Here is my bash code:
然后dir2将包含dir1中的所有文件,而不包含dir1本身。我试图在bash脚本中复制它,我收到一个错误。这是我的bash代码:
cp -r $pck_dir"/*" $TAR_DIR"/pck/"
I get this error:
我收到此错误:
cp: cannot stat ‘./mailman/lists/mailman/*’: No such file or directory
This is strange because I can verify that the directory in question exists. I believe bash is complaining about the '*' but I am not sure why. Can someone enlighten me as to what I am doing wrong?
这很奇怪,因为我可以验证相关目录是否存在。我相信bash抱怨'*',但我不确定为什么。有人可以告诉我我做错了什么吗?
2 个解决方案
#1
11
Expanding on devnull's comment:
扩展devnull的评论:
-
Quotes of any kind around a wildcard, like
*
, will prevent the shell from expanding the wildcard. Thus, you should only write"/*"
if you want a slash followed by a literal star.通配符周围的任何类型的引用(如*)都会阻止shell扩展通配符。因此,如果你想要一个斜杠后跟一个文字星,你应该只写“/ *”。
-
An unquoted variable will be subject to word splitting. So, if pck_dir had the value
my dir
, then$pck_dir"/*"
would be expanded to two wordsmy
anddir/*
and both words would be passed tocp
as separate arguments. Unless you want word splitting, shell variables should always be in double quotes.不带引号的变量将受到分词的影响。因此,如果pck_dir的值为my dir,那么$ pck_dir“/ *”将扩展为两个单词my和dir / *,并且这两个单词将作为单独的参数传递给cp。除非您想要分词,否则shell变量应始终使用双引号。
Thus, to get what you want, use:
因此,要获得您想要的,请使用:
cp -r "$pck_dir"/* "$TAR_DIR/pck/"
#2
2
Use rsync command line instead.
请改用rsync命令行。
rsync -auv dir1/ dir2/
rsync -auv dir1 / dir2 /
will synchronize recursively all files from the folder dir1 into the folder dir2.
将递归地将文件夹dir1中的所有文件同步到文件夹dir2中。
man rsync
男子rsync
to get more explanation to know how to use this comand line.
获得更多解释,知道如何使用此命令行。
#1
11
Expanding on devnull's comment:
扩展devnull的评论:
-
Quotes of any kind around a wildcard, like
*
, will prevent the shell from expanding the wildcard. Thus, you should only write"/*"
if you want a slash followed by a literal star.通配符周围的任何类型的引用(如*)都会阻止shell扩展通配符。因此,如果你想要一个斜杠后跟一个文字星,你应该只写“/ *”。
-
An unquoted variable will be subject to word splitting. So, if pck_dir had the value
my dir
, then$pck_dir"/*"
would be expanded to two wordsmy
anddir/*
and both words would be passed tocp
as separate arguments. Unless you want word splitting, shell variables should always be in double quotes.不带引号的变量将受到分词的影响。因此,如果pck_dir的值为my dir,那么$ pck_dir“/ *”将扩展为两个单词my和dir / *,并且这两个单词将作为单独的参数传递给cp。除非您想要分词,否则shell变量应始终使用双引号。
Thus, to get what you want, use:
因此,要获得您想要的,请使用:
cp -r "$pck_dir"/* "$TAR_DIR/pck/"
#2
2
Use rsync command line instead.
请改用rsync命令行。
rsync -auv dir1/ dir2/
rsync -auv dir1 / dir2 /
will synchronize recursively all files from the folder dir1 into the folder dir2.
将递归地将文件夹dir1中的所有文件同步到文件夹dir2中。
man rsync
男子rsync
to get more explanation to know how to use this comand line.
获得更多解释,知道如何使用此命令行。