Ubuntu HowTo: Alternative to xdotool which is already installed on Ubuntu

Original Source Link

I need to use xdotool to quickly automate some click at specific locations of my screen. I could use the following script:

#! /bin/bash
sleep 0.5
xdotool click 200 150
sleep 0.5
xdotool click 200 175
sleep 0.5
xdotool click 1050 150
sleep 0.5
xdotool click 1350 450
sleep 0.5
xdotool click 1100 150

And it would work fine. However, xdotool is not installed on the machine I’m using, and I do not have root access. I cannot install it locally because the X11/extensions/XTest.h library is not present. What are my options?

You can compile xdotool statically and copy it to that machine.

Alternatively you can copy the dynamically linked binary and see if the libraries resolve.

Tagged : / / /

Ubuntu HowTo: Automate firewall sign in

Original Source Link

The network in which the workstation I use is configured with fortinet firewall and needs authentication. Internet inactivity of 2400s makes the firewall logout automatically. I am using remote access applications to access the system from home. Is it possible to automate the sign in process for firewall using some shell script by configuring the username and password in the script.

Tagged : / / / /

Ubuntu HowTo: Using CUBIC to create a hardened installable Ubuntu ISO

Original Source Link

I have tried to run the hardening script in the CUBIC’s Virtual Linux Environment and created an installable ISO ,But it runs into error while installation , something to do with ubiquity, also tried to load up the script in /etc/profile.d but i dont think it ran at the startup,the script had this x right next to it
THE ICON

Tagged : / / / /

Ubuntu HowTo: How to check the process is already running or not

Original Source Link

I want to check the particular process in already running or not.

I refereed this Q&A.

But I didn’t get any specific solution. Following is the example that I tried:
I have created abc.sh file and run this script on background, like sh abc.sh &.

Now this file is running on background and I fire the ps aux | grep "abc" command.

Following is the output of this command:

prakash     3594  0.0  0.0   4388   820 pts/0    S+   16:44   0:00 grep --color=auto abc

After that I stop the abc.sh running script and fire the same command ps aux | grep "abc" command.

But I am getting same output like:

prakash     3594  0.0  0.0   4388   820 pts/0    S+   16:44   0:00 grep --color=auto abc

Is there any other way to find the process is running or not?

Every process will be listed in the output of ps aux; whether running, sleeping, zombie or stopped.

However, in your case, since you ran the process using sh abc.sh, sh is the application(shell) that is running and not abc.sh. Hence, ps aux will not contain the process abc.sh because of which grep could not yield any result.

So, the correct way you should have used it is as:

ps aux | grep sh

This may also return you other process that are running having the string sh anywhere in their output of ps aux.

You should note that the process will be “running” when the output of ps aux has its STAT as R. If it is something other than that, it is not running at the instance you fired the command to check the running processes. The different process states can be found in the man page for ps:

D    uninterruptible sleep (usually IO)
R    running or runnable (on run queue)
S    interruptible sleep (waiting for an event to complete)
T    stopped, either by a job control signal or because it is being traced
W    paging (not valid since the 2.6.xx kernel)
X    dead (should never be seen)
Z    defunct ("zombie") process, terminated but not reaped by its parent

You could as well run the top command to check if the process is running or sleeping and the amount of CPU, RAM it is consuming. (This will again list your process as sh).

However, if you do want your process to be listed as abc.sh, then you should have the first line of the script you are running as:

#!/bin/sh

