[SOLVED] Dealing with spaces in file names

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
neek
Level 2
Level 2
Posts: 65
Joined: Wed Feb 27, 2019 2:17 am

[SOLVED] Dealing with spaces in file names

Post by neek »

I'm trying to create a Nemo script that will create a text file containing file names of all selected files, in the format:

Code: Select all

file 'IMG_0315.mp4'
file 'IMG_0316.mp4'
I have the following code which works fine unless there are spaces in any of the file names, in which case the file names with spaces are omitted from the list.

Code: Select all

#!/usr/bin/env bash
for file in "$@" ; do 
files="$files ";
files=""$files"$(basename "$file")"
done
ls $files | sed "s/^/file '/g; s/$/'/g" > file.txt
I have tried modifying the $IFS variable like so:

Code: Select all

SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for file in "$@" ; do 
files="$files ";
files=""$files"$(basename "$file")"
done
IFS=$SAVEIFS
But no luck. Can anyone tell me what I'm missing or doing wrong here?
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Moonstone Man
Level 16
Level 16
Posts: 6078
Joined: Mon Aug 27, 2012 10:17 pm

Re: Dealing with spaces in file names

Post by Moonstone Man »

neek wrote: Thu Apr 08, 2021 2:50 am Can anyone tell me what I'm missing ...
The complete script.

Code: Select all

#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for f in *
  do
    echo "$f"
  done
IFS=$SAVEIFS
That works to correctly expand files with spaces in their names.
Last edited by Moonstone Man on Thu Apr 08, 2021 3:02 am, edited 1 time in total.
neek
Level 2
Level 2
Posts: 65
Joined: Wed Feb 27, 2019 2:17 am

Re: Dealing with spaces in file names

Post by neek »

The full script is just:

Code: Select all

#!/usr/bin/env bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for file in "$@" ; do 
files="$files ";
files=""$files"$(basename "$file")"
done
IFS=$SAVEIFS
ls $files | sed "s/^/file '/g; s/$/'/g" > file.txt
To be executed from the Nemo scripts context menu option when one or more files are selected.
Moonstone Man
Level 16
Level 16
Posts: 6078
Joined: Mon Aug 27, 2012 10:17 pm

Re: Dealing with spaces in file names

Post by Moonstone Man »

neek wrote: Thu Apr 08, 2021 3:02 am The full script is just:

Code: Select all

#!/usr/bin/env bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for file in "$@" ; do 
files="$files ";
files=""$files"$(basename "$file")"
done
IFS=$SAVEIFS
ls $files | sed "s/^/file '/g; s/$/'/g" > file.txt
To be executed from the Nemo scripts context menu option when one or more files are selected.
See my edit above. I hit submit too early.
neek
Level 2
Level 2
Posts: 65
Joined: Wed Feb 27, 2019 2:17 am

Re: Dealing with spaces in file names

Post by neek »

Kadaitcha Man wrote: Thu Apr 08, 2021 2:53 am

Code: Select all

#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for f in *
  do
    echo "$f"
  done
IFS=$SAVEIFS
Thanks. I'm admittedly not very experienced with writing shell scripts. If I use * instead of "$@" I seem to get a list of *all* files in the directory, not just the ones I have selected.
Moonstone Man
Level 16
Level 16
Posts: 6078
Joined: Mon Aug 27, 2012 10:17 pm

Re: Dealing with spaces in file names

Post by Moonstone Man »

neek wrote: Thu Apr 08, 2021 3:12 am Thanks. I'm admittedly not very experienced with writing shell scripts. If I use * instead of "$@" I seem to get a list of *all* files, not just the ones I have selected.
Replace * with $@

Note no quotes.
neek
Level 2
Level 2
Posts: 65
Joined: Wed Feb 27, 2019 2:17 am

Re: Dealing with spaces in file names

Post by neek »

Okay, with script

Code: Select all

#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for f in $@
  do
    echo "$f"
  done
IFS=$SAVEIFS
ls $f | sed "s/^/file '/g; s/$/'/g" > file.txt
I still end up with a text file missing the file names with spaces.
Moonstone Man
Level 16
Level 16
Posts: 6078
Joined: Mon Aug 27, 2012 10:17 pm

Re: Dealing with spaces in file names

Post by Moonstone Man »

neek wrote: Thu Apr 08, 2021 3:24 am I still end up with a text file missing the file names with spaces.
Give me five minutes. I'll test it.
neek
Level 2
Level 2
Posts: 65
Joined: Wed Feb 27, 2019 2:17 am

Re: Dealing with spaces in file names

Post by neek »

Thank you!
Moonstone Man
Level 16
Level 16
Posts: 6078
Joined: Mon Aug 27, 2012 10:17 pm

Re: Dealing with spaces in file names

Post by Moonstone Man »

neek wrote: Thu Apr 08, 2021 3:24 am I still end up with a text file missing the file names with spaces.
This is the code I used:

Code: Select all

#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for f in $@
  do
    echo "$f"
  done
IFS=$SAVEIFS
ls $f | sed "s/^/file '/g; s/$/'/g" > file.txt
This is the result I got:

Code: Select all

file 'akhenaten hosts.log'
file 'baqet.hosts.log'
file 'djehuti hosts.log'
file 'file.txt'
file 'khufu.hosts.log'
file 'semerkhet.hosts.log'
Are you 100% certain that you are editing the script in ~/.local/share/nemo/scripts/?

