Chapter 6. Working with Files

For simplicity, everything in UNIX and Linux-based operating systems is treated as a file. Files in the filesystem are arranged in a hierarchical tree like a structure with the root of the tree denoted by '/' (forward slash). A node of the tree is either a directory or file where the directory is also a special type of file containing inode numbers and a corresponding filename entry of the list of files inside it. An inode number is an entry in an inode table that contains metadata information related to the file.

In this chapter, we will take a closer look at the important and commonly used file types. We will see how we can create, modify, and perform other useful operations on files. We will also see how to monitor a list of files opened by a process or user.

This chapter will cover the following topics in detail:

  • Performing basic file operations
  • Moving and copying files
  • Comparing files
  • Finding files
  • Links to a file
  • Special files
  • Temporary files
  • Permission and ownership
  • Getting the list of open files
  • Configuration files

Performing basic file operations

Most commonly used files are regular files and directories. In the following subsection, we will see the basic file operations.

Creating files

We can create both regular files and directories in shell using different shell commands.

Directory file

A directory is a special type of file that contains a list of filenames and a corresponding inode number. It acts as a container or folder to hold files and directories.

To create a new directory through shell, we can use the mkdir command:

$ mkdir dir1

We can also provide multiple directories' name as arguments to the mkdir command as follows:

$ mkdir dir2 dir3 dir4  # Creates multiple directories

We can create a parent directory if the specified pathname to mkdir is not present. This is done using the -p option in mkdir:

$ mkdir -p /tmp/dir1/dir2/dir3

Here, if dir1 and dir2 are the parent directories for dir3 and don't exist already, the -p option will create the dir1 directory first and then dir2 subdirectory inside dir1 and the dir3 subdirectory inside dir2.

Regular file

In general, text and binary files are known as regular files. In shell, a regular file can be created in multiple ways. Some of them are mentioned in the following sections.

Touch command

A new regular file can also be created using the touch command. It is mainly used to modify the timestamp of the existing file, but if the file doesn't exist, a new file is created:

$ touch newfile.txt  # A new empty file newfile.txt gets created
$ test -f newfile.txt && echo File exists  # Check if file exists
File exists

Using the command line editors

We can open any command line editor; for example, vi/vim, emacs, nano in shell, write content, and save content in file.

Now, we will create and write a text using the vi editor:

$ vi foo.txt  # Opens vi editor to write content

Press the key I to enter the INSERT mode of vi and then type the text as shown in the following screenshot:

Using the command line editors

After writing the text, press the Esc key and then type the :wq command to save and exit from the vi editor. To know vi/vim in detail, refer to its man page or the online documentation (http://www.vim.org/docs.php):

Using the command line editors

Using the cat command

We can even use the cat command to write the content into an existing or a new regular file, as follows:

$ cat > newfile1.txt
We are using cat command
to create a new file and write into                         
it
[Ctrl + d]    # Press Ctrl + d to save and exit
$ cat newfile1.txt  # See content of file
We are using cat command
to create a new file and write into
it

By using the >> operator instead of >, we can append instead of overwriting the file's content.

Redirecting the command's output

While executing a command in bash or script, we can redirect results into an existing or a new file:

$ ls -l /home > newfile2.txt  #File gets created containing command output
$ cat newfile2.txt
total 24
drwx------.     2    root    root   16384  Jun  11  00:46   lost+found
drwx—x---+  41  foo     foo    4096   Aug  22  12:19   foo

Modifying files

To modify the content of a regular file in shell, open a file in an editor, make the required changes, and then save and exit. We can also use the >> operator to append the command's output to the specified file:

Command >> file.txt

For example, we will save the ls output of /home in the ls_output.txt file:

$ ls /home/ >> ls_output.txt
$ cat ls_output.txt  # Viewing content of file
lost+found
foo

Now, we will append the ls output of another directory /home/foo/ as follows:

$ ls /home/foo >> ls_output.txt
lost+found
foo
Desktop
Documents
Downloads
Pictures

We saw that the ls_output.txt file gets modified by appending the content of the ls command output.

Viewing files

To view the content of a regular file, we can simply open a file in an editor such as vi/vim, emacs and nano. We can also use the cat, less and more commands to view the file's content.

To view the contents of a directory, we use the ls command:

$ ls /home/
lost+found  foo

To view the contents of a directory recursively, use ls with the -R or --recursive option.

Viewing content using cat

We can use the cat command to view the content of the file as follows:

$ cat newfile1.txt
We are using cat command
to create a new file and write into
it
$ cat -n newfile1.txt    # Display line number as well
     1  We are using cat command
     2  to create a new file and write into
     3  it

more and less

The more and less commands are very useful and handy to view a large file that doesn't fit on the current terminal.

The more command displays the content of a file in page format, in which we can scroll up and down to view the remaining contents of the file:

$ more /usr/share/dict/words

A file path is passed as an argument to the more command. In the above example, it will display the content of the file words available in the /usr/share/dict/ directory.

The key s is used to skip forward k lines of text. The key f is used to skip forward k screenful of text. The key b is used to skip backward k screenful of text.

The less command is more popular and widely used to view the content of large files. One of the advantages of using the less command is that it doesn't load entire files in the beginning and as a result, viewing the content of large files is faster.

The usage of less is very similar to the more command:

$ less  /usr/share/dict/words

Navigation is much easier while using the less command. It also has more options to customize the filtered view of a file's content.

The more and less commands can take an input from stdin if no input file is provided. Use a pipe ('|') to give an input from stdin:

$ cat /usr/share/dict/words | more    #  cat output redirected to more
$ grep ^.{3}$ /usr/share/dict/words | less  # Matches all 3 character words

See the man page of more and less for the detailed usage.

Note

The behavior of the more command may vary on different systems because of its different implementations.

Deleting files

We can also delete regular files and directories if they are no longer required.

Deleting a regular file

To delete a regular file, we use the rm command in shell.

The rm command deletes the file if it exists, otherwise it prints an error on stdout if it doesn't exist:

$ rm newfile1.txt    # Deletes if file exists
$ rm newfile1.txt    # Prints error message if file doesn't exist
rm: cannot remove 'newfile1.txt': No such file or directory

To ignore an error message, rm can be used with the –f option:

$ rm -f newfile1.txt
$ rm -i  newfile.txt   # Interactive deletion of file
rm: remove regular empty file 'newfile.txt'? 

Enter the key y to delete a file and n to skip the deletion of a file.

Deleting a directory

To delete a directory, we can use the rmdir and rm commands. We will consider directories that are created in the Directory files under the File creation subtopic:

$ rmdir dir2/  # Deletes directory dir2
$ rmdir dir1/  #  Fails to delete because of non-empty directory
rmdir: failed to remove 'dir1/': Directory not empty

To delete a nonempty directory, first delete the contents and then remove the directory. We can also use rm to remove an empty or a nonempty directory.

The –d option removes an empty directory as follows:

$ ls dir3/  # Directory dir3 is empty
$ rm -d dir3/  # Empty diretcory dir3 gets deleted
$ ls dir1/  # Diretcory dir1 is not empty
dir2
$ rm -d dir1/  # Fails to delete non-empty directory dir1
rm: cannot remove 'dir1': Directory not empty

The option -r, -R, or --recursive removes the directory and its contents recursively:

$ rm -ri dir1/  # Asks to remove directory dir1 recursively
rm: descend into directory 'dir1'?  Y

Typing y confirms that dir1 should be deleted.

Note

Use rm carefully with the -r option. If possible, use it with the -i option to avoid an accidental deletion of an entire directory's contents.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.131.38.208