Little
1
hi,
i have a variable, in which i am storing the file names that are present in the current directory.
$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
$echo $FILES
s1.txt s2.txt s3.txt
is there a way??
Yoda
2
Simply run ls
without any options:
FILES="$( ls )"
OR
FILES="$( ls *.txt )"
Little
3
ls will take directory also.. n there can be other type of files also..
Yoda
4
FILES=$( ls -ltr | awk '/^-/{$0=$NF;ORS=FS;print}' )
Jotne
5
or
FILES=$( ls -ltr | awk '/^-/{ORS=FS;print $NF}' )
RudiC
6
FILES=$(find -name \*.txt -printf "%f ")
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
set -f; echo $FILES
for file in FILES; do
Little
8
thanks all of you for these many solutions..