LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-12-2009, 03:42 PM   #1
hawk__0
Member
 
Registered: Nov 2008
Posts: 105

Rep: Reputation: 15
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..)
 
Old 11-12-2009, 04:03 PM   #2
ramram29
Member
 
Registered: Jul 2003
Location: Miami, Florida, USA
Distribution: Debian
Posts: 848
Blog Entries: 1

Rep: Reputation: 47
sure, you can use a combination of commands for that in a script, such as: ls, head, mv.
 
Old 11-12-2009, 04:30 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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. 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.
 
Old 11-12-2009, 05:43 PM   #4
hawk__0
Member
 
Registered: Nov 2008
Posts: 105

Original Poster
Rep: Reputation: 15
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.
 
Old 11-12-2009, 06:20 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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.
 
Old 11-13-2009, 08:28 AM   #6
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
Originally Posted by hawk__0 View Post
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?
 
Old 11-13-2009, 10:20 AM   #7
hawk__0
Member
 
Registered: Nov 2008
Posts: 105

Original Poster
Rep: Reputation: 15
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.
 
Old 11-13-2009, 10:22 AM   #8
hawk__0
Member
 
Registered: Nov 2008
Posts: 105

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ghostdog74 View Post
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?
 
  


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
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
How might I restore kmail folders/mail/settings from a "badly" saved "home"? deh6 Linux - Software 5 03-08-2008 09:25 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
helping me in a bash script that perform a "select" menus Task adam_blackice Programming 5 09-15-2007 01:09 PM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:48 AM.

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