so that the shell will know what application to use to run the script(sh in this case, change it to #!/bin/bash for bash) and then provide executable permissions to the process using:

chmod +x /path/to/abc.sh

replacing /path/to/ with the location of the abc.sh file and then run abc.sh using

/path/to/abc.sh

again replacing /path/to/ with the location of the abc.sh file.

My problem with grep is that it’s a whole-line parser. In your example, you’re searching for “abc” but it’s pulling back the instance of grep (that’s looking for “abc”). A bit circular. You can filter that out but I find all that a little perverse.

I would turn to awk for a little panache.

ps aux | awk '$12=="abc.sh"'

awk splits the lines into fields based on whitespace (by default). Fields $11+ are the command column(s) so if the command is “sh abc.sh …”, $11 will be sh and $12 will be abc.sh.

If you want to control the output or chain on with Bash’s && and || operators, you can but you’ll need to be a little more clever. grep will exit with status code 1 (technical fail, triggers ||) if nothing is found but awk will always exit code 0. We can change this by telling it to exit if it finds something and throw a 1 if it doesn’t:

ps aux | awk '$12=="abc.sh" {exit 0} END{exit 1}' && echo running || echo not running

Of course if you just care about writing output to the screen, you could do that all in awk:

ps aux | awk '$12=="abc.sh" {print "running"; exit} END{print "not running"}'

The right way to check in the current bash session if a process started in background, like sh abc.sh &, is already running or not is to use jobs builtin command.

Example:

$ sh abc.sh &
[1] 6917
$ jobs
[1]+  Running                 sh abc.sh &

That’s mean that the sh abc.sh process started in background is running. If the process has finished you will see something like:

[1]+  Done                    sh abc.sh

If you have more processes running in background, you can restrict the output only to the
sh abc.sh process using:

jobs sh

See help jobs for more info.

The above method will work only in the current session of bash. But if you are in another bash session (e.g. in another terminal window or tab, or in a script), aside from:

ps aux | grep "[s]h bin/test.sh"

you can use also:

pgrep -a sh

which seems more simple to me. If you will see your typed command in the output (sh abc.sh in this case), that’s means that the process is still running. If not, the process has finished.

If your script is currently running, it must be shown by ps aux command. If it not shown by ps, then there may be your script execution is currently terminated.

Output of ps like this means your script is currently not running,

$ ps aux | grep hm.sh
avinash   6386  0.0  0.0  15940   932 pts/16   S+   17:34   0:00 grep --color=auto hm.sh

If it shows like this, which means your script is currently running.

$ ps aux | grep hm.sh
avinash   6454  0.0  0.0  16616  1384 pts/16   S+   17:35   0:00 /bin/bash ./hm.sh
avinash   6535  0.0  0.0  15940   932 pts/27   R+   17:35   0:00 grep --color=auto hm.sh

It’s all based on the contents of the script.

For example, if you create a script like this. It displays the message HI on execution. It takes a few seconds to display the output and got terminated after it produces the final result.

#!/bin/bash
echo "HI"

But if your script like this,

#!/bin/bash
sudo apt-get update

It takes some minutes to get the process terminated. On the meantime if you run ps aux | grep filename command, it will be shown on the output. Because the script is currently running.

you can always use pgrep to check if the process in running

pgrep -c "process name"

what this command does is count how many process were found with the name “process name” and returns thier number [0 is the process was not found]

  • if you want to check all processes then use ‘top’

  • if you want to know processes run by java then use ps -ef | grep java

  • if other process then just use ps -ef | grep xyz or simply /etc/init.d xyz status

  • if through any code like .sh then ./xyz.sh status

if you simply want to check whether there is any under process then go to that directory and type ll -tr if you see .csv or .work etc at the end of the file then it means under process else not.

Tagged : / / /

Ubuntu HowTo: how to execute a command after resume from suspend?

Original Source Link

I’ve got a script that is executed in order to have suspending/resuming working in my laptop. Then I have another series of xinput,xkbset and xmodmap commands that are executed when I initiate a session to have two-finger scrolling and keyboard shortcuts set up. When I resume from suspend, two-finger scrolling and my keyboard shortcuts won’t work. I need to manually execute the commands in the second file again. How can I add those to the suspend/resume script to have this done automatically? See below:

suspend/resume script

/etc/pm/sleep.d/20_custom-ehci_hcd

#!/bin/sh
#inspired by http://art.ubuntuforums.org/showpost.php?p=9744970&postcount=19
#...and http://thecodecentral.com/2011/01/18/fix-ubuntu-10-10-suspendhibernate-not-working-bug    
# tidied by tqzzaa :)

VERSION=1.1
DEV_LIST=/tmp/usb-dev-list
DRIVERS_DIR=/sys/bus/pci/drivers
DRIVERS="ehci xhci" # ehci_hcd, xhci_hcd
HEX="[[:xdigit:]]"
MAX_BIND_ATTEMPTS=2
BIND_WAIT=0.1

unbindDev() {
  echo -n > $DEV_LIST 2>/dev/null
  for driver in $DRIVERS; do
    DDIR=$DRIVERS_DIR/${driver}_hcd
    for dev in `ls $DDIR 2>/dev/null | egrep "^$HEX+:$HEX+:$HEX"`; do
      echo -n "$dev" > $DDIR/unbind
      echo "$driver $dev" >> $DEV_LIST
    done
  done
}

bindDev() {
  if [ -s $DEV_LIST ]; then
    while read driver dev; do
      DDIR=$DRIVERS_DIR/${driver}_hcd
      while [ $((MAX_BIND_ATTEMPTS)) -gt 0 ]; do
          echo -n "$dev" > $DDIR/bind
          if [ ! -L "$DDIR/$dev" ]; then
            sleep $BIND_WAIT
          else
            break
          fi
          MAX_BIND_ATTEMPTS=$((MAX_BIND_ATTEMPTS-1))
      done  
    done < $DEV_LIST
  fi
  rm $DEV_LIST 2>/dev/null
}

case "$1" in
  hibernate|suspend) unbindDev;;
  resume|thaw)       bindDev;;
