Create a list of directory contents


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create a list of directory contents
# 1  
Old 03-09-2011
Create a list of directory contents

Well I did a search and didn't anything for my specific case.

I got a directory with a bunch of text file. All of them have the following pattern on the filename "ABCD_<As of Date>.txt"
Example:
ABCD_20110301.txt
ABCD_20110302.txt
ABCD_20110303.txt



All I want to accomplish is a Korn Shell Script that outputs the list separated with a comma. I know that is very easy to accomplish but I'm having a very hard time understanding shell scripting.

Output Example:
ABCD_20110301.txt,ABCD_20110302.txt,ABCD_20110303.txt


Thank you all for your help.
# 2  
Old 03-09-2011
You could pipe your ls output through paste. i.e.:
Code:
ls ABCD_* | paste -sd, -

Or (if your ls command has the -m option):
Code:
ls -m ABCD_*

This User Gave Thanks to Scott For This Post:
# 3  
Old 03-09-2011
Quote:
Originally Posted by Shark Tek
Well I did a search and didn't anything for my specific case.

I got a directory with a bunch of text file. All of them have the following pattern on the filename "ABCD_<As of Date>.txt"
Example:
ABCD_20110301.txt
ABCD_20110302.txt
ABCD_20110303.txt



All I want to accomplish is a Korn Shell Script that outputs the list separated with a comma. I know that is very easy to accomplish but I'm having a very hard time understanding shell scripting.

Output Example:
ABCD_20110301.txt,ABCD_20110302.txt,ABCD_20110303.txt
Code:
list=$(printf "%s," *.txt)
printf "%s\n" "${list%,}"

This User Gave Thanks to cfajohnson For This Post:
# 4  
Old 03-09-2011
Nice solution wasn't aware that printf worked like that - the C version dosn't.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List files with date, create directory, move to the created directory

Hi all, i have a folder, with tons of files containing as following, on /my/folder/jobs/ some_name_2016-01-17-22-38-58_some name_0_0.zip.done some_name_2016-01-17-22-40-30_some name_0_0.zip.done some_name_2016-01-17-22-48-50_some name_0_0.zip.done and these can be lots of similar files,... (6 Replies)
Discussion started by: charli1
6 Replies

2. Shell Programming and Scripting

Need Help with Bash - comparing directory contents with list of files

Hey guys, need help with a script I'm trying to write. Basically I need to compare the contents of a folder called "profiles" with a list of files called "template". when the file matches the contents of the folder it needs to set a variable called "checked" to "1" Cookies to anyone... (4 Replies)
Discussion started by: Scriporium
4 Replies

3. UNIX for Dummies Questions & Answers

Need help how to create a file (xml) list all files from directory

I have more than 10K songs in two directories on a hard drive. I would like to create a file list all of files name then change to .xml extension to upload to iPhone so I have a Karaoke list on my iPhone. I need your help to create a file by using command in Linux. Files names: 0001 More... (4 Replies)
Discussion started by: ggcc
4 Replies

4. Homework & Coursework Questions

Create script to add user and create directory

first off let me introduce myself. My name is Eric and I am new to linux, I am taking an advanced linux administration class and we are tasked with creating a script to add new users that anyone can run, has to check for the existence of a directory. if the directory does not exist then it has... (12 Replies)
Discussion started by: pbhound
12 Replies

5. Shell Programming and Scripting

Create variables from directory list problem

I am trying to take a list of directories in a folder and give them a variable name. I have this working with the exception that the shell_exec command wants to place a return. Here is my code which might explain better what I am trying to do: <?php session_start(); $student1=$_SESSION;... (0 Replies)
Discussion started by: robp2175
0 Replies

6. Shell Programming and Scripting

How to create multiple list of files in a directory ?

Hi, i have say 100 files in a directory. file1.log file2.log file3.log file4.log file5.log file6.log ... ... ... file99.log file100.log ========= I need to create another file which contains the list of al these log files. each file should contain only 10 log file names. it shud... (4 Replies)
Discussion started by: robinbannis
4 Replies

7. UNIX for Dummies Questions & Answers

Best way to list a directory's contents?

Hey guys! I'm so glad I found this site, I've had so many questions and have been left alone for roughly a year scanning man pages but It's just not quite cutting it for some things. So, I often like to list directories when browsing around my local machine, a friend's machine, or my web... (6 Replies)
Discussion started by: bbilheimer
6 Replies

8. UNIX for Dummies Questions & Answers

list contents of directory

I want to list the contents of a directory, but I do not want to use the ls, is there another way?? (3 Replies)
Discussion started by: carl_vieyra
3 Replies

9. UNIX for Dummies Questions & Answers

Message trying to list contents of directory

I'm getting this return whenever I try to do anything on a directory root# ls -al /directory ls: .: Value too large to be stored in data type. total 0 I can change directory down two levels but can not list contents of the root of this directory. ANy one seen this? (1 Reply)
Discussion started by: sallender
1 Replies
Login or Register to Ask a Question