Segregate by suffixed file names using Korn Shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Segregate by suffixed file names using Korn Shell
# 1  
Old 12-16-2013
Segregate by suffixed file names using Korn Shell

I have following files at /dir1

Code:
a.csv.20131201
b.csv.20131201
c.csv.20131201
d.csv.20131201
a.csv.20131202
b.csv.20131202
c.csv.20131202
d.csv.20131202
.......................
.......................
.......................
.......................

I need to move these files to another location say /dir2 with yyyymmdd as subdirectories using shell script.
i.e in /dir2 I will have below subdirectories and
Code:
20131201
20131202
..............
..............

These should have respective a.csv.yyyymmdd,b.csv.yyyymmdd,c.csv.yyyymmdd and d.csv.yyyymmdd.


Could you please help me to achieve this using Korn Shell?

Last edited by radoulov; 12-16-2013 at 10:27 AM..
# 2  
Old 12-16-2013
Try
Code:
for file in /dir1/*
do
   filename=$(basename $file)
   destDir=$(echo ${filename} | awk -F. '{print $3}')
   [ ! -d /dir2/${destDir} ] && mkdir -p /dir2/${destDir}
   cp $file /dir2/${destDir}/.
done

# 3  
Old 12-16-2013
Using shell builtins:
Code:
#!/bin/ksh

for file in dir1/*
do
        mkdir -p "dir2/${file##*.}"
        mv "$file" "dir2/${file##*.}/"
done

This User Gave Thanks to Yoda For This Post:
# 4  
Old 12-17-2013
Thanks.
After moving all the files from the dir1 to dir2 if I run the script again it fails.
# 5  
Old 12-17-2013
Quote:
Originally Posted by JaisonJ
After moving all the files from the dir1 to dir2 if I run the script again it fails.
Maybe that is because the dir1 is empty and there is no more files to process.

Can you post what the error is.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to segregate a section from big file?

Hello, I need to know all IP range (ip_prefix), associated with us-west-2 region only from this link - https://ip-ranges.amazonaws.com/ip-ranges.json (it can be opened in wordpad for better visibility) Please suggest, how would I do it. If vi, awk or sed is needed, I have downloaded it on my... (7 Replies)
Discussion started by: solaris_1977
7 Replies

2. UNIX for Advanced & Expert Users

Segregate file content using sed backreference

I have some text like EU1BTDAT:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD' EU1BTDATEST:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD' EU1CLOSEDATES:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD' EU1DATED:ASSGNDD... (8 Replies)
Discussion started by: gotamp
8 Replies

3. Shell Programming and Scripting

Korn Shell help - Using parameter to create variable names

I'm using korn shell and I am wondering if it's possible to use a parameter passed into a function to build a variable name in a configuration file. I have the function in one source file, I'd like to have a global configuration file instead of hardcoding logins to each script. So I have a... (7 Replies)
Discussion started by: mrevello
7 Replies

4. Shell Programming and Scripting

segregate the file based on matching patterns

print 'test' SETUSER 'dbo' go create proc abc as /Some code here/ go SETUSER go print 'test1' SETUSER 'dbo' go Create Procedure xyz as /some code here/ go SETUSER go print 'test2' SETUSER 'dbo' (2 Replies)
Discussion started by: mad_man12
2 Replies

5. AIX

how to check the existence of a file using korn shell?

we have tranferred an ear from local server to remote server using ftp.consider, we have an ear file named a.ear in remote server,again if we transfer the same file named a.ear from local server to remote server.we need the kshell to check the existence of the ear file in remote server,and if the... (3 Replies)
Discussion started by: karthikprasathk
3 Replies

6. UNIX for Dummies Questions & Answers

korn shell script to keep history of same file

Hello, How do I write a korn shell that will rename file with the current date. What I want to do is, I have a file that is re-written every day. But instead, I want to keep a 14 day history of the file. So I want to write a script that will rename the current file with a date attached to the... (2 Replies)
Discussion started by: watson2000
2 Replies

7. Shell Programming and Scripting

Reading file names from a file and executing the relative file from shell script

Hi How can i dynamically read files names from a list file and execute them from a single shell script. Please help its urgent Thanks in Advance (4 Replies)
Discussion started by: anushilrai
4 Replies

8. Shell Programming and Scripting

Changing a CSV file in Korn shell

Hi All, Please can someone help me how I can read a CSV file with 10 columns and remove the 2nd, 4th, 6th, 8th column and build the new output file. Script to be done in Korn shell. File contains data like bnt, lb2, lb3, vnw, lb4, lb5, bst, lb6, lb7, vgw (multiple rows) Output file should... (2 Replies)
Discussion started by: riteshm
2 Replies

9. Shell Programming and Scripting

Read file - korn shell

Hello!! I'm want to read each line of a file. I'm doing a cut command, using \n - linefeed as delimeter and "list" as my file. cut -f1 -d"\n" -s < list Changing the file, putting spaces and doing cut -f1 -d" " -s < list it works, but with linefeed nothing is returned. Thanks... (5 Replies)
Discussion started by: alienET
5 Replies

10. UNIX for Dummies Questions & Answers

file activity (open/closed) file descriptor info using KORN shell scripting

I am trying to find a way to check the current status of a file. Such as some cron job processes are dependent on the completion of others. if a file is currently being accessed / modified or simply open state I will wait until it is done being processed before attempting the next process on that... (3 Replies)
Discussion started by: Gary Dunn
3 Replies
Login or Register to Ask a Question