Getting filenames from a directory in single line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting filenames from a directory in single line
# 1  
Old 10-04-2010
Getting filenames from a directory in single line

Hi All,

I have a requirement where I need to get the all file names present in a particular directory in to a single separated by space,

I have files in /home/RAAM/work directory as
test1.xls
test2.xls
test3.xls

I need to get all filenames from that directory in single line like
test1.xls test2.xls test3.xls

I am using the following commend
ls -ltr /home/RAAM/work/*.xls | awk '{print $9}' |cut -d'/' -f5

It is giving the output like
test1.xls
test2.xls
test3.xls

that means in separate lines it is giving. How can I remove the new line character from cut output without using any temporary variables?

I used the following to remove /n
ls -ltr /home/rameshch/bhanu/*.xls | awk '{print $9}' |cut -d'/' -f5 | tr -d'\n'

it is erroring out. Can any one please help?
# 2  
Old 10-04-2010
In awk, the print command will always add a newline, use printf() instead.

Code:
ls | awk ' { printf( "%s ", $NF ) } END { printf( "\n" ); }'

Also, you can 'cut' away the path with a simple sub() function call in the awk rather than adding the extra overhead required by using cut.

Code:
sub( ".*/", "", $NF );


Last edited by agama; 10-04-2010 at 10:29 PM.. Reason: typo
# 3  
Old 10-04-2010
Hi Agama,

Thank you very much, that worked.

I need one more small clue, I got the output like this
/home/RAAM/work/test1.xls /home/RAAM/work/test2.xls

I need to get only filenames with out directory structure, can you please help.


Thank you very much for quick response.

--Thanks,
--Raam.
# 4  
Old 10-04-2010
Or just use the shell wildcard expansion

Code:
FILES=$( cd /home/RAAM/work/ ; echo *.xls )

or simply the following if you dont need files in a variable

Code:
( cd /home/RAAM/work/ ; echo *.xls )


Last edited by Chubler_XL; 10-04-2010 at 10:43 PM.. Reason: Added non variable solution
This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 10-04-2010
Updated from Agama's code, that you need understand the FS (field separator) in awk.
Code:
awk -F \/ '{ printf( "%s ", $NF ) } END { printf( "\n" ); }'

but Chubler_XL's code is more simple.
# 6  
Old 10-04-2010
Thank you all,

It is working as it is required.

Appreciate your help.

--Thanks
--Raam
# 7  
Old 10-05-2010
Quote:
Originally Posted by Raamc
I used the following to remove /n
ls -ltr /home/rameshch/bhanu/*.xls | awk '{print $9}' |cut -d'/' -f5 | tr -d'\n'

it is erroring out. Can any one please help?
For what it's worth, I think the error in this attempt was simply that tr -d'\n' needs a space after the d.
Code:
(my ls -ltr only gives 8 columns so im using awk '{print $8}' )

# ls -ltr *.xls | awk '{print $8}' |cut -d'/' -f5 | tr -d'\n'
tr: invalid option -- '\'
Try `tr --help' for more information.

# ls -ltr *.xls | awk '{print $8}' |cut -d'/' -f5 | tr -d '\n'
test3.xlstest2.xlstest1.xls#

With the space, it doesn't error, but it shows that you'd need to use tr '\n' ' ' to replace newlines with a space instead of just deleting newlines. Of course "echo *.xls" is much more elegant, but I think your only trouble was that one little space.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing filenames in my current directory

Can someone give me a tip on writing a script that, for each file in the working directory, prints the filename, the # of lines, and the # of words to stdout? (2 Replies)
Discussion started by: flash123
2 Replies

2. Shell Programming and Scripting

How to take the filenames from a directory and store into a file??

hi, how can i take the file names from a directory and store only the filenames in the file. suppose i have a directory which contains the following files and subdirectories. $ ls -ltr total 16 -rw-rw-r-- 1 adm etc 4 Aug 6 20:37 s1.txt -rw-rw-r-- 1 adm etc 4 Aug 6 20:37 s2.txt... (11 Replies)
Discussion started by: Little
11 Replies

3. Shell Programming and Scripting

Change to directory and search some file in that directory in single command

I am trying to do the following task : export ENV=aaa export ENV_PATH=$(cd /apps | ls | grep $ENV) However, it's not working. What's the way to change to directory and search some file in that directory in single command Please help. (2 Replies)
Discussion started by: saurau
2 Replies

4. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

5. Shell Programming and Scripting

for $word in $line returns filenames in the current directory unexpectedly

I am writing a script below, which has 2 loops. The outer one reads file sufffixed with a number and inner inside which loop through each line of the file and display each space delimited string. However, i find that the string printed out in the inner loop includes not only the delimited string in... (2 Replies)
Discussion started by: martie
2 Replies

6. UNIX for Dummies Questions & Answers

Filenames change in a directory

Hi I have abc_ahb_one.v abc_ahb_two.v abc_ahb_three.v ........l like this -----upto abc_ahb_ninety.v in some directory. I need to change those file names to like below. ... (5 Replies)
Discussion started by: praneethk
5 Replies

7. Shell Programming and Scripting

Use filenames to create directory.

I have many files similar to this one: AC41_AC85_86_AC128_129_MC171_173_SF_207_FMV.pdf. I want a directory named AC41 and to put the file AC41_AC85_86_AC128_129_MC171_173_SF_207_FMV.pdf into the directory. Next, a directory named AC85 and put the file into it. Also, continue to cycle through... (1 Reply)
Discussion started by: ndnkyd
1 Replies

8. Shell Programming and Scripting

Change all filenames in a directory

I have a directory of files and each file has a random 5 digit string at the beginning that needs to be removed. Plus, there are some files that will be identically named after the 5 digit string is removed and I want those eliminated or moved. any ideas? (17 Replies)
Discussion started by: crumb
17 Replies

9. Shell Programming and Scripting

concatenating the filenames in a directory

hi all, I have a requirement where in i have to read all the filenames based on a pattern from a directory and concatenate all these file names and write it to another file. i am using the following code to do this var1='' for filename in $_DIR/${FILE_NAME}* do if if then... (7 Replies)
Discussion started by: nvuradi
7 Replies

10. Shell Programming and Scripting

looping thru filenames in a directory

Hi, i am very new to UNIX, i am trying to loop thru the files in a directory. I got the filenames into a variable using $files=`ls` Here $files will contain <filename1> <filename2> <filename3> I want to get one filename at a time and append it to some some text. forexample, ... (1 Reply)
Discussion started by: silas.john
1 Replies
Login or Register to Ask a Question