将使用 adb 从 phone 相机传输照片的脚本
Script that will transfer photos from phone camera using adb
故事
我用我的 phone 相机拍照和录制视频,并将它们全部保存在我的内部 storage/sdcard 中。我定期将它们备份到我的 PC 上,所以我将这些相机照片保存在 PC 存储上与 phone 存储同步。
多年来,我一直通过以下方式将我的 phone 相机照片备份到我的 PC:
- 将 phone 插入 PC 并允许访问 phone 数据
- 浏览phone存储→DCIM→相机
- 等待几分钟 系统加载所有照片列表
- 只复制几张尚未备份的最新照片
我认为等待几分钟才能加载所有照片是不必要的拖累所以我下载了adb platform tools。我已将文件夹 bin 添加到我的 Path
环境变量(即 %USERPROFILE%\Tools\adb-platform-tools_r28.0.3
),这样我就可以无缝地使用 adb
而不是每次都写它的完整路径。
脚本
我为 Git Bash 为 Windows 编写了以下脚本。如果您更改 $userprofile
变量,它也与 Unix 兼容。本质上,该脚本将两个日期之间的相机照片从 phone 存储中提取到 PC。
# Attach device and start deamon process
adb devices
# Initialize needed variables
userprofile=$(echo "$USERPROFILE" | tr "\" "/") # Windows adjustments
srcFolder="//storage/06CB-C9CE/DCIM/Camera" # Remote folder
dstFolder="$userprofile/Desktop/CameraPhotos" # Local folder
lsFile="$dstFolder/camera-ls.txt"
filenameRegex="2019061[5-9]_.*" # Date from 20190615 to 20190619
# Create dst folder if it doesn't exist
mkdir -p "$dstFolder"
# 1. List contents from src folder
# 2. Filter out file names matching regex
# 3. Write these file names line by line into a ls file
adb shell ls "$srcFolder" | grep -E "$filenameRegex" > "$lsFile"
# Pull files listed in ls file from src to dst folder
while read filename; do
if [ -z "$filename" ]; then continue; fi
adb pull "$srcFolder/$filename" "$dstFolder" # adb: error: ...
done < "$lsFile"
# Clean up
rm "$lsFile"
# Inform the user
echo "Done pulling files to $dstFolder"
问题
当我 运行 脚本 (bash adb-pull-camera-photos.sh
) 时,除 while
循环中的 adb pull
命令外,其他所有内容 运行 都很顺利。 出现以下错误:
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_204522.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190619_225739.jpg
我不确定为什么输出被破坏了。有时,当我调整 Git Bash window 的大小时,某些文本会乱七八糟。这是实际的错误文本:
adb: error: failed to stat remote object '//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg': No such file or directory
adb: error: failed to stat remote object '//storage/06CB-C9CE/DCIM/Camera/20190618_204522.jpg': No such file or directory
adb: error: failed to stat remote object '//storage/06CB-C9CE/DCIM/Camera/20190619_225739.jpg': No such file or directory
我确定这些文件存在于 phone 上的指定目录中。 当我在bash中手动执行失败的命令时,成功并输出以下内容:
$ adb pull "//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg" "C:/Users/User/Desktop/CameraPhotos/"
//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg: 1 file pulled. 15.4 MB/s (1854453 bytes in 0.115s)
问题
我不知道脚本有什么问题。我认为 Windows 系统可能会引起骚动,因为我看不出为什么相同的代码在手动输入时有效,但在脚本中输入 运行 时却不起作用。我该如何解决这个错误?
附加信息
- 请注意,我必须在 Windows 上的绝对路径开头使用
//
,因为 Git Bash 会将 /
解释为它自己的根目录(C:\Program Files\Git).
- 我已经
echo
编辑了脚本中的所有变量并获得了所有正确的路径,否则可以通过手动方法工作。
相机-ls.txt文件内容
20190618_124656.jpg
20190618_204522.jpg
20190619_225739.jpg
其他问题
- 是否可以在不使用名称的情况下导航到外部 SD 卡?我不得不使用
/storage/06CB-C9CE/
因为 /sdcard/
导航到内部存储。
- 为什么
tr "\" "/"
给我这个错误:tr: warning: an unescaped backslash at end of string is not portable
?
问题在于 Windows 行定界符。
轻松修复
只需在循环上方添加 IFS=$'\r\n'
,以便 read
命令知道实际的行分隔符。
IFS=$'\r\n'
while read filename; do
if [ -z "$filename" ]; then continue; fi
adb pull "$srcFolder/$filename" "$dstFolder"
done < "$lsFile"
说明
我尝试将整个 while
-loop 插入控制台,但失败并出现相同的错误:
$ bash adb-pull-camera-photos.sh
List of devices attached
9889db343047534336 device
tr: warning: an unescaped backslash at end of string is not portable
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_204522.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190619_225739.jpg
Done pulling files to C:/Users/User/Desktop/CameraPhotos
这次我开始调查 输出被破坏的原因 。我记得windows使用\r\n
作为换行符,意思是Carriage Return + Line Feed, (CR+LF),所以有些文字必须有已被覆盖。
这是因为 $filename
变量中存储了损坏的值。
这是脚本中的循环:
while read filename; do
if [ -z "$filename" ]; then continue; fi
adb pull "$srcFolder/$filename" "$dstFolder"
done < "$lsFile"
由于 while
循环的每次迭代都会以下列形式从 $lsFile
中读取一行:
exampleFilename.jpg\r\n
它将换行符错误地解释为文件名的一部分,因此 adb pull 尝试读取名称中包含这些空格的文件,但失败了,并且另外写入了损坏的输出。
Windows 批处理脚本
这是一个 .bat
脚本,可以通过 Windows 命令提示符 或 Windows 运行 PowerShell。不需要 Git Bash。
:: Start deamon of the device attached
adb devices
:: Pull camera files starting from date
set srcFolder=/storage/06CB-C9CE/DCIM/Camera
set dstFolder=%USERPROFILE%\Desktop\CameraPhotos
set lsFile=%USERPROFILE%\Desktop\CameraPhotos\camera-ls.txt
set dateRegex=2019061[5-9]_.*
mkdir %dstFolder%
adb shell ls %srcFolder% | adb shell grep %dateRegex% > %lsFile%
for /F "tokens=*" %%A in (%lsFile%) do adb pull %srcFolder%/%%A %dstFolder%
del %lsFile%
echo Done pulling files to %dstFolder%
- 只需编辑
srcFolder
以指向您的 phone 相机文件夹,
- 将模式插入
dateRegex
以匹配日期间隔和
- 将其保存为扩展名为
.bat
的文件,即:adb-pull-camera-photos.bat
.
- Double-click 文件,它会将过滤后的照片拉入桌面上的 CameraPhotos 文件夹。
请记住,您的 PC 上仍需要为 Windows 安装 adb。
亚行照片同步
这可能不是答案,但可能对寻找 android photo/files 备份解决方案的其他人有用。
我在 Windows 和 git bash 上使用此脚本。这可以很容易地用于 Linux。长时间备份过程的一个常见问题是它可能会被中断,您可能必须从头开始重新启动整个复制过程。
这个脚本可以帮你省去这个麻烦。您可以重新启动脚本或在两者之间中断,但它会从它离开的地方恢复复制操作。
只需更改 rfolder => android 文件夹,lfolder => 本地文件夹
#!/bin/sh
rfolder=sdcard/DCIM/Camera
lfolder=/f/mylocal/s8-backup/Camera
adb shell ls "$rfolder" > android.files
ls -1 "$lfolder" > local.files
rm -f update.files
touch update.files
while IFS= read -r q; do
# Remove non-printable characters (are not visible on console)
l=$(echo ${q} | sed 's/[^[:print:]]//')
# Populate files to update
if ! grep -q "$l" local.files; then
echo "$l" >> update.files
fi
done < android.files
script_dir=$(pwd)
cd $lfolder
while IFS= read -r q; do
# Remove non-printable characters (are not visible on console)
l=$(echo ${q} | sed 's/[^[:print:]]//')
echo "Get file: $l"
adb pull "$rfolder/$l"
done < "${script_dir}"/update.files
故事
我用我的 phone 相机拍照和录制视频,并将它们全部保存在我的内部 storage/sdcard 中。我定期将它们备份到我的 PC 上,所以我将这些相机照片保存在 PC 存储上与 phone 存储同步。
多年来,我一直通过以下方式将我的 phone 相机照片备份到我的 PC:
- 将 phone 插入 PC 并允许访问 phone 数据
- 浏览phone存储→DCIM→相机
- 等待几分钟 系统加载所有照片列表
- 只复制几张尚未备份的最新照片
我认为等待几分钟才能加载所有照片是不必要的拖累所以我下载了adb platform tools。我已将文件夹 bin 添加到我的 Path
环境变量(即 %USERPROFILE%\Tools\adb-platform-tools_r28.0.3
),这样我就可以无缝地使用 adb
而不是每次都写它的完整路径。
脚本
我为 Git Bash 为 Windows 编写了以下脚本。如果您更改 $userprofile
变量,它也与 Unix 兼容。本质上,该脚本将两个日期之间的相机照片从 phone 存储中提取到 PC。
# Attach device and start deamon process
adb devices
# Initialize needed variables
userprofile=$(echo "$USERPROFILE" | tr "\" "/") # Windows adjustments
srcFolder="//storage/06CB-C9CE/DCIM/Camera" # Remote folder
dstFolder="$userprofile/Desktop/CameraPhotos" # Local folder
lsFile="$dstFolder/camera-ls.txt"
filenameRegex="2019061[5-9]_.*" # Date from 20190615 to 20190619
# Create dst folder if it doesn't exist
mkdir -p "$dstFolder"
# 1. List contents from src folder
# 2. Filter out file names matching regex
# 3. Write these file names line by line into a ls file
adb shell ls "$srcFolder" | grep -E "$filenameRegex" > "$lsFile"
# Pull files listed in ls file from src to dst folder
while read filename; do
if [ -z "$filename" ]; then continue; fi
adb pull "$srcFolder/$filename" "$dstFolder" # adb: error: ...
done < "$lsFile"
# Clean up
rm "$lsFile"
# Inform the user
echo "Done pulling files to $dstFolder"
问题
当我 运行 脚本 (bash adb-pull-camera-photos.sh
) 时,除 while
循环中的 adb pull
命令外,其他所有内容 运行 都很顺利。 出现以下错误:
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_204522.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190619_225739.jpg
我不确定为什么输出被破坏了。有时,当我调整 Git Bash window 的大小时,某些文本会乱七八糟。这是实际的错误文本:
adb: error: failed to stat remote object '//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg': No such file or directory
adb: error: failed to stat remote object '//storage/06CB-C9CE/DCIM/Camera/20190618_204522.jpg': No such file or directory
adb: error: failed to stat remote object '//storage/06CB-C9CE/DCIM/Camera/20190619_225739.jpg': No such file or directory
我确定这些文件存在于 phone 上的指定目录中。 当我在bash中手动执行失败的命令时,成功并输出以下内容:
$ adb pull "//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg" "C:/Users/User/Desktop/CameraPhotos/"
//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg: 1 file pulled. 15.4 MB/s (1854453 bytes in 0.115s)
问题
我不知道脚本有什么问题。我认为 Windows 系统可能会引起骚动,因为我看不出为什么相同的代码在手动输入时有效,但在脚本中输入 运行 时却不起作用。我该如何解决这个错误?
附加信息
- 请注意,我必须在 Windows 上的绝对路径开头使用
//
,因为 Git Bash 会将/
解释为它自己的根目录(C:\Program Files\Git). - 我已经
echo
编辑了脚本中的所有变量并获得了所有正确的路径,否则可以通过手动方法工作。
相机-ls.txt文件内容
20190618_124656.jpg
20190618_204522.jpg
20190619_225739.jpg
其他问题
- 是否可以在不使用名称的情况下导航到外部 SD 卡?我不得不使用
/storage/06CB-C9CE/
因为/sdcard/
导航到内部存储。 - 为什么
tr "\" "/"
给我这个错误:tr: warning: an unescaped backslash at end of string is not portable
?
问题在于 Windows 行定界符。
轻松修复
只需在循环上方添加 IFS=$'\r\n'
,以便 read
命令知道实际的行分隔符。
IFS=$'\r\n'
while read filename; do
if [ -z "$filename" ]; then continue; fi
adb pull "$srcFolder/$filename" "$dstFolder"
done < "$lsFile"
说明
我尝试将整个 while
-loop 插入控制台,但失败并出现相同的错误:
$ bash adb-pull-camera-photos.sh
List of devices attached
9889db343047534336 device
tr: warning: an unescaped backslash at end of string is not portable
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_204522.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190619_225739.jpg
Done pulling files to C:/Users/User/Desktop/CameraPhotos
这次我开始调查 输出被破坏的原因 。我记得windows使用\r\n
作为换行符,意思是Carriage Return + Line Feed, (CR+LF),所以有些文字必须有已被覆盖。
这是因为 $filename
变量中存储了损坏的值。
这是脚本中的循环:
while read filename; do
if [ -z "$filename" ]; then continue; fi
adb pull "$srcFolder/$filename" "$dstFolder"
done < "$lsFile"
由于 while
循环的每次迭代都会以下列形式从 $lsFile
中读取一行:
exampleFilename.jpg\r\n
它将换行符错误地解释为文件名的一部分,因此 adb pull 尝试读取名称中包含这些空格的文件,但失败了,并且另外写入了损坏的输出。
Windows 批处理脚本
这是一个 .bat
脚本,可以通过 Windows 命令提示符 或 Windows 运行 PowerShell。不需要 Git Bash。
:: Start deamon of the device attached
adb devices
:: Pull camera files starting from date
set srcFolder=/storage/06CB-C9CE/DCIM/Camera
set dstFolder=%USERPROFILE%\Desktop\CameraPhotos
set lsFile=%USERPROFILE%\Desktop\CameraPhotos\camera-ls.txt
set dateRegex=2019061[5-9]_.*
mkdir %dstFolder%
adb shell ls %srcFolder% | adb shell grep %dateRegex% > %lsFile%
for /F "tokens=*" %%A in (%lsFile%) do adb pull %srcFolder%/%%A %dstFolder%
del %lsFile%
echo Done pulling files to %dstFolder%
- 只需编辑
srcFolder
以指向您的 phone 相机文件夹, - 将模式插入
dateRegex
以匹配日期间隔和 - 将其保存为扩展名为
.bat
的文件,即:adb-pull-camera-photos.bat
. - Double-click 文件,它会将过滤后的照片拉入桌面上的 CameraPhotos 文件夹。
请记住,您的 PC 上仍需要为 Windows 安装 adb。
亚行照片同步
这可能不是答案,但可能对寻找 android photo/files 备份解决方案的其他人有用。
我在 Windows 和 git bash 上使用此脚本。这可以很容易地用于 Linux。长时间备份过程的一个常见问题是它可能会被中断,您可能必须从头开始重新启动整个复制过程。
这个脚本可以帮你省去这个麻烦。您可以重新启动脚本或在两者之间中断,但它会从它离开的地方恢复复制操作。
只需更改 rfolder => android 文件夹,lfolder => 本地文件夹
#!/bin/sh
rfolder=sdcard/DCIM/Camera
lfolder=/f/mylocal/s8-backup/Camera
adb shell ls "$rfolder" > android.files
ls -1 "$lfolder" > local.files
rm -f update.files
touch update.files
while IFS= read -r q; do
# Remove non-printable characters (are not visible on console)
l=$(echo ${q} | sed 's/[^[:print:]]//')
# Populate files to update
if ! grep -q "$l" local.files; then
echo "$l" >> update.files
fi
done < android.files
script_dir=$(pwd)
cd $lfolder
while IFS= read -r q; do
# Remove non-printable characters (are not visible on console)
l=$(echo ${q} | sed 's/[^[:print:]]//')
echo "Get file: $l"
adb pull "$rfolder/$l"
done < "${script_dir}"/update.files