I want to write a script in bash, that produces a list of a directory into a file. It is necessary to mark every line as file or directory.
This is my unfinished attempt:
#!/bin/bash
if [ $# -eq 1 ]
then
if [ -d $1 ]
then
touch liste.txt
ls -l $1 | grep '^-' >> liste.txt
ls -l $1 | grep '^d' >> liste.txt
fi
fi
now I don t know how to print in every line “file” or “directory”. Maybe there is a more elegant way to solve this.
Greetings,
Haniball
Thanks Pavium,
here the finished script:
#!/bin/bash
if [ $# -eq 1 ]
then
if [ -d $1 ]
then
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 am sure that there a more elegant solution. Maybe grep can be thrown out, but I had to restrict the output to only lines that match the pattern.
Greetings,
Haniball
You can use sed as pavium already said.
Or in one command:
Or you can do something different: