Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Simple bash script: move by file extension.

  1. #1
    Join Date
    Jul 2011
    Beans
    34

    Simple bash script: move by file extension.

    I have just recently started with Bash scripting, and i am trying to move files according to their file extension. Directly at the bash prompt i can do like this:

    Code:
    mv *+(.jpg|.jpeg|.gif|.png) ./pictures
    but if i try the same in my script i get a syntax error, this is what the script look like:

    Code:
    mv *+(.mp3) ./music
    
    mv *+(.jpg|.jpeg|.gif|.png) ./pictures
    
    mv *+(.doc|.pdf|.odt|.txt) ./document
    
    mv *+(.deb|.zip|.gz) ./packets
    
    mv *+(.iso) ./iso-files
    
    exit 0
    Why is that?

  2. #2
    Join Date
    Jan 2008
    Location
    Nappanee, IN
    Beans
    602
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: Simple bash script: move by file extension.

    I think your script syntax is aborting on your .mp3 and .iso lines where you only have one option, but you are inserting the + and () which implies multiple options.

  3. #3
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Simple bash script: move by file extension.

    why don't you use standard globs

    Code:
    mv *.jpg *.jpeg *.gif *.png ./pictures
    or
    Code:
    mv *.{jpg,jpeg,gif,png} ./pictures
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  4. #4
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Simple bash script: move by file extension.

    +1 for standard globs or brace expansion.

    If you want to use extended globs, you have to enable them:
    Code:
    shopt -s extglob
    See:
    http://mywiki.wooledge.org/glob?acti...irect=globbing
    http://mywiki.wooledge.org/BraceExpansion

  5. #5
    Join Date
    Jul 2012
    Beans
    322
    Distro
    Xubuntu 18.04 Bionic Beaver

    Re: Simple bash script: move by file extension.

    Have a look here, might give you some help:

    http://ubuntuforums.org/showthread.php?t=2086723
    The best things in life are free, so what are we paying for?

  6. #6
    Join Date
    Nov 2011
    Location
    /dev/root
    Beans
    Hidden!

    Re: Simple bash script: move by file extension.

    There might be a problem with file names containing spaces.

    I tested the following command line, and it works with such file names

    Code:
    for i in *+(.doc|.pdf|.odt|.txt);do ls "$i";done
    and you can replace
    do ls "$i" with do mv "$i" ./documents

  7. #7
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Simple bash script: move by file extension.

    no, globs are not sensitive to whitespaces at all (that's why they should be used directly as often as possible)

    Code:
    $ touch "test 1.txt" "test 2.txt" "test 1.pdf"
    $ printf "[%s]\n" *.{txt,pdf}
    [test 1.txt]
    [test 2.txt]
    [test 1.pdf]
    $ shopt -s extglob
    $ printf "[%s]\n" *+(.txt|.pdf)
    [test 1.pdf]
    [test 1.txt]
    [test 2.txt]
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  8. #8
    Join Date
    Nov 2011
    Location
    /dev/root
    Beans
    Hidden!

    Re: Simple bash script: move by file extension.

    I see, but maybe don't see

    Running a script file containing
    Code:
    ls *+(.doc|.pdf|.odt|.txt)
    or
    Code:
    for i in *+(.doc|.pdf|.odt|.txt);do ls "$i";done
    won't work when run by bash
    Code:
    bash script
    but runs without problems when run by source
    Code:
    source script
    Please explain the difference!

  9. #9
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Simple bash script: move by file extension.

    Quote Originally Posted by sudodus View Post
    There might be a problem with file names containing spaces.
    EDIT: Yayy! Didn't refresh the page before posting. Vaphell beat me to it.

    Nope. The file names produced by the glob do not undergo any further word-splitting, so even if a file contains internal whitespace, the expansion of a glob that matches that file will still preserve each filename as a single word.

    If the glob does not match any file name and nullglob is not enabled then it remains unchanged. In this case mv will throw a `no such file or directory' error which can be safely ignored.
    Last edited by sisco311; February 10th, 2013 at 05:00 PM.

  10. #10
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Simple bash script: move by file extension.

    Quote Originally Posted by sudodus View Post
    I see, but maybe don't see

    Running a script file containing
    Code:
    ls *+(.doc|.pdf|.odt|.txt)
    or
    Code:
    for i in *+(.doc|.pdf|.odt|.txt);do ls "$i";done
    won't work when run by bash
    Code:
    bash script
    but runs without problems when run by source
    Code:
    source script
    Please explain the difference!
    `bash script' will run the script in a subshell where extglob is not enabled. You have to enable it in your script:

    Code:
    shopt -s extglob
    commmand *+(whatever)
    The `source' command or the dot (`.') command will run the commands from the script in the current shell.

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •