2.1 File operation


2.1.1 File and Directory

Basic work of the computer is to process information (= data). The form of file is used as a method of preserving data on the computer. When you make a document, you save the document file, and when you take a photograph with the digital camera, you save the picture file in the personal computer, and so on.
As the number of files increases, it becomes necessary to group them according to purpose such as contents and usage. What is needed then is the concept of directory. A directory can hold multiple files together. It is also possible to create directories in a directory.

Figure2-1
Figure 2-1: Be able to classify many files by using directories


2.1.2 List files and directories (ls)

When operating files, the first command required is the command to get the list. ls (LiSt) is the command.
With the ls command, you can get information by specifying a file name or directory name. If you specify a directory name, you can get a list of files and directory names in that directory.

Format

ls  [option] [file]

Options

-a
All hidden files starting with '.' are also displayed.
-l
It outputs in long format.
-t
Sort and output according to the last modification time.
-r
Sort in reverse order.



Practice: Execution of ls command


$ ls ↵
Desktop           ; We can see that there is a directory called 'Desktop'.


Practice: Getting the file list of various directories


$ ls Desktop ↵
$                                 ; Nothing is displayed when there is no file.
$ ls / ↵
bin  dev  home  lost+found  misc  net  proc  sbin  srv  tmp  var
boot  etc  lib  media  mnt  opt  root  selinux  sys  usr
                                     ; We can see these directories exist.
$ ls /etc ↵
a2ps.cfg    httpd    profile
a2ps-site.cfg    idmapd.conf    profile.d
     :      :      :
     :      :      :
     :      :      :


The format indicates the format in which the command is used. It shows how to list the necessary elements for command execution.


The command can change its behavior by adding an option.


Practice: Getting the file list of various directories


$ ls -l ↵
drwxr-xr-x 2 okada okada 4096 Mar 16 07:46 Desktop

(We can see there is a directory named Desktop. And the information is displayed in long format.)


Details about the directory called Desktop came out. This is because we added -l. Such -l is called an option.
ls is a command to display a list of files and directories. We can specify various additional functions by attaching an option to that command.

For example, if you want to display with long format and include hidden files, enter the following.


Practice: Execution with -la option


$ ls -la ↵
drwx------ 14 okada okada 4096 Mar 21 03:08 .
drwxr-xr-x  3 root    root    4096 Mar 14 23:40 ..
-rw-------    1 okada okada 1562 Mar 21 03:10 .bash_history
-rw-r--r--    1 okada okada    24 Jan 6 2007 .bash_logout
-rw-r--r--    1 okada okada  176 Jan 6 2007 .bash_profile
-rw-r--r--    1 okada okada  124 Jan 6 2007 .bashrc
               :
               :
               :
-rw-r--r--    1 okada okada 3481 Mar 20 07:05 .xsession-errors


Unlike ls - l, more information appeared. In particular, you can see files beginning with "."(dot) . In UNIX, there is an agreement that hidden files start with ".". And hidden files are displayed only when you add the -a option. Therefore it became output like the above.


The same results even if the option is brought together or not. In addition, because it is in no particular order it will be the same even if you swap. Therefore, in the case of -la above, all of the following will produce the same result.

Example

$ ls -la ↵
$ ls -al ↵
$ ls -l -a ↵
$ ls -a -l ↵


The order of the options is unordered for ls, and some other commands also can be done similarly. Depending on the command, the order and grammar may be ruled in detail, so you need to refer to the manual.

When specifying options, we used - (hyphen). The specification of the hyphen of the option has divided into the command that is necessary like ls and the command that is not necessary. This should also refer to the manual.


Practice: Execution of ls command with -l option


$ ls -l /etc/ ↵
              :
              :
drwxr-xr-x 4 root root 4096 Mar 14 22:31 xdg
drwxr-xr-x 2 root root 4096 Mar 14 22:39 xinetd.d
drwxr-xr-x 2 root root 4096 Mar 14 22:35 xml
-rw-r--r--   1 root root   585 Nov 10 23:30 yp.conf
drwxr-xr-x 3 root root 4096 Mar 14 22:51 yum
-rw-r--r--   1 root root   286 Nov 11 02:14 yum.conf
drwxr-xr-x 2 root root 4096 Nov 11 02:14 yum.repos.d


We could check the list of files and directories in the /etc directory.


Practice: Execution of ls command with -lt option


$ ls -lt /etc/ ↵
total 3408
-rw-r--r--   1 root root          358 Mar 16 07:46 mtab
-rw-r--r--   1 root root        3204 Mar 16 07:20 smartd.conf
-rw-r-----   1 root smmsp 12288 Mar 16 07:19 aliases.db
-rw-r--r--   1 root root          135 Mar 16 07:19 printcap
drwxr-xr-x 2 root root       4096 Mar 16 07:19 blkid
-rw-r--r--   1 root root         435 Mar 16 07:19 reader.conf
-rw-r--r--   1 root root           79 Mar 16 07:19 resolv.conf
-rw-r--r--   1 root root           46 Mar 15 12:10 adjtime
              :
              :


We can find that the files are listed in order of update time.


2.1.3 Copying files (cp)

When dealing with files, copying may be necessary, such as creating duplicates. It is the cp (CoPy) command to do that. You can duplicate the file with the cp command and specify the file name of the new duplicate file. The cp command performs the following processing.

Format

cp  src-file  dst-file


When src-file is a file name and dst-file doesn't exist, dst-file is made as a copy of src-file.
When src-file is a file name and dst-file is a directory name, a copy of src-file is made under dst-file.
When src-file is a file name and dst-file exists, dst-file is overwrited by src-file. Options

Options

-i
Confirming when processing. If you use the -i option when overwriting the file, etc., the computer will ask you for confirmation. It helps to prevent overwriting copying by mistake.
-R
Copy directories recursively. This option is for copying all files and directories in the directory.
-p
Preserve the attributes and security contexts. The file contains various information about the file such as user name, attribute, update date and time. When copying with the cp command, the new file will be copied with all new contexts. Use this -p option if you do not want to create new contexts and you want to create a copy while keeping old information.



It needs -R option above to copy directory using cp command.


Practice: Execution of cp command


Let's copy file by using cp command actually.


$ cp /etc/hosts . ↵
        (Character "." is meaning current directory. Be explained later)
$ ls -l ↵
-rw-rw-r--   1 okada okada 187 Mar 16 07:54 hosts
        (hosts is copied)
drwxr-xr-x 2 okada okada 4096 Mar 16 07:46 Desktop
$ cp hosts hosts.bak ↵
$ ls -l ↵
-rw-rw-r--   1 okada okada 187 Mar 16 07:54 hosts
-rw-rw-r--   1 okada okada 187 Mar 16 08:59 hosts.bak
        (hosts.bak is created)
drwxr-xr-x 2 okada okada 4096 Mar 16 07:46 Desktop
$ cp -p hosts hosts.new ↵
$ ls -l ↵
-rw-rw-r--   1 okada okada 187 Mar 16 07:54 hosts
-rw-rw-r--   1 okada okada 187 Mar 16 08:59 hosts.bak
-rw-rw-r--   1 okada okada 187 Mar 16 07:54 hosts.new
        (timestamp of hosts is preserved)
drwxr-xr-x 2 okada okada 4096 Mar 16 07:46 Desktop


2.1.4 Moving files (mv)

The command to use when moving (MoVe) files is the mv command. You can also change the file name using the mv command.

Format

mv  src-file  dst-file


Options

-i
Confirming when overwriting. If you use the -i option when overwriting the file, the computer will ask for confirmation. It helps to prevent overwriting copying by mistake.
-f
Forcibly execute the processing. In some processing, the mv command will ask for confirmation. Ignoring that confirmation, this -f option forcibly execute the process.



Using this mv command, it can move src-file to dst-file.


$ mv src-file dst-file ↵


When src-file is a file name and dst-file is a directory, src-file is moved into dst-file directory.


When src-file is a directory name and dst-file is a directory, src-file is moved into dst-file directory.


When src-file is a file name and dst-file is a not existing file name, src-file is changed to dst-file.


