Expanding a list of wildcard filenames with spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Expanding a list of wildcard filenames with spaces
# 1  
Old 02-15-2016
Expanding a list of wildcard filenames with spaces

I think I must be missing something obvious but I have a file containing a list of files and paths, some with wildcard, others with spaces. e.g.

Code:
/this/is/a/file
/this/is/a/directory/
/this/is/a/collection/*
/this/has spaces/in/it
/this/had spaces/and/list/of/files*
~/and/a/relative/file.txt

I need a script in sh or bash to expand this list of all matching files, something like:

Code:
while IFS= read -r  FILENAME; do ls -d1 $FILENAME; done <  ~/files.lst

The above example though will treat a space in any filename as separating the parameters, causing ls to see /this/had and space/in/it as separate files to list.

Escaping or quoting the entries with spaces within files.lst simply sees the quote or slash treated as being part of the file name, and so the space still separates the parameter.

And quoting $FILENAME in the ls command obviously solves the problem with spaces, but it then treats the wildcard as a filename character instead.

What am I missing to be able to take a variable containing a path with a space and a wildcard and expand it?

Thanks.
# 2  
Old 02-16-2016
That's two problems that you can't solve with one generic approach. For your special setup above, try
Code:
while IFS= read -r  FILENAME; do echo "$FILENAME"; ls -d1 "${FILENAME%\*}"${FILENAME#${FILENAME%\*}}; done < file

This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-16-2016
You can set IFS="" for the entire loop, then it works for the read and the ls
Code:
(
IFS=
while read -r  FILENAME; do ls -d1 $FILENAME; done <  ~/files.lst
)
# subshell ended. The original IFS is back.

This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 02-16-2016
I knew it had to be something simple, I did not realize it was just a case of ls also needing the IFS to be null too. For my needs both solutions work perfectly but that version is the most flexible.

Thank you so much to you both.
# 5  
Old 02-16-2016
The read command really uses the IFS.
The ls command does not use it, but the IFS lets the shell process the command arguments differently.
Thank you for this interesting question!
# 6  
Old 02-16-2016
To be a bit more more specific, since IFS is set to the empty string, the expansion of the variable FILENAME, $FILENAME is not being split into fields by the shell and will be passed as a single field to the ls command, while wildcard expansion (globbing) is still being performed.
# 7  
Old 02-16-2016
In a way it is only the shell using the IFS as read is a built-in command, at least for the shells I use.

Although I take your point, MadeInGermany. It affects how read interprets its input but that ls has no awareness of it as, as Scrutinizer says, it instead affects what the shell passes to it.

Thanks for the explanations, always more helpful to understand the hows and whys. (And thanks for showing me the icode tag!)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to list and move files with spaces and wildcard?

I am writing a code that can move and archve all the files in a directory except the latest file based on file pattern provided in a controlfile. The filename is in the form of pattern. So basically we find the all the files of the pattern provided and archive all of them, leaving one latest file.... (3 Replies)
Discussion started by: Saanvi1
3 Replies

2. UNIX for Advanced & Expert Users

ls -ltr a list of filenames-with-spaces within a text file

OS: RHEL 5.8 shell: bash 3.2.25 Directory /home/guest/ contains these files: file a file b file c fileD fileE fileF testFile.txt I'm trying to find the syntax to run ls -ltr against this list of files that is contained within a text file, testFile.txt. The file testFile.txt has... (4 Replies)
Discussion started by: uschaafm
4 Replies

3. Shell Programming and Scripting

How to print the last column of the list contain filenames with spaces.

Hi experts, I have the following data: I want the last filed in the output. How to print the last field , It contains the file names and few filenames with white spaces . -rw-r--r-- 1 root root 0 2010-04-26 16:57 file1 2space_File.txt -rw-r--r-- 1 root root 0 2010-04-26... (2 Replies)
Discussion started by: rveri
2 Replies

4. Shell Programming and Scripting

Moving filenames containing spaces

I want to ftp all the sh files in the directory. Also if any of the file name contains spaces in them, it should be converted to underscores before it is ftped. I wrote the following code below: FILESSH=$(ls /mysh/*.sh) --- FILESH being used here for some other task --- echo "$FILESSH" |... (3 Replies)
Discussion started by: amicon007
3 Replies

5. Shell Programming and Scripting

spaces in filenames

Hi I hope someone will be able to resolve this little teaser! I am running a script for file in `ls directory` do echo "$file" ...other code here.... done this works fine unless we receive a file with a name which has a space in it ie "filena me" (I know its not good... (8 Replies)
Discussion started by: Bab00shka
8 Replies

6. Shell Programming and Scripting

How to list filenames with spaces in shell script

Hi, I want to list all the files matching in a directory directory given below Here one of the folder has a space in the path. /MAS02/RMS/WBDev/Krishna/Krishna Mohan/FMSplitByKeyAfterChanges1000075383.fileman.*.txt.out.1000075383.0 The following works from command line... (1 Reply)
Discussion started by: hikrishn
1 Replies

7. Shell Programming and Scripting

spaces in filenames, for do

Hi All, I see similar problems in past threads but so far no answers have worked for me. I am trying to write a script which parses a txt file that contains one filename per line, then finds those files on the local disk and copies them to a specified directory. What I have: ... (4 Replies)
Discussion started by: naviztirf
4 Replies

8. Shell Programming and Scripting

Unix filenames and spaces

I have files on my unix boxes that users have created with spaces. Example: /tmp/project plan ls -l "/tmp/project plan" works fine. $/tmp>ls -l "/tmp/project plan" -rw-r--r-- 1 root other 0 Jan 31 12:32 /tmp/project plan I created a file called test and put just the... (2 Replies)
Discussion started by: x96riley3
2 Replies

9. Shell Programming and Scripting

spaces in filenames

I have a problem with the script below #!/bin/sh for vo in `find -maxdepth 1 -type f -regex "^\./*$"` do ls -l "$vo" some other commands done It works fine until `find ...` returns files with spaces. I've tryed to change IFS but haven't succeed Any solutions? (4 Replies)
Discussion started by: Hitori
4 Replies

10. UNIX for Dummies Questions & Answers

wildcard for spaces

I want to do a search and replace in a file but there may or may not be one space, two or none. i.e.: test&nbsp;3,1 test&nbsp;&nbsp; 3,1 test,3,1 test&nbsp;&nbsp;&nbsp;3,1 The output I will be looking for is this: test&nbsp;3,2 test&nbsp;&nbsp;3,2 test,3,2... (6 Replies)
Discussion started by: Shakey21
6 Replies
Login or Register to Ask a Question