esac

touchpad two-finger scrolling and keyboard shortcuts script

xinput set-int-prop "SynPS/2 Synaptics TouchPad" "Two-Finger Scrolling" 8 1
xinput set-int-prop "SynPS/2 Synaptics TouchPad" "Synaptics Two-Finger Scrolling" 8 1 1
xinput set-int-prop "SynPS/2 Synaptics TouchPad" "Synaptics Two-Finger Pressure" 32 10
xinput set-int-prop "SynPS/2 Synaptics TouchPad" "Synaptics Two-Finger Width" 32 8
setxkbmap -layout gb
xkbset m
xkbset exp =m
xmodmap -e "keycode 135 = Pointer_Button2"

You can place your scripts in the /etc/pm/sleep.d directory to have them run after suspend. You will need to add a conditional to make your script run only during resume and not during the suspend process as well. For example, your touchpad script would look like:

case "${1}" in
    resume|thaw)
        DISPLAY=:0.0 ; export DISPLAY
        xinput set-int-prop "SynPS/2 Synaptics TouchPad" "Two-Finger Scrolling" 8 1
        xinput set-int-prop "SynPS/2 Synaptics TouchPad" "Synaptics Two-Finger Scrolling" 8 1 1
        xinput set-int-prop "SynPS/2 Synaptics TouchPad" "Synaptics Two-Finger Pressure" 32 10
        xinput set-int-prop "SynPS/2 Synaptics TouchPad" "Synaptics Two-Finger Width" 32 8
        setxkbmap -layout gb
        xkbset m
        xkbset exp =m
        su $USER -c "sleep 3; /usr/bin/xmodmap -e "keycode 135 = Pointer_Button2"" &
;;
esac

Be sure your script is marked globally executable and change $USER to the corresponding username.

You can find more detailed information in the pm-suspend manpage (man pm-suspend) or by looking at the documentation in /usr/share/doc/pm-utils (particularly /usr/share/doc/pm-utils/HOWTO.hooks.gz).

On Ubuntu 16.04 I had to create service this way:

  1. create file

    sudo gedit /etc/systemd/system/somename.service
    
  2. put inside

    [Unit]
    Description=Some description
    Before=sleep.target
    StopWhenUnneeded=yes
    
    [Service]
    Type=oneshot
    RemainAfterExit=yes
    ExecStop=-/path/to/your/script.sh
    
    [Install]
    WantedBy=sleep.target
    
  3. enable service

    sudo systemctl enable somename
    
  4. (optional) if not working after resume from suspend > check for errors with

    journalctl -u somename.service
    

Open this file:

sudo vim /lib/systemd/system-sleep/hdparm

Contents:

#!/bin/sh

case $1 in 
  post)
    /usr/lib/pm-utils/power.d/95hdparm-apm resume
    ## Paste your command to run your script
    ;; esac

Your command will execute with admin privileges.

Here is the version I use to restart my VPN daemon after resume:

#!/bin/sh

case "$1" in
        pre)
                echo "$(date) - $1: Restarting norvpnd" >> /tmp/nordvpn/suspend.txt
                /usr/bin/systemctl restart nordvpnd
                ;;
        post)
                echo "$(date)-  $1: Doing nothing" >> /tmp/nordvpn/suspend.txt
                ;;
esac

This file is put in:

/lib/systemd/system-sleep//00restart_nordvpnd.sh

This works fine in Ubuntu 20.4. The pre part is executed before putting the computer to sleep (suspend) and the post part is executed when it is resumed, as that can be verified by looking at the /tmp/nordvpn/suspend.txt file.

Side note (irrelevant to the subject but worth noting): Regarding the task in this example, it might seem illogical to restart a daemon before putting the computer to sleep, but it’s the (only?) way it works.

