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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get filenames in a directory and write them in to a file?
# 1  
Old 12-25-2011
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:
Code:
<path>
   <dir>
         <file>testfile1</file>
   </dir>
   <dir>
         <file>testfile2</file>
   </dir>
<path>

# 2  
Old 12-25-2011
bash code:
  1. #! /bin/bash
  2. echo "<path>" >> file.xml
  3. for x in `ls filename*`
  4. do
  5.     echo -e "\t<dir>" >> file.xml
  6.     echo -e "\t\t<file>$x</file>" >> file.xml
  7.     echo -e "\t<dir>" >> file.xml
  8. done
  9. echo "</path>" >> file.xml
This User Gave Thanks to balajesuri For This Post:
# 3  
Old 12-25-2011
Thanks a lot. It worked perfectly.

---------- Post updated at 08:55 AM ---------- Previous update was at 08:44 AM ----------

How can i append a constant string to the filename?
Code:
<dir>
       <file>location/testfile1</file>
       <date>2004-10-01T18:23:17+00:00<date>
 </dir>

Also please suggest me how can i get current date in the below format and append it as a date in the XML file.

Last edited by vel4ever; 12-25-2011 at 10:05 AM..
# 4  
Old 12-25-2011
bash code:
  1. #! /bin/bash
  2. echo "<path>" >> file.xml
  3. for x in `ls test*`
  4. do
  5.     echo -e "\t<dir>" >> file.xml
  6.     echo -e "\t\t<file>location/$x</file>" >> file.xml
  7.     echo -e "\t\t<date>`date`</date>" >> file.xml
  8.     echo -e "\t<dir>" >> file.xml
  9. done
  10. echo "</path>" >> file.xml
This User Gave Thanks to balajesuri For This Post:
# 5  
Old 01-04-2012
Is there any i can achieve entity escaping, URL escaping & UTF-8 encoded for the xml generated through shell script?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

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

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

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

4. UNIX for Advanced & Expert Users

Extracts files names and write those to another file in different directory

Hi , Need to shell script to extracts files names and write those to another file in different directory. input file is inputfile.txt abc|1|bcd.dat 123 david123 123 rudy2345 124 tinku5634 abc|1|def.dat 123 jevid123 123 qwer2345 124 ghjlk5634 abc|1|pqr.txt 123 vbjnnjh435 123 jggdy876... (9 Replies)
Discussion started by: dssyadav
9 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

Write filenames into an array variable

Hello there, I am a newbie to unix and i have been trying to code a shell script for bash shell which reads the files in a folder using ls and writes them into an array variable. But, i am running into several errors while running the script. The script is given below: #!/bin/bash... (16 Replies)
Discussion started by: amrutha0303
16 Replies

7. Shell Programming and Scripting

How to find empty files in a directory and write their file names in a text?

I need to find empty files in a directory and write them into a text file. Directory will contain old files as well, i need to get the empty files for the last one hour only. (1 Reply)
Discussion started by: vel4ever
1 Replies

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

9. Programming

How to read and write directory or file contents in c++ ?

Dear Friends, I m beginner unix programmer. I want to know, how to read and write directory or file contents in c++ ? (3 Replies)
Discussion started by: namrata5
3 Replies
Login or Register to Ask a Question