( click here to skip navigation links )
 
 NC State University  College of Engineering

-( Eos )-
 
 < Home  News  Guide  Labs  Software  Help  About
 
Accounts and Policies : Labs and Platforms : Logging In and Out : User Space and Quota : Application Software : Mail and Web :
Working Directories : Files and Commands : Printing : Managin File Space : UNIX and Linux : AFS Overview : Remote Access
 
     
 

Files and Commands

   
 
         
     

There are many ways to manipulate and work with files in Solaris, Linux, and Windows. Windows relies on functions selected from menus rather than command control. This chapter discusses common commands used on Solaris/Linux for the management of files and data.

A command is simply a program that the computer runs. UNIX commands are typed after the prompt (eos% or unity%) in an Xterm window. Commands must be typed in the precise syntax that the interpreting shell program accepts. UNIX is case sensitive, and most commands are lowercase. When the user presses the Enter key, the shell processes the command and then displays the prompt again, which asks (prompts) the user for the next command.

Before typing commands, check the path (pwd) to make sure you issuing commands in the directory you intend. You can always find out exactly where you are in the file tree with pwd. The system responds by displaying the path of the working (current) directory.

File Naming

Operating systems have conventions that users need to adhere to in naming their files and directories. File names can be up to 256 characters in length, but obviously, users will want their file names to be relatively short, easy to remember, and accurately descriptive of the file contents. UNIX file-naming, like command entry, is case sensitive. As a result, the three names--file, FILE, and File--would represent three different files, not one. Also, no spaces are permitted. On Windows, case is not accurately represented, and spaces are permitted. To move among platforms easily, create file names with lowercase letters and no spaces.

Although most characters will be accepted in a file name, many should be avoided since they can cause problems or confusion. Some characters have already been assigned special meaning. For example, you should not use the "slash" character ( / ), which is the symbol for a directory (folder). Also, spaces in file and directory names should be avoided. Although they are permissible on the Windows OS, they are not on UNIX, so moving files between platforms can cause you problems.

The period should be avoided since it is usually used to create an extension. An extension adds characters to the end of a file name to identify it further; for example, file.c is C code, file.xls is a Microsoft Excel file, and file.txt is ASCII text. Also, if you use the period as the first character of a file name, you create a hidden file or dotfile, e.g., .mylogin. A dotfile is generally a configuration file not usually seen in ordinary directory listings.

UNIX Command Options and Arguments

Commands tell the system what actions to perform. However, if the user wants to modify those actions and specify that the action be performed in a particular way on a certain file, then the user must add that information to the command as options and arguments.

The portion of a command that names the files(s) or entity to be affected by the command is called an argument. Options are generally single characters or a whole word preceded by a hyphen. The option specifies a variation in the basic command, telling it to do something in a specific way.

Command syntax order:
command option(s) argument

A full command consists of a command, followed by zero or more options, followed by zero or more arguments. Command options may be:

  • a single character (-l )
  • a series of single-character options (-alt)
  • a word (-landscape)
  • characters or words followed by a word or number (-K2)

For example, if you type the command ls to list the files in a directory, the system will display a multi-column list of file and subdirectory names only. However, if you want more information about these files, e.g., when they were created or how big they are (View -> Details in Windows), then you would add the -l option to the command to specify a "long" listing. If you want hidden files (dotfiles) listed as well, you would add the option -a for "all."

Additional options and arguments can be added on. For example, if jqpublic is in his home directory and wants a long listing of all files in his ~/homework subdirectory in the order they were created with the most recent first, he adds t for time to -al, or:

ls -alt homework

His directory listing would look something like the following:

access

links

user

size

date last modified

time last modified

file

rwxr-xr-x

l

jqpublic

1298

May 8

10:03

hwk1

-rw-rw-r-

l

jqpublic

8900

June 19

08:23

hwk2

Common Commands

To list files in a directory, see ls command in Managing Directories.

Display File Contents (more, less)

The more command displays the contents of a file one screenful of text at a time in a terminal window. It normally pauses after each screenful, printing -More- at the bottom of the screen. If the user presses the spacebar, another screenful is displayed. The user can also move forward
one page at a time by pressing the f key, and backward by pressing the b key. The q key quits or closes the file, the h key brings up a help screen, and the RETURN key scrolls the text line by line. When more reaches the file's end, it exits and returns the prompt.

The less program is similar to more. However, instead of printing -More- at the bottom of each screen, less tells the percentage of the file that has been displayed up to that point, giving you some idea about how much of the file is left to page through. The less program also does not exit when you reach the end of the file; you must type q to exit.

more is also on Windows as a filter command to display the contents of a file or the output of a command one screen at a time. To send input from a file to a filter command, use the less-than sign (<), more < list.txt. Use the pipe (|) if more gets its input from another command.

Concatenate Files (cat)

The cat command (short for concatenate, to link together) also displays the contents of a file on your screen. cat file dumps the contents of a file in one burst onto the screen, so if the file is very long, it will scroll past faster than you can read it. Used with other operators, cat will combine or merge the contents of two or more files in another file. In addition to these commands, the cat command can also be used to read input directly from the keyboard to create a new file, i.e., cat >> file

To merge or link two files together and create a third file:

cat file1 file2 > file3

To append two files to the contents of the third file:

cat file1 file2 >> file3

To merge or link two files together and overwrite a third file:

cat file1 file2 >! file3

To input (type) directly to an already existing file:

cat >> filename

When finished with direct keyboard input, press CONTROL d to quit.

Copy Files (cp)

The cp command copies the contents of one file to another. The file name and contents remain unchanged in the source file, and a copy of them is placed in the target file. The target file may have the same name or a new one, but the contents of the two files will be identical.

Say, for example, you want to copy the file hwk1 to the file hwk1a (cp hwk1 hwk1a). If hwk1a does not exist, it will be created and will contain the contents of the hwk1 file. If it does exist, its contents will be overwritten by the contents of the hwk1 file. However, if hwk1a is a directory, the system will know that, and will place a copy of the hwk1 file in that directory, hwk1a/hwk1. The file is the same in both places. If you want to copy files from one directory to another, you must specify a path.

cp hwknew/hwk1 hwkold

to copy the file hwk1 to the hwkold directory.

To copy a complete directory with all its files, add -r :

cp -r directory1 directory2

On Windows, you select the file and Edit -> Copy. Or, right-click the file and choose Copy from the pop-up menu. Open the folder where you want to copy the file and Edit -> Paste. Or, right-click and choose Paste from the menu.

You can also drag and drop files into new folders. If you drag a file to a folder on the same disk, it will be moved. If you drag it to a folder on another disk, it will be copied. You can hold down a key while dragging for the following:

To move a file, press SHIFT.
To copy a file, press CTRL.

Move and Rename Files (mv)

The mv command is used both for moving and renaming files. The mv command moves the contents of the source file to a target file and then erases the source file. The effect is essentially that of renaming the original file. For example,

mv hwk1 hwk1old

will give the hwk1 file the new name hwk1old. The contents of the hwk1old file are the same as they were in hwk1; only the name of the file is different.

The mv command also moves files as well as renames them. To move your hwk1 file from the hwknew directory to the hwkold directory

mv hwknew/hwk1 hwkold

The system knows that hwkold is a directory and will not try to rename the hwk1 file hwkold. It knows to move the hwk1 file into the hwkold directory and to preserve the file name. hwk1 no longer exits in the hwknew directory.

On Windows, you select the file and Edit -> Cut. Or, right-click the file and choose Cut from the pop-up menu. Open the folder where you want to move the file and Edit -> Paste. Or, right-click and choose Paste from the menu. To rename, select File -> Rename, or right-click and select Rename from the menu.

You can also drag and drop files into new folders. If you drag a file to a folder on the same disk, it will be moved. If you drag it to a folder on another disk, it will be copied. You can hold down a key while dragging for the following:

To move a file, press SHIFT.
To copy a file, press CTRL.

Remove Files (rm)

To remove files from your account, use the rm command. This command will permanently remove the files from your directory space and cannot be undone. For more information about rm and its options, man rm.