Tagged : / /

Ubuntu HowTo: Why doesn’t “cd” work in a shell script?

Original Source Link

I just want to write a script which changes my directory.

I put the below commands in the file /home/alex/pathABC

#!/bin/sh
cd /home/alex/Documents/A/B/C
echo HelloWorld

I did

chmod +x pathABC

In the Terminal, while in /home/alex, I ran ./pathABC, but the output is just HelloWorld and the current directory is not changed.

So what is wrong?

As others have explained, the directory is changed in the child process of your script, not in the terminal process from which the script is called. After the child process dies, you are back in the terminal which is left where it was.

Several alternatives:

1. Symbolic link

Put a symlink in your home to the long path you want to easily access

$ ln -s /home/alex/Documents/A/B/C ~/pathABC

then access the directory with:

$ cd ~/pathABC

2. Alias

Put an alias in your ~/.bashrc:

alias pathABC="cd /home/alex/Documents/A/B/C"

(from here)

3. Function

Create a function that changes the directory, the function runs in the process of your terminal and can then change its directory.

(from here)

4. Avoid running as child

Source your script instead of running it. Sourcing (done by . or source) causes the script to be executed in the same shell instead of running in its own subshell.

$ . ./pathABC

(from here and here)

5. cd-able vars

Set the cdable_vars option in your ~/.bashrc and create an environment variable to the directory:

shopt -s cdable_vars
export pathABC="/home/alex/Documents/A/B/C"

Then you can use cd pathABC

(from here)

When you run script in a Terminal, a child process runs. In this child program i.e. your script will change to whatever directory specified. But in the parent process, i.e. where you run the script is still in the old path.
OR simply we can say:

The scope of cd command is only for child process not parent.

You are making a thinking error. While the current shell stays in the same directory, the script has moved to the new directory.

You could see that by creating another script in the new directory, and running it from your script, after it has changed directory:

#!/bin/sh
cd /home/alex/Documents/A/B/C && ./another_script.sh # (if it is executable)

The second script would run from the new directory.

HelloWorld 

is just the output of the script.

Because hello world is just a trace statement, let’s try this:

Create bash script file cd.sh containing:

#!/bin/bash
echo "/home/mike/Documents/A/B/C"
  • The .sh extension is an older convention of giving bash script filenames an extension. It’s purely cosmetic and usually unnecessary. However in this case it’s important to differentiate from the core cd command.

Mark the bash script file executable using:

chmod a+x cd.sh

Now run the file:

$ cd $(./cd.sh)
bash: cd: /home/alex/Documents/A/B/C: No such file or directory
  • cd we all know.
  • $(...) executes command inside parenthesis and returns output.
  • If cd.sh was in your path you don’t need to specify where it is. We prefix with ./ to specify the command is in the current directory.
  • The echo output from the cd.sh script is sent back to the parent via the $(...). The parent (our shell prompt) uses this output and passes it to the Linux cd command.

As others have mentioned a child process can’t change the parent’s directory. This is one way the child can tell the parent where to go after the process ends.

Trying to use cd inside the shell script does not work because the shell script runs in the subshell and once the script is over it returns to the parent shell, which is why the current directory does not change.

To achieve changing of the directory use sourcing.You can either use . scriptname.sh or source scriptname.sh command to use sourcing.

Note : Also when you use sourcing do not use the exit command because it then closes your connection.

CDPATH might help in some cases.

# add this to .bashrc .zshrc or whatever
export CDPATH="/home/alex/Documents/A/B:$CDPATH"

# and then you can just do...
cd C

Note u have to add ‘A/B’ to CDPATH, not ‘A/B/C’
You can add multiple paths to CDPATH just like PATH.

Actually, I just found, after many searches, that if you need to change the directory, and still keep the same shell, so you will get all the answers in your currect script, you can use:

(cd your_dir; do_some_command_there)

For example, what I needed to use, was:

