Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Script to rename and move files

Posted on 2010-11-25
3
Medium Priority
?
616 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 1200 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 800 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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.:
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

722 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