I need to use the ls > filelist
and, however when is use "cat filelist"
it displays all the content in the filelist
with filelist
as one of the names in it, but i need to change it "* * * File List * * *"
. I am unsure how to do this only to the filelist
in file list if that makes sense.

- 9,927

- 71
4 Answers
ls > /tmp/filelist
echo "* * * File List * * *"
cat /tmp/filelist
assumes you are not listing the files in /tmp
. If that is the case change the /tmp
to /var/tmp

- 6,966
You can do:
ls | grep -vx filelist > filelist
The -v
option makes it exclude the matching lines from the output, and -x
makes it match the whole line (so it won't skip otherfilelist
).

- 9,927
The Unix commands are quite terse, and don't decorate their output with headers/footers or irrelevant details, precisely to make their use in random pipelines easier. Adding such is quite easy:
(echo '* * * This is a header * * *'; ls ; echo --* footer *--) > filelist

- 18,253
-
(echo "***File List***"; ls | grep -v filelist;)> filelist
This is what I used and got the right answer through you really really great hint, thanks again but when it comes to pipes does it go in order such as starting echo then doing thels
and doing it only do>filelist
? any help with pipes is great, i will however also read more into it in textbook – Phantom1421 Mar 08 '16 at 21:51
Just for deeper understanding : You can easily split up the whole command three parts:
echo "File List"; echo prints out ( if you don't have pipe the output elsewhere ) on STDIN ( Standard Input )
ls | grep -vx filelist; lists the content of your current directory and pipes the output to grep. Normally "grep" would just print lines which contains the given keyword ( "filelist") in our case. With the option -v we are looking for lines which does NOT contain the keyword. As mentioned by Barmar , it would also exclude files which contain "filelist" like e.g. "myfilelist", "filelist2", ... , so the usage of the -x is recommended.
filelist redirects the output to the file "filelist". As (1) and (2) are in brackets and seperated by semicolons, the output (which would have been printed out on STDIN) of both commands will be redirected to this file.

- 1
file
andlist
? – Barmar Mar 08 '16 at 20:09ls
. – Barmar Mar 08 '16 at 20:18