LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   script to copy files (https://www.linuxquestions.org/questions/linux-newbie-8/script-to-copy-files-914745/)

me. 11-21-2011 01:23 PM

script to copy files
 
hi all,

trying to write script that will copy all files from one directory to another directory one by one (don’t use
Code:

cp * /another/directory/
)
and prompt if any errors example if another directory dont exist, or trying to copy files that already are there.

I managed to list all files and check if they exist or not but how can I change it so script will copy one file at a time and display each copied file in separate lines:

Quote:

#!/bin/bash

dest=~/Documents/Scripts/Copy

for file in $( ls )
do
if [ -d $file ]; then
[ -f $dest/$file ]
cp $file $dest/${file}1
else
echo " file $file already exist "
fi
done


Nermal 11-21-2011 01:37 PM

I would use cpio, just to be different.

Code:


# Make the directory
mkdir -p ~/Documents/Scripts/Copy

# Copy files from here aka .
find . -name -print | cpio -o | (cd ~/Documents/Scripts/Copy ; cpio -ivBud)


me. 11-21-2011 01:44 PM

that would be handy but I dont want to use cp...

another issue arrived, I thought I can use counter for copying files (if its good idea help me with it :) ) if not just suggest something else:

when I use these code:
Code:

#!/bin/bash
dir=/home/andre/Documents/Scripts/Copy
files=$(pwd || ls -m || wc -w )
counter=

echo " $files "

echo " $files " shows how many files I do have in folder BUT I want it to BOTH display all files and count how many of them is, just to use counter later for a copying....

does that make sense?

grail 11-21-2011 06:58 PM

You seem to keep saying:
Quote:

I dont want to use cp
If this is the case, are you able to explain the following from your code?
Code:

cp $file $dest/${file}1


All times are GMT -5. The time now is 11:01 AM.