Solved

Unix script

Posted on 2011-03-03
2
338 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 250 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 250 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now