Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6969895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:38:24+00:00 2026-05-27T16:38:24+00:00

I saw a question on stackflow about parsing arguments. I tried to write this,

  • 0

I saw a question on stackflow about parsing arguments. I tried to write this, but it’s not working and now it’s getting on my nerves.

The usual way of running a script on the terminal is ./scriptname, but I later introduced the argument -d. So, if I put ./scriptname it will not run. 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 won’t run. But, if I put ./scriptname -d path I want to replace $To in the existing script with the command argument after -d.

#!/bin/bash
From="/home/mark/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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:38:25+00:00Added an answer on May 27, 2026 at 4:38 pm

    Your desired usage isn’t completely clear, but it seems to be:

    scriptname -d path
    

    So, you can do it the extensible way, or the brute force way. Since you’re changing directories willy-nilly, you also need to ensure that the paths are absolute, not relative.

    Brute force

    #!/bin/bash
    From="/home/mark/doc"
    if [ $# = 2 ] && [ "$1" = '-d' ] && [ -d $2 ]
    then
        case "$2" in
        (/*) cd "$From" &&
             for extn in pdf txt doc
             do find . -type f -name "*.$extn" -exec mv {} "$To" \;
             done;;
        (*)  echo "$0: path name must be absolute ($2 is not)" 1>&2; exit 1;;
        esac
    else
        echo "Usage: $0 -d /absolute/dirname" 1>&2; exit 1
    fi
    

    Extensible

    #!/bin/bash
    From="/home/mark/doc"
    To=""
    
    usage()
    {
        echo "Usage: $(basename $0 .sh) -d /absolute/dirname" 1>&2
        exit 1
    }
    
    while getopts d: opt
    do
        case "$opt" in
        (d)  if [ ! -d "$OPTARG" ]
             then echo "$0: $OPTARG is not a directory" 1>&2; exit 1
             else
                 case "$OPTARG" in
                 (/*) To="$OPTARG";;
                 (*)  echo "$0: path name must be absolute ($2 is not)" 1>&2; exit 1;;
                 esac
             fi;;
        (*)  usage;;
        esac
    done
    shift $(($OPTIND - 1))
    
    if [ $# != 0 ] || [ -z "$To" ]
    then usage
    fi
    
    cd "$From" &&
    for extn in pdf txt doc
    do find . -type f -name "*.$extn" -exec mv {} "$To" \;
    done
    

    For example, it will be very easy to add a -f from option to deal with changing the source of the files.

    Note that you could also use:

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

    This would allow you to permit relative names for the ‘from’ and ‘to’ directories because it does not change directory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I saw this question , but the answers there are not very relevant. A
I saw this question already on this forum but I do not know why
I saw this question asked about C# I would like an answer for PHP.
I saw this question asking about whether globals are bad . As I thought
I saw this question in a forum about how an application can be developed
This might be a stupid question, but I just saw a question asking how
I saw this question , which focuses on the Brittney Spears problem. But I
I saw this question Simple example of threading in C++ but my problem is
I already saw this question however, it didn't work for me whatever I tried.
I saw this question, but I didn't find my answer in it.. So, why

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.