?
Solved

Unix script

Posted on 2011-03-03
2
Medium Priority
?
349 Views
Last Modified: 2012-05-11
I need to write a Unix Script (Chron Job) which does the following:

1.  read files in a directory
2.  if the file contains an & rename the file replacing the & with and
3.  move the file to another directory
4.  If the file does not contain an & then just move the file to anotehr directory

Thankx
0
Comment
Question by:CipherIS
2 Comments
 
LVL 10

Accepted Solution

by:
APNFSSC earned 1000 total points
ID: 35033800
Try this

You need to alter the values of in_dir and out_dir and also I've put a # infront of the line that actually moves the file so you can take that out after you have done your testing. In its current state it will just print what it would do to the screen.

 
#!/bin/sh
#set -x
in_dir=/tmp/test1
out_dir=/tmp/test2
cd $in_dir

for i in `ls`
do
out_filename=`echo $i | sed 's/&/and/g'`
echo mv "$in_dir/$i" "$out_dir/$out_filename"
# mv "$in_dir/$i" "$out_dir/$out_filename"
done

Open in new window

0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 1000 total points
ID: 35034072
This version will correctly move the files into the two separate directories.

#!/bin/bash

DIR1=/path/to/dir1
DIR2=/path/to/dir2

cd /path/to/files

find . -maxdepth 1 -type f | while read file
do
  if echo $file | grep -q '&'
  then
     mv $file $DIR1/${file/&/and}
  else
     mv $file $DIR2
  fi
done

Open in new window

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
I have written articles previously comparing SARDU and YUMI.  I also included a couple of lines about Easy2boot (easy2boot.com).  I have now been using, and enjoying easy2boot as my sole multiboot utility for some years and realize that it deserves …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month16 days, 21 hours left to enroll

862 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