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


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users ls -ltr a list of filenames-with-spaces within a text file
# 1  
Old 11-27-2012
[Solved] 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
Code:
ls -ltr

against this list of files that is contained within a text file, testFile.txt.

The file testFile.txt has the contents below, with file names within quotes and with files space delimited.

"/home/guest/file a" "/home/guest/file b" "/home/guest/file c" "/home/guest/fileF" "/home/guest/fileE" "/home/guest/fileD"

All attempts are failing upon encountering whitespace.
1.
Code:
ls -ltr `cat testFile.txt | xargs -0` 
 ls: "/home/guest/file: No such file or directory
 ls: a": No such file or directory
 ls: "/home/guest/file: No such file or directory
 ls: b": No such file or directory
 ls: "/home/guest/file: No such file or directory
 ls: c": No such file or directory
 ls: "/home/guest/fileF": No such file or directory
 ls: "/home/guest/fileE": No such file or directory
 ls: "/home/guest/fileD": No such file or directory

2. Same error from
Code:
ls -ltr `awk '{print}' /home/guest/testFile.txt

3. Same error from
Code:
cat testFile.txt | while read i ; do ls -ltr $i ; done

Any assistance would be greatly appreciated. Thanks!
# 2  
Old 11-27-2012
Code:
while read i
do
   ls -ltr "$i"
done < testFile.txt

# 3  
Old 11-27-2012
Code:
ls -ltr $(sed -e 's|\" \"|\n|g' -e 's|"||g' testFile.txt)

# 4  
Old 11-27-2012
Try:
Code:
xargs ls -ltr < testFile.txt

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 11-28-2012
Thanks, Scrutinizer! That worked perfectly.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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. /this/is/a/file /this/is/a/directory/ /this/is/a/collection/* /this/has spaces/in/it /this/had spaces/and/list/of/files*... (6 Replies)
Discussion started by: mij
6 Replies

2. Shell Programming and Scripting

I have two commands “ls -h” and “ls -ltr”. How do i make sure “ls -ltr” is run after “ls -h” is suc

help me (2 Replies)
Discussion started by: sonu pandey
2 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

Adding specific text and spaces to each line in a text file

Hi, I wanted to add specific text to each row in a text file containing three rows. Example: 0 8 7 6 5 5 7 8 9 0 7 9 7 8 9 0 1 2 And I want to add a 21 at the beginning of the first row, and blank spaces at the beginning of the second two rows. To get this: 21 0 8 7 6 5 5 7 8... (4 Replies)
Discussion started by: hertingm
4 Replies

5. Shell Programming and Scripting

read list of filenames from text file and remove these files in multiple directories

I have a large list of filenames from an Excel sheet, which I then translate into a simple text file. I'd like to use this list, which contains various file extensions , to archive these files and then remove them recursively through multiple directories and subdirectories. So far, it looks like... (5 Replies)
Discussion started by: fxvisions
5 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

read list of filenames from text file, archive, and remove

I posted a week ago regarding this scripting question, but I need to revisit and have a few more questions answered.. User cfajohnson was extremely helpful with the archive script, but clarification on my part is needed to help steer the answer in a direction that works in this particular... (5 Replies)
Discussion started by: fxvisions
5 Replies

9. UNIX for Dummies Questions & Answers

what does the ls -ltr command list

Hi, THe following is the output when i run the command ls -ltr can anyone explain the meaning of the field in red -rw-r----- 3 orca orca 20924 Sep 08 19:21 BTL027SASI.gnt -rw-r----- 3 orca orca 20924 Sep 08 19:21 BTL027RITD.gnt -rw-r----- 3 orca orca ... (2 Replies)
Discussion started by: ranjita.c
2 Replies

10. 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
Login or Register to Ask a Question