David_D
David_D

Reputation: 1402

Copy entire content from a folder to another in shell script

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.

Upvotes: 9

Views: 27674

Answers (6)

Michael Ford
Michael Ford

Reputation: 861

how about this, where src and dest are directories:

cp -r src dest

Upvotes: 3

Lucio Mollinedo
Lucio Mollinedo

Reputation: 2424

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)

Upvotes: 17

PyDevSRS
PyDevSRS

Reputation: 1865

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.

Upvotes: 0

Abhi
Abhi

Reputation: 1255

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

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 246807

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.

Upvotes: 0

that other guy
that other guy

Reputation: 123460

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

Upvotes: 2

Related Questions