((cd your_dir; git remote -v | wc -l)

Works like a charm!

Tagged : / / /

Ubuntu HowTo: zsh: read:-p: no coprocess error while using read command with -p flag

Original Source Link

community. I recently started to use zsh instead of bash and now i’m getting some problems that cannot be found on bash.
One of theme: when i use read -p command, obviously with flag -p, i get an error that says:

read: -p: no coprocess

On the other hand when I switch to bash again, this command works perfectly.

My full command:

read -p "what's your name" name

Thank you for help <3

The usage is not compatible between shells.

As explained in man zshbuiltins, the way to emit a prompt string for a zsh read command is to append it to the identifier name after a ?:

          If the first argument contains a `?', the remainder of this word
          is used as a prompt on standard error when the shell is interac‐
          tive.

So for example

 % read name?"what's your name? "
what's your name? 

(the ? needs to be escaped here to prevent the shell treating it as a globbing character) or

 % read "name?what's your name? " 
what's your name? 

See also:

Tagged : / / /

Ubuntu HowTo: Bulk image download from piwigo-based web gallery

Original Source Link

I wish to download original sized images from this website, built upon piwigo CM http://mermaid.pink/

Single image download is allowed, but that would take ages.

Is there an application, extension, script or anything else capable of this task?

You can use wget as shown here:

Downloading an Entire Web Site with wget

Sep 05, 2008 By Dashamir Hoxha in HOW-TOs

If you ever need to download an entire Web site, perhaps for off-line
viewing, wget can do the job—for example:

$ wget 
     --recursive 
     --no-clobber 
     --page-requisites 
     --html-extension 
     --convert-links 
     --restrict-file-names=windows 
     --domains website.org 
     --no-parent 
         www.website.org/tutorials/html/

This command downloads the Web site
http://www.website.org/tutorials/html/.

The options are:

  • --recursive: download the entire Web site.
  • --domains website.org: don’t follow links outside website.org.
  • --no-parent: don’t follow links outside the directory tutorials/html/.
  • --page-requisites: get all the elements that compose the page (images, CSS and so on).
  • --html-extension: save files with the .html extension.
  • --convert-links: convert links so that they work locally, off-line.
  • --restrict-file-names=windows: modify filenames so that they will work in Windows as well.
  • --no-clobber: don’t overwrite any existing files (used in case the download is interrupted and resumed).

Out of these --page-requisites & --recursive will likely be needed, though --convert-links or --no-clobber may be useful. For more information on using wget run man wget (or look here).

This is my solution for this as your question get the images

So first create the folder to save the images, then cd into it

#terminal
mkdir imagesFolder
cd imagesFolder/

# this one will take a long time but will download 
# every single image related to this website
wget -r -nd -H -p -A '*.jpg','*.jpeg','*.png' -e robots=off http://mermaid.pink/

#I recomend to use this one better as the images in this site are all jpg
# And recursion level is set to 1
wget -r -l 1 -nd -H -p -A '*.jpg' -e robots=off http://mermaid.pink/

The wget arguments explained:

  • -r | –recursive:

    • Turn on recursive retrieving. The default maximum depth is 5.
  • -l depth | –level=depth:

    • Specify recursion maximum depth level depth.
  • -nd | –no-directories:

    • Do not create a hierarchy of directories when retrieving
      recursively. With this option turned on, all files will get saved
      to the current directory, without clobbering (if a name shows up
      more than once, the filenames will get extensions .n).
  • -H | –span-hosts:

    • Enable spanning across hosts when doing recursive retrieving.
  • -p | –page-requisites:

    • This option causes Wget to download all the files that are
      necessary to properly display a given HTML page. This includes
      such things as inlined images, sounds, and referenced stylesheets…
  • -A | –accept:

    • Specify comma-separated lists of file name suffixes or patterns to
      accept. Note that if any of the wildcard characters, ,
      ?, [ or ], appear in an element of acclist , it will be
      treated as a pattern, rather than a suffix. In this case, you have
      to enclose the pattern into quotes to prevent your shell from
      expanding it, like in -A “
      .mp3″ or -A ‘*.mp3’.
  • -e | –execute:

    • Execute command as if it were a part of .wgetrc. A command thus
      invoked will be executed after the commands in .wgetrc, thus taking
      precedence over them. If you need to specify more than one wgetrc
      command, use multiple instances of -e.
    • In this case the robots=off is the argument of -e

For more info in wget type in terminal

man wget

OR check THIS

Thanks T04435

Tagged : / / / /

Ubuntu HowTo: How to refer environment variable in terminal command?

Original Source Link

I am new to Linux world, here i am trying to pass the env variable in command to aws in this way

sudo aws s3 cp s3://bucket/path/to/file.sh file.sh

Should download the file.sh to my local, here i want the user to set env variable FILE_PATH and use it while downloading file.

echo $FILE_PATH 

displays

s3://bucket/path/to/file.sh

But when I do

sudo aws s3 cp $FILE_PATH file.sh

the file is corrupted and not downloading actual file, how do I refer env in this command?

Tagged : / / /

Ubuntu HowTo: Rotate images from terminal

Original Source Link

I have a directory with a lots of images but they are in the wrong orientation. I want to rotate the images to correct the orientation (mostly ±90o). Using image (shotwell photo) viewer I can rotate them individually by clicking the rotate button but that’s too tedious.

I looked at man shotwell and shotwell --help-all but there’s nothing that explains how to invoke the rotate command from the command line.

Is there any way I can invoke the rotate command of shotwell (or any other viewer) from the terminal? Or any other methods to rotate images are welcome too.

If you’re looking for a pure bash implementation, ImageMagick’s convert command is what you’re looking for:

for szFile in /path/*.png
do 
    convert "$szFile" -rotate 90 /tmp/"$(basename "$szFile")" ; 
done

Above will leave existing files intact and copy the newly rotated ones to /tmp so you can move or copy them somewhere else or even replace the existing ones after the conversion and after verification.

(and it’ll work on all recent releases of Ubuntu as it’s standard software)

for file in *.JPG; do convert $file -rotate 90 rotated-$file; done

This will copy-rotate-and-rename your files.

If you want to leave the original files untouched just yet, this method may work well for you…

Note that this is case-sensitive: if your files are named *.jpg replace with lower-case (or *.png …) accordingly.

If you want to overwrite in-place, mogrify from the ImageMagick suite seems to be the easiest way to achieve this:

# counterclockwise:
mogrify -rotate -90 *.jpg

# clockwise:
mogrify -rotate 90 *.jpg

CAVEAT: This isn’t a lossless rotation method for JPEG files, https://www.imagemagick.org/discourse-server/viewtopic.php?t=5899.
jpegtran achieves this (untested):

# counterclockwise
ls *.jpg | xargs -n 1 jpegtran -perfect -rotate 270

# clockwise
ls *.jpg | xargs -n 1 jpegtran -perfect -rotate 90

Here’s how I do it:

  1. Install gThumb

     sudo apt-get install gthumb
    
  2. Open up nautilus and go to your images directory. Right click on one and choose Open with -> gthumb.

  3. Under the view menu choose Browser or press the Esc key. This will open the directory browser showing all your images.

    enter image description here

  4. Press Ctrl and select the images you want to rotate or select all of them via Ctrl + A.

  5. On the toolbar, select Tools and then Rotate Right or Rotate Left depending on your preference.

    enter image description here

A nice solution is to make a set of backups (e.g. prefixed backup-), rotate the original files producing a set of new files (prefixed e.g. rotated-), giving you a set of

  • img-1.png
  • backup-img-1.png
  • rotated-img-1.png
  • img-2.png
  • …and so on

The mv/cp tools [bash globbing] can only add prefixes, it’s messy to take them away (it’d use parameter expansion, ewww…)

The rename tool allows you to use s/before/after/ substitution syntax (from the sed tool) to swap that safeguard prefix and overwrite the original files, so your overall process for a given set of pictures img-{1..n}.png would be:

for imgf in img-*.png; do mv "$imgf" "backup-$imgf"; done
for imgf in backup-img-*.png; do convert "$imgf" -rotate 90 "rotated-$imgf"; done

Note:

  • you could use cp rather than mv, but then there’s 2 copies of the original lying around (downside is you get concatenation of prefixes, “rotated-backup-…”)
  • rotation is clockwise (-rotate 270 gets you 90° anti-clockwise)
  • to track progress, add echo "Rotating ${imgf#backup-} ... "; after convert calls (before done)
  • for a more compact form (e.g. a set of numbered files), use some parameter expansion like echo "$(echo ${imgf#backup-img-} | cut -d. -f 1)..."; instead

    ( You can’t remove prefix and suffix in the same bash param expansion hence use cut )

Then after verification you’ve not messed up, delete the pictures by moving them back to the original

rename 's/^rotated-//;' rotated-*
rm backup-img-*

You can copy/paste this code, and save it as rotate.sh

#!/bin/bash -e

CUR_DIR=$(pwd)
cd $1
for file in *.jpg
do
    convert $file -rotate 90 $file
done
cd $CUR_DIR

After saving this file, run it from terminal using ./rotate.sh folder_containing_images.

Tagged : / / /