- Working with Files -

The following commands are used to list, display, move and delete files. Creating and editing files is covered in the section File Editing. This is because there are a number of editors you can use to create files, and each of them is used differently.

ls

Typing ls will display the files and directories in the current directory.

hostname:~bugsy> ls

unix1.html   unix2.html   unix3.html   utemp.html
      

If the filename begins with a . it will not be displayed. To display these files, you will need to use the -a option with ls, i.e.:

hostname:~bugsy> ls -a

.  ..   unix1.html   unix2.html   unix3.html   utemp.html
      

Note the 2 new files listed . and .. these are a unix shorthand to represent the current directory (.) and the parent of the current directory (..)

Now this still doesn't tell us which of the files are directories, and which are files. To do this, we would use the -F option.

hostname:~bugsy> ls -aF

./   ../   unix1.html   unix2.html   unix3.html   utemp.html
      

The / after the file name, indicates that the file is in fact a directory. If the file was a command that was able to be run it would be considered to be executable, and would be followed by a *.

The -l option will give you still further information about the files in any directory.

hostname:~bugsy> ls -al

Output from ls -al
Output of ls -al

The long format provides the following information about each file:
Total n - n amount of storage used by the files in this directory.
Type - The first character of the file type & access mode field, tells whether the file is a directory (d) or a plain file (-). (There are other less common types that aren't explained in this tutorial).
Access modes - The remaining 9 characters in the file type & access mode field, specifies three types of users (yourself, your group, all others) who are allowed to read (r), write (w), or execute (x) files.
Links - The number of files and directories linked to the file.
Owner - Who created or owns the file.
Group - The group that owns the file.
Size (in bytes) - The size of the file.
Modification date - When the file was last modified.
Name - Name of the file or directory.

cat

Typing cat filename will display the contents of filename on your screen. If the file you wish to view is longer than the length of the screen it is more advisable to use the more command.

more

This command differs from cat, in the fact that it displays the file one page at a time. It also allows you to move forward and backwards by any number of pages or lines. It is invoked by typing more file(s) at the prompt.

Useful More Commands
space - Display next page.
return - Display next line.
nf - Move forward n pages.
b - Move backward one page.
nb - Move backward n pages.
/word - Search forward for word.
?word - Search backwards for word.
v - start the vi editor at this point.
Ctrl-L - Redisplay current screen
h - Help.
:n - Go to next file on command line.
:p - Go back to previous file on command line.
q - Quit more before end of file.

cp

This command allows you to copy files and directories. It can be used in one of three ways.

cp old_filename new_file_name - to copy the file old_file_name to a new file called new_file_name.

cp filename directory_name - to copy the file filename into the directory directory_name. The new file will have the same name as the original file.

cp -r old_directory_name different_directory. The -r option recursively copies old_directory and any files and subdirectories it contains into an existing directory, different_directory

mv

This command allows you to move files and directories around on the system with the command mv Source Target
It works as follows:

Source Target Result
File name Rename file as name
File Existing file Overwrite existing file.
Directory name Rename directory as name
Directory Existing Directory Move directory into Existing Directory
One or more Existing Files Directory Move File(s) to Directory
rm

The rm command removes unwanted files, using rm filename(s).

NOTE: Deleted files can not be undeleted.



Page Created 6th March 2005.
Last Modified 1st April 2005.