Solved

Unix script

Posted on 2011-03-03
2
344 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
[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
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

696 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