Linux 如何在Android shell中复制和编辑文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4714411/
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
How to copy and edit files in Android shell?
提问by psihodelia
The Android shell does not have the cpcommand. Android shell also has no sedor grepor vi. I have no adbdaemon available. There is mvcommand but it rejects to work if source is on a read-only device.
Android shell 没有cp命令。Android shell 也没有sed或grep或vi。我没有可用的adb守护进程。有mv命令,但如果源位于只读设备上,它会拒绝工作。
- What to do if I have to copy some directories from read-only device recursively?
- How to change a line in a text file (e.g. "PATH=/cache" to be "PATH=/mnt/asec") ?
- 如果我必须递归地从只读设备复制一些目录怎么办?
- 如何更改文本文件中的一行(例如“PATH=/cache”为“PATH=/mnt/asec”)?
采纳答案by p_l
The most common answer to that is simple: Bundle few apps (busybox?) with your APK (assuming you want to use it within an application). As far as I know, the /data partition is not mounted noexec, and even if you don't want to deploy a fully-fledged APK, you could modify ConnectBot sources to build an APK with a set of command line tools included.
最常见的答案很简单:将一些应用程序(busybox?)与您的 APK 捆绑在一起(假设您想在应用程序中使用它)。据我所知,/data 分区没有挂载 noexec,即使您不想部署成熟的 APK,您也可以修改 ConnectBot 源以构建包含一组命令行工具的 APK。
For command line tools, I recommend using crosstool-ng and building a set of statically-linked tools (linked against uClibc). They might be big, but they'll definitely work.
对于命令行工具,我建议使用 crosstool-ng 并构建一组静态链接工具(链接到 uClibc)。它们可能很大,但它们肯定会起作用。
回答by Diego Torres Milano
If you have root access install busybox (google for instructions).
如果您有 root 访问权限,请安装 busybox(请使用google 获取说明)。
回答by gnclmorais
To copy dirs, it seems you can use adb pull <remote> <local>
if you want to copy file/dir from device, and adb push <local> <remote>
to copy file/dir to device. Alternatively, just to copy a file, you can use a simple trick: cat source_file > dest_file
. Note that this does not work for user-inaccessible paths.
要复制目录,adb pull <remote> <local>
如果您想从设备adb push <local> <remote>
复制文件/目录,并将文件/目录复制到设备,似乎可以使用。或者,只需复制文件,您可以使用一个简单的技巧:cat source_file > dest_file
. 请注意,这不适用于用户无法访问的路径。
To edit files, I have not found a simple solution, just some possible workarounds. Try this, it seems you can (after the setup) use it to edit files like busybox vi <filename>
. Nano seems to be possible to usetoo.
要编辑文件,我还没有找到简单的解决方案,只有一些可能的解决方法。试试这个,看来你可以(设置后)用它来编辑busybox vi <filename>
. Nano 似乎也可以使用。
回答by jmbouffard
Also if the goal is only to access the files on the phone. There is a File Explorer that is accessible from the Eclipse DDMS perspective. It lets you copy file from and to the device. So you can always get the file, modify it and put it back on the device. Of course it enables to access only the files that are not read protected.
此外,如果目标只是访问手机上的文件。有一个可从 Eclipse DDMS 透视图访问的文件资源管理器。它允许您从设备复制文件和向设备复制文件。因此,您始终可以获取文件、修改它并将其放回设备上。当然,它只能访问未受读保护的文件。
If you don't see the File Explorer, from the DDMS perspective, go in "Window" -> "Show View" -> "File Explorer".
如果您没有看到文件资源管理器,请从 DDMS 的角度进入“窗口”->“显示视图”->“文件资源管理器”。
回答by Viriatvs
You can do it without root permissions:
您可以在没有 root 权限的情况下执行此操作:
cat srcfile > /mnt/sdcard/dstfile
回答by kubo
You can use cat > filename
to use standart input to write to the file. At the end you have to put EOF CTRL+D
.
您可以使用cat > filename
标准输入来写入文件。最后你必须放 EOF CTRL+D
。
回答by 18446744073709551615
Since the permission policy on my device is a bit paranoid (cannot adb pull
application data), I wrote a script to copy files recursively.
由于我设备上的权限策略有点偏执(不能adb pull
应用数据),我写了一个脚本来递归复制文件。
Note: this recursive file/folder copy script is intended for Android!
注意:此递归文件/文件夹复制脚本适用于 Android!
copy-r:
复制-r:
#! /system/bin/sh
src=""
dst=""
dir0=`pwd`
myfind() {
local fpath=
if [ -e "$fpath" ]
then
echo $fpath
if [ -d "$fpath" ]
then
for fn in $fpath/*
do
myfind $fn
done
fi
else
: echo "$fpath not found"
fi
}
if [ ! -z "$dst" ]
then
if [ -d "$src" ]
then
echo 'the source is a directory'
mkdir -p $dst
if [[ "$dst" = /* ]]
then
: # Absolute path
else
# Relative path
dst=`pwd`/$dst
fi
cd $src
echo "COPYING files and directories from `pwd`"
for fn in $(myfind .)
do
if [ -d $fn ]
then
echo "DIR $dst/$fn"
mkdir -p $dst/$fn
else
echo "FILE $dst/$fn"
cat $fn >$dst/$fn
fi
done
echo "DONE"
cd $dir0
elif [ -f "$src" ]
then
echo 'the source is a file'
srcn="${src##*/}"
if [ -z "$srcn" ]
then
srcn="$src"
fi
if [[ "$dst" = */ ]]
then
mkdir -p $dst
echo "copying $src" '->' "$dst/$srcn"
cat $src >$dst/$srcn
elif [ -d "$dst" ]
then
echo "copying $src" '->' "$dst/$srcn"
cat $src >$dst/$srcn
else
dstdir=${dst%/*}
if [ ! -z "$dstdir" ]
then
mkdir -p $dstdir
fi
echo "copying $src" '->' "$dst"
cat $src >$dst
fi
else
echo "$src is neither a file nor a directory"
fi
else
echo "Use: copy-r src-dir dst-dir"
echo "Use: copy-r src-file existing-dst-dir"
echo "Use: copy-r src-file dst-dir/"
echo "Use: copy-r src-file dst-file"
fi
Here I provide the source of a lightweight find
for Android because on some devices this utility is missing. Instead of myfind
one can use find
, if it is defined on the device.
在这里,我提供了一个轻量级find
Android的来源,因为在某些设备上缺少此实用程序。如果它在设备上定义,则myfind
可以使用代替find
。
Installation:
安装:
$ adb push copy-r /sdcard/
Running within adb shell
(rooted):
在adb shell
(根)内运行:
# . /sdcard/copy-r files/ /sdcard/files3
or
或者
# source /sdcard/copy-r files/ /sdcard/files3
(The hash #
above is the su
prompt, while .
is the command that causes the shell to run the specified file, almost the same as source
).
(#
上面的hash是su
提示,.
而是导致shell运行指定文件的命令,和 几乎一样source
)。
After copying, I can adb pull
the files from the sd-card.
复制后,我可以adb pull
从sd卡中获取文件。
Writing files to the app directory was trickier, I tried to set r/w permissions on files
and its subdirectories, it did not work (well, it allowed me to read, but not write, which is strange), so I had to do:
将文件写入应用程序目录比较棘手,我尝试在files
其子目录上设置 r/w 权限,但它不起作用(好吧,它允许我读取,但不允许写入,这很奇怪),所以我不得不这样做:
String[] cmdline = { "sh", "-c", "source /sdcard/copy-r /sdcard/files4 /data/data/com.example.myapp/files" };
try {
Runtime.getRuntime().exec(cmdline);
} catch (IOException e) {
e.printStackTrace();
}
in the application's onCreate().
在应用程序的onCreate() 中。
PS just in case someone needs the code to unprotect application's directories to enable adb shell
access on a non-rooted phone,
PS以防万一有人需要代码来取消保护应用程序的目录以启用adb shell
在非root手机上的访问,
setRW(appContext.getFilesDir().getParentFile());
public static void setRW(File... files) {
for (File file : files) {
if (file.isDirectory()) {
setRW(file.listFiles()); // Calls same method again.
} else {
}
file.setReadable(true, false);
file.setWritable(true, false);
}
}
although for some unknown reason I could read but not write.
尽管出于某种未知的原因,我可以阅读但不能写作。
回答by Hubbitus
I could suggest just install Terminal-ideon you device which available in play market. Its free, does not require root and provide convenient *nix environment like cp, find, du, mc and many other utilities which installed in binary form by one button tap.
我建议您在游戏市场上可用的设备上安装Terminal-ide。它是免费的,不需要 root 并提供方便的 *nix 环境,如 cp、find、du、mc 和许多其他通过一键点击以二进制形式安装的实用程序。
回答by Jose
I tried following on mac.
我尝试在 mac 上关注。
- Launch Terminal and move to folder where adb is located. On Mac, usually at
/Library/Android/sdk/platform-tools
. - Connect device now with developer mode on and check device status with command
./adb status
."./"
is to be prefixed with "adb". - Now we may need know destination folder location in our device. You can check this with
adb shell
. Use command./adb shell
to enter an adb shell. Now we have access to device's folder using shell. - You may list out all folders using command
ls -la
. - Usually we find a folder
/sdcard
within our device.(You can choose any folder here.) Suppose my destination is/sdcard/3233-3453/DCIM/Videos
and source is~/Documents/Videos/film.mp4
- Now we can exit adb shell to access filesystem on our machine. Command:
./adb exit
- Now
./adb push [source location] [destination location]
i.e../adb push ~/Documents/Videos/film.mp4 /sdcard/3233-3453/DCIM/Videos
- Voila.
- 启动终端并移动到 adb 所在的文件夹。在 Mac 上,通常在
/Library/Android/sdk/platform-tools
. - 现在在开发人员模式下连接设备并使用命令检查设备状态
./adb status
。"./"
以“adb”为前缀。 - 现在我们可能需要知道我们设备中的目标文件夹位置。您可以使用
adb shell
. 使用命令./adb shell
进入 adb shell。现在我们可以使用 shell 访问设备的文件夹。 - 您可以使用命令列出所有文件夹
ls -la
。 - 通常我们会
/sdcard
在我们的设备中找到一个文件夹。(您可以在这里选择任何文件夹。)假设我的目的地是/sdcard/3233-3453/DCIM/Videos
,源是~/Documents/Videos/film.mp4
- 现在我们可以退出 adb shell 来访问我们机器上的文件系统。命令:
./adb exit
- 现在
./adb push [source location] [destination location]
即./adb push ~/Documents/Videos/film.mp4 /sdcard/3233-3453/DCIM/Videos
- 瞧。
回答by jrc
Android supports the dd command.
Android 支持 dd 命令。
dd if=/path/file of=/path/file