Solved

Script to rename and move files

Posted on 2010-11-25
3
606 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 78

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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 …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

751 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