Tuesday, February 1, 2011

BASH : Move Files

touch test{1,2}
mv test{1,2} newfile{1,2} -> It fails!
mv test1 test2 newfile1 newfile2 -> It fails!
touch movemanyfiles.sh
CONTENT OF FILE
#!/bin/bash
#Author
#Date
#Purpose
for file in `ls -A test*` # returns the 2 new files created
do
echo $file
done
#END
Return the 2 files
CONTENT OF FILE
#!/bin/bash
for file in `ls -A test*` # returns the 2 new files created
do
# echo $file
mv $file $file.old
done
#END
The 2 files are moves in file1.old and file2.old
CONTENT OF FILE
#!/bin/bash
BADARG=165
REQUIREDPARAM=2
if [ $# != $REQUIREDPARAM]
then
echo At least $REQUIREDPARAM parameter is required!
exit $BADARG
fi
for file in `ls -A $1*` #'*' gobal paraemter
do
mv $file $file.$2
done
#END
chmod u+x movemanyfiles.sh
./movemanyfiles.sh test new -> the first param is the patern to match and the second param is the extension of the new file
Result : test1 is transformed to test1.new ; test2 is transformed to test2.new

No comments:

Post a Comment