adb 拉取多个文件

问题描述 投票:0回答:16

使用拉取多个文件的最佳方法是什么

adb pull

我的

/sdcard/
上有 25 个文件,其名称如下:

gps1.trace
gps2.trace
...
gps25.trace

通配符不起作用:

adb pull /sdcard/gps*.trace .
android adb
16个回答
156
投票

您可以使用

xargs
以及接受通配符的
adb shell ls
命令的结果。这允许您复制多个文件。令人烦恼的是,
adb shell ls
命令的输出包含换行控制字符,您可以使用
tr -d '\r'
删除这些字符。

示例:

# Using a relative path
adb shell 'ls sdcard/gps*.trace' | tr -d '\r' | xargs -n1 adb pull
# Using an absolute path 
adb shell 'ls /sdcard/*.txt' | tr -d '\r' | sed -e 's/^\///' | xargs -n1 adb pull

112
投票

adb pull
可以接收目录名称而不是文件,它将拉取包含其中所有文件的目录。

将所有 GPS 轨迹拉入 /sdcard/gpsTraces

adb pull /sdcard/gpsTraces/ . 

递归目录的

adb pull
adb push
示例:

C:\Test>adb pull /data/misc/test/ .
pull: building file list...
pull: /data/misc/test/test1/test2/test.3 -> ./test1/test2/test.3
pull: /data/misc/test/test1/test2/test.2 -> ./test1/test2/test.2
pull: /data/misc/test/test1/test2/test.1 -> ./test1/test2/test.1
pull: /data/misc/test/test1/test.3 -> ./test1/test.3
pull: /data/misc/test/test1/test.2 -> ./test1/test.2
pull: /data/misc/test/test1/test.1 -> ./test1/test.1
pull: /data/misc/test/test.3 -> ./test.3
pull: /data/misc/test/test.2 -> ./test.2
pull: /data/misc/test/test.1 -> ./test.1
9 files pulled. 0 files skipped.
0 KB/s (45 bytes in 0.093s)

C:\Test>adb push . /data/misc/test/
push: ./test1/test2/test.3 -> /data/misc/test/test1/test2/test.3
push: ./test1/test2/test.2 -> /data/misc/test/test1/test2/test.2
push: ./test1/test2/test.1 -> /data/misc/test/test1/test2/test.1
push: ./test1/test.3 -> /data/misc/test/test1/test.3
push: ./test1/test.2 -> /data/misc/test/test1/test.2
push: ./test1/test.1 -> /data/misc/test/test1/test.1
push: ./test.3 -> /data/misc/test/test.3
push: ./test.2 -> /data/misc/test/test.2
push: ./test.1 -> /data/misc/test/test.1
9 files pushed. 0 files skipped.
0 KB/s (45 bytes in 0.062s)

28
投票

./adb pull /sdcard
<-- fails

./adb pull /sdcard/
<-- works recursively - note the trailing slash

使用 Nexus 5 和 2014 年 3 月下载的 adb 进行测试。


18
投票

解析“ls”的输出通常是一个坏主意。相反,请使用“查找”。

adb shell 'find /sdcard/ -name "gps*.trace" -print0' | xargs -0 -n 1 adb pull

为什么你不应该解析 ls 的输出


7
投票

我为 Windows 盒子创建了这个,使用通配符传输文件而不挂载文件系统非常有用。您可以将此脚本包含在路径环境中的某个位置。

adbpull.bat

@echo off
setlocal enabledelayedexpansion
if %1.==. (
    echo Wilcard parameter is required.
    goto end
)
for /F "tokens=* USEBACKQ" %%F in (`adb shell ls %1`) do (
    set text=%%F
    set mfile=!text:~0,-1!
    adb pull "!mfile!"
)
:end
endlocal

示例:

adbpull /sdcard/DCIM/Camera/IMG_2016*


6
投票

即使

adb pull
命令开始接受远程参数的文件夹名称,我仍然更喜欢使用
tar
命令。它提供了更大的灵活性 - 允许文件名模式(includeexclude)、符号链接控制、保留文件权限。从 Android 6.0 开始,您可以使用内置的。在此之前,您必须使用第三方工具,例如
busybox
:

adb exec-out tar c sdcard/amazonmp3 > amazon.tar

确保省略路径中的前导

/


5
投票

ADBFS 用于 Android 调试桥的 FUSE 文件系统(如果您使用的是 Linux 或 Mac)


4
投票

目录拉取可在新的 Android 工具上使用。 (我不知道它是从哪个版本添加的,但它在最新的 ADT 21.1 上工作)

