this script moves all the doc files to a specified directory....i have managed to put an argument but the problem im facing is puting the full path where the scripts are moving to for example i want to run the script like this below
./loo -d then path where im moving the files (i.e ./loo -d the second argument where files are moving to)
this is my code
#!/bin/bash
From="/home/elg19/lone/doc"
To="/home/elg19/documents"
if [ $1 = -d ]; then
cd "$From"
for i in pdf txt doc; do
find . -type f -name "*.${i}" -exec mv "{}" "$To" \;
done
fi
You may try something like this:
#!/bin/bash
From=/home/elg19/lone/doc
To=/home/elg19/documents
while getopts :d: opt; do
case $opt in
( d ) To=$OPTARG ;;
( ? ) printf >&2 'usage: %s -d <dest_dir>\n' "${0##*/}"; exit 1 ;;
esac
done
shopt -s nullglob
mv -- "$From"/*.{pdf,txt,doc} "$To/"
Note that there is no recursion in this case,
let me know if you really need to move the files
from the From directory an its subdirectories.
If the filenames exceed the ARG_MAX limit of your system, you will need xargs.
i want to replace $To in the existing script with the command argument after -d ...
this code is not working i dont know why....
#!/bin/bash
From="/home/elg19/lone/doc"
To=$2
if [ $1 = -d ]; then
cd "$From"
for i in pdf txt doc; do
find . -type f -name "*.${i}" -exec mv "{}" "$To" \;
done
fi