LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 09-15-2012, 08:27 AM   #1
ronss
Member
 
Registered: Mar 2002
Location: phoenix,az
Distribution: red hat linux enterprise-centos
Posts: 766

Rep: Reputation: 31
linux commands


do any of you use linux redirect commands for redirecting standard input , output, stanard error....and pipe commands?
 
Old 09-15-2012, 08:47 AM   #2
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
If you mean shell redirect and pipe commands, yes. Is that really your question?
 
Old 09-15-2012, 11:05 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
I would expect most people with much experience in Linux/Unix to do so routinely. I cannot imagine a day going by without using some kind of commandline redirection of standard IO. Its use is part of the fundamental style of Linux/Unix text-mode tools; a set of small tools each doing one thing well, and connecting to solve bigger real world problems.

--- rod.
 
Old 09-15-2012, 01:38 PM   #4
linux_BSD
Member
 
Registered: Sep 2012
Posts: 47

Rep: Reputation: 4
Quote:
Originally Posted by ronss View Post
do any of you use linux redirect commands for redirecting standard input , output, stanard error....and pipe commands?
Yes indeed. The more knowledge you have of using redirection and pipes, you can filter out results and extract any info you want and you can create custom commands. Linux is powerful.
 
Old 09-15-2012, 09:42 PM   #5
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Debian, Mageia, and whatever VMs I happen to be playing with
Posts: 11,690
Blog Entries: 12

Rep: Reputation: 2904Reputation: 2904Reputation: 2904Reputation: 2904Reputation: 2904Reputation: 2904Reputation: 2904Reputation: 2904Reputation: 2904Reputation: 2904Reputation: 2904
As basically a hobbyist user, I use redirection and pipes frequently, especially in conjunction with the ps and cat commands.

It makes troubleshooting much more efficient.
 
Old 09-16-2012, 04:36 AM   #6
ronss
Member
 
Registered: Mar 2002
Location: phoenix,az
Distribution: red hat linux enterprise-centos
Posts: 766

Original Poster
Rep: Reputation: 31
thanks for the answers, very good ones...i quess i am still trying to figure out where exactly i would use the redirection operatives,,?
 
Old 09-16-2012, 04:40 AM   #7
ronss
Member
 
Registered: Mar 2002
Location: phoenix,az
Distribution: red hat linux enterprise-centos
Posts: 766

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by linux_BSD View Post
Yes indeed. The more knowledge you have of using redirection and pipes, you can filter out results and extract any info you want and you can create custom commands. Linux is powerful.
okay,,,i just used the cat command to combine the text of 2 files in the home directory......trying to learn a few things, sometimes just does not sink in to my dense brain fast

Last edited by ronss; 09-16-2012 at 05:29 AM.
 
Old 09-16-2012, 06:20 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
Originally Posted by ronss View Post
thanks for the answers, very good ones...i quess i am still trying to figure out where exactly i would use the redirection operatives,,?
Common examples:
Using SED to replace words in a file, and write to a new file:
Code:
sed 's/old/new/g' filename > newfilename
Reading from a file "filename":
Code:
while read line; do
   stuff to do
done < filename
 
Old 09-16-2012, 07:37 AM   #9
Fred Caro
Member
 
Registered: May 2007
Posts: 999

Rep: Reputation: 166Reputation: 166
ronss,
redirection can input the result of one command into the next command. That can be taken to the next command and so on. The output is normally sent to the screen but can be sent also to a file as well, with the tee command or just to file.
At a simple level it is useful to make a copy of the results of command enquiries,e.g.,

dmesg > file.txt

or you can get the results displayed by a program:

dmesg | less

that would make it easier to read but it is not recorded.

or,

you could do:

dmesg | tee dm1.text | less

This would make and copy to a file while sending to a program that displays the results on the screen in a way that more readable, see man less.

Fred.
 
Old 09-16-2012, 10:25 AM   #10
linux_BSD
Member
 
Registered: Sep 2012
Posts: 47

Rep: Reputation: 4
Quote:
Originally Posted by ronss View Post
okay,,,i just used the cat command to combine the text of 2 files in the home directory......trying to learn a few things, sometimes just does not sink in to my dense brain fast
I was going to give you examples of doing this but the others did a good job of doing this.

However, there is another redirection that is important. It is the double right arrows. The double right arrows allows you to append data to an existing file. For example, if you did something like this

