How can you reliably move files from android to windows pc? - android

I'm trying to set up a bash script that can move video files (.mp4) from my android phone to my windows PC (or more accurately, to an external SSD). The goal is to save time transferring files as I will be doing this multiple times a day in between other things, so I want to do it automatically.
I know that it's possible to use adb pull to move the files and then use adb shell to delete them on the phone afterwards, but I am worried that if adb pull fails, it might delete some of the video files. Hence, I am looking for a solution that checks if the files were transferred correctly before deleting them from the phone.

Check the exit status of the command and delete if there is not error
#!/bin/bash
file=/data/local/tmp/file1
if adb pull "$file"
then
adb shell rm "$file"
fi

Related

shell command to create files

I want to create files with different sizes on an Android device.
The one approach I have already tried was to create dummy files using fsutil and push it to devices.
Is there any way to achieve similar result with a command inside adb shell?
Yes, you can do it using dd command. I am not pretty sure it's available in your device. It works fine in my device, you can give a try.
In your host which connects with your Android device, with adb debug turn on, using the following command to create dummy file.
adb shell 'dd if=/dev/zero of=/data/local/tmp/test.img bs=4k count=800'
# check the result
adb shell ls -l /data/local/tmp/test.img
-rw------- shell shell 3276800 2017-06-21 17:33 test.img
The command above will get data from /dev/zero and output to /data/local/tmp/test.img (a public writable directory for Android device), adjust bs and count value in your situation.

How can I get a working adb shell on android when /system/bin/sh is missing?

I'm trying to build Android Jellybean from source for the Measy U2C HDMI stick. I've managed to build and install all the partitions (boot, kernel, misc, recovery, system...). The problem I'm having is that the system partition doesn't seem to be mounting. When I run
adb ls /system
I get the following output:
000041ed 00000400 51301410 .
000041c0 00000800 00000003 lost+found
000041ed 00000000 00000001 ..
I'd like to adb shell into the device and try to debug why the system partition is not mounting but adb wants there to be a working shell in /system/bin/sh.
$ adb shell
- exec '/system/bin/sh' failed: No such file or directory (2) -
My question is, how can I get adb to look elsewhere for the shell command so i can get this working? Or is there an alternate way to remote into the device and debug this? There is a busybox install at /sbin/busybox so if I can just invoke that somehow, I can figure this out.
"SHELL_COMMAND" appears to be hardcoded in adb/services.c an unofficial copy of which is browsable at
https://github.com/android/platform_system_core/blob/master/adb/services.c
Given that you are building from source you should be able to change this. But since you want to point it to a shorter path, you could also probably edit the binary and move up the terminating null.
Another approach to investigating your problem could be to see if you can get a working adb shell after booting to the recovery partition, and try manually mounting the problematic system partition there to see what errors result.
Still another idea would be to put something in the startup scripts which launches an alternate shell listening on something which you could forward a socket to using adb - I'm not thinking of an obvious reason why setting up adb forwards would depend on the device side shell, but I haven't verified that by experiment or examining the code.
If you wanted to get really clever, I believe that you could create a /system/bin containing a copy of sh on the root filesystem. My recollection is that you can mount a filesystem over a non-empty directory - not sure if there would be an issue with open file descriptors to that directory, such as for the running sh itself, but your mount is failing anyway, and you could try doing a manual mount elsewhere in order to debug that issue.

How to use adb command to push a file on device without sd card

