Purple_haze
Purple_haze

Reputation: 68

Shell Script to redirect to different directory and create a list file

             src_dir="/export/home/destination"
             list_file="client_list_file.txt"
             file=".csv"

             echo "src directory="$src_dir
             echo "list_file="$list_file
             echo "file="$file

             cd /export/home/destination

             touch $list_file

             x=`ls *$file | sort >$list_file`



            if [ -s $list_file ]
             then
            echo "List File is available, archiving now"
                    y=`tar -cvf mystuff.tar $list_file`
             else
            echo "List File is not available"
             fi

The above script is working fine and it's supposed to create a list file of all .csv files and tar's it.

However I am trying to do it from a different directory while running the script, so it should go to the destination directory and makes a list file with all the .csv in destination directory and make a .tar from the list file(i.e archive the list file)

So i am not sure what to change

Upvotes: 0

Views: 706

Answers (1)

Jason Hu
Jason Hu

Reputation: 6333

there are a lot of tricks in filename handling. the one thing you should know is file naming under POSIX sucks. commands like ls or find may not return the expected result(but 99% of the time they will). so here is what you have to do to get the list of files truely:

for file in $src_dir/*.csv; do
    echo `basename $file` >> $src_dir/$list_file
done
tar cvf $src_dir/mystuff.tar $src_dir/$list_file

maybe you should learn bash in a serious manner and try to google first before you asking question in SO next time.

http://www.gnu.org/software/bash/manual/html_node/index.html#SEC_Contents

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html

Upvotes: 1

Related Questions