Scan directories and create a list of files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Scan directories and create a list of files
# 1  
Old 10-22-2015
Scan directories and create a list of files

Gents,

Please can you help.

I want to create a list which contends the complete patch of the location of some directories with the size of each file.

need to select only .txt file

In this case I am try to find the subdirectories tp1 and tp2 and create the output list.

Code:
jd175-1
	tp1
	1.txt
	2.txt
	tp2
	1.txt
	2.txt
jd176-1
	tp1
	3.txt
	4.txt
	tp2
	3.txt
	4.txt
jd177-1
	tp1
	5.txt
	6.txt
	tp2
	5.txt
	6.txt

output 2 files with the following results>

tp1
Code:
/jd175-1/tp1/1.txt/sizefile
/jd175-1/tp1/2.txt/sizefile
/jd176-1/tp1/3.txt/sizefile
/jd176-1/tp1/4.txt/sizefile
/jd177-1/tp1/5.txt/sizefile
/jd177-1/tp1/6.txt/sizefile

tp2
/jd175-1/tp2/1.txt/sizefile
/jd175-1/tp2/2.txt/sizefile
/jd176-1/tp2/3.txt/sizefile
/jd176-1/tp2/4.txt/sizefile
/jd177-1/tp2/5.txt/sizefile
/jd177-1/tp2/6.txt/sizefile

Thanks for your help
# 2  
Old 10-22-2015
Hello jiam912,

Could you please try following and let me know if this helps you.
Code:
awk '/^jd/{A=$0;next} {sub(/^[[:space:]]+/,X,$0);} ($0 ~ /tp/){B=$0} ($0 ~ /txt/){Q[B]=Q[B]?Q[B] ORS A"/"B"/"$0"/sizefile":A"/"B"/"$0"/sizefile"} END{for(j in Q){E=j;sub(/[[:space:]].*/,X,E);print E ORS Q[j]}}' Input_file

Output will be as follows.
Code:
tp1
jd175-1/tp1/1.txt/sizefile
jd175-1/tp1/2.txt/sizefile
jd176-1/tp1/3.txt/sizefile
jd176-1/tp1/4.txt/sizefile
jd177-1/tp1/5.txt/sizefile
jd177-1/tp1/6.txt/sizefile
tp2
jd175-1/tp2/1.txt/sizefile
jd175-1/tp2/2.txt/sizefile
jd176-1/tp2/3.txt/sizefile
jd176-1/tp2/4.txt/sizefile
jd177-1/tp2/5.txt/sizefile
jd177-1/tp2/6.txt/sizefile

EDIT: Adding a non one-liner form of solution on same.
Code:
awk '/^jd/{
                A=$0;
                next
          }
          {
                sub(/^[[:space:]]+/,X,$0);
          }
                ($0 ~ /tp/) {
                                B=$0
                            }
                ($0 ~ /txt/){
                                Q[B]=Q[B]?Q[B] ORS A"/"B"/"$0"/sizefile":A"/"B"/"$0"/sizefile"
                            }
     END{
                for(j in Q) {
                                E=j;
                                sub(/[[:space:]].*/,X,E);
                                print E ORS Q[j]
                            }
          }
    ' Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-22-2015 at 04:15 AM.. Reason: Adding a non one-liner form of solution.
# 3  
Old 10-22-2015
Please show us what you have tried...

Please tell us what operating system and shell you're using.
# 4  
Old 11-18-2015
Dear R. Singh.

What I should have inside of
Code:
Input_file

??

All information i want to get is from directories and subdirectories
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Alignment tool to join text files in 2 directories to create a parallel corpus

I have two directories called English and Hindi. Each directory contains the same number of files with the only difference being that in the case of the English Directory the tag is .english and in the Hindi one the tag is .Hindi The file may contain either a single text or more than one text... (7 Replies)
Discussion started by: gimley
7 Replies

2. Shell Programming and Scripting

How to create a long list of directories with mkdir?

Hi... Thanks to read this... I want to use mkdir to create many directories listed in a text file, let's say. How do I do this? Sorry for this maybe very basic question :) (13 Replies)
Discussion started by: setub
13 Replies

3. Shell Programming and Scripting

Copy of "How to create a long list of directories with mkdir?"

To bakunin and corona688: My result when text in file is ms_ww_546 ms_rrL_99999 ms_nnn_67_756675 is https://www.unix.com/C:\Users\Fejoz\Desktop\ttt.jpg I hope you can see the picture. There is like a "whitespace character" after 2 of the 3 created directories. ---------- Post... (0 Replies)
Discussion started by: setub
0 Replies

4. Shell Programming and Scripting

How to list all the files, directories and sub-directories in the current path except one directory?

Can anyone come up with a unix command that lists all the files, directories and sub-directories in the current directory except a folder called log.? Thank you in advance. (7 Replies)
Discussion started by: Manjunath B
7 Replies

5. UNIX for Dummies Questions & Answers

list directories with more than X files

I want to search a server beginning at /home and list directories with more than X files I found a hack that injects tons of files into a directory How can I search the server recursively and list directories with more than X files? Thank you! like, find /home (directories, that meet the... (5 Replies)
Discussion started by: vanessafan99
5 Replies

6. Shell Programming and Scripting

create more than 100 directories and copy files into them

Hi, I have several files containing experiment measurements per hour (hour_1.txt has measurements for first hour, etc..etc..). I have 720 of these files (i.e. up to hour_720.txt) and i want to create 720 directories and in every one of them i want to copy its associative file (e.g.... (4 Replies)
Discussion started by: amarn
4 Replies

7. Shell Programming and Scripting

Create unique tar archives from a list of directories

I'm looking to archive a client directory from a CIFS share There are multiple directories that will be stored in a text file and I'm looking to create an individual tar archive of each folder in the directory. I've tried a number of commands to no avail. Here's what I would like. ... (2 Replies)
Discussion started by: Steelysteel
2 Replies

8. UNIX for Dummies Questions & Answers

List directories and sub directories recursively excluding files

Hi, Please help me, how to get all the direcotries, its sub directories and its sub directories recursively, need to exclude all the files in the process. I wanted to disply using a unix command all the directories recursively excluding files. I tried 'ls -FR' but that display files as... (3 Replies)
Discussion started by: pointers
3 Replies

9. Shell Programming and Scripting

Scan directory and sub directories

I am really stuck on a issue and have not been able to find a solution. With the code below I need to not only scan the current directory which as you can see below is... /lcl/sit/apps/Tivoli/ But I also want it to scan all sub directories it finds under Tivoli as well for the same thing... (2 Replies)
Discussion started by: LRoberts
2 Replies
Login or Register to Ask a Question