Visit Our UNIX and Linux User Community


How to store the file name in a single line??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to store the file name in a single line??
# 1  
Old 08-10-2013
How to store the file name in a single line??

hi,
i have a variable, in which i am storing the file names that are present in the current directory.
Code:
$ls -1
s1.txt
s2.txt
s3.txt

$FILES=`ls -ltr | grep "^-" | awk '{print $NF}'`
$echo $FILES
s1.txt
s2.txt
s3.txt

i want to store the files names in a single line in the variable FILES delimited by a space.
eg
Code:
$echo $FILES
s1.txt s2.txt s3.txt

is there a way??
# 2  
Old 08-10-2013
Simply run ls without any options:
Code:
FILES="$( ls )"

OR
Code:
FILES="$( ls *.txt )"

# 3  
Old 08-10-2013
ls will take directory also.. n there can be other type of files also..
# 4  
Old 08-10-2013
Code:
FILES=$( ls -ltr | awk '/^-/{$0=$NF;ORS=FS;print}' )

# 5  
Old 08-11-2013
or
FILES=$( ls -ltr | awk '/^-/{ORS=FS;print $NF}' )
# 6  
Old 08-11-2013
Code:
FILES=$(find -name \*.txt -printf "%f ")

# 7  
Old 08-11-2013
Code:
FILES=$(find -maxdepth 1 -type f -name \*.txt)

I recommend to store them like this - newline separated!
You have the option to convert to a space-separated list on the fly
Code:
set -f; echo $FILES
for file in FILES; do


Previous Thread | Next Thread
Test Your Knowledge in Computers #69
Difficulty: Easy
Grep stands for 'global regular expression process'.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Output to file print as single line, not separate line

example of problem: when I echo "$e" >> /home/cogiz/file.txt result prints to file as:AA BB CC I need it to save to file as this:AA BB CC I know it's probably something really simple but any help would be greatly appreciated. Thank You. Cogiz (7 Replies)
Discussion started by: cogiz
7 Replies

2. UNIX for Dummies Questions & Answers

To find and display the middle line in a file using single line command.

Hi all, How can i display the middle line of a file using a single line command? (6 Replies)
Discussion started by: Lakme Pemmaiah
6 Replies

3. Shell Programming and Scripting

Execute sequential files and store data in single file

1)In a particualr path i have a set of inputfiles like path:/defaultmis/MonthlyLoads/INFA_EXPORT_022013/map* example: 1)map_de 2)map_cod 3)map_feg ........and so on in above path there wil be nearly 15 to 20 files starting with map and in other path i have another file input file... (4 Replies)
Discussion started by: katakamvivek
4 Replies

4. Shell Programming and Scripting

Formatting File having big single line into 95 Char Per Line

Hi All, I have 4 big files which contains one big line containing formatted character records, I need to format each file in such way that each File will have 95 Characters per line. Last line of each file will have newline character at end. Before:- File Name:- File1.dat 102 121340560... (10 Replies)
Discussion started by: lancesunny
10 Replies

5. UNIX for Dummies Questions & Answers

Writing a script that will take the first line from each file and store it in an output file

Hi, I have 1000 files names data1.txt through data1000.txt inside a folder. I want to write a script that will take each first line from the files and write them as output into a new file. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

6. Shell Programming and Scripting

Parse a single line file and store value.

I have a single line file like this : Average Fragmentation Quotient : 3.084121 Now I want to store the value which comes after ":" i,e 3.084121 into a variable. And if this variable crosses above 6 i want to call another script... can any one help me on this... (7 Replies)
Discussion started by: Hyp_Todd
7 Replies

7. Shell Programming and Scripting

awk concatenate every line of a file in a single line

I have several hundreds of tiny files which need to be concatenated into one single line and all those in a single file. Some files have several blank lines. Tried to use this script but failed on it. awk 'END { print r } r && !/^/ { print FILENAME, r; r = "" }{ r = r ? r $0 : $0 }' *.txt... (8 Replies)
Discussion started by: sdf
8 Replies

8. Shell Programming and Scripting

How to read a file line by line and store it in a variable to execute a program ?

Hello, I am quite new in shell scripting and I would like to write a little scritp to run a program on some parameters files. all my parameters files are in the same directory, so pick them up with ls *.para >>dirafter that I have a dir file like that: param1.para param2.para etc... I... (2 Replies)
Discussion started by: shadok
2 Replies

9. Shell Programming and Scripting

store the first line of a file in a variable

i have to store the files in a folder and assign a variable to the the files. (0 Replies)
Discussion started by: dineshr85
0 Replies

10. UNIX for Dummies Questions & Answers

Read and store each line of a file to a variable

Hi all, I'm quite new to unix and hope that someone can help me on this. I'm using csh. Below is what i intend to do. 1. I stored some data in a file. 2. I intend to read the file line by line and store each line of data into a variable, so that i can used it later. Anyone have any... (4 Replies)
Discussion started by: seijihiko
4 Replies

Featured Tech Videos