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 profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

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:  30614 views
Comments:  

Files

Now that you know how to move around the file system on the command line, it's time to start working with files. This section teaches you how to create an example file, copy a file, remove a file, and view and change basic file permissions. In a multiuser operating system like UNIX, it's crucial to understand ownership and permission constructs.

touch

To begin, create an empty file that you can use for this tutorial. The touch command can be used to create an empty file (it's normally used to update the modified date and accessed date of a file by touching it).

Go back to the TUTORIAL directory in your user's home, and create a file by typing the following command:

$ cd ~/TUTORIAL
$ touch example.txt
				


cp

The cp command copies files. Type cp followed by the name of the file you want to copy, followed by the directory you want to copy the file to (you have the option of specifying a new filename as well). For instance, try copying the example.txt file to /tmp/:

$ cp example.txt /tmp/
$ ls /tmp/
				

You should see example.txt in /tmp/. Now, copy the file in /tmp/ back to your current directory, but give it a new name:

$ cp /tmp/example.txt ./example2.txt
$ ls
				

Notice the use of a dot to specify that you want to put this new file in your current directory. It isn't necessary to include ./ in this case (because the default path for a copy is your current working directory), but it helps clearly illustrate what you intend to do. The subsequent ls command shows that you now have two example files in your current working directory.


mv

The move command is completed with mv. Most syntax and command-line options for move and copy are the same. If you want to move your new file, example2.txt, out of the current directory and into /tmp/, type the following:

$ mv example2.txt /tmp/.
				

Note again that a dot is used to explicitly call out what you're doing.


rm

Remove the files created in /tmp/ to tidy up your system. The rm command deletes files from your file system. This isn't like moving a file into the Recycle Bin or Trash; the command deletes the file pointer, so use the rm command with caution. Type the following:

$ rm /tmp/example.txt
$ rm /tmp/example2.txt
$ ls /tmp/
				

Both example files in /tmp/ should be gone.

Depending on which UNIX-like operating system you're using, you might have other delete commands available, such as srm or can. Try typing man srm and man can to see if you have these commands. srm is used as a secure version of rm, which writes random data over deleted files to keep them from being restored. can is the opposite of srm in some ways; can retains the file but moves it into a special trash directory similar to the Windows Recycle Bin.

4 of 12 | Previous | Next

Comments



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=