8

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:

#!/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

2) Copy directly the folder app in prova1

3) Use a java code that do one of these two things..

I have root so i can do this operation.

Ross Drew
  • 8,163
  • 2
  • 41
  • 53
David_D
  • 1,404
  • 4
  • 31
  • 65
  • yes this is my goal.. i have wrong.. the only two var are the first two on top of script – David_D Nov 26 '13 at 20:50
  • what is strange is that if i try with a single file works:`cp /data/app/com.ab.application.apk /sdcard/prova1` but for all app not. – David_D Nov 26 '13 at 20:53

6 Answers6

17

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 quotes in the source did the magic for me (using bash 4.3.11 here)

Lucio Mollinedo
  • 2,295
  • 1
  • 33
  • 28
2

how about this, where src and dest are directories:

cp -r src dest
Amessihel
  • 5,891
  • 3
  • 16
  • 40
Michael Ford
  • 851
  • 6
  • 9
1

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.

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
that other guy
  • 116,971
  • 11
  • 170
  • 194
  • One additional step is needed: the files need to be copied to `$dstdir/app`, or more generally `$dstdir/$(base name $srcdir)`, I think. – chepner Nov 26 '13 at 20:32
  • This one `cp /data/app/*.apk /sdcard/prova1` would be the best way for me but doesn't work.. – David_D Nov 26 '13 at 20:39
  • @David_D Why not? are you trying to copy recursively, e.g. `/data/app/foo/bar/baz.apk` as well? – that other guy Nov 26 '13 at 21:15
0

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.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
0

I think using rsync resolves your issue. Try something like this: rsync -avz source_path dest_path

Abhi
  • 1,153
  • 1
  • 23
  • 38
0

Just try out below code. copy and paste that code in a text editor and save them as your_file_name.sh

Then try to run on terminal 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.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
PyDevSRS
  • 1,715
  • 16
  • 17