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.

2
  • yes this is my goal.. i have wrong.. the only two var are the first two on top of script
    – David_D
    Nov 26, 2013 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, 2013 at 20:53

6 Answers 6

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)

2
  • 2
    It's because wildcards are not interpreted inside a quoted text.
    – Amessihel
    Nov 8, 2022 at 18:51
  • Oh, Dang it I had the * inside the "". It makes sense why it goes outside, thank you. Nov 17, 2023 at 22:56
3

how about this, where src and dest are directories:

cp -r src dest
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
3
  • 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, 2013 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, 2013 at 20:39
  • @David_D Why not? are you trying to copy recursively, e.g. /data/app/foo/bar/baz.apk as well? Nov 26, 2013 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.

1
  • so this copy the entire folder to another?
    – David_D
    Nov 26, 2013 at 20:50
0

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.