Writing File Moving Script

Question 1:

How to make a script that moves all files with a keyword on it to another folder and lets me know how many files it moved with that keyword?

Answer 1:

Code:

#/bin/ksh  -x
file_name=`ls *key_word*`
for i in ${file_name}
do
cp ${i} /home/target_dircetory
count=`expr ${count} + 1 `
done
echo "number of file transferd = ${count} "

Note:
Replace key_word with the word you want to search and replace /home/target_dircetory with your direcoty address

or

Code:

num=0
for files in *keyword*
do
  cp $files /destination && ((num++))
done
echo "how many files : $num"

or

find / -name "*keyword*" -print -exec mv {} /target/dir \;
 

Question 2:

Shell script for moving all the file from the same folder

How to write a shell script which basically moves all the files from one folder say folder x to folder y and once they are moved to folder y a datetimestamp should be attached to there name

for example: 
file a should be moved to y folder and renamed as a_20991015

Answer 2:

Code:

dir_x=/path/to/directory_x
dir_y=/path/to/directory_y
datestamp=$(date +%Y%m%d)
cd "$dir_x" || exit 1
for file in *
do
mv "$file" "$dir_y/${file}_$datestamp"
done

Unix Tips

Have a Unix Problem
Unix Forum - Do you have a UNIX Question?

Unix Books :-
UNIX Programming, Certification, System Administration, Performance Tuning Reference Books

Return to : - Unix System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.