LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-21-2011, 01:23 PM   #1
me.
LQ Newbie
 
Registered: Nov 2011
Posts: 4

Rep: Reputation: Disabled
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
 
Old 11-21-2011, 01:37 PM   #2
Nermal
Member
 
Registered: Jan 2009
Distribution: Debian
Posts: 59
Blog Entries: 2

Rep: Reputation: 6
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)
 
Old 11-21-2011, 01:44 PM   #3
me.
LQ Newbie
 
Registered: Nov 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
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?
 
Old 11-21-2011, 06:58 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] A script to copy files. stf92 Linux - Newbie 17 12-08-2010 10:08 AM
Copy Files help script. da_bizkit Linux - Newbie 6 12-15-2009 08:10 PM
Creating a script to move or copy files into multiple directories below the files matthes138 Linux - Newbie 5 08-25-2009 04:57 PM
script to copy files scofiled83 Programming 9 07-06-2009 08:18 PM
Script to copy files defend Linux - Newbie 2 12-11-2008 10:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration