List directories, subs and files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers List directories, subs and files
# 1  
Old 03-30-2011
List directories, subs and files

Hi,

I try to list all files in a folder, including all the subdirs (and their subdirs) and all files contained in each of these folders. I then print it to a simple txt file.

I use ls -R -1 >test.txt

This sort of does what I need, yet, the result is something like:
Quote:
directory1
directory2
file1.txt
file2.txt

./directory1:
directory11

./directory11:
text-in-dir11.txt
It reasonably comes close, yet it would be a lot more conveniant if it resulted in somethin like:
Quote:
directory1
./directory1/directory11
text-in-dir11.txt
./directory1/directory12
directory2
file1.txt
file2.txt
# 2  
Old 03-30-2011
You should use the command find instead

Code:
# find . -type d ! -name \. | while read a
do echo DIRECTORY : ${a##*/}
find $a -type f -prune
done

will for example give :

Code:
DIRECTORY : taitai
./taitai/tutututu
DIRECTORY : tyty
./tyty/tyty.f
./tyty/toutou/toutou.f
./tyty/toutou/whatever/whatever.f
DIRECTORY : toutou
./tyty/toutou/toutou.f
./tyty/toutou/whatever/whatever.f
DIRECTORY : whatever
./tyty/toutou/whatever/whatever.f

or you can just

Code:
find . ! -name \.

use the -type f or -type d options
see
Code:
man find


Last edited by ctsgnb; 03-30-2011 at 11:52 AM..
# 3  
Old 03-30-2011
Brilliant

This indeed works perfectly.

Now I only need to figur out how to do this in command promt. But thanks for the swift reply.
# 4  
Old 03-30-2011
What are you trying to achieve ?
# 5  
Old 04-01-2011
Thanks again for the reply.

I try to achieve the following:
List all dirs, subdirs and files + the file type (dir, file, symlink...) + folder it belongs to and a unique incremental ID (in the folder I navigated to)

This to get a list of all files and dirs, so I can process it in excel.

In other words for the following folder dirs/files:
Code:
Folder 1
-- Folder 1.1
---- File 1.1.1
---- File 1.1.2
Folder 2
-- Folder 2.1
-- Folder 2.2

To print it out as:
Code:
(--ID1--) ./Folder 1 (--Directory--)
(--ID2--) ./Folder 1/Folder 1.1 (--Directory--) (--./Folder 1--)
(--ID3--) ./Folder 1/Folder 1.1/File 1.1.1 (--File--) (--./Folder 1/Folder 1.1--)
(--ID4--) ./Folder 1/Folder 1.1/File 1.1.2 (--File--) (--./Folder 1/Folder 1.1--) (--./Folder 1--)  
(--ID5--) ./Folder 2 (--Directory--)  
(--ID6--) ./Folder 2/Folder 2.1 (--Directory--) (--./Folder 2--)   
(--ID7--) ./Folder 2/Folder 2.2 (--Directory--) (--./Folder 2--)

# 6  
Old 04-01-2011
Something like this ?

Code:
find . -ls | awk '{print $3,$11}'

Code:
$ find . -ls | awk '{print $3,$11}'
drwxr-xr-x .
-rw-r--r-- ./in
-rw-r--r-- ./infile
-rw-r--r-- ./e.dFp
drwxr-xr-x ./tyty
-rw-r--r-- ./tyty/tyty.f
drwxr-xr-x ./tyty/toutou
-rw-r--r-- ./tyty/toutou/toutou.f
drwxr-xr-x ./tyty/toutou/whatever
-rw-r--r-- ./tyty/toutou/whatever/whatever.f
$

---------- Post updated at 05:42 PM ---------- Previous update was at 05:37 PM ----------

You could even Take the inode as the ID (so it will be uniq)

Code:
find . -ls | awk '{print $1,$3,$11}'


Code:
$ find . -ls | awk '{print$1,$3,$11}'
6253 drwxr-xr-x .
6160 -rw-r--r-- ./in
6488 -rw-r--r-- ./infile
7871 -rw-r--r-- ./e.dFp
9008 drwxr-xr-x ./tyty
9009 -rw-r--r-- ./tyty/tyty.f
9010 drwxr-xr-x ./tyty/toutou
9011 -rw-r--r-- ./tyty/toutou/toutou.f
9012 drwxr-xr-x ./tyty/toutou/whatever
9013 -rw-r--r-- ./tyty/toutou/whatever/whatever.f
$


Last edited by ctsgnb; 04-01-2011 at 12:47 PM..
# 7  
Old 04-01-2011
Man, thanks a lot!

I indeed thought I should use ls. Will look into what the above code actually does.

But not exactly what I need. Let me give a very clear example:
Folder 1
-- File 1 (belongs to folder 1)
Folder 2
-- Folder 2-1
---- File 2

Resulting in
Folder 1 (Directory) (root)
File 2 (File) (Folder 1)
Folder 2 (Directory) (root)
Folder 2-1 (Directory) (Folder 2)
File 2 (File) (Folder 2-1)
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

A way to list directories that contain specific files.

Hi everyone My issue is this, I need to list all the sub directories in a directory that contains files that have the extension *.log, *.dat and *.out . After reviewing the output i need to delete those directories i do not need. I am running Solaris 10 in a bash shell. I have a script that I... (2 Replies)
Discussion started by: jsabo40
2 Replies

2. Shell Programming and Scripting

List directories and count files inside

I'm trying to make a script that will list all directories under a selection as well as the number of files in each. I cannot get it to work under a symbolic link. The file structure is: XXX_20131127_001 dir01 (sym link) 2404x912 file.0000.xxx to ... (10 Replies)
Discussion started by: scribling
10 Replies

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

4. UNIX for Dummies Questions & Answers

List biggest files (Not Directories)

Hello, can you please help me writing a command that would output the biggest files on my system from biggest to smallest? I want this to print only the files, not the directories. I have tried du -a ~ | sort -nr | head -10 However, this also prints out all the directories - which I do... (8 Replies)
Discussion started by: tonydaniels1980
8 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. 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

7. Shell Programming and Scripting

How to get a list of files in a dir w/o sub-directories?

Hi I am looking for the correct syntax to find all files in the current directory without listing sub-directoris. I was using the following command, but it still returns subdirectoris and files inside them: $ ls -laR | grep -v ^./ Any idea? Thanks PS I am in ksh88 (4 Replies)
Discussion started by: aoussenko
4 Replies

8. Shell Programming and Scripting

Command to list only files omit directories.

Hi All I am writting a script that does a comparison between files in 2 diffectent directories. To do this I need a command that will list out only the files in a give directory and omit any sub dorectories with that directory. But I am unable to find it. Please Help. I tried ls... (5 Replies)
Discussion started by: Veenak15
5 Replies

9. UNIX for Dummies Questions & Answers

List directories and files

I want to count how many levels there are under a directory. I repeat level. Also how i count only all the files in a directoy ( all files of all directories of all leves down!) and how can i count only all the directories under a directory (including subdirectories, all levels down) ... (2 Replies)
Discussion started by: psalas
2 Replies

10. Shell Programming and Scripting

List specific files from directories

Hello, I would like to list the files from all directories that has been modified more than 1 month ago, and whose name is like '*risk*log'. I think a script like this should work : ls -R | find -name '*risk*.log' -mtime 30 -type f But it tells me "no file found" though I can see some. ... (4 Replies)
Discussion started by: Filippo
4 Replies
Login or Register to Ask a Question