LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-06-2006, 08:49 AM   #1
tongar
Member
 
Registered: Jan 2005
Distribution: SUSE ES 9 for x86
Posts: 43

Rep: Reputation: 15
How to make a command actually change a file


In the bash shell, I am learning all about redirection and command usage. It is clear that the disjoint committee that designed all these commands had no regard for a standard syntax. One thing they all valued, however, was a desire to make each command and option as short, criptic and non-verbose as possible.

Thats why it puzzles me that I have not encountered a way yet to just have a command actually change the file it reads as stdin. Attempts to have the same file as stdout result in a blanking out of the stdin file. For example, don't do this if you want the file:

nl <somefile.txt >somefile.txt
or
nl <somefile.txt | tee >somefile.txt

It seems a violation of non-verboseness, the everything on one line rule, and a missed oportunity to create a new criptic one character operand for there to be no way to do this other than:

nl <somefile.txt >somefile2.txt
mv -f somefile2.txt somefile.txt

Am I missing something?

thanks,
tongar

Last edited by tongar; 01-06-2006 at 08:52 AM.
 
Old 01-06-2006, 09:01 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
What you refer to as short, cryptic, and non-verbose is part of the essence of *ix systems. One day, you will learn to like it.

You don't "change files" at the command line---that is done in text editors--or hex editors. What you can do with files at the command line:
read
append
write
change ownership, access, and access time

to read or write (or append), you use any command that needs or generates data and you redirect the stdin and stdout to the file, eg:
ls > list creates a new file with the output of the ls command
cat file1 > file2 reads file1 and sends the output to file2
cat file1 >> file2 reads file1 and APPENDS the output to file2
cat file1 | grep word > file2 reads file1 and writes lines containing "word" to file2.
etc.....
touch file either creates file or updates its access time
rm file removes the file
 
Old 01-06-2006, 10:10 AM   #3
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
In fact, may help, if you think about commands more like a filter that change the input in some way and output the modified data. In general, there is no files here, just a input flow and an output flow. Theses flows can come from files and goes to files but its just a special case !

An example:
cat | tr '[a-z]' '{A-Z]' | nl

Here, what you type is the input flow. But it can be ANY program's output, either the normal output or the error output, whatever. There is no file here. And the final result can be used inside a logical test inside a program, without files taking part in this game.

for file in `find $DIR -type f | sed -e 's#/# #g' | rev | cut -d" " -f 1 | rev`; do
echo "do something with file $file"
done

Here, I list all files below $DIR, and extract from the full path (/dir1/dir2/dir3/filename) just the filename part.
Again, there is no need to intermediate files. (Hey guys, I known /usr/bin/basename, this is just a example, ok?)

The point here is theses commands was designed to handle input flow and produce another flow.

Well, about the cryptic names I must agree without. But, I prefer cryptic names than aCommandWithVeryLongName, just like Pascal commands.
 
Old 01-06-2006, 09:18 PM   #4
shengchieh
Member
 
Registered: Jul 2004
Location: Palo Alto, CA
Distribution: #! Korora
Posts: 472

Rep: Reputation: 30
Take a look at the command, sed. You can use sed to edit your file
without entering a text editor.

man sed and/or
info sed

should gives more info. Googling sed will gives alot of docs.

Sheng-Chieh
 
  


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
what is the command to make a user change their password after creating a new user? naweenio Linux - Newbie 7 01-05-2005 07:07 AM
I accidentally deleted make file in /usr/local/bin, now cannot use make command.... Niceman2005 Linux - Software 2 11-17-2004 07:55 PM
How do I make a change to a current kernel? Would 'make oldconfig' work... jtp51 Slackware 11 11-01-2004 11:02 PM
cp command change the file permission? myunicom Linux - General 6 09-24-2003 08:15 AM
Is it possible to make a "file" that can change its output when accessed? oot Linux - General 5 05-11-2003 08:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:41 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