A A
RSS

Unix/Linux 101 – Pipes, Redirections & Wildcards

Sat, Jan 5, 2008

Unix/Linux

Pipes and Redirections

Pipes and Redirections are used when you want to use an input or output of one command or file into another command or file. For instance, you know you can see the users logged onto your machine using the “who” command. Now, to put that list to a file you can use the output redirection symbol “>” followed by the file name:

# who > users.txt

The single “bigger-than” symbol is used to store the data into the users.txt file. In order to append to that file, or – in other words – add more data to it without overwriting, you would use the double “bigger-than” symbols:

# Date >> users.txt

We have just appended the date to our users.txt file. If you use the single “>” instead of the double “>>” you would rewrite the file with the new data and previous information will be erased:

# ls > users.txt

This stores the directory listing in users.txt. If the data of the “who” listing and the “date” were in that file before directing the output of “ls” to that file, then it is now erased.

The “smaller-than” (“<”) redirection is used for input. We can use this redirection when we want to retrieve data from a file into our current command. In this example we’ll use the “mail” command and use the content of the “message.txt” file as the body of the message:

# mail -s “subject” username@address < message.txt

The pipe symbol is the long vertical line “|”. Pipes are used when you want to export the output data of one command into another. For Instance – to sort alphabetically the output of the “who” command, you would type:

# who | sort

To view a directory listing one page at a time:

# ls -la | more

To show just the day in “date”:

# date | cut -c1-3

To find the pattern “maya” in the process list:

# ps -ef | grep maya

For more information about the commands above see the man pages.

Pipes and redirections can work together. E.g. – to store the sorted output of “who” in a file:

# who | sort > users2.txt

Wildcards

Wildcards are characters that represent one, or a set, of other characters, and are meant to save time in typing those out. Like the “..” and “~” shown earlier to represent the parent directory and the home-space, wildcards are used to represent characters in file names. There are 3 main wild cards:

1.”*” – the asterisk represents any number of characters. E.g. – “a*b” matches ab, adrgter345fsdfb or a23b, but not cab

2.”?” – the question mark represents only one character. E.g. – “a?b” matches a0b, afb but not ab or cadb

3.”[]” – the brackets hold an expression which represents a character or a range of characters. E.g. “ab[xyz7-9]c” matches abxc, ab8c, but not abdc or axyc.

Useful examples:
To show a listing of all the files in my home-space that have the extension of “txt”:

# ls ~/*.txt

To copy images 1-7 to a different directory:

# cp image000[1-7].tga /tmp

To rename all files starting with any letter followed by “bc” so they begin with the letter “a”:

# mv ?bc* abc*

To erase all files and all sub-directories from your current location (beware of this command):

# rm -r *

Share and Enjoy:
  • Digg
  • StumbleUpon
  • del.icio.us
  • LinkedIn
  • Facebook
  • MySpace
  • Twitter
  • Technorati
  • Mixx
  • Google Bookmarks
  • email
  • Print
  • Add to favorites
  • NewsVine
  • Yahoo! Bookmarks

Tags: , , ,

Leave a Reply

You must be logged in to post a comment.

Become Our Fan!

Ad Ad

Contact us!

Have something to say? Want to contribute? Want something featured? Let us know! We are still working on our mind reading skills, so feel free to email us: info (at) thescratchpost (dot) com We would love to hear from you!!!
Rss Feed Tweeter button Facebook button Technorati button Reddit button Linkedin button Webonews button Delicious button Digg button Stumbleupon button Newsvine button