Solved

Unix script

Posted on 2011-03-03
2
337 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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

771 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

10 Experts available now in Live!

Get 1:1 Help Now