UNIX / Linux File Management - Unix/Linux

What are UNIX / Linux File Management?

In this section, we will consider in detail about file management in UNIX. All information in UNIX is organized into files. All files are prepared into directories. These directories are structured into a tree-like structure called the file system.

When you occupation with UNIX, one way or another, you use most of your time working with records. This tutorial will help you recognize how to make and remove files, copy and rename them, make links to them, etc.

In UNIX, there are three basic types of records −

  • Ordinary Files − an ordinary file is a file on the system that contains data, text, or program instructions. In this tutorial, you look at working with ordinary files.
  • Directories − Directories store both special and ordinary files. For users familiar with Windows or Mac OS, UNIX directories are equivalent to folders.
  • Special Files − some special files provide access to hardware such as hard drives, CD-ROM drives, modems, and Ethernet adapters. Other special files are similar to aliases or shortcuts and enable you to access a single file using different names.

Listing Files

To list the files and directories store in the present listing, use the following command –

Here is the illustration output of the above command –

The command less supports the -l selection which would help you to get more information about the scheduled files –

Here is the information concerning all the listed columns −

  • First Column − Represents the file type and the permission given on the file. Below is the description of all type of files.
  • Second Column − Represents the number of memory blocks taken by the file or directory.
  • Third Column − Represents the owner of the file. This is the Unix user who created this file.
  • Fourth Column − Represents the group of the owner. Every Unix user will have an associated group.
  • Fifth Column − Represents the file size in bytes.
  • Sixth Column − Represents the date and the time when this file was created or modified for the last time.
  • Seventh Column − Represents the file or the directory name.

In the less -l listing instance, every file line begins with a d, -, or l. These typescripts specify the type of the file that's listed.

S.No. Prefix & Description
1
-
Regular file, such as an ASCII text file, binary executable, or hard link.
2
b
Block special file. Block input/output device file such as a physical hard drive.
3
c
Character special file. Raw input/output device file such as a physical hard drive.
4
d
Directory file that contains a listing of other files and directories.
5
l
Symbolic link file. Links on any regular file.
6
p
Named pipe. A mechanism for interprocess communications.
7
s
Socket used for interprocess communication.

Met characters

Met characters have a particular meaning in UNIX. For instance, * and? Are met characters. We use * to match 0 or more characters, a question mark (?) matches with a solo character.

For instance –

Displays all the records, the names of which begin with chi and end with .doc

Here, * workings as Meta character which matches with any character. If you want to show all the files finish with just .doc, then you can apply the following command –

Hidden Files

A visible file is one, the first character of which is the dot or the period character (.). UNIX programs (including the shell) use mainly of these files to store arrangement information.

Some general instance of the hidden files contains the files −

  • .profile − The Bourne shell ( she) initialization script
  • .kosher − The Koran shell ( kasha) initialization script
  • .chic − The C shell ( cash) initialization script
  • .roosts − The remote shell configuration file

To list the hidden files, identify the -a option to less

  • Single dot (.) − This represents the current directory.
  • Double dot (...) − this represents the parent directory.

Creating Files

You can apply the VI editor to generate ordinary files on any UNIX system. You simply want to give the follow command –

The above command will open a file with the given filename. Currently, press the key I to come into the edit method. Once you are in the edit method, you can begin writing your content in the file as in the following program –

Once you are complete with the program, follow these steps −

  • Press the key esc to come out of the edit mode.
  • Press two keys Shift + ZZ together to come out of the file completely.

You will currently have a file formed with filename in the current listing.

Editing Files

You can edit a live file using the VI editor. We will talk about in short how to open an existing file –

Once the file is open, you can come in the edit method by pressing the key and then you can proceed by editing the file. If you desire to move here and there inside a file, then first you need to come out of the edit mode by pressing the key Esc. Following this, you can use the following keys to move about inside a file −

  • L key to move to the right side.
  • H key to move to the left side.
  • K key to move upside in the file.
  • J key to move downside in the file.

So using the above keys, you can location your cursor wherever you desire to edit. Once you are located, then you can use the I key to come in the edit mode. Once you are done with the editing in your file, press Esc and at last two keys Shift + ZZ together to come out of the file finally.

Display Content of a File

You can use the cat demand to observe the content of a file. Following is an easy instance to observe the content of the above created file –

You can present the line information by using the -b option along with the cat command as follow –

Counting Words in a File

You can draw on the we command to get a count up of the total number of lines, words, and characters controlled in a file. Following is an easy instance to notice the information about the file formed above –

Here is the feature of all the four columns −

  • First Column − Represents the total number of lines in the file.
  • Second Column − Represents the total number of words in the file.
  • Third Column − Represents the total number of bytes in the file. This is the actual size of the file.
  • Fourth Column − Represents the file name.

You can give several files and get information concerning those files at a time. Following is easy syntax –

Copying Files

To construct a copy of a file use the cp command. The necessary syntax of the command is –

Following is the instance to generate a copy of the existing file filename.

You will now find one more file copyfile in your current directory. This file will exactly be the same as the original file filename.

Renaming Files

To modify the name of a file, use the my command. Following is the basic syntax –

The following program will rename the existing file filename to new file.

The command will move the existing file completely into the new file. In this case, you will locate only new file in your present directory.

Deleting Files

To delete an existing file, use the ram command. Following is the essential syntax –

Caution − A folder may hold useful information. It is forever optional to be careful while using this Delete command. It is better to use the -I option along with ram command.

Following is the instance which shows how to totally eliminate the active file filename.

You can eliminate many files at a time with the command given below –

Standard UNIX Streams

Under common conditions, every UNIX program has three streams (files) open for it when it start up −

  • Stein − this is referred to as the standard input and the associated file descriptor is 0. This is also represented as STDIN. The Unix program will read the default input from STDIN.
  • Stout − this is referred to as the standard output and the associated file descriptor is 1. This is also represented as STDOUT. The Unix program will write the default output at STDOUT
  • Steer − this is referred to as the standard error and the associated file descriptor is 2. This is also represented as STDERR. The UNIX program will write all the error messages at STDERR.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Unix/Linux Topics