3

This script moves all the doc files to a specified directory. I have managed to put an argument but the problem I am facing is putting 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
0

3 Answers 3

3

You need getopts. getopts is a library designed to handle commandline arguments for you, and is generally available in many languages. In bash, you use it like this:

This is borrowed from a tutorial I found:

From="/home/elg19/lone/doc"
To="/home/elg19/documents"

while getopts "d:" optionName; do
    case "$optionName" in
        d) To="$OPTARG";;
        [?]) exit 255;;
    esac
done

[[ -d "$To" ]] || exit 255

cd "$From"
for i in pdf txt doc; do
    find . -type f -name "*.${i}" -exec mv "{}" "$To" \;
done

This is untested, but basically it's just your script with the option to override the "$To" argument with getopts.

The [?] bit tells it to exit if any unrecognised options are found.

12
  • im a newbie to bash is there any way you can modify my script using the getopts so i that i understand it better?
    – kickev
    Dec 18, 2011 at 11:19
  • its giving me errors besides its try to move all files on the pc
    – kickev
    Dec 18, 2011 at 11:56
  • Post your bash script for debugging, preferably on pastebin.
    – Panther
    Dec 18, 2011 at 18:22
  • @bodhi.zazen i found out $2 can work but how to do it,may you help me insert it in my code?
    – kickev
    Dec 18, 2011 at 18:36
  • I can not tell what you are wanting to pass to your script.
    – Panther
    Dec 18, 2011 at 18:45
2

FWIW, rsync will do this for you

rsync --filter="+ *.doc" --filter="+ *.pdf" --filter="+ *.txt" --filter="- *" ~lone/doc/* ~/documents/

You can make an alias in .bashrc if you wish

alias backup='rsync --filter="+ *.doc" --filter="+ *.pdf" --filter="+ *.txt" --filter="- *" ~lone/doc/* ~/documents/'

If you need more then *.doc, add to the --filer="+ *.txt" ;)

See my comment if you want to debug your script. Your problem in your script may be as simple as your conditional if [ $1 = -d ]

try

if [ -d "$1" ]; then

But I can not tell from your script what options you are passing and why (you defined your directories in your script).

3
  • i want to run it on the terminal like this ./loo -d path i read somewhere that if i use $2 it will allow me run the script like the way i want it
    – kickev
    Dec 18, 2011 at 18:52
  • Caesium's script is working as advertized here. When you use -d, use the full path, such as ~/new_directory . Again, post your script if it is not working.
    – Panther
    Dec 18, 2011 at 19:13
  • the usual way of running a script on the terminal is ./scriptname right but i later introduced the argument -d so if i put ./scriptname it will not run but if i put ./scriptname -d it will now i want to put another argument for the path(where the files are moving in this case "/home/elg19/documents") such that when i do not include the path it wont run but if i put ./scriptname -d path
    – kickev
    Dec 18, 2011 at 19:24
1

With your most recent comment

#!/bin/bash

From="/home/elg19/lone/doc"
#To="/home/elg19/documents"

while getopts "d:" optionName; do
    case "$optionName" in
        d) To="$OPTARG";;
        [?]) exit 255;;
    esac
done

[[ -d "$To" ]] || exit 255

for i in pdf txt doc; do
    find $From -type f -name "*.${i}" -exec mv "{}" "$To" \;
done
2
  • the script is running but no files are being moved
    – kickev
    Dec 18, 2011 at 19:52
  • The script uses mv so if you have been running / testing is is likely they have already been moved. The above script works here as advertized and if you need assistance you will need to pastebin your script, while you are at it pastebin ls $source $destination before and after you run the script. Keep in mind, the script exits if the $destination does not exist, and it takes the full path as an argument.
    – Panther
    Dec 18, 2011 at 19:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .