Solved

Script to rename and move files

Posted on 2010-11-25
3
612 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 79

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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
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 …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.
Suggested Courses

623 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