How to push a file from computer to an Android device having no SD Card in it. I tried:
C:\anand>adb push anand.jpg /data/local
3399 KB/s (111387 bytes in 0.032s)
C:\anand>adb push anand.jpg /data/opt
3199 KB/s (111387 bytes in 0.034s)
C:\anand>adb push anand.jpg /data/tmp
3884 KB/s (111387 bytes in 0.028s)
Above commands to move a file anand.jpg to a device but I didn't get this jpg file in the device.
I didn't get any success result on cmd prompt, I only got:
3399 KB/s (111387 bytes in 0.032s).
From Ubuntu/Mac Terminal, the below command should work.
./adb push '/home/hardik.trivedi/Downloads/one.jpg' '/data/local/'
For adb v33 and above if you are getting a permission denied error, try what I tried. The following command and it works fine.
The only caveat is you might need to use tmp directory on such an emulator.
adb shell //Entering into shell
su //Super user mode
chmod 777 /data/local/tmp/ //Grantint RWX access
exit
chmod 777 /data/local/tmp/ //Grantint RWX access
exit
And then try
./adb push '/home/hardik.trivedi/Downloads/one.jpg' '/data/local/tmp/'
I did it using the push command, which has syntax:
adb push filename.extension /sdcard/0/
Example of copying directory, and sub-directory content:
adb push C:\my-location\data\. /storage/emulated/0/Android/data
Note that push did just hang in latest platform-tools (33.0.1, at time of writing) for a certain amount of files, beside the adb.exe suddenly taking 5MB+ instead of 1.5MB, hence I just did replace the adb.exe with one I had from 28.0.0 version of platform-tools (I did not downgrade the entire platform-tools, because adb.exe is kind of stand-alone).
I don't say there is any conspiracy around data folder,
But my Samsung device puts limits on my USB file transfer, beside Android v11+ not allowing access to data folder anymore, hence I needed above command to work with 100% speed (without hanging one hour for little more files).
Follow these steps :
go to Android Sdk then 'platform-tools' path on your Terminal or Console
(on mac, default path is : /Users/USERNAME/Library/Android/sdk/platform-tools)
To check the SDCards(External and Internal) installed on your device fire these commands :
1) ./adb shell (hit return/enter)
2) cd -(hit return/enter)
now you will see the list of Directories and files from your android device
there you may find /sdcard as well as /storage
3) cd /storage (hit return/enter)
4) ls (hit return/enter)
you may see sdcard0 (generally sdcard0 is internal storage) and sdcard1 (if External SDCard is present)
5) exit (hit return/enter)
to come out of adb shell
6) ./adb push '/Users/SML/Documents/filename.zip'
/storage/sdcard0/path_to_store/ (hit return/enter)
to copy file
Sometimes you need the extension,
adb push file.zip /sdcard/file.zip
run below command firstly
adb root
adb remount
Then execute what you input previously
C:\anand>adb push anand.jpg /data/local
C:\anand>adb push anand.jpg /data/opt
C:\anand>adb push anand.jpg /data/tmp
After Trying all the answers this worked for me
Where I am Pushing a file on Desktop to Android Device (Redmi K20 pro) connected Over the air using adb.
This command pushes the file to the downloads folder on my phone
adb push ~/Desktop/notifications.drawio ./storage/emulated/0/Download
after running this if you get a permission denied error
try running these commands in order (which basically changes the directory permission)
adb shell
chmod 777 /data/local/tmp
exit
and then run try the adb push command
I have documented this here feel free to share your views and help improve it.
Try this to push in Internal storage.
adb push my-file.apk ./storage/emulated/0/
Works in One plus device, without SD card.
My solution (example with a random mp4 video file):
Set a file to device:
adb push /home/myuser/myVideoFile.mp4 /storage/emulated/legacy/
Get a file from device:
adb pull /storage/emulated/legacy/myVideoFile.mp4
For retrieve the path in the code:
String myFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myVideoFile.mp4";
This is all. This solution doesn't give permission problems and it works fine.
Last point: I wanted to change the video metadata information. If you want to write into your device you should change the permission in the AndroidManifest.xml. Add this line:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I've got a Nexus 4, that is without external storage. However Android thinks to have one because it mount a separated partition called "storage", mounted in "/storage/emulated/legacy", so try pushing there: adb push anand.jpg /storage/emulated/legacy
As there are different paths for different versions. Here is a generic solution:
Find the path...
Enter adb shell in command line.
Then ls and Enter.
Now you'll see the files and directories of Android device. Now with combination of ls and cd dirName find the path to the Internal or External storage.
In the root directory, the directories names will be like mnt, sdcard, emulator0, etc
Example: adb push file.txt mnt/sdcard/myDir/Projects/
This might be the best answer you'll may read.
Setup Android Studio
Then just go to view & Open Device Explorer.
Right-click on the folder & just upload a file.
In Mac: To push files via adb
adb push /Users/Downloads⁩/amazon.sdktester.json '/mnt/sdcard/amazon.sdktester.json'
You are trying to write to system folders. With ADB you have root (admin) access so you see the system folders of which sdcard is one of them so to send a picture you could use
D:\Program Files\Android\sdk\platform-tools\adb push am files\android sdk\adb.exe push C:\Downloads\anand.jpg /sdcard/pictures/
NB: C:\Downloads\anand.jpg replace with path and name to picture..
Certain versions of android do not fire proper tasks for updating the state of file system.
You could trigger an explicit intent for updating the status of the file system.
(I just tested after being in the same OP's situation)
adb shell am broadcast -a android.intent.action.MEDIA_MOUNTED -d file:///
(You could pass a specific filepath instead of file:/// like file:///sdcard )
In my case, I had an already removed SDCard still registered in Android.
So I longpressed the entry for my old SDCard under:
Settings | Storage & USB
and selected "Forget".
Afterwards a normal
adb push myfile.zip /sdcard/
worked fine.
To push all the files at your directory to the Android device use:
PS D:\myFiles> adb push . '/data/local/tmp/'

Pull special files with ADB

I have a rooted Android device and I need to pull everything to pc.
First try in DDMS File explorer did not work out, it hangs and has to be force closed.
Trying again in powershell with
adb pull /
skips "special files"
is there a way to pull everything including special files?
EDIT: I have tried booting in Engineer mode and Meta mode and I have tried to copy from shell
No success
2 things:
Try booting the phone into FastBoot and then do adb pull. Some of the files might be in use while the phone is running and causing them to be skipped.
I haven't done this personally, but I would try to use adb to get to shell on the phone and copy the files that were skipped.
adb shell

Android ADB shell commands: Mount host PC folder?

Hope this isn't too much of an amateur/moron question: I'm trying to replace most ringtones and alarms (Dell Streak 5 (2.2.2 rooted)) with a few of my own pet sounds.
For reasons I won't bother you with, I can't use an SD card, so my .ogg replacements need be transferred straight from my PC to their respective default folders under /system/media/audio/ .
ADB push and pull commands don't seem to accept widlcards (which is a pain) but anyway, despite setting the system folder to rw, I get 'permission denied' when I tediously attempt to pull or push files one by one.
But I should be able to rm and cp interactively from ADB's shell # prompt, with simple Unix commands to transfer groups of files between the /system/media/audio folders and my Windows PC. But to do this I presumably need to mount my PC source folder, and I have no idea how to do this.
I'd be grateful for any info or ideas...
You need to have root access on the phone. My advice as a programmer would be to
1) Write a program to do this...
or just do it the easy way haha. Try this (your device must be rooted):
adb shell into your device using $> adb shell
move to the local folder using $> cd /data/local
Now create a directory for your tones with $> mkdir my_tones - this shouldn't require su
Next you must push your .ogg files from your computer to the /data/local/my_tones folder
Now you can become su user with $> su
Now you should be able to copy the files from the /data/local/my_tones directory to where ever it is you would like them.
Hope this helps.

Categories

Resources