Results 1 to 10 of 10

Thread: Terminal rename and move - [not what you think]

  1. #1
    Join Date
    May 2012
    Location
    ザ・ワ&
    Beans
    152
    Distro
    Xubuntu 12.04 Precise Pangolin

    Terminal wget, rename, and move - [not what you think]

    Greetings,

    Ok, I bet you are thinking "this has been asked and answered, search the forums before you post n00b" but this is a unique situation with (I hope) a unique solution.

    Some background: I used the terminal to download a large amount of .jpg files from www.wizards.com (available for private use) and ended up with (by design) files with names like this:

    leaving.asp?url=%2Fdnd%2Fimages%2Fpsi_gallery%2FPs i_1.jpg
    leaving.asp?url=%2Fdnd%2Fimages%2Fpsi_gallery%2FPs i_2.jpg
    leaving.asp?url=%2Fdnd%2Fimages%2Fpsi_gallery%2FPs i_3.jpg
    ...and so on.

    what I want to know, is it possible to rename these files en masse, and move them based on the resulting name, say:

    leaving.asp?url=%2Fdnd%2Fimages%2Fpsi_gallery%2FPs i_1.jpg
    leaving.asp?url=%2Fdnd%2Fimages%2Fpsi_gallery%2FPs i_2.jpg
    leaving.asp?url=%2Fdnd%2Fimages%2Fpsi_gallery%2FPs i_3.jpg

    into

    /home/username/dnd/images/psi_gallery/Psi_1.jpg
    /home/username/dnd/images/psi_gallery/Psi_2.jpg
    /home/username/dnd/images/psi_gallery/Psi_3.jpg

    Preferably in some manner that replaces "leaving.asp?url=" with "/home/username" and the remaining "%2F" with "/"

    I have already attempted this with Gprename, and as useful as that is it does not do the trick here. If it is not possible I can make do with what I know, but I should really like to have this done properly and organized. Assume the directories are already made.

    Any tips, tricks, and how-tos would be appreciated!
    Last edited by ntzrmtthihu777; November 4th, 2012 at 08:08 PM.

  2. #2
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Terminal rename and move - [not what you think]

    Well it looks like the perl-based 'rename' command will do multiple substitutions in one go separated by ';' (like sed) i.e.

    Code:
    rename -nv 's#leaving\.asp\?url=#/home/username#;s#%2F#/#g' *.jpg
    (the target directory must already exist) - if it looks like it's doing the right thing then remove the 'n' switch to do it for real

  3. #3
    Join Date
    May 2012
    Location
    ザ・ワ&
    Beans
    152
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: Terminal rename and move - [not what you think]

    Well I dun goofed, lol. I ran the code as-is, and forgot to substitute my own username, lol. Posting this here as an example of what NOT to do, lol.

    Code:
    rename -nv 's#leaving\.asp\?url=#/home/username#;s#%2F#/#g' *.jpg
    leaving.asp?url=%2Fdnd%2Fimages%2Fpsi_gallery%2FPsi_25.jpg renamed as /home/username/dnd/images/psi_gallery/Psi_25.jpg
    Code:
    rename -v 's#leaving\.asp\?url=#/home/username#;s#%2F#/#g' *.jpg
    Can't rename leaving.asp?url=%2Fdnd%2Fimages%2Fpsi_gallery%2FPsi_25.jpg /home/username/dnd/images/psi_gallery/Psi_25.jpg: No such file or directory

  4. #4
    Join Date
    Jun 2006
    Location
    Nux Jam
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: Terminal rename and move - [not what you think]

    nautilus-filename-repairer should do the job.

    otherwise here is an other way:
    https://code.google.com/p/nautilus-b...downloads/list

  5. #5
    Join Date
    May 2012
    Location
    ザ・ワ&
    Beans
    152
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: Terminal rename and move - [not what you think]

    Quote Originally Posted by steeldriver View Post
    Well it looks like the perl-based 'rename' command will do multiple substitutions in one go separated by ';' (like sed) i.e.

    Code:
    rename -nv 's#leaving\.asp\?url=#/home/username#;s#%2F#/#g' *.jpg
    (the target directory must already exist) - if it looks like it's doing the right thing then remove the 'n' switch to do it for real
    Hmm, here is another question, is there a way to "pipe in" the results of

    Code:
    cut -d\" -f10 page.html | grep 'default.asp?x=dnd/pc/' |
    sort -u | while read link;do echo "http://www.wizards.com$link";done |
    while read page; do lynx --dump "$page" | grep "url=/dnd/" |
    awk '{print $2}';done | cut -d\& -f1 | while read img;do wget -c "$img";done
    (the code by which I got the images) into

    Code:
    rename -nv 's#leaving\.asp\?url=#/home/username#;s#%2F#/#g' *.jpg
    so that they are automatically renamed as you save them? I tried the good old | in between, but that is not cutting it here. Don't get me wrong, downloading them first and then renaming them works just fine, I just would like to be able to do it at one go in this script I am writing.

  6. #6
    Join Date
    Jan 2009
    Location
    'Murica!
    Beans
    185
    Distro
    Xubuntu 16.04 Xenial Xerus

    Re: Terminal wget, rename, and move - [not what you think]

    Quote Originally Posted by ntzrmtthihu777 View Post
    Greetings,

    Ok, I bet you are thinking "this has been asked and answered, search the forums before you post n00b" but this is a unique situation with (I hope) a unique solution.

    Some background: I used the terminal to download a large amount of .jpg files from www.wizards.com (available for private use) and ended up with (by design) files with names like this:

    leaving.asp?url=%2Fdnd%2Fimages%2Fpsi_gallery%2FPs i_1.jpg
    leaving.asp?url=%2Fdnd%2Fimages%2Fpsi_gallery%2FPs i_2.jpg
    leaving.asp?url=%2Fdnd%2Fimages%2Fpsi_gallery%2FPs i_3.jpg
    ...and so on.

    what I want to know, is it possible to rename these files en masse, and move them based on the resulting name, say:

    leaving.asp?url=%2Fdnd%2Fimages%2Fpsi_gallery%2FPs i_1.jpg
    leaving.asp?url=%2Fdnd%2Fimages%2Fpsi_gallery%2FPs i_2.jpg
    leaving.asp?url=%2Fdnd%2Fimages%2Fpsi_gallery%2FPs i_3.jpg

    into

    /home/username/dnd/images/psi_gallery/Psi_1.jpg
    /home/username/dnd/images/psi_gallery/Psi_2.jpg
    /home/username/dnd/images/psi_gallery/Psi_3.jpg
    [snip]
    Any tips, tricks, and how-tos would be appreciated!
    Haven't tested this, but if psi_gallery is under the current dir, and the images are in the current dir, this should work:

    Code:
    $ for f in leaving.asp*jpg; do n=$(echo "$f" | cut -c49-); mv -v "$f" psi_gallery/"$n"; done

  7. #7
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Terminal rename and move - [not what you think]

    Quote Originally Posted by ntzrmtthihu777 View Post
    Hmm, here is another question, is there a way to "pipe in" the results of [...] (the code by which I got the images) into

    Code:
    rename -nv 's#leaving\.asp\?url=#/home/username#;s#%2F#/#g' *.jpg
    so that they are automatically renamed as you save them?
    I'm not much of a wget expert but I think you should be able to do that on the fly using the -O flag to specify a name for the local file, something like

    Code:
    wget -O "$img_local" -c "$img"
    where you can derive img_local from img using essentially the same regex substitution - either using sed e.g.

    Code:
    $ img="leaving.asp?url=%2Fdnd%2Fimages%2Fpsi_gallery%2FPs i_2.jpg"
    $ img_local=$(echo "$img" | sed 's#leaving\.asp?url=#/home/username#;s#%2F#/#g'); echo "$img_local"
    /home/username/dnd/images/psi_gallery/Ps i_2.jpg
    or using the bash ${parameter//pattern/substitution} syntax, e.g.

    Code:
    $ img_local=${img//leaving\.asp\?url=/"/home/username"}; img_local=${img_local//%2F//}; echo "$img_local"
    /home/username/dnd/images/psi_gallery/Ps i_2.jpg
    (unfortunately I don't think there's any way to do both substitutions in one go with the bash syntax)
    Last edited by steeldriver; November 5th, 2012 at 12:22 AM.

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

    Re: Terminal rename and move - [not what you think]

    additionally /home/username can be replaced by $HOME which is maintenance free

    Code:
    $ f="leaving.asp?url=%2Fdnd%2Fimages%2Fpsi_gallery%2FPsi_1.jpg"
    $ fl=${f/#*=/$HOME}; fl={fl//%2F//}
    $ echo "$fl"
    /home/me/dnd/images/psi_gallery/Psi_1.jpg
    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

  9. #9
    Join Date
    May 2012
    Location
    ザ・ワ&
    Beans
    152
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: Terminal rename and move - [not what you think]

    Ah, thank you all so very much, have gotten what I needed done. I have one more query, and I shall be set: I have managed to use the command line to list all the .jpg files, but cannot get them into a .txt file for error checking I am using:
    Code:
    cat list1.txt | grep "http://www.wizards" | while read page;
    do lynx -source "$page" | grep "url=/dnd/images/" | cut -d\& -f1 | sort -u;done
    but I need to pipe the output into a text file, say list2.txt. Any of you ubuntu wizards got something for this?

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

    Re: Terminal rename and move - [not what you think]

    Code:
    cat list1.txt | grep "http://www.wizards" | while read page;
    do lynx -source "$page" | grep "url=/dnd/images/" | cut -d\& -f1 | sort -u;done
    you get better readability when you construct while read loop like this:

    Code:
    while read page
    do
      lynx -source "$page" | grep "url=/dnd/images/" | cut -d\& -f1 | sort -u
    done < <( grep "http://www.wizards" list1.txt ) > list2.txt
    also grep can work with files just fine so there's no need to have that unnecessary cat step
    > list2.txt at the end will dump the whole output of while read scope to the file.
    Last edited by Vaphell; November 5th, 2012 at 02:24 AM.
    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

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
  •