create t a filelist with the latest file by YYYYMMDD and move to subfolder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting create t a filelist with the latest file by YYYYMMDD and move to subfolder
# 1  
Old 08-28-2012
create t a filelist with the latest file by YYYYMMDD and move to subfolder

Hi all,

I am receiving files named like:
ABC_20120730.csv
ABC_20120801.csv
ABC_20120812.csv

They are residing in a folder named Incoming.
I am supposed to write a script that will create a filelist which will contain only the name of the latest file beginning with ABC_, by YYYYMMDD sufix.

Then I am supposed to move the file from the filelist into a subfolder Incoming/Processing.

Please help.
Thanks!
# 2  
Old 08-28-2012
Code:
echo "mv $(ls -ltr /Incoming/ABC_*  | tail -1 | awk '{print $NF}') /Incoming/Processing"

Remove echo if the o/p is as expected
# 3  
Old 08-28-2012
Your directions a not entirely clear, but I believe the following performs the actions you requested and verifies that each action was performed successfully.
Code:
#!/bin/ksh
# The following assumes that this script will be run in the parent directory of the
# Incoming directory.  If this script will be run while sitting in the Incoming directory,
# remove the following cd and error checking.  Otherwise, cd to an absolute pathname
# for the Incoming directory.

if ! cd Incoming
then    echo "$0": processing aborted >&2
        exit 1
fi
ls -r ABC_[0-9][0-9][0-9][0-9][01][0-9][0-3][0-9].csv|(
        if read oldest
        then    if mv $oldest Processing/
                then    echo $oldest moved to Incoming/Processing.
                        exit 0
                else    echo $oldest not moved. >&2
                        exit 2
                fi
        else    echo $0: No matching Incoming file found. >&2
                exit 3
        fi
)

Although this script uses ksh, it should also work with at least sh and bash.

Last edited by Don Cragun; 08-29-2012 at 02:14 AM..
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 08-29-2012
Thanks for your quick replies!

pravin27, unfortunatelly haven't been able to execute the code successfully. Probably due to my lack of knowlege and understanding.
I am a rookie, I'm afraid Smilie

Don Kragun, that worked perfectly, problem solved.
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 create symlink for latest file only?

Hello, On Solaris 10, here are entries for logs in httpd.conf ErrorLog "|/export/apache/apache-2.2.17/bin/rotatelogs -l -f /var/log/apache/error_log.%Y%m%d 86400" It keeps creating daily logs with below names - -rw-r--r-- 1 root root 1016747232 Apr 5 23:59... (16 Replies)
Discussion started by: solaris_1977
16 Replies

2. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies

3. Shell Programming and Scripting

Script to move latest zip file to another directory

Hi folks, In my application there is a job running which create a .dat file along with it zip file also at unix box location /opt/app/cvf/temp1 so in temp1 directory I have one .dat file and its zip file also. Now since this job runs every day so if a job runs today there will be two files... (5 Replies)
Discussion started by: punpun66
5 Replies

4. Shell Programming and Scripting

Shell script to get the latest file from the file list and move

Hi, Anybody help me to write a Shell Script Get the latest file from the file list based on created and then move to the target directory. Tried with the following script: got error. A=$(ls -1dt $(find "cveit/local_ftp/reflash-parts" -type f -daystart -mtime -$dateoffset) | head... (2 Replies)
Discussion started by: saravan_an
2 Replies

5. UNIX for Dummies Questions & Answers

Finding latest dir based on it's name (yyyymmdd)

Hi Folks, As part of my application I need to find out what the latest directory based on the name of that directory (not it's file system timestamp). Example: I have a directory which contains below directories (each of while contains files etc) 20120000/ 20120000/ latest (symbolic link to... (5 Replies)
Discussion started by: DOWD_R
5 Replies

6. Shell Programming and Scripting

move files defined in filelist

Hi, I have a filelist named filestomove.txt. It can contain any number of files that need to be moved to a subdirectory. All the files from filelist are in the same directory as the filelist, let say it's called Folder1. All the files need to be moved to subfolder Folder1/Subfolder1. ... (1 Reply)
Discussion started by: kedrick
1 Replies

7. Shell Programming and Scripting

Create subfolder within folder in one command

suppose if i am checking folder g as shown below path a/b/c/d/e/f/g ,and some directory c,d,e,f,g not present then is there anyway to create directory in one command or i need to use mkdir for everyfolder (3 Replies)
Discussion started by: lalitpct
3 Replies

8. Shell Programming and Scripting

Recursively move a subfolder to one upper level

Hi, all: My folder structure is like: What I'm expecting to realize is to have my folders look like: How to realize it? Cheers Pei (0 Replies)
Discussion started by: jiapei100
0 Replies

9. Shell Programming and Scripting

Link multiple files from different subfolder to a new subfolder

Hi, I have the following subfolder with files: /data/a/1/xxx.txt /data/b/2/yyy.txt /data/c/3/zzz.txt And i have a set of new folders which have exactly the same structure as above but different disk without the files: /data_02/a/1/ /data_02/b/2/ /data_02/c/3/ Now i would like to... (6 Replies)
Discussion started by: total_ysf
6 Replies

10. Shell Programming and Scripting

Move the latest or older File from one directory to another Directory

I Need help for one requirement, I want to move the latest/Older file in the folder to another file. File have the datetimestamp in postfix. Example: Source Directory : \a destination Directory : \a\b File1 : xy_MMDDYYYYHHMM.txt (xy_032120101456.txt) File2: xy_MMDDYYYYHHMM.txt... (1 Reply)
Discussion started by: pp_ayyanar
1 Replies
Login or Register to Ask a Question