What is Redirection and How Does it Work?
Redirecting the Input or Output of Linux Commands
Another useful bash feature is its ability to redirect the input and output of Linux commands. You can save the results of a command in a file instead of displaying the results on the screen, or you can feed data from a file to a program instead of entering data from the keyboard.
Let's look at redirection first. Imagine a fictitious command called nocats that prompts the user for a number and then waits for that many lines of text to be entered before processing them. (The program looks at each input line and pr ints only the ones that do not contain the word cat.)
You could feed the program by entering the data from the console (bold text is your typed input, normal text is console output):
$ nocats
3
Dogs are much better than those other household animals.
A cat would never beg for jerky treats.
Dogs are pretty stupid, but at least they stick around.
Dogs are much better than those other household animals.
Dogs are pretty stupid, but at least they stick around.
Or using a text editor, you could put all the input data in a file called stuff and feed the nocats program like this:
% nocats < stuff
Dogs are much better than those other household animals.
Dogs are pretty stupid, but at least they stick around.
The less-than (<) symbol causes the program to get input from the stuff file instead of waiting for keyboard input. The greater-than (>) symbol, on the other hand, redirects output to a file instead of to the console. Thus, the co mmand
% nocats < stuff > bother
will cause the nocats program to read its input from one file (stuff) and write it to another (bother), without the keyboard or console entering the picture. Note that the nocats progra m doesn't know or care about all this redirection. It still thinks it is reading data from the keyboard and writing to the console--but the shell has temporarily reassigned the input and output to files instead of physical devices.
To append to an existing file instead of creating a new one, use two greater-than symbols (>>), as in this example:
zippity > somefile
doodah >> somefile
The zippity command runs first, and the output is placed in a new file called somefile. Then doodah runs, and its output is added (appended) to the somefile file.
LINKS TO THE OTHER TUTORIALS
Leason 1: Living in a shell
Leason 2: Root and Other Users
Leason 3: Virtual Consoles
Leason 4: Logoff and Shutdown
Leason 5: Choosing a Shell
Leason 6: The Command Prompt
Leason 7: Wildcards
Leason 8: Command History and Editing
Leason 9: Aliases
Leason 10: Redirection
Leason 11: Pipelines
Leason 12: Processes
Leason 13: Stopping a Program
Leason 14: Environment Variables
Leason 15: Help!
- Board index General Knowledge Everything Linux
-
- It is currently 03 May 2025, 18:18
- All times are UTC-04:00
Linux Basics (tutorial) #10
Discuss everything about Linux here!
Moderator: Community Moderator
- MrNiitriiX
- Premium Uploader
- Posts: 2197
- Joined: 19 Apr 2010, 14:52
- Location: between space and time
- Has thanked: 24 times
- Been thanked: 164 times
Linux Basics (tutorial) #10
Post by MrNiitriiX »
Last edited by MrNiitriiX on 31 Jul 2010, 13:04, edited 1 time in total.
Jump to
- ↳ Site Help & Support
- ↳ Site Tutorials & How To
- ↳ Site Support/Bugs
- ↳ Suggestion & Feedback
- ↳ General Discussions
- ↳ News
- ↳ Trades & Offers
- ↳ Sports
- ↳ Relax & Enjoy!
- ↳ Jokes
- ↳ General Knowledge
- ↳ Tips, Tweaks & Quick Tutorials
- ↳ OpenSource/Freeware & Useful Links
- ↳ Everything Linux
- ↳ General Support
- ↳ Technical Support
- ↳ Software Chat & Support
- ↳ Hardware Chat & Support
- ↳ Smartphones and other Portable Devices Chat & Support
- ↳ Programming/Web Building Chat & Support
- ↳ User Uploads list
- ↳ Graphics & Images
- ↳ Graphics Requests & Helpdesk
- ↳ CC Graphics & Images
- ↳ ChattChitto RG
- ↳ ChattChitto RG - Apps
- ↳ ChattChitto RG - Movies
- ↳ ChattChitto RG - Music
- ↳ Apps
- ↳ Portable
- ↳ Beta
- ↳ Games
- ↳ PC
- ↳ PS2
- ↳ PS3
- ↳ Wii
- ↳ Xbox
- ↳ PSP
- ↳ Other Consoles
- ↳ Movies
- ↳ Rips
- ↳ Pretime Releases
- ↳ DVD-Rip
- ↳ BR-Rip/BD-Rip
- ↳ 3D-Rips
- ↳ Full Rips
- ↳ DVD-R
- ↳ BR-R
- ↳ TV Shows
- ↳ HDTV Shows
- ↳ Anime/Cartoons
- ↳ Sport
- ↳ Music
- ↳ Electronic / Dance Music
- ↳ Pop / Rock Music
- ↳ Hip-Hop / Rap Music
- ↳ R&B / Soul Music / Jazz / Reggae
- ↳ Soundtrack / Classical Music
- ↳ Other Genres
- ↳ Music Videos
- ↳ DJ Mix sets & Mash-Ups
- ↳ eBooks
- ↳ Audio
- ↳ Magazine
- ↳ Mac and Other OSes
- ↳ Others
- ↳ Templates, Fonts & Scripts
- ↳ Wallpapers & Images Pack
- ↳ Tutorials - Educational Material
- ↳ Mobiles & Media Players
- ↳ Apps
- ↳ Portable
- ↳ Beta
- ↳ Games
- ↳ PC
- ↳ PS2
- ↳ PS3
- ↳ Wii
- ↳ Xbox
- ↳ PSP
- ↳ Other Consoles
- ↳ Movies
- ↳ Rips
- ↳ Pretime Releases
- ↳ DVD-Rip
- ↳ BR-Rip/BD-Rip
- ↳ 3D-Rips
- ↳ Full Rips
- ↳ DVD-R
- ↳ BR-R
- ↳ TV Shows
- ↳ HDTV Shows
- ↳ Anime/Cartoons
- ↳ Sport
- ↳ Music
- ↳ Electronic / Dance Music
- ↳ Pop / Rock Music
- ↳ Hip-Hop / Rap Music
- ↳ R&B / Soul Music / Jazz / Reggae
- ↳ Soundtrack / Classical Music
- ↳ Other Genres
- ↳ Music Videos
- ↳ DJ Mix sets & Mash-Ups
- ↳ eBooks
- ↳ Audio
- ↳ Magazine
- ↳ Mac and Other OSes
- ↳ Others
- ↳ Templates, Fonts & Scripts
- ↳ Wallpapers & Images Pack
- ↳ Tutorials - Educational Material
- ↳ Mobiles & Media Players
- ↳ Short Videos
- ↳ Regular Movies
- ↳ Hentai Movies
- ↳ Ebooks, Pics & Wallpapers
- ↳ Jokes
- ↳ Adult Sites Passwords
- ↳ Requests
- ↳ Apps requests
- ↳ Completed Requests
- ↳ Games requests
- ↳ Completed Requests
- ↳ Movies & TV Shows requests
- ↳ Completed Requests
- ↳ Music requests
- ↳ Mix Tape
- ↳ Completed Requests
- ↳ Other requests
- ↳ Mac requests
- ↳ Completed Requests
- Social Life
- ↳ Animals and Pets
- ↳ Photography
- ↳ Handy Hints for Everyday Living
- ↳ Traveling
- ↳ Motor Racing
- ↳ Cars and Bikes
- ↳ Aircrafts
- ↳ Marine/Submarine Vessels
- ↳ Cooking
- ↳ Sports
- ↳ Gadgets
- ↳ Gifts
- ↳ Forex