Shell script to arrange files into several folders


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to arrange files into several folders
# 1  
Old 03-01-2010
Shell script to arrange files into several folders

Hello this is the script Im working on

I have a picture collection that I rescued from a hard drive and there are thousands of pictures saved in one folder. What I need is to create several folders and put lets say around 200 pictures in each folder.

At the end instead of having one huge folder with pictures I will end with many folders with pictures in them

SO far this is what I have been able to get

Code:
dirx=JPG
cd "$dirx"

for (( i=1; i<=60; i++))
do
  mkdir $i

done

So this creates the folders in the directory, now what I am not sure how to do is to grab 200 pictures of the folder (*.jpg) and put them in each of the folders. I guess it will be with one for loop within the for loop I have but I dont know how to get an individual .jpg file

Any help will be appreciated

Thanks

Last edited by Franklin52; 03-02-2010 at 03:27 AM.. Reason: Please use code tags!
# 2  
Old 03-01-2010
Not tested.

Code:
dirx=JPG
cd "$dirx"
ls *.jpg > List
start=1

for (( i=1; i<=60; i++))
do
# mkdir $i
  end=`expr $start +200`
  sed -n "$start,$end"p List|xargs -i cp {} $i
  $start=$end
done

# 3  
Old 03-01-2010
You can use the following script to achieve your requirement.

Code:
cd JPG
files=`ls *.jpg` #getting only the picture files
j=0
k=0
for i in $files #reading file names one by one from the list
do
if [[ $j -eq 0 || $j%200 -eq 0 ]] #creating the directory if the file is first one as well as numbe of files is 200 multiples such as 200,400,600,etc.,
then
mkdir JPG$k #creating the directory JPG0,JPG1,JPG2,etc.,
dir="Dir$k"
cp $i $dir #copying the files to directory
let k+=1
let j+=1
else
cp $i $dir #copying the files to directory
let j+=1 #incrementing the counter to know the number of files
fi
done

# 4  
Old 03-01-2010
Thanks for the reply

Ok the script complained after the
Code:
12 sed -n "$start,$end"p List|xargs -i cp {} $i 
13 $start=$end

sed: -e expression #1, char 3: unexpected `,'

it said ./script.ssh: line 13: 1=: command not found

Btw what exactly does the sed command? and also the |xargs

Thanks

Last edited by Franklin52; 03-02-2010 at 03:28 AM.. Reason: Please use code tags!
# 5  
Old 03-02-2010
SED:
-------
SED stands for stream editor.It is nothing but a editor to perform basic text transformations on an input stream by line by line operations.It works by making only one pass over the inputs, and is consequently more efficient.

XARGS:
----------
xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored.
# 6  
Old 03-02-2010
sed is known as stream editor. This is used to edit several files at the same time. This is one of the powerful utility of linux.
This is used to perform text transformations on several input stream or input files.


xargs is unix's piping related utility. piping means passing the input of one command to another command as its input.
If we want to create the argument list for one command we can use the xargs.
The xargs command will create an argument list for a command from the standard input.

Last edited by thillai_selvan; 03-02-2010 at 12:11 AM..
# 7  
Old 03-02-2010
After you create the directories (please backup the images first)...
Code:
for i in $(ls -d */)
do
list=$(ls *.jpg | head -200)
mv "$list" "$i"
done

for every directory in a list of directories, get a list of 200 jpgs, move them to that directory
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script for enter folders and join file

Hello everyone. I have a dlink cam that save recordings splitted for hours. Each hour folder contain a lot of small avi files. Repository has the following directories structure: \_20140101/ \_ 01 \_a.avi \_b.avi \_ 02 \_c.avi ... (3 Replies)
Discussion started by: setterx
3 Replies

2. Shell Programming and Scripting

how to arrange datas in columns in shell script.

Hello All, I have datas in the below format. Mark 45 Steve 60 Johnson 79 Elizabeth 90 My requirement is to arrange the data in the order as shown in the attachment. My OS is Solaris. ... (6 Replies)
Discussion started by: vashece
6 Replies

3. Shell Programming and Scripting

Shell Script to delete files within a particular time frame under multiple sub folders

Greetings! I'm looking for starting information for a shell script. Here's my scenario: I have multiple folders(100) for example: /www/test/applications/app1/logs /www/test/applications/app2/logs Within these folders there are log files files that need to be deleted after a month. ... (3 Replies)
Discussion started by: whysolucky
3 Replies

4. Shell Programming and Scripting

automating a shell script to run across all the folders

I would like to know how to automate an already made shell script (all the steps of all the commands done on a specific folder) by utilizing a list (a .txt) of all folder names that the shell scripts goes and executes For example: In the working folder called main/ there are subfolders named... (3 Replies)
Discussion started by: Lucky Ali
3 Replies

5. Shell Programming and Scripting

Urgent- shell script to create sub folders

Hi All, Could any one help me with a shell script which will create different sub folders in a folder and of which the sub folders names should be taken from a text file. Thanks (1 Reply)
Discussion started by: chetansingh23
1 Replies

6. Shell Programming and Scripting

How to arrange xml tags in single row using shell script?

I want to put one xml record in one row and so on... sample two records are here. <?xml version="1.0"?> <Object> <Header> <XCOMVers>V1.0</XCOMVers> <REPORT>XXXXX</REPORT> <CODE>002</CODE> </Header> <IssueCard> <Record> <L>CAR SYSTEM -SSSSS -</L> ... (3 Replies)
Discussion started by: sene_geet
3 Replies

7. Shell Programming and Scripting

How to arrange xml tags in single row using shell script?

I want to put one xml record in one row and so on... sample two records are here. <?xml version="1.0"?> <Object> <Header> <XCOMVers>V1.0</XCOMVers> <REPORT>XXXXX</REPORT> <CODE>002</CODE> </Header> <IssueCard> <Record> <L>CAR SYSTEM -SSSSS -</L> ... (1 Reply)
Discussion started by: sene_geet
1 Replies

8. Shell Programming and Scripting

fixperms on folders in shell script

Hi, I am using fixperms command to change permissions of diretories in my script, i have to change 3 directories permissions one by one using fixperms. i tried the below code code: exec DIR1/fixperms -rRy DIR1 exec DIR2/fixperms -rRy DIR2 exec DIR3/fixperms -rRy DIR3 but this is... (4 Replies)
Discussion started by: sreelu
4 Replies

9. Shell Programming and Scripting

Shell script to move files to 3 different folders

Hi guys: I've got this problem, I want to move a bunch of files to 3 different folders, without any specific order, and I'm trying to automatize it with a shell script. I'm a newbie at shell scripting so this is my first try: #!/bin/bash COUNTER=`ls -1 | wc -l` while do ARRAY=(... (11 Replies)
Discussion started by: wretchedmike
11 Replies

10. Shell Programming and Scripting

runnning a shell script in multiple folders

Hey everyone, I'm fairly new to both unix and shell scripts. Right now I have a script that I can run in one folder (if a certain text file is there, do one thing, if it's not, do something else). I want to modify this to run in multiple directories. My setup is: a company directory, and within it... (2 Replies)
Discussion started by: melearlin
2 Replies
Login or Register to Ask a Question