File Move Based on 1st Character of File Name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File Move Based on 1st Character of File Name
# 8  
Old 01-27-2009
Thanks quirkasaurus!

It appears the original script worked the 1st time, but when I ran it again it created another subdirectory below the previous one. I tried running several times before I looked at the output and now I have a huge mess ...

/images/main/a/a/a/a/a/A101.jpg
/images/main/b/b/b/b/B201.jpg

Depending on where I killed the program there are a varying number of sub-directories.

How can I get back to everything in the /images/main directory and start all over again with an alternative suggestion?

Thanks for your patience on this thread and your help is greatly appreciated.
# 9  
Old 01-27-2009
OY!! what a mess!!

First, put everything back to normal:

mkdir /tmp/my_images

find images/main -name \*.jpg -exec /bin/cp {} /tmp/my_images \; -print

Verify that all the images are there:

ls /tmp/my_images

Nuke everything in images/main:

cd images/main

\rm -rf *

Put all the images back:

cp /tmp/my_images/* .

Now.

Don't use the solutions that used the find command.
Instead, use mine, posted earlier. This one will avoid
any recursive directory building problem.

HTH
# 10  
Old 01-27-2009
HTH - Thank you! OK - Recovered!

I am now using your script. How do I modify to handle "Arguement List Too Long" error because I have 30,000 images in the directory?
# 11  
Old 01-27-2009
Quote:
Originally Posted by srdconsulting
HTH - Thank you! OK - Recovered!

I am now using your script. How do I modify to handle "Arguement List Too Long" error because I have 30,000 images in the directory?

typeset -u -L1 first_letter

/bin/ls |
grep '.jpg$' |
while read file ; do

first_letter=$file

mkdir $first_letter
/bin/mv $file $first_letter

done

Last edited by quirkasaurus; 01-27-2009 at 02:07 PM.. Reason: missed the typeset.
# 12  
Old 01-27-2009
With much help from quirkasaurus this problem was resolved using BASH script below:

Code:
#!/bin/sh

cd "$1"

/bin/ls |
grep '.jpg$' |
while read file ; do
  first_letter=`echo $file | cut -c 1 | tr '[A-Z]' '[a-z]'`
  mkdir -p $first_letter
  /bin/mv $file $first_letter
done


Last edited by otheus; 01-28-2009 at 05:15 AM.. Reason: formatting, [code] tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Need to combine two lines in a file based on first character of each line in a file

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: Code: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,... (1 Reply)
Discussion started by: jayaP
1 Replies

2. UNIX for Dummies Questions & Answers

Need to combine two lines in a file based on first character of each line in a file

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: Code: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,... (1 Reply)
Discussion started by: jayaP
1 Replies

3. Shell Programming and Scripting

File Move based on date

how to move file based on date in linux (1 Reply)
Discussion started by: syedzoniac
1 Replies

4. Shell Programming and Scripting

How to replace character in a file based on another file.?

Hi Gurus, I have really hard job need you guys to help. i have two files one is data file, one is instruction file like below 0000000010000233154825032720204abc BC15 0000000010000233154825032720204defg DB15 1 9 o 10 6 r 16 5 o 21 10 r 31 the requirement is based... (5 Replies)
Discussion started by: ken6503
5 Replies

5. Shell Programming and Scripting

File character adjustment based on specific character

i have a reqirement to adjust the data in a file based on a perticular character the sample data is as below 483PDEAN CORRIGAN 52304037528955WAGES 50000 89BP ABCD MASTER352 5434604223735428 4200 58BP SOUTHERN WA848 ... (1 Reply)
Discussion started by: pema.yozer
1 Replies

6. Shell Programming and Scripting

Move input file based on condition

Hello, I have File1 in a directory A, a File2 in a directory B. If the File2 is not empty Then I have to move File1 from directory A to a directory archive Else no action. Is it possible to do this from one command line? Thank you in advance for your answers. Madi (2 Replies)
Discussion started by: AngelMady
2 Replies

7. Fedora

Move file based time stamp

Hi all, I've already tired to try to solved this problem. Also search in Internet didn't find anything solution I have a directory like this : # pwd /opt/projects/juventini # ls -al | more total 3627460 drwxr-xr-x 2 app apps 12472320 Sep 24 14:59 . drwxr-xr-x 11 app apps 4096 Jun... (8 Replies)
Discussion started by: sunardo
8 Replies

8. Shell Programming and Scripting

Move file based on filename

Hi All I need a script to manipulate files based on a filename: example filename: 66600_042706.pdf the script will create a directory 66000 only if this directory is not existing. If that directory is existing it will just move the file to 66000/666000_042706.pdf in addition, i want to... (4 Replies)
Discussion started by: aemestech
4 Replies

9. Shell Programming and Scripting

Need to move a file based on size

Hi All, I have some files in a directory. If the file size is 0 i want to move the file to new directory using unix script. Can anyone help me in this regard Thanks Vinny (4 Replies)
Discussion started by: vinny81
4 Replies

10. UNIX for Dummies Questions & Answers

How to copy/move to a file with a special character as the 1st char in the filename?

I am trying to create files with special characters in its filenames for testing purposes. This is on a Linux RHEL4 but this should also be applicable on a Unix shell. I am able to create files with special characters in the filenames...e.g. cp -pv foo.gif \*special.gif cp -pv foo.gif \... (6 Replies)
Discussion started by: sqa777
6 Replies
Login or Register to Ask a Question