4

I'd need a file reporting for each folder the content and the name of the folder itself. If I do

ls -l * 

I have (this is just a subset of hundreds of folders)

secondary_endosymbiont_of_Heteropsylla_cubana_Thao2000_uid172738:
total 1232
-rw-r--r--  1 FrancescaDeFilippis  staff  627404  2 Nov  2012 NC_018420.ffn

syncytium_symbiont_of_Diaphorina_citri_uid213384:
total 896
-rw-r--r--  1 FrancescaDeFilippis  staff  446934 29 Lug  2013 NC_021885.ffn
-rw-r--r--  1 FrancescaDeFilippis  staff    5594 29 Lug  2013 NC_021886.ffn

uncultured_Sulfuricurvum_RIFRC_1_uid193658:
total 4840
-rw-r--r--  1 FrancescaDeFilippis  staff     1002  9 Apr  2013 NC_020503.ffn
-rw-r--r--  1 FrancescaDeFilippis  staff  2470123  9 Apr  2013 NC_020505.ffn

uncultured_Termite_group_1_bacterium_phylotype_Rs_D17_uid59059:
total 3392
-rw-r--r--  1 FrancescaDeFilippis  staff  851852  1 Mar  2013 NC_020419.ffn
-rw-r--r--  1 FrancescaDeFilippis  staff    6684  1 Mar  2013 NC_020420.ffn
-rw-r--r--  1 FrancescaDeFilippis  staff    3869  1 Mar  2013 NC_020421.ffn
-rw-r--r--  1 FrancescaDeFilippis  staff    2394  1 Mar  2013 NC_020422.ffn
-rw-r--r--  1 FrancescaDeFilippis  staff  848808 28 Ago  2012 NS_000191.ffn
-rw-r--r--  1 FrancescaDeFilippis  staff    6684 28 Ago  2012 NS_000192.ffn
-rw-r--r--  1 FrancescaDeFilippis  staff    3869 28 Ago  2012 NS_000193.ffn
-rw-r--r--  1 FrancescaDeFilippis  staff    2394 28 Ago  2012 NS_000194.ffn

I'd like to get something like this:

secondary_endosymbiont_of_Heteropsylla_cubana_Thao2000_uid172738:  NC_018420.ffn

syncytium_symbiont_of_Diaphorina_citri_uid213384:  NC_021885.ffn
syncytium_symbiont_of_Diaphorina_citri_uid213384:  NC_021886.ffn

and so on, so I'd need the name of the folder repeated for each file.

How could I get this?

  • Something like ls *? – muru Feb 3 '15 at 14:22
  • Do the files need to be sorted (like in your current solution)? Do you want to exclude hidden files (like in your current solution). Do you want to list the content of directories pointed to by symlinks in the current directory (unlike in your current solution)? – Stéphane Chazelas Feb 3 '15 at 14:26
  • it doesn't matter if they are sorted, I just need the name of the file and the name of the folder on the same line – Francesca de Filippis Feb 3 '15 at 14:39
3

This works in any POSIX shell:

find <directory> -type f -exec sh -c '
  for f do
    printf "%s: %s\n" "${f%/*}" "${f##*/}"
  done' sh {} +

This command executes on every file (file name stored in the variable f) and displays the directory (${f%/*}), a colon and the file name (${f##*/}').

| improve this answer | |
5
find .

or

find . -ls

if you want details...

Using ls with a single level (directories containing files, not other directories):

ls -1d -- */*

for a simple list,

ls -ld -- */*

for details (that's the digit 1 in the first example, the letter L in lower case in the second).

| improve this answer | |
1

With zsh:

for d (*(/N)) {for f ($d/*(N:t)) printf '%s:  %s\n' $d $f; echo}

Now, if you don't care about sorting or excluding hidden files, or having a blank line in-between directories, then (assuming file names don't contain newline characters), you could simply do:

find . -path './*/*' -prune -print | sed 's|\./||;s|/|:  |'

With GNU find, you can also do:

find . -path './*/*' -prune -printf '%P\n' | sed 's|/|:  |'

Or:

find . -path './*/*' -prune -printf '%h:  %f\n'

if you don't care about the leading ./.

The -path './*/*' -prune is to only report files at depth 2 (./a/b but not ./a or ./a/b/c). With the GNU (and a few others) find implementation, you can replace that with -mindepth 2 -maxdepth 2.

| improve this answer | |
  • this doesn't work : -bash: syntax error near unexpected token `(' – Francesca de Filippis Feb 3 '15 at 14:40
  • 2
    @FrancescadeFilippis It works in zsh as Stéphane mentioned, not bash. – Marco Feb 3 '15 at 14:42
-1

how about . . .

find `pwd` -type f | perl -lane '@x=split(/\//); print "$x[$#x-1]: $x[$#x]";' 
| improve this answer | |
  • the pwd has to be enclosed in backticks - which understandably enough are not allow in input here. And the /// is / \ / / – pedrito Feb 3 '15 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.