File Commands
Creating and Editing Files
To create files, you should use a UNIX text editor such as pico, emacs or vi.
Listing Files
The see a listing of files and directories, you can use the following command:
ls
common options:
-a
| List all entries; in the absence of this option, entries whose names begin with a "." are not listed. |
-l |
List in long format, giving mode, number of links,owner, size in bytes, and time of last modification for each file. |
-R |
Recursively list subdirectories encountered. |
-F |
Mark directories with a trailing slash (/), executable files with a trailing asterisk (*) and symbolic links with a trailing at-sign (@). |
For a full listing of the options available for use with the ls command, see the man
pages. Type:
man ls
Example: ls -al
Removing Files
To remove a file, you can use the following command:
rm <filename>
common options:
-i |
Interactive.With this option, rm prompts for confirmation before removing any write-protected files. |
-r |
Recursively remove directories and subdirectories in the argument list. The directory will be emptied of files and removed. |
Example: rm -i homework1
Note: To delete a file starting with a "-", type rm - -<filename>
Viewing Files
To view files, you should use a pager such as more or less. Both commands will display the contents of the file,
one screenful at a time. less, however, allows backswards movement within the file as well.
more <filename>
Example: more quiz1
Example: less quiz1
Copying Files
To make a copy of a file, you can use the following command:
cp <filename> <filename>
common options:
-i |
Interactive. cp will prompt for confirmation whenever the copy would overwrite an existing target. |
-r |
If filename is a directory, cp will copy the directory and all its files, including any subdirectories and their files; target must be a
directory. |
Example: cp essay1 essay2
Moving and Renaming Files
To rename a file, you can use the following command:
mv <filename> <filename>
common options:
-i |
Interactive. mv will prompt for confirmation whenever the move would overwrite an existing target. |
Example: mv program1 program2
Determining File Formats
To determine the format of a file, you can use the following command:
file <filename>
Example: file project1
Comparing Files
To compare two files, you can use the following command:
diff <filename> <filename>
Example: diff lecture1 lecture1b
Concatenating Files
To combine two or more files, you can use the following command:
cat <file1> <file2> ><file3>
Example: cat homework1 homework2 >homework3
This will concatenate homework1 and homework2, then
write the results to homework3.
Last updated:
February 03, 2011