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
-rw-rw-r--  1 adm etc    6 Aug  6 20:37 s3.txt
drwxrwxr-x  2 adm etc 4096 Aug  7 21:23 ARC

i want to select only the filenames excluding the directories and send the file names to a temp file. how can i do this. the output should be:

cat filelist
s1.txt
s2.txt
s3.txt

Hello,

Could you please use the following code. We are saving file names into a file named filelist as follows.

 
 ls -ltr | grep '^-' | awk '{print$9}' > filelist
 

You will get the following Ouptut in a file named filelist.

 
$ cat filelist
without_length_70_lines
value_braces
to_find_min_and_max_number
timings_file_query.ksh
timings_file_query
test_data
test_chumma1
test5
test4
test13
test1212
test1211
test121
test11
test1
test.ksh

Thanks,
R. Singh

Can be written like this

ls -ltr | awk '/^-/ {print$9}' > filelist

is it always 9 column when we use ls -ltr? i tried this before but for some location i was getting it correctly and sometimes it was picking up some other column. is there a way so that i can always get the last column instead of hard coding the column number? i tried using

ls -ltr | grep "^-" | awk '{print $NF}' > filelist

output:

cat filelist
s1.txt
s2.txt
s3.txt

it gives the correct output but when i specify the path name like

ls -ltr /home/* | grep "^-" | awk '{print $NF}' > filelist

output:

/home/s1.txt
/home/s2.txt
/home/s3.txt
 

Hello,

It's because you are giving

 /home/*

if you will give only

home/

then it should not give the path with file names.

Actually what this

/home/* 

will do it will look for further directories in Home and check the files there too, so to make a differance between the other directory files and Home directory files it shows complete path as

/home/s1.txt

for files which are presnet there.

Please let me know if this helps or if you need any assistance please.

Thanks,
R. Singh

the above thing works, but some times i want to take only .txt files then i need to specify the /home/*.txt to take only .txt files.

Hello,

Let me share an example with you hope this will help you more.
Let's say I want to see all ksh files at "/home/user/bin/singh_testing/*.ksh
same you can put here .txt in place of ksh please.

ls -ltr /home/user/bin/singh_testing/*.ksh | awk -F"/home/user/bin/singh_testing/" '{print$2}'

Output will be as follows.

check.ksh
test_testing.ksh
check_file.ksh
diff.ksh
checking_awk1.ksh
diff1.ksh
check1_awk1.ksh
champaa112.ksh
chu1.ksh
$

Please try the same using txt and let me know if it helps.

Thanks,
R. Singh

1 Like

ya it works, thanks.. you have used the path a delimeter. but thats fine. thanks

since you know that your *.ksh files are not directories, you can shorten it further ...

cd /dir
ls *.ksh

or

ls /dir/*.ksh | sed "s/\/dir\///"
for  file in *
do
        [ "$file" = "*" ] && continue # no files
        [ "$0" = "$file" ] && continue # myself
        [ -d "$file" ] && continue  # dir
        [ -f "$file" ] && echo "$file"  # regular file
        # [ ....   # other testing what you maybe need / are useful
done > result.txt

shorter version ...

for file in *
do
    [ -f $file ] && echo $file
done

Try also

find /home/ -name \*.txt -printf "%f\n"