简体   繁体   中英

Shell script to recursively print full directory tree using ls

Assignment: I have to create a shell script using diff and sort, and a pipeline using ls -l, grep '^d', and awk '{print $9}' to print a full directory tree.

I wrote a C program to display what I am looking for. Here is the output:

ryan@chrx:~/Documents/OS-Projects/Project5_DirectoryTree$ ./a.out 
TestRoot/
[Folder1]
  [FolderC]
  [FolderB]
  [FolderA]
[Folder2]
  [FolderD]
  [FolderF]
  [FolderE]
[Folder3]
  [FolderI]
  [FolderG]
  [FolderH]

I wrote this so far:

ls -R -l $1 | grep '^d' | awk '{print $9}' 

to print the directory tree but now I need a way to sort it by folder depth and possibly indent but not required. Any suggestions? I can't use find or tree commands.

EDIT: The original assignment & restrictions were mistaken and changed at a later date. The current answers are good solutions if you disregard the restrictions so please leave them for any people with similar issues. As for the the new assignment in case anybody was wondering. I was to recursively print all sub directories, sort them, then compare them with my program to make sure they have similar results. Here was my solution:

#!/bin/bash
echo Program:
./a.out $1 | sort
echo Shell Script:
ls -R -l $1 | grep '^d' | awk '{print $9}' | sort

diff <(./a.out $1 | sort) <(ls -R -l $1 | grep '^d' | awk '{print $9}' | sort)
DIFF=$?

if [[ $DIFF -eq 0 ]]
then
    echo "The outputs are similar!"
fi

You don't need neither ls nor grep nor awk for getting the tree. The Simple recursive bash function will be enouh, like:

#!/bin/bash
walk() {
        local indent="${2:-0}"
        printf "%*s%s\n" $indent '' "$1"
        for entry in "$1"/*; do
                [[ -d "$entry" ]] && walk "$entry" $((indent+4))
        done
}
walk "$1"

If you run it as bash script.sh /etc it will print the dir-tree like:

/etc
    /etc/apache2
        /etc/apache2/extra
        /etc/apache2/original
            /etc/apache2/original/extra
        /etc/apache2/other
        /etc/apache2/users
    /etc/asl
    /etc/cups
        /etc/cups/certs
        /etc/cups/interfaces
        /etc/cups/ppd
    /etc/defaults
    /etc/emond.d
        /etc/emond.d/rules
    /etc/mach_init.d
    /etc/mach_init_per_login_session.d
    /etc/mach_init_per_user.d
    /etc/manpaths.d
    /etc/newsyslog.d
    /etc/openldap
        /etc/openldap/schema
    /etc/pam.d
    /etc/paths.d
    /etc/periodic
        /etc/periodic/daily
        /etc/periodic/monthly
        /etc/periodic/weekly
    /etc/pf.anchors
    /etc/postfix
        /etc/postfix/postfix-files.d
    /etc/ppp
    /etc/racoon
    /etc/security
    /etc/snmp
    /etc/ssh
    /etc/ssl
        /etc/ssl/certs
    /etc/sudoers.d

Borrowing from @jm666's idea of running it on /etc:

$ find /etc -type d -print | awk -F'/' '{printf "%*s[%s]\n", 4*(NF-2), "", $0}'
[/etc]
    [/etc/alternatives]
    [/etc/bash_completion.d]
    [/etc/defaults]
        [/etc/defaults/etc]
            [/etc/defaults/etc/pki]
                [/etc/defaults/etc/pki/ca-trust]
                [/etc/defaults/etc/pki/nssdb]
            [/etc/defaults/etc/profile.d]
            [/etc/defaults/etc/skel]
    [/etc/fonts]
        [/etc/fonts/conf.d]
    [/etc/fstab.d]
    [/etc/ImageMagick]
    [/etc/ImageMagick-6]
    [/etc/pango]
    [/etc/pkcs11]
    [/etc/pki]
        [/etc/pki/ca-trust]
            [/etc/pki/ca-trust/extracted]
                [/etc/pki/ca-trust/extracted/java]
                [/etc/pki/ca-trust/extracted/openssl]
                [/etc/pki/ca-trust/extracted/pem]
            [/etc/pki/ca-trust/source]
                [/etc/pki/ca-trust/source/anchors]
                [/etc/pki/ca-trust/source/blacklist]
        [/etc/pki/nssdb]
        [/etc/pki/tls]
    [/etc/postinstall]
    [/etc/preremove]
    [/etc/profile.d]
    [/etc/sasl2]
    [/etc/setup]
    [/etc/skel]
    [/etc/ssl]
    [/etc/texmf]
        [/etc/texmf/tlmgr]
        [/etc/texmf/web2c]
    [/etc/xml]

Sorry, I couldn't find a sensible way to use the other tools you mentioned so it may not help you but maybe it'll help others with the same question but without the requirement to use specific tools.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM