Visit Our UNIX and Linux User Community


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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to take the filenames from a directory and store into a file??
# 1  
Old 08-08-2013
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.
Code:
$ 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:
Code:
cat filelist
s1.txt
s2.txt
s3.txt

# 2  
Old 08-08-2013
Hello,

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

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

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

Code:
 
$ 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
# 3  
Old 08-08-2013
Can be written like this
Code:
ls -ltr | awk '/^-/ {print$9}' > filelist

# 4  
Old 08-08-2013
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
Code:
ls -ltr | grep "^-" | awk '{print $NF}' > filelist

output:
Code:
cat filelist
s1.txt
s2.txt
s3.txt

it gives the correct output but when i specify the path name like
Code:
ls -ltr /home/* | grep "^-" | awk '{print $NF}' > filelist

output:
Code:
/home/s1.txt
/home/s2.txt
/home/s3.txt

# 5  
Old 08-08-2013
Hello,

It's because you are giving
Code:
 /home/*

if you will give only
Code:
home/

then it should not give the path with file names.

Actually what this
Code:
/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
Code:
/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
# 6  
Old 08-08-2013
Quote:
if you will give only
Code:
home/

then it should not give the path with file names.
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.
# 7  
Old 08-08-2013
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.

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

Output will be as follows.

Code:
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

Last edited by RavinderSingh13; 08-08-2013 at 02:48 AM.. Reason: a slit path change.
This User Gave Thanks to RavinderSingh13 For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #105
Difficulty: Easy
The programming interface, file abstraction, built-in networking and persistent background processing are all features and capabilities supported by a Unix or Linux OS.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to manipulate a text file and store each version for every changes in a directory?

I attached both picturehttps://1drv.ms/t/s!Aoomvi55MLAQh1jODfUxa-xurns_ and *.txt file of a sample work file. In this file Reactions which only start with "r1f", "r2f", "r3f"......and so on. And for each reaction the reaction rates is situated couple of lines later with a "+" sign. For each... (1 Reply)
Discussion started by: Atta
1 Replies

2. UNIX for Beginners Questions & Answers

How do I custom sort the files in a directory using the filenames in a text file.?

Hi all, (5 Replies)
Discussion started by: KMusunuru
5 Replies

3. Shell Programming and Scripting

Store filenames for wget in bash

I have a bash that downloads a list of files as a text file using wget. What I now need to do is store those files names and pass them to a download call also using wget. List.txt in /home directory FilterDuplicates.html file1.bam file2.bam file3.bam file1.vcf.gz file2.vcf.gz... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

There are multiple filenames in the directory.How to return the the lastest files for each file name

there are mutiple file nams in the directory. How to return the the lastest files for each file name. ex. abc1234_050201 abc1234_050206 abc1234_050208 xyz34_050204 xyz34_050210 xyz34_050218 thanks (4 Replies)
Discussion started by: grand_sam
4 Replies

5. Shell Programming and Scripting

print all filenames in directory with path to another file

hi, i have a directory at /path/unix with the following files 1.txt 2.txt 3.txt 4.txt I want to make another file called filenames.txt at a different location called /path/home. So, my output file would be /path/home/filenames.txt with contents /path/unix/1.txt... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

6. Shell Programming and Scripting

How to get filenames in a directory and write them in to a file?

I need to get the names of files which are starting with a string testfile. Also i want to create a XML file in the same location and write these file names into the XML. Ex: <path> <dir> <file>testfile1</file> </dir> <dir> <file>testfile2</file> </dir>... (4 Replies)
Discussion started by: vel4ever
4 Replies

7. Shell Programming and Scripting

New file should store all the 7 existing filenames and their record counts and ftp th

Hi, I need help regarding below concern. There is a script and it has 7 existing files(in a path say,. usr/appl/temp/file1.txt) and I need to create one new blank file say “file_count.txt” in the same script itself. Then the new file <file_count.txt> should store all the 7 filenames and... (1 Reply)
Discussion started by: pr293
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

Replacing string in all instances (both filenames and file contents) in a directory

Hi, I have a set of files stored in a single directory that I use to set parameters for a physics code, and I would like to streamline the process of updating them all when I change a parameter. For instance, if the files are called A2000p300ini, A2000p300sub, A2000p300run, and the text in each... (3 Replies)
Discussion started by: BlueChris
3 Replies

10. 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

Featured Tech Videos