add a constant string to the end of the terminal command filename

Asked 2 years ago, Updated 2 years ago, 95 views

This is a mac.

The contents of a directory are as follows:

1_X.jpg
2.jpg
3.jpg
4_X.jpg
5.jpg
.
.
.

The file name is always ".jpg" or "_X.jpg".
I would like to make all of these ***_X.jpg.
"In other words, I would like to give ""_X"" to all the file names that do not have ""_X"" on them."

What should I do?

unix

2022-09-29 22:35

2 Answers

You can also use find to find and rename the appropriate file.

find.!-name*_X.jpg-type f-execdir sh-c'mv "$1" "${1%.jpg}_X.jpg"'_{}\;


2022-09-29 22:35

How about this shellscript?

#!/bin/sh

while [-n "$1" ]
do
    fullname = $1
    extension=${fullname##*.}
    filename = ${fullname%.*}
    suffix = ${filename##*_}
    if ["$suffix"!="X"]; then
        mv${filename}"."${extension}${filename}"_X."${extension}
    fi
    shift
done

The answer from @metropolis in the comment may be sufficient, but
Save the above to a file, try saving it to the same directory as the jpeg file, execute the command chmod+x755 filename, and then try ./filename*.jpg.You will get the result of your expectations.

Once the execution is confirmed, from Terminal, click

 mkdir-p/usr/local/bin
sudo cp filename /usr/local/bin

Run two commands to move the command to an executable location that does not pollute the system.
Also, if your shell is bash (this shell is usually used)

in your preferred editor~/.bashrc
PATH=/usr/local/bin:$PATH

Look for the line and add it if you don't have one.
Now, the next time you open Terminal, you can execute the command to add _x from any directory if the filename *.jpg does not have _X at the end of the jpeg file.

shift from while [-n "$1"] to done on the previous line. It handles arguments sequentially and can be applied, so there is no loss in remembering them.


2022-09-29 22:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.