adb pull /sdcard/Robotium-Screenshots
pull: building file list...
pull: /sdcard/Robotium-Screenshots/090313-110415.jpg -> ./090313-110415.jpg
pull: /sdcard/Robotium-Screenshots/090313-110412.jpg -> ./090313-110412.jpg
pull: /sdcard/Robotium-Screenshots/090313-110408.jpg -> ./090313-110408.jpg
pull: /sdcard/Robotium-Screenshots/090313-110406.jpg -> ./090313-110406.jpg
pull: /sdcard/Robotium-Screenshots/090313-110404.jpg -> ./090313-110404.jpg
5 files pulled. 0 files skipped.
61 KB/s (338736 bytes in 5.409s)

2
投票

基于大卫的回答,我发现这个稍微好一些:

adb shell ls /foo | tr -d '\r' | xargs -n1 adb pull

除了少输入一个字符(很重要)之外,它不会将

-r
转换为空格。这是一个显着的差异,就像你尝试做的那样

adb shell ls /foo/myFile* | tr '\r' ' ' | xargs -i -n1 adb pull {} someDir

你会得到错误提示

remote object '/foo/myFile1 ' does not exist

相反,你可以这样做,这会起作用:

adb shell ls /foo/myFile* | tr -d '\r' | xargs -i -n1 adb pull {} someDir 

2
投票

PowerShell:

adb shell ls /sdcard/gps*.trace | foreach {adb pull $_}


0
投票

通配符适用于我的情况,我一直在使用以下简单脚本将虚拟设备的 Whatsapp 图像导入到我的桌面

#! /bin/bash
mkdir -p ~/Pictures/Pictures_adb
rm -f ~/Pictures/Pictures_adb/*
cd ~/Pictures/Pictures_adb
adb root
adb shell 'cp /data/media/0/WhatsApp/Media/WhatsApp\ Profile\ Photos/* /sdcard/Pictures/;exit'
adb pull /sdcard/Pictures
mv ~/Pictures/Pictures_adb/Pictures/* ~/Pictures/Pictures_adb/
rmdir ~/Pictures/Pictures_adb/Pictures
cd

0
投票

我想扩展

David Momenso
的答案并解决有关通配符问题的特定部分。我把他的建议变成了一个 bash 函数,该函数允许传入一个附加到通配符的参数。

这是一个 bash 函数,允许利用传入的参数和通配符根据日期提取所有视频。

function vids () { adb shell 'ls /sdcard/DCIM/Camera/VID_2022'"$1"'*.mp4' | tr -d '\r' | sed -e 's/^\///' | xargs -n1 adb pull }

  • /sdcard/DCIM/Camera/VID_2022
    指向相机文件夹,并具有代表当前年份的文件前缀
  • "$1"
    是取函数调用后的第一个参数
      示例用法中的
    • 1027
      vids 1027


0
投票

我使用的是Windows

adb.exe
gitbash
,即使语法是正确的,
adb.exe
说如果在
xargs
中使用它找不到文件。

你必须使用cmd

for
循环:

for /f "delims=" %G in ('adb shell find sdcard/DCIM/Camera/20221111*') do adb pull -a "%G"

这将下载符合条件的所有照片和视频(在我的例子中,拍摄于 2022 年 11 月 11 日)。

通过谷歌搜索

for /f
linux find
查找更多信息。


如果您没有可用的

find
(未安装 Gitbash),解决方法是将手机上所需的所有文件移动到另一个新相册,然后从那里移动
adb pull
。不是解决方案,而是解决方法。

adb pull -a sdcard/DCIM/new-album . # will pull all photos to a folder called "new-album"

0
投票

我用过这个:

adb pull $(adb shell ls /storage/9016-4EF8/DCIM/Camera/wildcard*) ./


0
投票

这可以使用

adb exec-out
实现。例如,要获取今天的所有屏幕截图,您可以执行以下操作:

adb exec-out "cd /sdcard/Pictures/Screenshots; tar c Screenshot_20210907*" | tar x

这将传输临时 tar 存档中的所有文件并将其解压到当前工作目录。信用归功于https://stackoverflow.com/a/39429196/3347392


-3
投票

在Android中,有一些文件夹具有关联的权限!某些文件夹属于 root 用户或系统用户。

你们应该在执行“adb pull”之前更改这些文件、文件夹的权限。

以下命令可能会有所帮助:

adb shell
su
chmod -R 777 target_folder
exit
...
adb pull /.../target_folder/ . (current local folder)
© www.soinside.com 2019 - 2024. All rights reserved.