Results 1 to 10 of 10

Thread: How can I directly read a directory as a file from Bash?

  1. #1
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    5,288
    Distro
    Ubuntu

    Question How can I directly read a directory as a file from Bash?

    EDIT: What looked like the file actually isn't, and what I want isn't straightforward.

    I know that a directory is actually a specialised file, e.g. ~/Pictures is actually a file containing details about what's inside the Pictures folder.

    I can view the folder-file directly using vim:
    Code:
    view ~/Pictures
    vim gives a warning message, "~/Pictures/" Illegal file name, but still allows me to view it.

    (Presumably, I would lose data to edit the file, so I won't attempt to do that! Actually, I tried this in a VM to see what would happen, and the system wouldn't let me directly modify it, at least not with Bash or vim.)

    I'm curious about how to read that file directly from a Bash script. I tried using this:
    Code:
    while read ENTRY
    do
            printf '%s\n' "${ENTRY}"
    done <${HOME}/Pictures
    But this doesn't work, because Bash (correctly) returns the error that Pictures is a directory. Likewise, I can't use cat to display the folder-file:
    Code:
    $ cat ~/Pictures
    cat: /home/paddy/Pictures: Is a directory
    Is there some command that I can use within a script to directly read the folder-file?

    I know that this is an odd request. I'm asking out of sheer curiosity, not because I need it.

    Thank you 😊
    Last edited by Paddy Landau; March 12th, 2022 at 05:17 PM.
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

  2. #2
    Join Date
    Jul 2005
    Location
    I think I'm here! Maybe?
    Beans
    Hidden!
    Distro
    Xubuntu 24.04 Noble Numbat

    Re: How can I directly read a directory as a file from Bash?

    Not sure what you mean by "read a directory"..

    What sort of content or information do you want to see? You can see what the directory contains with many variations of the ls command but I assume that is not what you're looking for so tell us in more detail what you hope to be able to get from this.

  3. #3
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    5,288
    Distro
    Ubuntu

    Re: How can I directly read a directory as a file from Bash?

    Quote Originally Posted by ajgreeny View Post
    Not sure what you mean by "read a directory".
    It seems that I didn't quite explain clearly enough.

    Remember that a directory is actually a file in Linux. So, ~/Pictures is in reality just a file.

    Try for yourself. Issue this command:
    Code:
    vim -R ~/Pictures
    See what happens? You see the contents of the actual file that is Pictures.

    That's what I want to access from a script.
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

  4. #4
    Join Date
    Dec 2014
    Beans
    2,656

    Re: How can I directly read a directory as a file from Bash?

    Would an array with the file names be O.K. ?
    Code:
    declare -a files
    files=(*)
    #number of files:
    echo ${#files[*]}
    If you need more than the name about a single file, 'stat' can give that to you. See 'man stat'.

    Holger

  5. #5
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    5,288
    Distro
    Ubuntu

    Re: How can I directly read a directory as a file from Bash?

    Quote Originally Posted by Holger_Gehrke View Post
    Would an array with the file names be O.K. ?
    Unfortunately, you have misunderstood my query.

    Open a terminal and enter this command:
    Code:
    vim -R ~/Pictures
    Notice what happens.

    That is what I want to be able to access.
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

  6. #6
    Join Date
    Oct 2005
    Location
    Lab, Slovakia
    Beans
    10,804

    Re: How can I directly read a directory as a file from Bash?

    Try dd and hexedit.

    It would be wise to experiment on a test system, such as a Raspberry Pi and not on your main computer.

  7. #7
    Join Date
    Dec 2014
    Beans
    2,656

    Re: How can I directly read a directory as a file from Bash?

    That's netrw, the file manager module of vim, similar to Emacs' diredit. It's not editing the raw directory. The only thing I can find which actually allows you to play around with the raw data structures of the file system is the Linux Disk Editor which is modelled on the old disk editor from the Norton Utilities for DOS.

    Holger

  8. #8
    Join Date
    Mar 2011
    Location
    U.K.
    Beans
    Hidden!
    Distro
    Ubuntu 22.04 Jammy Jellyfish

    Re: How can I directly read a directory as a file from Bash?

    Perchance referring to reading extra folder permissions?

    https://linuxconfig.org/how-to-use-s...nd-sticky-bits.

  9. #9
    Join Date
    Oct 2005
    Location
    Lab, Slovakia
    Beans
    10,804

    Re: How can I directly read a directory as a file from Bash?

    Apart from dd and hexedit mentioned above, you can also try gdb.

  10. #10
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    5,288
    Distro
    Ubuntu

    Re: How can I directly read a directory as a file from Bash?

    Quote Originally Posted by HermanAB View Post
    It would be wise to experiment on a test system, such as a Raspberry Pi and not on your main computer.
    I have a VM, so I'll experiment there
    Quote Originally Posted by HermanAB View Post
    Try dd and hexedit.
    dd and hexedit both complained about it being a directory.
    Quote Originally Posted by Holger_Gehrke View Post
    That's netrw, the file manager module of vim, similar to Emacs' diredit. It's not editing the raw directory.
    Ah, this is interesting, thank you. In other words, you're telling me that vim isn't actually looking at the file, but instead is reporting it in a useful format?
    Quote Originally Posted by Holger_Gehrke View Post
    The only thing I can find which actually allows you to play around with the raw data structures of the file system is the Linux Disk Editor which is modelled on the old disk editor from the Norton Utilities for DOS.
    I had a look, but I don't know how to install this
    Quote Originally Posted by dragonfly41 View Post
    Perchance referring to reading extra folder permissions?
    No, that's not what I'm after.
    Quote Originally Posted by HermanAB View Post
    … you can also try gdb.
    gdb looks like a debugger for programs. I don't know how to use it for a file.

    ────────────

    Thank you all for your help.

    It looks as though @Holger_Gehrke has it correct, that it's not actually the file that I'm looking at. Instead, vim is interpreting the contents for me, which is not the same thing!

    This was a fun experiment, but in the end, it seems that I'm looking for something that is hardly straightforward.

    I shall mark this thread as solved, and thank you all again.
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •