0

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.

Barmar
  • 9,927
  • Do you really have a space between file and list? – Barmar Mar 08 '16 at 20:09
  • I'm sorry for confusion, but the answer to your question is no. – Phantom1421 Mar 08 '16 at 20:15
  • Don't redirect to a file in the same directory you're listing, since the redirection creates the file before starting ls. – Barmar Mar 08 '16 at 20:18
  • well see thats what i have to do for this question, as a hint i was told to use grep -v, which makes sense a little because it gets rid of the filelist in filelist so it looks like *File List* – Phantom1421 Mar 08 '16 at 20:27
  • this is a question on my homework that i was working on but can't seem to finish it, i get confused on the part when i do ls > filelist and the cat filelist when the contents are displayed one of them is filelist and i need to get rid of that when. – Phantom1421 Mar 08 '16 at 20:48
  • On a side note: please be aware of why not to parse ls. The homework should be aware of this or mention it at least. Well save the read for later. – FelixJN Mar 08 '16 at 20:50
  • alright so i was able to do with ls | grep -v filelist > filelist and then echo "*File List*" >> filelist. however it shows up at the end of the text file, how can i make it go to the top of the text file when everything is displayed on screen. i think i need to use pipe – Phantom1421 Mar 08 '16 at 21:15
  • i need a title line is what the homework question wants me to do – Phantom1421 Mar 08 '16 at 21:18

4 Answers4

0
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

MelBurslan
  • 6,966
0

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).

Barmar
  • 9,927
0

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
vonbrand
  • 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 the ls 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
0

Just for deeper understanding : You can easily split up the whole command three parts:

  1. echo "File List"; echo prints out ( if you don't have pipe the output elsewhere ) on STDIN ( Standard Input )

  2. 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.

  3. 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.

LeTeX
  • 1