Code:
cat file1 file2 > newfile
Now, lets say you want to add data from file3 to newfile without erasing it. You do it like so

Code:
cat file3 >> newfile
Am example of using pipes to extract any info from a program's output

Code:
mplayer -identify video.mp4 -vo dummy -ao dummy 2>&1 | grep AUDIO
ID_AUDIO_ID=0
ID_AUDIO_FORMAT=MP4A
ID_AUDIO_BITRATE=159896
ID_AUDIO_RATE=48000
ID_AUDIO_NCH=2
AUDIO: 48000 Hz, 2 ch, s16le, 159.9 kbit/10.41% (ratio: 19987->192000)
ID_AUDIO_BITRATE=159896
ID_AUDIO_RATE=48000
ID_AUDIO_NCH=2

I want to extract 48000 in bold to use as a variable in a shell script. Here is how I do itv

Code:
mplayer -identify video.mp4 -vo dummy -ao dummy 2>&1 | grep ID_AUDIO_RATE | cut -d\= -f2 |uniq
48000

As you can see 48000 is the only output I wanted.

Here's an example of command line chaining that is taking the output of one command and piping it to another.

Code:
seq -w 1 39 | sort -R | head -5 |sort -n | fmt
02 09 30 34 37

This will produce 5 random numbers from 1 through 39

I hope this and the other members insight of pipes and redirection has help you. If you need more info just ask or google shell scripting pipes and redirection for additional tutorials.

good luck

Last edited by linux_BSD; 09-16-2012 at 03:03 PM.
 
Old 09-16-2012, 12:11 PM   #11
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Read the solutions to problems posted in these forums. A fairly high percentage of solutions to commandline-oriented problems will make some use of IO redirection by the shell.

Allow me to speak to the broader subject of standard input and output.
Understanding and making use of the concept of standard IO (and not just in commandline-based applications) is one thing that distinguishes a Windows-work-alike user from a Linux/Unix expert, IMHO. The concept is not complex or difficult, but is under-emphasized by most training material that I've seen. It is an elegant glue that binds together small pieces in order to make bigger systems.
In some cases, it simplifies the creation of more substantial framework systems. For instance, the xinetd daemon leverages the use of stdio by having all of it's sub-member components simply read standard input and write standard output. Because it connects to those sub-components through stdio, it becomes trivial for the writers of those components to gain network access, even though they don't know anything about the network machinery that is in use. Similarly for WWW CGI application writing. The web server just listens for the CGI's standard output data, and forwards it to the client browser. All of the networking is handled by the server, and the CGI application writer is free to focus on the application, knowing that the network machinery is being managed by the component most suited for the task.
Did you know that the concept has been present (in a limited form) on MS OS's since the very early days of MS-DOS?

--- rod.
 
Old 09-16-2012, 04:55 PM   #12
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
Even in the early days of windows 3.1 I'm sure I used to use
Code:
dir c:\ | more
and
Code:
ipconfig /all > network.txt
I don't think of these shell redirect characters as being specific to Linux, I suppose.
 
Old 09-17-2012, 05:52 AM   #13
ronss
Member
 
Registered: Mar 2002
Location: phoenix,az
Distribution: red hat linux enterprise-centos
Posts: 766

Original Poster
Rep: Reputation: 31
thanks all .....will take a look at the threads, and do a bit command work..didn,t believe i would get this much response to my question..but thats cool..
 
Old 09-17-2012, 08:00 AM   #14
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 12,604
Blog Entries: 25

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Member Response

Hi,

Look at Rute for some good examples;Just a few more links to aid you to gaining some understanding;



1 Linux Documentation Project
2 Rute Tutorial & Exposition
3 Linux Command Guide
4 Bash Beginners Guide
5 Bash Reference Manual
6 Advanced Bash-Scripting Guide
7 Linux Newbie Admin Guide
8 LinuxSelfHelp
9 Utimate Linux Newbie Guide
10 Linux Home Networking
11 Virtualization- Top 10

The above links and others can be found at 'Slackware-Links'. More than just SlackwareŽ links!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Need help for Windows cmd commands into Linux terminal commands. windowsNilo Linux - Software 2 07-02-2008 07:26 PM
Need help for Windows cmd commands into Linux terminal commands. windowsNilo Linux - General 2 07-01-2008 07:53 AM


All times are GMT -5. The time now is 01:11 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration