Solved

Script to rename and move files

Posted on 2010-11-25
3
592 Views
Last Modified: 2013-12-27
Hi,

A directory(/arp/incoming/) in my red Hat box has lots of files.But i'm interested in only files begining with 'ARP_'.I need a shell script which will be monitoring this directory for every one hour and then moving the files accumalted with this name to /arp/STGAREA/ .While moving  each file need to be renamed to ARP_<filename> to ARP_<filename>.tx
Appreciate if each line has a comment for understanding

Thanks  a lot
0
Comment
Question by:prashubk
3 Comments
 
LVL 76

Accepted Solution

by:
arnold earned 300 total points
ID: 34215419
the location of bash on Solaris is often in /usr/bin/bash. On Linux system it is in /bin/bash so adjust the script to match the location where bash is on your system.
 
#!/usr/bin/bash

while true; do #infinite loop since the test is always true
cd /arp/incoming; #change to the underlying directory where the files are
ls | grep '^ARP_' | while read a; do #list the current contents of cwd filtering to only have the ones of interest  (ARP_) and pass it to a loop $a will have a file at a time 
inuse=`/sbin/fuser "$a" 2>/dev/null` #setting up  test to make sure that the file is not still in use.  The variable will be assigned the PIDs of the processes that have the file open.

if ( [ ! -z "$inuse" ] ) ; then #check/test whether $inuse was set to a PID 
#man test to get the information on the evaluation in []
#-z is true if the string is empty
#! negation of the condition
continue; #skip this item since it is currently being used
fi  #close if
/bin/mv "$a" "/arp/STGAREA/$a.tx"  #move the file while renaming it.

done; #close listing files loop


sleep 3450 #pause for almost an hour 3600 would be an hour

done #close the infinite loop

Open in new window

0
 
LVL 1

Assisted Solution

by:cheezebail
cheezebail earned 200 total points
ID: 34215651
setup a cron job or execute every hour the following line:

find -P /arp/incoming -regex ".*/ARP_.*" | xargs -I STR sh -c 'mv STR /arp/STGAREA/`basename STR`.tx'

use of find | xargs is more scalable than ls | grep, to my understanding
I ignored the inuse requirement, though.
0
 

Author Closing Comment

by:prashubk
ID: 34216028
I tried arnold solution it did work well.Cheezebail slution is also impressive.Will try that out.Thanks guys
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now