LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Shell Script Problem (https://www.linuxquestions.org/questions/linux-general-1/shell-script-problem-550055/)

Anirban.Adhikary 04-30-2007 01:29 AM

Shell Script Problem
 
Hi This is Anirban.This is my first posting in this site.
I am trying to write a simple shell script that will search for a specific files ( e.g. .txt) from one directory;move the files to another directory and mention the time when the job is done.I am facing some problems. when i am searching for a specific file pattern by writing
#! /bin/bash
cd /tmp
ls -l | grep txt* > /misc/backup
date >> /misc/backup
This script is executing but sometimes i can only see the date by issueing cat /misc/backup and sometimes it shows there are txt files ( e.g. a.txt, b.txt) in /misc/backup but i am not able to see the files individually.When I am adding the following line in this script [ mkdir /misc/backup] files are not redirected to the directory.An error messege is showing /misc/backup is a directory.Before executing the script i am doing
chmod 744 backup.sh and i am executing the script by
./backup.sh.

Thanks and regards in advance
Anirban Adhikary.

mechdave 04-30-2007 02:47 AM

If you want to move the files:
Code:

#!/bin/sh
$SRC_DIR='/path/to/search/dir'
$DEST_DIR='/path/to/dest/dir'
$LOG_FILE='/path/to/logfile.txt'       
if [ -f $SRC_DIR/*.txt]
    then
        mv $SRC_DIR $DEST_DIR &> $LOG_FILE         
        echo "Script Success" >> $LOG_FILE
else
    echo "No files present" > $LOG_FILE
fi

date >> $LOG_FILE
#debug output to stdout
cat $LOG_FILE

If there are no files in the directory "no files present " is printed to the log file and so is the date. The cat command is just to output your log file to std out for debugging the script.

[edit]Thinking about it, the * wildcard may cause problems, if so just remove it. The script should work anyway. BTW I hope this isn't a homework question :) [/edit]


All times are GMT -5. The time now is 11:59 PM.