All data in Unix is organized in files. All files are organized in folders. These directories are organized in a tree structure which is called the file system.
When you work with Unix, one way or another, you spend most of your time working with files. This chapter will show you how to create and move files, copy and rename them, create links for them, and so on.
In Unix, there are three basic file types:
- Ordinary File: Normal file, is a file on the system that stores data, documents, or program instructions. In this chapter, you will learn how to work with these files.
- Directory: Directories, store both regular and special files. Similar to the folders in Windows, Mac OS, the folders in Unix are the folders.
- Special File: Special files, some special files that provide access to hardware such as hard drives, CD-ROMs, modems and Ethernet readers. Some other special files are similar to shortcuts and you can access a specific file using different names.
List files in Unix / Linux
To list the files and folders stored in the current directory, use the following command:
Below is an example of the result of the above command.
$ls bin hosts lib res.03 ch07 hw1 pub test_results ch07.bak hw2 res.01 users docs hw3 res.02 work
Comeinand ls Optional support -l 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 root 4096 Dec 9 2007 urlspedia -rw-r--r-- 1 root root 276480 Dec 9 2007 urlspedia.tar drwxr-xr-x 8 root root 4096 Nov 25 2007 usr drwxr-xr-x 2 200 300 4096 Nov 25 2007 webthumb-1.01 -rwxr-xr-x 1 root root 3192 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 amrood 166255 Aug 9 2007 yourfile.swf drwxr-xr-x 11 amrood amrood 4096 May 29 2007 zlib-1.2.3 $
Here is the information about all the columns listed:
- The first column: represents the file type and the permissions provided for the file. Below are descriptions of file types.
- Column 2: represents the amount of memory occupied by files or directories.
- Column 3: represents the ownership of the file. It is the Unix user who created this file.
- Column 4: representing the ownership group. Every Unix user has an associated group.
- Column 5: represents the size in bytes of the file.
- Column 6: shows the date when the file was last created or edited.
- Column 7: represents file or folder name.
In the ls – l example, each file line begins with a character d, -, or l. These characters indicate the type of file listed.
|–||Regular files such as an ASCII text file, binary or hard links.|
|b||Special file group. file group input / output device such as a physical hard disk.|
|c||Special character file. Raw input / output device file such as a physical hard disk.|
|d||A directory that contains a list of other files and directories.|
|l||File linking characters. Connect on any regular file.|
|p||Pipe is named. A mechanism for inter-process communication.|
|S||Socket used for inter-process communication.|
Hyper-characters in Unix / Linux
Metacharacters are important in Unix. For example * and? is metacharacter. We use * to match 0 or more characters, using? to connect to a single character.
Displays all the files whose names start with ch and end with .doc.
ch01-1.doc ch010.doc ch02.doc ch03-2.doc ch04-1.doc ch040.doc ch05.doc ch06-2.doc ch01-2.doc ch02-1.doc c
The following * characters match with any character. If you want to display all files ending with .doc, then use the following command:
Hidden files in Unix / Linux
An invisible file is a file whose first characters are dots (.). Unix programs (including the shell) use most of these files to hold configuration information.
A few examples of hidden files include:
.profile: is the Bourne shell initialization script (sh)
.kshrc: is the Korn shell (ksh) initialization script
.cshrc: is the C shell initialization script (csh)
.rhosts: is a remote control shell configuration file.
To list invisible (hidden) files, specify the -a option for the ls command:
$ ls -a . .profile docs lib test_results .. .rhosts hosts pub users .emacs bin hw1 res.01 work .exrc ch07 hw2 res.02 .kshrc ch07.bak hw3 res.03 $
Single dot (.): Represents the current directory
Double colon (..): represents the root.
Create files in Unix / Linux
You can use Because The editor for creating files is often called filename on Unix systems. You simply need to use the following command:
$ vi filename
The above command will open a file with the given name. You will need to press the i key to enter editing mode. Once you are in editing mode, you can start writing content to the file as 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’re done, follow these steps:
Press esc to exit this mode
Press the key combination Shift + ZZ to get rid of the file completely.
You should now have a file created with filename in the current directory.
$ vi filename $
Edit files in Unix / Linux
You can edit the current file using the vi editor. We will go into each in detail in a specific tutorial chapter. But here, you can open an existing file as follows:
$ vi filename
Once this file is opened, you can enter editing mode by pressing the key i And then you can edit the content as you like. If you want to move here or elsewhere inside the file, then first you need to exit editing mode by pressing the key. esc, then you can use the following keys to move inside a file:
Keys l to move to the right;
Keys H to move to the left;
Keys k to move upward in a file;
Keys j to move downward in a file
So by using the above keys, you can place the cursor anywhere you want to edit. Once you have located, then you can use the key i to enter editing mode. After you are done, press esc and finally press the key combination Shift + ZZ to get rid of the file completely.
Display the contents of a file in Unix / Linux
You can use the cat command to view the contents of a file. Following is a simple example to view the content of a file that has been created.
$ 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 number of lines using the option -b along with the command cat 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. $
Calculate the number of words in a file in Unix / Linux
You can use the command WC to get the number of lines, the number of words and the number of characters in a file. Below is a simple example to see information about the file that was created above.
$ wc filename 2 19 103 filename $
Here are details in the 4 columns:
- Column 1: represents the total number of lines in the file;
- Column 2: represents the total number of words in the file;
- Column 3: represents the total amount of bytes occupied by the file. This is the actual size of the file.
- Column 4: represents the file name.
You can get information about multiple files at the same time with the following simple syntax:
$ wc filename1 filename2 filename3
Copy files under Unix / Linux
To make a copy of a file, you use the command cp. The simple syntax of this command is:
$ cp source_file destination_file
Inside, source_file The file contains the information you need to copy to destination_file. Here is an example to make a copy of filename existing:
$ cp filename copyfile $
Now you will find one copyfile in your current directory. This file will look exactly the same filename.
Rename the file in Unix / Linux
To change the name of a file, use the mv command. The simple syntax of this command is:
$ mv old_file new_file
Here is an example that resets filename is existing in newfile:
$ mv filename newfile $
Comeinand mv will move all existing files into the new file. So in this situation you will only find one newfile only in the current directory.
Delete files in Unix / Linux
To delete an existing file, use the rm command. Its simple syntax is:
$ rm filename
Attention: It can be very dangerous to delete a file because it contains useful information. So you need to be careful while executing this command. It suggests the -i option in parallel with the command rm.
Here is an example that removed completely filename existing:
$ rm filename $
You can remove multiple files at the same class as follows:
$ rm filename1 filename2 filename3 $
The standard Unix Stream in Unix / Linux
Details in a Unix program have 3 streams (or files) open for it when it starts:
- stdin: It is as a standard input and the associated file description element is 0. It is also represented as STDIN. The Unix program will read the default input from STDIN.
- stdout: It is as a standard output and the associated file description element is 1. It is also represented as STDOUT. The Unix program will write the default output at STDOUT.
- stderr: It is as a standard error and the associated file description element is 2. It is also represented as STDERR. The Unix program will record all error information at STDERR.
Previous lesson: What is Unix / Linux?
Next: Managing directories in Unix / Linux