The only other explanation is that you're looking at the wrong output file.
neek
Level 2
Level 2
Posts: 65
Joined: Wed Feb 27, 2019 2:17 am

Re: Dealing with spaces in file names

Post by neek »

Kadaitcha Man wrote: Thu Apr 08, 2021 3:39 am Are you 100% certain that you are editing the script in ~/.local/share/nemo/scripts/?

The only other explanation is that you're looking at the wrong output file.
I'm 100% certain, see my attached screenshot (it's the 'list files' script, and it has all the relevant execute permissions). And the output file is definitely okay; I can delete it and see it being recreated each time I run the script.

I noticed that it works fine if I right-click an empty space in the directory in Nemo so that all files in that directory are listed, but as soon as I select anything it actually just creates a text file with a single file name in it.
Attachments
Screenshot from 2021-04-08 17-48-27.png
Moonstone Man
Level 16
Level 16
Posts: 6078
Joined: Mon Aug 27, 2012 10:17 pm

Re: Dealing with spaces in file names

Post by Moonstone Man »

neek wrote: Thu Apr 08, 2021 3:57 am I noticed that it works fine if I right-click an empty space in the directory in Nemo so that all files in that directory are listed, but as soon as I select anything it actually just creates a text file with a single file name in it.
Well, that's to be expected. Nemo is simply passing your selection to the script. Your script needs to check what it's being passed, then it needs to decide what to do about what it's been passed. For example, if it's passed a directory, what should it do? If it's passed a single file, what should it do? If it's passed multiple files, what should it do?

Your original problem is clearly solved. What remains are design problems that you need to work out.
neek
Level 2
Level 2
Posts: 65
Joined: Wed Feb 27, 2019 2:17 am

Re: Dealing with spaces in file names

Post by neek »

Kadaitcha Man wrote: Thu Apr 08, 2021 4:04 am
Well, that's to be expected. Nemo is simply passing your selection to the script. Your script needs to check what it's being passed, then it needs to decide what to do about what it's been passed. For example, if it's passed a directory, what should it do? If it's passed a single file, what should it do? If it's passed multiple files, what should it do?

Your original problem is clearly solved. What remains are design problems that you need to work out.
Okay, but my original script was already doing that; it was taking only files that are selected and sending the file names to a text file. That worked, except in the case of file names with spaces in them. So I'm not really sure my original problem has been solved (i.e., the new script is outputting basically the same as the original script)
neek
Level 2
Level 2
Posts: 65
Joined: Wed Feb 27, 2019 2:17 am

Re: Dealing with spaces in file names

Post by neek »

To clarify, the newer script is creating a text file with only a single file name in it, regardless of how many files are selected, and won't include any files with a space in the name. The original script would create a text file with all files except those with spaces in the name.

If nothing is selected, both scripts will correctly list everything in the directory in the text file (which is not what I'm interested in).
Moonstone Man
Level 16
Level 16
Posts: 6078
Joined: Mon Aug 27, 2012 10:17 pm

Re: Dealing with spaces in file names

Post by Moonstone Man »

neek wrote: Thu Apr 08, 2021 4:37 am If nothing is selected, both scripts will correctly list everything in the directory in the text file ...
I already told you what you need to do.
Your script needs to check what it's being passed...
You then need to...
... decide what to do about what it's been passed.
(which is not what I'm interested in).
Not now since the original space problem has been solved. Your original problem statement was:
I have the following code which works fine unless there are spaces in any of the file names, in which case the file names with spaces are omitted from the list.
So, you've now moved the goalposts. I don't like being given the run around.
neek
Level 2
Level 2
Posts: 65
Joined: Wed Feb 27, 2019 2:17 am

Re: Dealing with spaces in file names

Post by neek »

Sorry, I appreciate the help, but I don't understand what you think has been solved here. My original problem (emphasis mine):
I'm trying to create a Nemo script that will create a text file containing file names of all selected files
...
I have the following code which works fine unless there are spaces in any of the file names
That's still the problem.
Your script needs to check what it's being passed...
I don't understand what you mean by this. Again, my shell scripting experience is limited.
Moonstone Man
Level 16
Level 16
Posts: 6078
Joined: Mon Aug 27, 2012 10:17 pm

Re: Dealing with spaces in file names

Post by Moonstone Man »

neek wrote: Thu Apr 08, 2021 5:20 am
Your script needs to check what it's being passed...
I don't understand what you mean by this.
The script works fine provided that it's used in a particular way. It doesn't give you the desired results under some other conditions.

Yes/no/true/false?

Those other conditions are caused by not invoking the script in one or more particular ways from within nemo.

Yes/no/true/false?

The script relies on the input it receives from nemo, so, in your script, you need to inspect the input that has been passed when it is called from nemo. then the script needs to take appropriate action based on that input. You need to work that out. I have now told you that three times in three different ways.
Again, my shell scripting experience is limited.
I'm not writing it for you. I've given you all the clues you need to solve it yourself. Spoon-feeding is not part of my job description.
neek
Level 2
Level 2
Posts: 65
Joined: Wed Feb 27, 2019 2:17 am

Re: Dealing with spaces in file names

Post by neek »

Ugh, I don't why I was using a for loop at all. This one line script does the job:

Code: Select all

#!/usr/bin/env bash
ls "$@" | sed "s/^/file '/g; s/$/'/g" > file.txt
Problem solved. Thanks for the replies.
Locked

Return to “Scripts & Bash”