bash 在 shell 脚本中将整个内容从一个文件夹复制到另一个文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20227236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copy entire content from a folder to another in shell script
提问by David_D
I'm trying every single code to copy all things inside a folder to another but i can't do it! I'm trying to use the code in my terminal emulator in my android device because i need that code in my application. This is the last code i use that doesn't work:
我正在尝试将每个代码都复制到另一个文件夹中的所有内容,但我做不到!我正在尝试在我的 android 设备的终端模拟器中使用代码,因为我需要在我的应用程序中使用该代码。这是我使用的最后一个不起作用的代码:
#!/bash/sh
srcdir="/data/app"
dstdir="/sdcard/prova1"
for f in ${srcdir}/*.apk
do
cp $f $dstdir $dstfile
done
and the terminal says:
终端说:
: not found
' unespectedsyntax error: 'do
can anyone help me? These are the possibility that could be good:
谁能帮我?这些可能是好的:
1) Copy all files in the /data/app folder to /sdcard/prova1
1) 将 /data/app 文件夹中的所有文件复制到 /sdcard/prova1
2) Copy directly the folder app in prova1
2)直接复制prova1中的文件夹app
3) Use a java code that do one of these two things..
3)使用执行这两件事之一的Java代码..
I have root so i can do this operation.
我有root,所以我可以做这个操作。
回答by Lucio Mollinedo
None of these answers worked for me for recursive copy. Found this in a script in one of my libraries and thought I'd share it (with a subfolder example for both the source and destination, which is what I needed):
这些答案都不适用于我的递归复制。在我的一个库的脚本中找到了这个,并认为我会分享它(源和目标的子文件夹示例,这是我需要的):
SOURCE="/my/folder"
DESTINATION="/my/destination"
cp -r "$SOURCE/subdir/"* "$DESTINATION/another_sub/"
With this, every subfolder and every file was copied.
这样,每个子文件夹和每个文件都被复制了。
I don't know why but the asterisk outside the quotesin the source did the magic for me (using bash 4.3.11 here)
我不知道为什么,但源代码中引号外的星号对我来说很神奇(此处使用 bash 4.3.11)
回答by Michael Ford
how about this, where src and dest are directories: cp -r src dest
这个怎么样,其中 src 和 dest 是目录:cp -r src dest
回答by that other guy
Your file contains carriage returns, which is why you're getting these trashed error messages. Use tr -d '\r' <yourscript >fixedscript
to get rid of them.
您的文件包含回车符,这就是您收到这些垃圾错误消息的原因。使用tr -d '\r' <yourscript >fixedscript
摆脱他们的。
As for your script, the correct way to do it is
至于你的脚本,正确的做法是
#!/bin/sh
cp /data/app/*.apk /sdcard/prova1
While the smallest fix to make your version work is
虽然使您的版本正常工作的最小修复是
#!/bin/sh
srcdir="/data/app"
dstdir="/sdcard/prova1"
for f in ${srcdir}/*.apk
do
cp $f $dstdir
done
回答by glenn Hymanman
Here's a complex method to copy an entire source tree:
这是复制整个源代码树的复杂方法:
cd $srcdir && tar cf - . | ( cd $dstdir && tar xf - )
This tars the contents of the src directory to stdout, then pipes that to a subshell where you change to the dst dir and untar from stdin.
这会将 src 目录的内容压缩到 stdout,然后通过管道将其传送到一个子shell,在那里您更改为 dst 目录并从 stdin 解压缩。
回答by Abhi
I think using rsync resolves your issue. Try something like this: rsync -avz source_path dest_path
我认为使用 rsync 可以解决您的问题。试试这样的: rsync -avz source_path dest_path
回答by PyDevSRS
Just try out below code. copy and paste that code in a text editor and save them as your_file_name.sh
试试下面的代码。将该代码复制并粘贴到文本编辑器中,并将它们保存为 your_file_name.sh
Then try to run on terminal sh ./your_file_name.sh
然后尝试在终端上运行 sh ./your_file_name.sh
cd "/data/app"
dstdir="/sdcard/prova1"
for f in *.apk
do
cp -v "$f" ${dstdir} "${f%.apk}".apk
done
echo " "
echo "File copied successfully !!"
This will copy your file from source directory to your destination folder or directory.
这会将您的文件从源目录复制到目标文件夹或目录。