?
Solved

Unix script

Posted on 2011-03-03
2
Medium Priority
?
345 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 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

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

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month12 days, 2 hours left to enroll

752 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