Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Script to rename and move files

Posted on 2010-11-25
3
Medium Priority
?
618 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 80

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
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 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…
Suggested Courses
Course of the Month12 days, 5 hours left to enroll

916 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