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
# 1  
Old 01-27-2009
File Move Based on 1st Character of File Name

I need some help with a unix script to mv image files to subdirectories based on the 1st character of the filename. See example below...

/images/main

1191.jpg
9999.jpg
A101.jpg
A102.jpg
B201.jpg
c333.jpg
...
Z999.jpg

I would like to move to the following:

/images/main/1/1191.jpg
/images/main/9/9999.jpg
/images/main/a/A101.jpg
/images/main/a/A102.jpg
/images/main/b/B201.jpg
/images/main/c/c333.jpg
...
/images/main/z/Z999.jpg

Any help would be greatly appreciated.

Thanks!
# 2  
Old 01-27-2009
Another one:

Code:
#!/bin/sh

dir="/images/main"

for file in *.jpg
do
  sd="$dir"/`echo "$file"|cut -c 1`
  mkdir -p "$sd"
  mv "$file" "$sd"
done

Regards
# 3  
Old 01-27-2009
franklin52 script is very good but needs a tweak to change the directory to lowercase.
tr "[A-Z]" "a-z"

BTW. Don't use this syntax because it is upset by single character filenames!
tr [A-Z] [a-z]

If you have a very large number of files (or the filenames contain space characters) the script will need some work to generate the list of files in a pipe without decending the directory tree. Depends on which unix and which version of "find" you have.

Last edited by methyl; 01-27-2009 at 01:53 PM.. Reason: tr wrong
# 4  
Old 01-27-2009
Worked great. Thanks for your help!
# 5  
Old 01-27-2009
I know he already has his answer . . . but I see a lot of answers on here
that are overly complicated. The ksh itself has much of the functionality
of cut, awk and sed. For example:


# This creates a variable that is only 1 character-wide, left-justified,upper-case
typeset -u -L1 first_letter

cd images/main

/bin/ls *.jpg |
while read file ; do

first_letter=$file

mkdir $first_letter
/bin/mv $file $first_letter

done

This script has the added feature of being a single process.
Therefore, if it mattered, which it probably doesn't nowadays,
it would be the fastest way performance-wise to do this.
# 6  
Old 01-27-2009
Worked great the 1st time, but now when I try add more files to the directory and re-run it just hangs. Is there an issue because the directory ($dir) has already been created? If so, how do I modify to check first to see if $dir exists?
# 7  
Old 01-27-2009
You can run it like so:

ksh -xvf script_file 2>&1 | more

And see exactly where it's getting hung up.

Other answer to your question:

if [ ! -d $dir ]; then
mkdir $dir
fi
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