LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-03-2011, 02:26 PM   #1
Quest ion
Member
 
Registered: Oct 2011
Posts: 45

Rep: Reputation: Disabled
filelist question


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!
 
Old 10-03-2011, 02:35 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Hi,

Welcome to LQ!

An ls|tee filelist for instance will avoid that.


Cheers,
Tink
 
1 members found this post helpful.
Old 10-03-2011, 02:41 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
ls -d * > filelist
 
1 members found this post helpful.
Old 10-03-2011, 02:45 PM   #4
Quest ion
Member
 
Registered: Oct 2011
Posts: 45

Original Poster
Rep: Reputation: Disabled
Thank you so much! I will try both of these option, as you can see, I am brand new to linux! lol

How would I add a title line "***Filelist***" at the beginning of that same file?
 
Old 10-03-2011, 03:00 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
sed -i '1i ***Filelist***' filelist
 
1 members found this post helpful.
Old 10-03-2011, 03:01 PM   #6
Quest ion
Member
 
Registered: Oct 2011
Posts: 45

Original Poster
Rep: Reputation: Disabled
I'm going to try echo "***Filelist***" > filelist, or some variations of that and see what happens....
 
Old 10-03-2011, 03:03 PM   #7
Quest ion
Member
 
Registered: Oct 2011
Posts: 45

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
Code:
sed -i '1i ***Filelist***' filelist
Just read your reply, thanks!! Is there a way to make it at the beginning of the file "filelist"?
 
Old 10-03-2011, 03:07 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Quest ion View Post
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 View Post
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?

Last edited by colucix; 10-03-2011 at 03:09 PM.
 
Old 10-03-2011, 03:13 PM   #9
Quest ion
Member
 
Registered: Oct 2011
Posts: 45

Original Poster
Rep: Reputation: Disabled
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
 
Old 10-03-2011, 03:18 PM   #10
Quest ion
Member
 
Registered: Oct 2011
Posts: 45

Original Poster
Rep: Reputation: Disabled
Well I removed the ***Filelist***, now trying to find a way to add ***Filelist*** that to the top of the filelist.

I was close with the echo, it was third or so on the list rather than at the top though. lol
 
Old 10-03-2011, 03:22 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by Quest ion View Post
Well I removed the ***Filelist***, now trying to find a way to add ***Filelist*** that to the top of the filelist.

I was close with the echo, it was third or so on the list rather than at the top though. lol

Code:
(echo "***Filelist***";ls) | tee filelist
 
1 members found this post helpful.
Old 10-03-2011, 03:41 PM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
@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.
 
Old 10-03-2011, 03:41 PM   #13
Quest ion
Member
 
Registered: Oct 2011
Posts: 45

Original Poster
Rep: Reputation: Disabled
It worked!!! Thank you so much, that was extremely helpful! I was wrestling with this one for a long time!
 
Old 10-03-2011, 06:43 PM   #14
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by colucix View Post
@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?

Positive. Maybe my box is slower than yours?! :)


Cheers,
Tink
 
Old 10-04-2011, 12:25 AM   #15
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Tinkster View Post
Positive. Maybe my box is slower than yours?!
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..

 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Unrar from filelist casoe84dk Linux - Newbie 2 04-05-2010 02:34 PM
what is a Filelist? criacuervo Linux - Newbie 3 08-24-2009 02:29 AM
scripting problem rm filelist milke Programming 9 07-07-2006 05:41 AM
wine dc++ shuts down when trying to download or get filelist tudilica Linux - Software 3 04-18-2005 06:54 AM
Slackware's FILELIST.TXT DaHammer Slackware 5 09-26-2004 04:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration