LinuxQuestions.org
Visit the LQ Articles and Editorials section
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
 
LinkBack Search this Thread
Old 12-19-2006, 09:12 PM   #1
resolute155
LQ Newbie
 
Registered: Dec 2006
Posts: 9

Rep: Reputation: 0
Linux folder and file manipulation help


I have a folder with 336 separate files such as:
sr_01_loc_0001.img
sr_01_loc_0002.img
sr_01_loc_0003.img
...
sr_01_loc_0336.img

What I want to do is divide these up into groups of 48 files apiece. So I want to take files 1-48 and put them into a new folder, and files 49-97 into a second new folder, etc. until I have all 336 in 7 new folders. The problem is the files are pretty much identical except for slight differences in their names as you can see.

Is there any Linux command that can help me do this (other then having to list 001,002,003 etc.)? For example some Linux command that would allow me to take the first x number of files in a folder and move them (so I could select the first 48 files of the folder for example)?

If there is not single command to help me, is it possible to write a shell script to accomplish this goal?


Thanks,

David
 
Old 12-19-2006, 10:01 PM   #2
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Hi,
if you execute this:
Code:
ls *img | sort -n | head -n 10
you will get your first 10 files (ls gets the file, sort -n sorts them and head take the first 10 of that list).
Then if you want to move them, you can do something like this:
Code:
ls *img | sort -n | head -n 10 | while read FILE_NAME; do mv $FILE_NAME ./yourDir; done
between the do and the done you can write any code which will be executed once for each filename (and the variable FILE_NAME have the current filename).

Hope this is useful.
Cheers!
 
Old 12-19-2006, 10:24 PM   #3
carl0ski
Member
 
Registered: Sep 2004
Location: Melbourne, Victoria Australia
Distribution: Support those that support you :)
Posts: 872
Blog Entries: 12

Rep: Reputation: 30
Quote:
Originally Posted by demon_vox
Hi,
if you execute this:
Code:
ls *img | sort -n | head -n 10
you will get your first 10 files (ls gets the file, sort -n sorts them and head take the first 10 of that list).
Then if you want to move them, you can do something like this:
Code:
ls *img | sort -n | head -n 10 | while read FILE_NAME; do mv $FILE_NAME ./yourDir; done
between the do and the done you can write any code which will be executed once for each filename (and the variable FILE_NAME have the current filename).

Hope this is useful.
Cheers!
i always seem to prefer for

for filename in `ls *img | sort -n | head -n 10`
do echo moving $filename
mv $filename directory
done
 
Old 12-20-2006, 10:07 AM   #4
resolute155
LQ Newbie
 
Registered: Dec 2006
Posts: 9

Original Poster
Rep: Reputation: 0
Great sutff! Thanks!


David
 
Old 12-20-2006, 10:32 AM   #5
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Quote:
Originally Posted by carl0ski
i always seem to prefer for

for filename in `ls *img | sort -n | head -n 10`
do echo moving $filename
mv $filename directory
done
Just for the sake of completeness I'll say what are the difference (advantage / disadvantages) of both of them.

My one-liner solution is more practical if you just want to use it in the shell and may want to change it often. Since you can write in a single command line, its easy to modify and re-execute. On the other hand, if you want to do more than just a mv it will get ofuscated and hard to use very quickly. So, it is good if you have simple commands in your while loop.

carl0ski idea of writing a for instead of a while, is better if you want to end up doing more complicated things. As you'll end up writting it in a file and then executing the script. I dont think it is very comfortable to use it directly in the shell (whenever you write a script in 4 lines, the shell is not so good at reediting that line).

Just some thoughts
Cheers!
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
script for file manipulation yongitz Programming 3 08-17-2006 11:22 AM
file name manipulation chocolatetoothpaste Linux - Software 19 04-03-2006 10:18 AM
Mass file manipulation Drack Linux - General 5 02-27-2006 06:40 AM
file manipulation with c C.Aymen Programming 2 09-01-2005 12:48 PM
C File Manipulation in Linux drigz Programming 5 10-01-2004 07:28 AM


All times are GMT -5. The time now is 04:53 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration