Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

UNIX tips and tricks for a new user, Part 1: File maintenance tools

Tim McIntire, Consultant, Freelance Writer
Photo of Tim McIntire
Tim McIntire works as a consultant and co-founder of Cluster Corporation, a market leader in HPCC software, support, and consulting. He also contributes periodically to IBM developerWorks and Apple Developer Connection. Tim's research, conducted while leading the computer science effort at Scripps Institution of Oceanography's Digital Image Analysis Lab, has been published in a variety of journals, including Concurrency and Computation and IEEE Transactions on Geoscience and Remote Sensing. You can visit TimMcIntire.net to learn more.

Summary:  Systems administrators can use a number of programs to maintain files in a UNIX® system from the command line. In this tutorial, you'll experiment with commands, such as cd, cp, and tar, to navigate a UNIX file system from the command line and work with files and directories. The cd command changes directories, cp duplicates files or directories, and tar quickly groups files into an archive. You'll also learn how to deal with file permissions and perform simple input/output.

View more content in this series

Date:  26 Sep 2006
Level:  Intermediate PDF:  A4 and Letter (72 KB | 22 pages)Get Adobe® Reader®

Activity:  22078 views
Comments:  

Input and output

Input and output are often thought of in simple default terms: keyboard/mouse and monitor/speakers. In UNIX, access to a system of input and output streams (and an error stream) gives users and developers a way to efficiently move input and output to and from applications, which can streamline complex processes by minimizing human interaction. stdin, stdout, and stderr are significant parts of what make UNIX an ideal platform for scripting.

stdin, stdout

Most command-line applications can take input from stdin and direct output to stdout. stdin stands for Standard Input. stdout stands for Standard Output. By default, input from stdin comes from the keyboard (what you type in your terminal), and output to stdout goes to your display (what you see on your screen). An additional type of output, stderr, prints errors and debugs messages, but this tutorial focuses on stdin and stdout.


Redirection

Redirection allows users to send output that would normally go to stdout to another destination -- a file, for instance. Create a text file of your TUTORIAL directory listing, as follows:

 
$ cd ~/TUTORIAL 
$ ls > listing.txt 
$ ls
				

Another form of redirection is >>, which appends a file as opposed to creating a new file. You can also redirect stderr to a file with 2> or to redirect all output ( stdin and stderr) to a file with &>. You can combine forms of redirection; for instance, use 2>> to append stderr to a file.


cat

Now that you have a file with some text in it, look at what's inside. The quickest way to examine the contents of a file is by using the cat command, which stands for Concatenate; it can be used in combination with redirection to concatenate text. Type the following command:

 
$ cat listing.txt 
				

If you've executed each step in the tutorial, you should see something like this:

 
example.txt 
example2.txt
listing.txt 
script.sh 
				


more

The previous cat command was executed on a relatively small file, so you can easily see all the data on a single page. If you're viewing a file that doesn't fit on a single page, the more command is commonly used; it pauses the output each time a full page of data is displayed to stdout. Pressing the Spacebar advances the output to the next page. Try creating a longer file to use for an example:

 
$ ls /etc/ > listing2.txt
$ cat listing2.txt 
$ more listing2.txt 
				

If you use cat, the file scrolls much too quickly to read; but if you use more, you can press the Spacebar to advance through the output step by step.


head and tail

If you quickly want to view the first or last few lines of a file, you can use head or tail. These commands are commonly used to view the top of script or the bottom of a log file. It's also handy to use them as a quick sanity check on an output file when you're debugging code. Try typing the following:

 
$ head listing2.txt 
				

To view the last few lines of a file instead of the first few lines, try testing tail:

 
$ tail listing2.txt 
				

Both commands commonly default to displaying 10 lines, but you can use the -n option to display any number of lines. For instance, type this command:

 
$ head -n 2 listing2.txt 
				


grep

Now that you're starting to create files with more data in them, you might want to search for specific text. grep is a powerful search utility used here in a bare-bones example:

 
$ grep host listing2.txt 
				

This command outputs all the lines in listing2.txt that contain the string host.


pipe

To skip the file-creation step, you can use the pipe character (|) to use the output of one command as the input for another command. This is another form of redirection and is incredibly powerful for linking long lists of commands to efficiently manage input and output. Try this simple example:

 
$ ls /etc/ | grep host 
				

This command gives the same output as the two-step process listed previously. The single line takes the output of the ls command and uses it as the search input for the grep command. Here's another example:

 
$ du -sh /etc/* | more 
				

In this case, you check the disk usage (du) for each file and directory in /etc/. The output is more than one page, so it's useful to pipe the result to more, which makes the output pause each time one page of data is displayed.

9 of 12 | Previous | Next

Comments



Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=AIX and UNIX, Linux
ArticleID=162888
TutorialTitle=UNIX tips and tricks for a new user, Part 1: File maintenance tools
publish-date=09262006
author1-email=Tim.McIntire@gmail.com
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.