ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How does one create a file named "filelist" under another file without adding itself?
For example, "ls > filelist" will make that list, but will include itself in the actual list. I'm trying ot do it where it excludes itself, any help would be much appreciated!
I'm going to try echo "***Filelist***" > filelist, or some variations of that and see what happens....
If you run it before the ls command, it will causes ls to include the output file into the list. Furthermore, take in mind that there is no chance to run ls and redirect the output to a file without listing the file itself, because when you launch the command, the first thing accomplished by the shell is to create an empty output file (before the ls command is actually executed). On the other hand, the command
Code:
ls -d *
causes the shell to expand the wildcard with the name of the files placed in the current working directory, before any other task, included the creation of the output file. This is the way the shell works.
---------- Post added 03-10-11 at 22:08 ----------
Quote:
Originally Posted by Quest ion
Just read your reply, thanks!! Is there a way to make it at the beginning of the file "filelist"?
It does exactly what you're looking for. Have you tried it?
Ah! I mean that I'd like to exclude filelist form adding itself to the filelist, but in it's place I'd like to include a file named ***Filelist*** to it that is visible at the top inside of the filelist.
I kind of almost got it with the echo attempt, but it was listed third. When I removed the file (rm) and recreate it with ls |tee , it seems to re-add the ***Filelist*** file to it, which is interesting, but this is a good way to learn, by doing. lol
@Tinkster: on my system the tee solution adds the output file name to the list:
Code:
$ rm -f filelist
$ ls | tee filelist
<screen output omitted>
$ grep filelist filelist
filelist
You sure it doesn't happen on your system?
@Quest ion: what does prevent you from running these two commands in sequence?
Code:
$ ls -d * > filelist
$ sed -i '1i ***Filelist***' filelist
If I correctly understand, you want to add the string "***Filelist***" at the beginning of the output file. The suggested sed command should take care of it.
Yes, maybe! I've tried many times the same commands and sometimes it works as you describe, sometimes not. Maybe it's really a matter of the time required by the ls command to retrieve its output..
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.