On the Eos/ Unity system, the rm command is "aliased" to rm -i (-i for interactive) so that rm will prompt the user for confirmation before removing any write-protected files. This safeguard is good to maintain. However, if you want to remove files without prompting, you can unalias rm with \rm, e.g., type \rm file, or \rm * to remove all the files in the directory you are working in. Use \rm with great care!

To see more options, type manfile to bring up a page from the online UNIX manual on the command.

On Windows, you select the file and File -> Delete. Or, right-click the file and choose Delete from the pop-up menu.

Aliases (alias)

Often, you find yourself typing the same commands, paths, or filenames over and over. If you wish, you can create substitutions or aliases for these, using something shorter and easier to remember. For example, if you want to use the shorter command md instead of mkdir to make a directory:

% alias md mkdir

The command alias is followed by the substitution you want to use and then the original path, filename, or command (or a combination of these) that you want the alias to replace. If you have aliases that you want to use all the time, put them in your ~/. mycshrc file.

To remove an alias, use the unalias command. For example, to undo the md alias you created for the UNIX mkdir command:

% unalias md


   
Related Resources

E115 Course

Working with Files (in Guide, PDF)

Command Summary (in Guide, PDF)

File Naming

Avoid in file names:

Ampersand &
Asterisk *
"At" sign @
Brackets [ ] { }
Caret ^
Dollar sign $
Parentheses ( )
Percent sign %
Pipe |
Quotation marks " " ' '
Tilde ~
Slashes / \
Spaces

Naming Conventions for Extensions

.bmp bitmap graphics
.c C program
.C C++ program
.doc Word doc
.f Fortran program
.gif Graphics Interchange Format
.html, htm HyperText Markup Language
.pdf Portable Document Format
.ps PostScript
.tar tape archive
.tex TeX document
.txt ASCII text
.xls Excel
.Z compressed

Do not give files in a directory the same name, and make sure that you follow a system that helps you remember file names.

Definitions

alias
argument
cursor
dotfile
executable
file
filter
man pages
metacharacter
option
operators
path(name)
pipe
program
prompt
redirection
standard input
standard output
wildcard

Command Summary

alias
unalias
create pseudonym or shortcut

cat file

display, merge, or con- catenate files

cp file newplace

copy a file to a new location

help

launch Web browser to Eos/Unity help

head file
tail file

display first or last ten
lines of a file

history
up arrow to repeat command

list all commands used in a session

kill pid end process (see ps)

kreset

obtain more tickets to continue working

less file
more file

display fille contents

ls

list contents of a directory

logout

log user out

man command (see UNIX)

show UNIX manual page for the named command

mv old new
mv file newplace

rename file, preserving the contents but under a new name; move file from one location to another

ps -e list processes and process ID numbers (pid)

pwd

display path of working directory

rm file

remove file; file cannot be restored if removed

Copy and Paste between Windows

On Solaris/Linux:

Drag over content, point where content should go, and click middle mouse button.

On Windows:

Drag over content or Ctrl a to select all, then Ctrl x (cut) or Ctrl c (copy), and Ctrl v (paste).

Tab, Don't Type

Rather than typing out long path and file names, type enough to distinguish your selection from other names in the directory, and hit Tab to finish the typing for you.

Repeat Commands

Use the up-arrow cursor key to find and repeat commands you just typed. Also, the history command lists your previous commands (!! to repeat last command, ! followed by line number to repeat command on a certain line, e.g., !34).

Cancel and Suspend

Ctrl c to cancel or Ctrl z to suspend a command or process

Wildcard

The asterisk * (called a star) can be used in place of any string of characters. For example, if you want to list, copy, remove, etc., only the files in a directory with the extension .doc, type
command *. doc
to operate on all of the files with this extension rather than singly.

 
 
         

< to Top]

 < Home  News  Guide  Labs  Software  Help  About

 

Information Technology and Engineering Computer Services (ITECS)
College of Engineering, North Carolina State University, Raleigh, NC 27695
Comments to eoshelp@ncsu.edu. URL: http://www.eos.ncsu.edu/

[ ENGR Template ]

 

This support page is for students, faculty, and
staff at North Carolina State University.