want to move files in a dir into different directories based on the filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting want to move files in a dir into different directories based on the filename
# 1  
Old 03-10-2010
want to move files in a dir into different directories based on the filename

I want to move the files in a dir to different dirs based on their file names.

Ex: i have 4 different files with name -
CTS_NONE_10476031_MRL_PFT20081215a.txt
CTS_NONE_10633009_MRL_PFT20091020a.txt
CTS_NONE_10345673_MRL_PFT20081215a.txt
CTS_NONE_10872456_MRL_PFT20091020a.txt

and the 1st file should go to the dir 'OLD_10476031', 2nd file into dir 'OLD_10633009' , 3rd into dir OLD_10345673 and so on.

tried differnt ways in ksh script but none are working correctly Image

I have a HP-UX machine and using KSH.

Thanks in advance for help.
# 2  
Old 03-10-2010
In Ksh:
Code:
for file in *.txt
do
dirname=`echo $file | cut -d '_' -f3`
mv $file $dirname/
done


cheers,
Devaraj Takhellambam

Last edited by devtakh; 03-10-2010 at 05:59 AM.. Reason: syntax
# 3  
Old 03-10-2010
Quote:
Originally Posted by devtakh
In Ksh:
Code:
for file in *.txt
do
dirname=`echo $file | cut -d '_' -f3`
mv $file $dirname/
done


cheers,
Devaraj Takhellambam

Just added OLD_ to the dir name

Code:
 
for file in *.txt
do
dirname=`echo $file | cut -d '_' -f3`
path1=OLD_${dirname}
mv $file $path1/
done

# 4  
Old 03-10-2010
Small addition Deva.

Code:
for file in *.txt
do
dirname=`echo $file | cut -d '_' -f3`
ndirname="OLD_"$dirname
mv $file $ndirname/
done


Last edited by pludi; 03-10-2010 at 06:34 AM.. Reason: code tags, please...
# 5  
Old 03-10-2010
Looks perfect. thanx guys Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to move all files in a dir into a certain dir

Hello all, I'm very new to shell scripting and need quite urgently to do this thing for my student job. I have a directory called "vectors" with a bunch of files all named ".vector". also i have for each of those files a directory with the name . I now want to move each of those *.vector files... (2 Replies)
Discussion started by: sherresh
2 Replies

2. Shell Programming and Scripting

Move all files from source to destination directory based on the filename

Move all files starting with a specific name to different directory. This shell script program should have three parameters File Name Source Directory Destination Directory User should be able to enter ‘AB_CD*' in file name parameter. In this case all the files starting with AB_CD will... (1 Reply)
Discussion started by: chetancrsp18
1 Replies

3. Shell Programming and Scripting

Need a script to move the files from one dir to other other dir

Need a script to move the files from one dir to other dir and at the same time it has to read the log in the source dir. Please help me ASAP. (4 Replies)
Discussion started by: viswanathkishor
4 Replies

4. Shell Programming and Scripting

Move files based on date in filename

I know this gets covered quite a bit in the forum and I think there is enough there for me to figure out how to do what I am trying to do, I just don't think I would do it very efficiently so I am going to ask the question... I have database log files with date and time stamps in the file like ... (7 Replies)
Discussion started by: slatoms
7 Replies

5. 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

6. UNIX for Dummies Questions & Answers

want to move files in a dir into different directories based on the filename

I want to move the files in a dir to different dirs based on their file names. Ex: i have 4 different files with name - CTS_NONE_10476031_MRL_PFT20081215a.txt CTS_NONE_10633009_MRL_PFT20091020a.txt CTS_NONE_10345673_MRL_PFT20081215a.txt CTS_NONE_10872456_MRL_PFT20091020a.txt and the 1st... (2 Replies)
Discussion started by: Sriranga
2 Replies

7. Shell Programming and Scripting

need to move files of particular day from one dir to another dir

Hi, I have hundered's of files of the name CMP_PORT_IN_P200903271623042437_20090328122430_err.xml in error directory of todays date ie 20090328 and in the file name 5th field specifies date only now i want to move all files of 20090328 to another directory i.e reprocess directory. So... (3 Replies)
Discussion started by: ss_ss
3 Replies

8. Shell Programming and Scripting

grep files without header and move to new dir

Hi, i have number of files in a directory to be processed. the problem is some of the files does not have a header and the process is giving an error of no header found. example of good file : file1 HDR|20080803233401 record 1 record 2 TRA|2 example of a bad file in the same dir ... (6 Replies)
Discussion started by: sitaldip
6 Replies

9. UNIX for Dummies Questions & Answers

how to move files into different folders based on filename

I need to move a bunch of files into folders that have the same name. I wanted to either do this with some filter command or some type of batch file that I could save that would already include all of the mv commands since I will have to do this process often. Whatever method you think is easier. ... (7 Replies)
Discussion started by: italia5
7 Replies
Login or Register to Ask a Question