linux - Realizing recognition of files/directories in bash -
i want write script in bash, produces list of directory file. necessary mark every line file or directory.
this unfinished attempt:
#!/bin/bash if [ $# -eq 1 ] if [ -d $1 ] touch liste.txt ls -l $1 | grep '^-' >> liste.txt ls -l $1 | grep '^d' >> liste.txt fi fi now don t know how print in every line "file" or "directory". maybe there more elegant way solve this.
greetings, haniball
thanks pavium,
here finished script:
#!/bin/bash if [ $# -eq 1 ] if [ -d $1 ] rm liste.txt touch liste.txt ls -l $1 | grep '^-' | sed -e "s/^-/file /g" >> liste.txt ls -l $1 | grep '^d' | sed -e "s/^d/directory /g" >> liste.txt fi more liste.txt fi i sure there more elegant solution. maybe grep can thrown out, had restrict output lines match pattern.
greetings, haniball
you can use sed pavium said.
ls -l $1 | grep '^-' | sed 's/^/file: /' >> liste.txt ls -l $1 | grep '^d' | sed 's/^/directory: /' >> liste.txt or in 1 command:
ls -l $1 | sed -n -e '/^-/{s/^/file: /p;d;}' -e '/^d/{s/^/directory: /p;d;}' > liste.txt or can different:
for f in $1/* ; if [ -d "$f" ]; echo "directory: $f" >> liste1.txt else echo "file: $f" >> liste1.txt fi done
Comments
Post a Comment