When src-file is a directory name and dst-file is a not existing directory name, src-file is changed to dst-file.


Practice: Execution of mv command


$ ls -l ↵
drwxr-xr-x 2 okada okada 4096 Mar 16 07:46 Desktop
- rw-rw-r-- 1 okada okada 187 Mar 16 07:54 hosts
- rw-rw-r-- 1 okada okada 187 Mar 16 08:59 hosts.bak
- rw-rw-r-- 1 okada okada 187 Mar 16 07:54 hosts.new
$ mv hosts hosts.new2       ; change file name form ‘hosts’ to ‘hosts.new2’
$ ls -l ↵
drwxr-xr-x 2 okada okada 4096 Mar 16 07:46 Desktop
- rw-rw-r-- 1 okada okada 187 Mar 16 08:59 hosts.bak
- rw-rw-r-- 1 okada okada 187 Mar 16 07:54 hosts.new
- rw-rw-r-- 1 okada okada 187 Mar 16 09:05 hosts.new2
$ ls -l /tmp/ ↵
total 32
drwx------ 3 okada okada 4096 Mar 16 07:46 gconfd-okada
drwx------ 2 root root 4096 Mar 16 07:46 gconfd-root
              :
              :



Confirm that there is not the file "hosts.new".


$ mv hosts.new /tmp/
      (move file ’hosts.new’ to under directory /tmp/)
$ ls -l ↵
total 4
drwxr-xr-x 2 okada okada 4096 Mar 16 07:46 Desktop
-rw-rw-r-- 1 okada okada 187 Mar 16 08:59 hosts.bak
-rw-rw-r-- 1 okada okada 187 Mar 16 09:05 hosts.new2
      (file ‘hosts.new’ is moved)


$ ls -l /tmp/ ↵
total 32
drwx------ 3 okada okada 4096 Mar 16 07:46 gconfd-okada
drwx------ 2 root root 4096 Mar 16 07:46 gconfd-root
-rw-rw-r-- 1 okada okada 187 Mar 16 09:10 hosts.new
              :
              :


2.1.5 Removing files (rm)

The command used when it is necessary to delete a file is rm(ReMove) command. The rm command specifies the file to be deleted.

Format

rm  filename

Options

-i
Confirming when processing. You will be asked for confirmation as to whether you really want to delete the target file. It is used to prevent accidental deletion.
-f
Forcibly execute the processing. Depending on the file, you may be prompted to confirm deletion, but the -f option forcibly continues processing without confirmation.
-r
Target is directory. Also delete files and directories in the directory.


Example

$rm file-delete ↵



This command deletes specified file. The file deleted once cannot be revived in UNIX based OS containing Linux.
Moreover, the rm command can not delete a directory (with no options). If you want to delete one directory, you use rmdir command which will be explained later.The option of the rm command is as above.
The explanation of the basic commands to deal with files is finished.



Practice: Execution of rm command


Let’s delete a file by using rm command.


$ ls -l ↵
total 3
drwxr-xr-x 2 okada okada 4096 Mar 16 07:46 Desktop
- rw-rw-r-- 1 okada okada 187 Mar 16 08:59 hosts.bak
- rw-rw-r-- 1 okada okada 187 Mar 16 09:05 hosts.new2
$ rm hosts.new2↵
$ ls -l ↵
total 2
drwxr-xr-x 2 okada okada 4096 Mar 16 07:46 Desktop
- rw-rw-r-- 1 okada okada 187 Mar 16 08:59 hosts.bak


You can confirm that you deleted the file.


Column: Common options


Did you notice that there is the same option for both cp and rm? One of them is -r.
With cp command, it means all under specific directory are copied. With rm command, it means all under specific directory are removed.
-r is abbreviation of "recursive", and it means do something recursively.
There are some tips.
In many case, option comes from the initial of English word, so it can be remembered associating to original word.
The similar kind of commands have same kind of options, so grouping them helps your memorizing them.
Similar commands has same kind of options.
( For example, both rm and cp belong to the similar kind of commands which handles files.)



Previous Next