May 23, 2021 UNIX Getting started
All data in UNIX is organized into files. A ll files are organized into directories. These directories are organized into a tree structure called a file system.
When you use UNIX, you spend most of your time working with files in one way or another. This tutorial will teach you how to create and delete files, copy and rename them, create links to them, and more.
There are three basic types of files in UNIX:
Normal file: A normal file is a file on the system that contains data, text, or program instructions. In this tutorial, you'll use normal files.
Directory: The directory stores special and normal files. The UNIX directory is the equivalent of a folder for users familiar with Windows or Mac OS.
In order to list the files and directories stored in the current directory. Use the following command:
$ls
Here is an example output of the above command:
$ls
binhosts lib res.03
ch07 hw1pub test_results
ch07.bak hw2res.01 users
docs hw3res.02 work
The command ls supports the -l option, which will help you get more information about the files listed:
$ls -l
total 1962188
drwxrwxr-x 2 amrood amrood 4096 Dec 25 09:59 uml
-rw-rw-r-- 1 amrood amrood 5341 Dec 25 08:38 uml.jpg
drwxr-xr-x 2 amrood amrood 4096 Feb 15 2006 univ
drwxr-xr-x 2 root root4096 Dec 9 2007 urlspedia
-rw-r--r-- 1 root root 276480 Dec 9 2007 urlspedia.tar
drwxr-xr-x 8 root root4096 Nov 25 2007 usr
drwxr-xr-x 2200300 4096 Nov 25 2007 webthumb-1.01
-rwxr-xr-x 1 root root3192 Nov 25 2007 webthumb.php
-rw-rw-r-- 1 amrood amrood 20480 Nov 25 2007 webthumb.tar
-rw-rw-r-- 1 amrood amrood 5654 Aug 9 2007 yourfile.mid
-rw-rw-r-- 1 amrood amrood166255 Aug 9 2007 yourfile.swf
drwxr-xr-x 11 amrood amrood 4096 May 29 2007 zlib-1.2.3
$
Here is information about all the columns listed:
Column 1: represents the file type and gives permissions for the file. This is followed by a description of all types of files.
Column 2: Represents the number of blocks of memory taken by a file or directory.
Column 3: Represents the owner of the file. This is the UNIX user who created this file.
Column 4: Represents a user group. Each UNIX user has an associated group.
Column 5: indicates that the file size is in bytes.
Column 6: Represents the date and time this file was created or last modified.
In
ls -l
manifest example, the lines for each file begin
d
-
, or
l
These characters indicate the type of file listed.
Prefix | Describe |
---|---|
- | Regular files, such as ASCII text files, binary executables, or hard links. |
B | Special block files. Block input and output device files such as physical hard drives. |
C | Character special file. The original input/output device file such as a physical hard drive. |
D | A directory file that contains a list of other files and directories. |
l | Symbolic link file. Link to any ordinary file. |
P | The named pipe. Inter-process communication mechanism. |
s | A socket used for interstational communication. |
Meta characters have a special meaning in UNIX. F
or
*
and
?
Is a meta-character. W
e use
*
to match 0 or more characters, question
?
Matches a single character.
For example:
$ls ch*.doc
Displays all files with names that start .doc ch and end with :
ch01-1.doc ch010.doc ch02.docch03-2.doc
ch04-1.doc ch040.doc ch05.docch06-2.doc
ch01-2.doc ch02-1.doc c
Here, as a meta-character, you can match any character. If you just want to display all .doc ends with , you can use the following command:
$ls *.doc
A hidden file is a file in which the first character is a dot or
(.)
Most UNIX programs, including shells, use these files to store configuration information.
Some common examples of hiding files include files:
To list invisible files, specify the
ls -a
option:
$ ls -a
. .profile docs lib test_results
...rhostshostspub users
.emacsbinhw1 res.01 work
.exrc ch07 hw2 res.02
.kshrcch07.bak hw3 res.03
$
You can use the vi editor to create normal files on any UNIX system. All you have to do is give the following command:
$ vi filename
The above command opens a file with a given file name. Y ou will need to press i to enter edit mode. Once you are in edit mode, you can write to your content in a file like the one shown below:
This is unix file....I created it for the first time.....
I'm going to save this content in this file.
Once you've done the next step, follow these steps:
Now you will have a filename file that has been created in the current directory.
$ vi filename
$
You can use the vi editor to edit existing files. W e'll cover it in more detail in a separate tutorial. But in summary, you can open an existing file as follows:
$ vi filename
Once the file is opened, you will be able to press i in edit mode, and then you can edit the file as you want. If you want to move left and right in a file first you need to press the key esc to exit edit mode, and then you can use the following keys to move inside the file:
Using the keys above, you can place your cursor where you want to edit it. O nce you're positioned, you can use the i key to edit the file in edit mode. When you have finished editing the file you can press the esc key and then press the Shift plus ZZ key to exit the file completely.
You can use the cat command to view the contents of the file. Here's a simple example to see what you created the file above:
$ cat filename
This is unix file....I created it for the first time.....
I'm going to save this content in this file.
$
You can display the line number by using the -b option and the cat command as follows:
$ cat -b filename
1 This is unix file....I created it for the first time.....
2 I'm going to save this content in this file.
$
You can use the wc command to get the total number of lines, words, and characters in a file. Here's a simple example to see information about the files created above:
$ wc filename
2 19 103 filename
$
Here are the details of all four columns:
Column 1: Represents the number of rows in the file.
Column 2: Represents the number of words in the file.
Column 3: Represents the number of characters in the file. This is the actual size of the file.
When you get information about these files, you can give multiple files. Here is the simple syntax:
$ wc filename1 filename2 filename3
To use a copy of the cp command file. The basic syntax of the command is as follows:
$ cp source_file destination_file
The following is an example of creating a copy of an existing filename.
$ cp filename copyfile
$
Now you will find one more file copyfile in your current directory. This file is identical to the original filename.
To change the name of a file, use the mv command. Its basic syntax is:
$ mv old_file new_file
Here's an example of renaming an existing file filename to newfile:
$ mv filename newfile
$
The mv command moves the existing file completely to the new file. So in this case you can only find newfile in your current directory.
Use the rm command to delete an existing file. Its basic syntax is:
$ rm filename
Warning: It can be dangerous to delete a file because it may contain useful information. S o be careful when using this command. This recommends using the -i option and the rm command.
Here's an example of deleting an existing file filename entirely:
$ rm filename
$
You can delete multiple files in one line, as follows:
$ rm filename1 filename2 filename3
$
Under normal circumstances, each UNIX program opens three streams (files) when it starts: