unix - Shell Script to copy all files in a directory to a specified folder -
i'm new in shell script , trying figure out way write script copies files in current directory directory specified .txt file , if there matching names, adds current date in form of filename_yyyymmddmmss name of file being copied prevent overwritting.
can me out?
i saw thinking along lines of
#!/bin/bash source=$pwd #i dont know wheter makes sense want #say source directory 1 in right destination=$1 #as said want read destination off of .txt file in $source #i pseudo coded part because didn't figure out. if(file name exists) copy changing name else copy fi done
the problem have no idea how check whether name exist , copy , rename @ same time.
thanks
how this? supposing target directory in file new_dir.txt.
#!/bin/bash new_dir=$(cat new_dir.txt) now=$(date +"%y%m%d%m%s") if [ ! -d $new_dir ]; echo "$new_dir doesn't exist" >&2 exit 1 fi ls | while read ls_entry if [ ! -f $ls_entry ]; continue fi if [ -f $new_dir/$ls_entry ]; cp $ls_entry $new_dir/$ls_entry\_$now else cp $ls_entry $new_dir/$ls_entry fi done
Comments
Post a Comment