LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script to select "first x folders"? (https://www.linuxquestions.org/questions/programming-9/bash-script-to-select-first-x-folders-768751/)

hawk__0 11-12-2009 03:42 PM

Bash script to select "first x folders"?
 
Is there a way to have bash "select" 20 folders and move them in a directory? The goal of this script would be to move 20 folders into 1 new folder elsewhere, then repeat until there are no folders left (so the last selected amount will likely be less than 20 folders..)

ramram29 11-12-2009 04:03 PM

sure, you can use a combination of commands for that in a script, such as: ls, head, mv.

PTrenholme 11-12-2009 04:30 PM

For the list of directories, try tree -lfid "path_to_top_directory" > "/tmp/dir_list" or look at the -type option in the findcommand.

But I don't understand why you want to do the move in blocks of 20 directories. :scratch: If the source and target are on the same drive, the move only involves the directories, and no actual data is physically moved. If the target is on a different drive, then each file in each directory is copied one at a time, so you save nothing by moving the data in groups of 20 directories.

Bottom line: Why not just mv the top-level directory to the destination? The only complication I could see would be caused by the presence of links defined using relative path notation in the items being moved, although there are options in the mv command that address that issue.

hawk__0 11-12-2009 05:43 PM

Thanks for the replies. The data is being "batched" Several people will each take a chunk of files and process them. It's just the way things have been done here for years.

I think you are a bit confused...

take this example: I have 40 directories in a folder. I want to create 2 new directories and move 20 of the original 40 into each of those 2 directories. Now those 2 new directories contain 20 files each.

ghostdog74 11-12-2009 06:20 PM

from last year till now, you have at least have exposure to Perl and shell scripting. what have you got so far? you didn't study any of them at all? a combination of wc , head, mv etc will do the job. you can also do everything in Perl if you want.

PTrenholme 11-13-2009 08:28 AM

Quote:

Originally Posted by hawk__0 (Post 3754875)
Thanks for the replies. The data is being "batched" Several people will each take a chunk of files and process them. It's just the way things have been done here for years.

I think you are a bit confused...

take this example: I have 40 directories in a folder. I want to create 2 new directories and move 20 of the original 40 into each of those 2 directories. Now those 2 new directories contain 20 files each.

In your first post you only mentioned one target directory which implied that you had a single target. This explanation is, I hope, a more correct description of your actual problem.

So, a few other questions:
  1. Is the 20 directories a fixed number? If the top folder contains N folders and you have P people to process folders, do you, instead, want to divide the N folders as equally as possible among the P people?
  2. Are all the people "processing" folders equally capable? If not, can they be ranked or grouped by ability?
  3. Are all folders equally hard to "process?" If not, can the difficulty of processing a folder be automatically (i.e., by computer) estimated before processing? If so, would that estimate be a numerical score or just a rank (e.g.: Trivial, Easy, Average, Hard, or Impossible)?
  4. Should the factors listed in items 2 and 3 be used when assigning folders to people?
My point here is that, when asking for help, it's best to describe the real problem you want solved rather then asking for help with one possible solution.

Oh, I just thought of another set of questions: Why group the directories for processing by individuals? Why not just pass the next available unassigned directory to a processor when they finish processing one? Is there, in fact, any point in creating the groups of directories?

hawk__0 11-13-2009 10:20 AM

1. Yes. I always want to move the initial sets of folders into new folders containing 20 of the original. There could be any amount of initial folders. 365, 521, 50, etc. Does this make sense?

2. This doesn't matter

3. This also doesn't matter

Because of the software used and the way this business runs, it just doesn't work that way. This is basically used to split the work, and to keep track of what is and isn't done.

Because I'm having difficulty explaining this, I'll try to illustrate it. I will make the example of folders containing 5 folders instead of 20 as I am requiring:

/home/abc/files/ABC1
/home/abc/files/ABC2
/home/abc/files/ABC3
/home/abc/files/ABC4
/home/abc/files/ABC5
/home/abc/files/ABC6
/home/abc/files/ABC7

<script runs>
ls -laR

/home/abc/files2/COMPLETE1
./ABC1
./ABC2
./ABC3
./ABC4
./ABC5

/home/abc/files2/COMPLETE2
./ABC6
./ABC7


I really hope this makes sense, lol.

hawk__0 11-13-2009 10:22 AM

Quote:

Originally Posted by ghostdog74 (Post 3754909)
from last year till now, you have at least have exposure to Perl and shell scripting. what have you got so far? you didn't study any of them at all? a combination of wc , head, mv etc will do the job. you can also do everything in Perl if you want.

Yes, a little TINY bit of perl, and I've gotten fairly good with bash scripting since. The problem is, I think I would also need to read lines to complete this with those command, wouldn't I?


All times are GMT -5. The time now is 05:46 AM.