7

Lets say I have three image files to edit. Consider all the files are in ~/Pictures. Let name of the first file be 1.svg, name of the second file be 2.svg and name of the third file be 3.svg .I have Inkscape editor. Now, I want a bash script to be executed so that the following must happen:

  1. First 1.svg must open for me to edit.
  2. After editing, when I close that file, automatically 2.svg must open for me to edit.
  3. Now when I complete editing 2.svg, I want 3.svg to open for editing.
  4. Now after editing 3.svg the script must end by closing GIMP.

PS:- If you are wondering if this is home work, it is not! See here . You will notice that I have to edit images there. To make that answer better I need help here. So please help. Also I AM A COMPLETE BEGINNER. I don't know anything about bash. So explain your answer as elaborately as possible. Also you might consider giving an answer with GIMP editor to edit .jpg files . It might be useful for others. By the way, I am using 14.04 LTS . Thanks in advance.

UPDATE :- The above linked answer was edited according to the accepted answer below.

4
  • Why don't you want to open them all at the same time?
    – Nattgew
    Commented Apr 25, 2014 at 19:53
  • @Nattgew Well that would be a mess if I have a lot of files. Did you see the "Alternate method" mentioned in Andrew's answer in the link I provided?
    – Venkatesh
    Commented Apr 25, 2014 at 19:54
  • @Nattgew Ok.. I edited the answer there and it aint approved there still.. Please wait for sometime till approval..
    – Venkatesh
    Commented Apr 25, 2014 at 19:55
  • 3
    for i in 1.jpeg 2.jpeg 3.jpg; do gimp $i; done? This will relaunch gimp every time, tough. If you want to do that thing without closing gimp you should probably use a gimp script (which I do not grok at all). I still think that opening them as tabs as @Nattgew says is the best option... (BTW, in your linked answer, .svg files are better not edited by GIMP; they are vector shapes, you should use f.e. Inkscape).
    – Rmano
    Commented Apr 25, 2014 at 19:56

3 Answers 3

8

As Rmano says, you can simply do a for loop in the terminal:

for i in 1.jpg 2.jpg 3.jpg; do gimp $i; done

Either run this in the directory where the files are, or each file should have the rest of the path to it. You can put as many files as you want in there.

GIMP will open the file, you edit it, and then close GIMP. Once GIMP closes, the for loop continues and opens the next file in the list.

If you don't want to close GIMP every time, you can try adding a read -p:

for i in 1.jpg 2.jpg 3.jpg; do gimp $i && read -p "Press [Enter] key to continue..."; done

Once you finish editing a file, you should be able to press Enter in the terminal and the next file should open in GIMP, without having to close GIMP.

This is the simplest way I could think of, it may get complicated trying to detect when GIMP closes a file.

Note for your case

Most of the files are SVG files, as Rmano pointed out. So list all of your SVG files and replace gimp with inkscape. The PNG files will be fine with gimp.

6
  • Right.. BTW i can add any number of files right? Not just three...Also I should be in the ~/Pictures folder for this to run or can I mention the path there?
    – Venkatesh
    Commented Apr 25, 2014 at 20:07
  • @Venki You will need to be in the ~/Pictures directory, yes. and you can just replace gimp with whatever editor you want to use.
    – Seth
    Commented Apr 25, 2014 at 20:40
  • @Nattgew Your method works fine! But totally changing the subject it says it couldn't save my file..any help with that?
    – Venkatesh
    Commented Apr 25, 2014 at 20:52
  • @Nattgew I ran Seth's answer below as a bash script(I mean i saved it as a .sh file and ran it) and tried saving my edits in it and it worked just fine(though it throws other insignificant errors)..
    – Venkatesh
    Commented Apr 25, 2014 at 20:59
  • @Venki It probably couldn't save the file because you aren't using sudo privileges. You need to use sudo because you're editing files in /usr. IMO this answer is still better than mine.
    – Seth
    Commented Apr 25, 2014 at 21:19
7

The following command opens the files in the default associated application:

edit 1.svg 2.svg 3.svg

If you want to open all the .svg files in alphabetical order, you can use the * wildcard:

edit *.svg

* stands for any sequence of characters, so you can use it in other ways; for example, to successively open the .svg files whose name contains wibble:

edit *wibble*.svg

Other wildcards exist if you want to select files based on finer-grained patterns.

2
  • This is by far the best answer.
    – Seth
    Commented Apr 25, 2014 at 22:09
  • I would use find -exec if I want to selectively open some of them.
    – Braiam
    Commented Apr 26, 2014 at 14:19
2

You could try something like this:

shopt -s nullglob
for i in *
do
    if [ -d "$i"  ]
     then
        continue
    else
        inkscape "$i"

    fi
done

I tested it on my Pictures directory and it seemed to work.

I don't entirely understand the first line yet, but it is there to help make sure my * works. The for loop then loops through everything in the current directory one at a time. If it isn't a directory it opens it in Inkscape for editing.

When you're done editing and close inkscape the next image will open until there are no more images.


For your specific case (after reading the linked question) this might work better. Remember to cd to /usr/share/unity/icons and then run this:

for i in file1.svg file2.svg file3.svg
do
    if [ -d "$i"  ]
     then
        continue
    else
        inkscape "$i"

    fi
done  

replacing file1.svg file2.svg file3.svg with a list of the files you want to edit.

5
  • I think nullglob means it won't try to iterate on a literal * if it matches nothing else?
    – Nattgew
    Commented Apr 25, 2014 at 20:26
  • @Nattgew That is correct, after looking into it more.
    – Seth
    Commented Apr 25, 2014 at 20:31
  • @Seth I put your answer in gedit saved it as a .sh file and ran it ... it worked like a dream!!!! But there are a few errors ... here is the .sh file I saved: pastebin.ubuntu.com/7332714 and here is the terminal message that shows up : pastebin.ubuntu.com/7332719 Probably there is something wrong with the cd command I added in my bash script? Or is this how your answer should run ?
    – Venkatesh
    Commented Apr 25, 2014 at 21:08
  • 2
    @Venki I probably don't need the double brackets. I've removed them. You might also need to use sudo inkscape since you're editing files in /usr.
    – Seth
    Commented Apr 25, 2014 at 21:18
  • 2
    @Venki the errors were because you were using /bin/sh (which is dash on Ubuntu systems) and not bash. The [[ ]] test is not available in dash. Just change your first line (the shebang) to #!/bin/bash or, #!/usr/bin/env bash and Seth's first version will work fine.
    – terdon
    Commented Apr 25, 2014 at 23:14

You must log in to answer this question.

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