LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-25-2012, 01:29 PM   #1
jim.thornton
Member
 
Registered: May 2007
Posts: 404

Rep: Reputation: 19
Help with bash scripting


I'm writing a script to copy all files within a directory that start with A-L a-l or 0-9 to one destinations and all files starting is m-z M-Z to another destination.

I got it working with the following commands:
Code:
RUNAL="cp -R {} /destination/directory/a-l"

find source_parent_dir -type d -regex "/source/parent/directory/[a-lA-L0-9].*" -exec "$RUNAL" \;
This works perfectly. However I then want to delete the files that were copied. Originally I was running the find command once to copy and then again to delete the directories/files. The problem is that I'm going to set this up on a cron. In the unlikely situation the files copy, then in the time of when the copying starts to when the copying finishes a new files is saved to the directory then the second find command would delete a file that wasn't copied.

I then came up with the idea to run the -exec command with && and a rm -rf:
Code:
RUNAL="cp -R {} /destination/directory/a-l && rm -rf {}"

find source_parent_dir -type d -regex "/source/parent/directory/[a-lA-L0-9].*" -exec "$RUNAL" \;
The weird thing is when I run the cp && rm command by itself in the shell it works. When I add it to the -exec parameter it doesn't work.

Now my idea is to run the find command, save the results into an array, then process the array and copy the files and then process the array and delete the files.

could anyone here please tell me how to save the results of the find command in an array. This is what I've done but it doesn't work:
Code:
declare -a files_found

find /source/parent/directory -maxdepth 1 -type d -regex "^/source/parent/directory/[a-l-A-L0-9].*" -exec array[${#files_found[*]} "{}" \;
 
Old 04-25-2012, 01:38 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,561

Rep: Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127
Why not just change your first script to move instead of copy?
 
Old 04-25-2012, 01:39 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,565

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
Maybe it would be better to pipe the data into a loop and then process the files (or should we say directories seeing the type -d switch) to be copied and then delete them.
Something like:
Code:
while read -r dir
do
    cp -R "$dir" /destination/directory/a-l && rm -rf "$dir"
done< <(find /source/parent/directory -maxdepth 1 -type d -regex "^/source/parent/directory/[a-l-A-L0-9].*")
 
Old 04-25-2012, 01:40 PM   #4
jim.thornton
Member
 
Registered: May 2007
Posts: 404

Original Poster
Rep: Reputation: 19
suicidaleggroll: Unfortunately you can't use the mv command when there are directories involved and you are moving from one device to another.
 
Old 04-25-2012, 01:53 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,561

Rep: Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127
Quote:
Originally Posted by jim.thornton View Post
suicidaleggroll: Unfortunately you can't use the mv command when there are directories involved and you are moving from one device to another.
Why not? Here is me moving a directory with two files onto and off of a USB flash drive:
Code:
$ ls
$ mkdir testdir
$ touch testdir/testfile1
$ touch testdir/testfile2
$ mv testdir ~/
$ ls
$ mv ~/testdir .
$ ls
testdir
$ls testdir
testfile1  testfile2
Barring that, I would put it inside a loop like grail suggested
 
Old 04-25-2012, 01:56 PM   #6
jim.thornton
Member
 
Registered: May 2007
Posts: 404

Original Poster
Rep: Reputation: 19
suicidaleggroll: All I know is I am getting an error message when using mv and when researching that error message it seems to be a limitation with mv.
 
Old 04-25-2012, 02:02 PM   #7
jim.thornton
Member
 
Registered: May 2007
Posts: 404

Original Poster
Rep: Reputation: 19
grail: Thank you, that worked perfectly and is alot easier than what I was doing.

Could you please explain a few things for me:

1. What does the -r flag do on the while read -r dir?

2. done< <(command)
- what exactly is < < ?
- does there have to be a space between them
- can I use any command in there that outputs something?
 
Old 04-25-2012, 02:32 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,565

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
Quote:
1. What does the -r flag do on the while read -r dir?
I find help is easier to use than man here:
Code:
-r		do not allow backslashes to escape any characters
Quote:
2. done< <(command)
a what exactly is < < ?
b does there have to be a space between them
c can I use any command in there that outputs something?
a) Process substitution - http://mywiki.wooledge.org/ProcessSubstitution

b) Yes (see link)

c) Yes (see link)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash scripting ninjafairy Programming 11 05-18-2011 11:00 AM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 03:29 AM
Bash scripting saje Linux - Newbie 4 08-22-2007 09:03 PM
need help with bash scripting rich2oo1 Programming 2 12-17-2003 01:50 PM
HELP with BASH scripting atwah Linux - Newbie 6 09-09-2003 02:10 AM

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

All times are GMT -5. The time now is 03:11 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration