Bug 428180 - A shell script to rename files, which works perfectly in terminal, leaves a leading space in filename
Summary: A shell script to rename files, which works perfectly in terminal, leaves a l...
Status: RESOLVED NOT A BUG
Alias: None
Product: dolphin
Classification: Applications
Component: general (show other bugs)
Version: 19.12.3
Platform: Other Linux
: NOR normal
Target Milestone: ---
Assignee: Dolphin Bug Assignee
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-10-24 13:35 UTC by Sadi
Modified: 2023-10-24 08:01 UTC (History)
4 users (show)

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments
servicemenu script (206 bytes, text/plain)
2020-10-29 07:41 UTC, Sadi
Details
servicemenu .desktop file (292 bytes, text/plain)
2020-10-29 07:42 UTC, Sadi
Details
terminal output and rename package (130.14 KB, image/png)
2020-10-31 09:40 UTC, Sadi
Details
terminal output and rename package (478.88 KB, image/png)
2020-10-31 10:08 UTC, Sadi
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Sadi 2020-10-24 13:35:28 UTC
SUMMARY
I have a shell script to use in Dolphin ServiceMenu, cleaning filenames particularly for cross-platform compatibility, which runs as expected in terminal, but when used in a Dolphin ServiceMenu, it leaves a leading space in filename.

STEPS TO REPRODUCE
1. Run this command in terminal: touch "   a truly <horrible>|"awful" "$'\n'"name     with a newline and *globs*, and even a 'single' quote or two?!   .txt"
2. And then this command: rename 's/[*\n<|>"[\]]/ /g; s/\?//g; s/\\/ /g; s/ ,/,/g; s/:/-/g; s/\s+/ /g; s/^\s*//; s/\s+\././g; '"s/'//g" *
3. You will see a clean filename if you run this command: ls -N
4. Enter that command in a shell script file, starting with "for filename in "${@}"; do" and ending with " "$filename"; done".
5. Add this shell script file to an appropriate ServiceMenu file.
6. Repeat step 1 and then run this script this time from context menu.

OBSERVED RESULT
One leading space is left in filename.

EXPECTED RESULT
No leading space should be left as in terminal.

SOFTWARE/OS VERSIONS
Windows: 
macOS: 
Linux/KDE Plasma: Kubuntu 20.04
(available in About System)
KDE Plasma Version: 5.18.5
KDE Frameworks Version: 5.68.0
Qt Version: 5.12.8

ADDITIONAL INFORMATION
Comment 1 David Greengas 2020-10-29 00:24:39 UTC
Can you attach the shell script as well? I am having trouble getting the rename command to work given your instructions.
Comment 2 Sadi 2020-10-29 07:41:17 UTC
Created attachment 132854 [details]
servicemenu script
Comment 3 Sadi 2020-10-29 07:42:59 UTC
Created attachment 132855 [details]
servicemenu .desktop file
Comment 4 David Greengas 2020-10-30 14:23:45 UTC
Thank you providing those files. I tried your rename command and it returns
rename: not enough arguments

Then I created a new script that purely uses logger to log the filename in the for loop. I confirmed that the the filename was logged properly.

Can you confirm that the rename step from your script works on its own? I am not familiar enough with the rename command, nor your regex to figure out what it is trying to do.
Comment 5 Sadi 2020-10-30 19:33:31 UTC
(In reply to David Greengas from comment #4)

Unfortunately I don't know much about this rename package I've installed from official Ubuntu repositories. 
It seems it's a small Perl tool making use of sed to rename files.

We can test it in terminal like this:

mkdir rename
cd 

touch "   a truly file name"$'\n'"with a newline    and *globs*, and even a 'single' quote or two   .txt"

This file can be seen (better than ls) with this command:
for f in *; do echo "$f"; done

Then we can run this command:
for f in *; do rename 's/[*\n<|>"[\]]/ /g; s/\?//g; s/\\/ /g; s/ ,/,/g; s/:/-/g; s/\s+/ /g; s/^\s*//; s/\s+\././g; '"s/'//g" "$f"; done

This results in a "clean" name, without potential incompatibilities, and no leading space, as different from when this same command is executed via Dolphin ServiceMenu.
Comment 6 David Greengas 2020-10-30 21:23:44 UTC
This is what I get when I run your suggested for loop
rename: not enough arguments
Try 'rename --help' for more information.
rename: not enough arguments
Try 'rename --help' for more information.

This returns 1. This is consistent with what I am getting when running as a service from Dolphin.

I was able to use a similar service file to the one you suggested and loop through the files correctly and call the file command against them.

In order to help with debugging the service definition, I used the logger command to log output the journald. Then I was able to tail the journal via journalctl -f and watch the output from the service being called.

$? is a special variable that has the return code from the last command (in ZSH and BASH). I also used logger to log the return code from the rename command.
Comment 7 Sadi 2020-10-31 09:40:40 UTC
Created attachment 132906 [details]
terminal output and rename package
Comment 8 Sadi 2020-10-31 09:43:07 UTC
(In reply to David Greengas from comment #6)

Strange in deed. I've added another attachment showing how it works in my terminal as well as the package details from my Kubuntu 20.10 system.
Comment 9 Sadi 2020-10-31 09:48:00 UTC
Oh, I've just noticed that recently I was omitting the <horrible>|"awful" part in the filename! Makes no difference though. ;-)
Comment 10 Sadi 2020-10-31 10:08:50 UTC
Created attachment 132908 [details]
terminal output and rename package
Comment 11 Christoph Feck 2020-11-09 23:46:57 UTC
New information was added with recent comments; changing status for inspection.
Comment 12 fanzhuyifan 2023-10-24 02:04:41 UTC
I was able to reproduce the behavior the reporter mentioned (the `rename` program they used is called `perl-rename` on many distros).

Examining the issue further, I noticed that the filename passed to the script is the full path, i.e., something like `/home/user/   a truly <horrible>|"awful" "$'\n'"name     with a newline and *globs*, and even a 'single' quote or two?!   .txt`.

Hence, the white space is no longer at the beginning at the string, and will no longer be removed by the script the reporter provided. 
As passing in the full path is intended behavior, I am closing this report.

As an alternative, the reporter could consider using another script that can handle taking in full paths to files, and intelligently removing spaces in front.
Comment 13 Sadi 2023-10-24 08:01:27 UTC
(In reply to fanzhuyifan from comment #12)

Oh, thank you so much!
I've revised my script as per your advice, and it's working perfectly now:

for filename in "${@}"; do
	source_dir="$(dirname "$filename")"
	source_file="$(basename "$filename")"
	cd "$source_dir"
	rename 's/[*\n<|>[\]]/ /g; s/\?//g; s/!//g; s/"//g; s/\\/ /g; s/ ,/,/g; s/:/-/g; s/\s\s*/ /g; s/^\s\s*//; s/\s\s*\./\./g; '"s/'//g" "$source_file"
done