[Unix-Linux] File Management

Date:     Updated:

Categories:

Tags:

📋 This is my note-taking from what I learned in the UNIX/LINUX Tutorial!


Three Basic Types of Files in Unix

  • 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.


File Management in Unix

All data in Unix is organized into files. All files are organized into directories. These directories are organized into a tree-like structure called the filesystem.

Listing Files

List the files and directories stored in the current directory:

ls

Get more information about the listed files:

ls -l

Metacharacters

  • * and ? are metacharacters.
  • We use * to match 0 or more characters, a question mark (?) matches with a single character.

Displays all the files, the names of which start with ch and end with .doc

ls ch*.doc

Display all the files ending with just .doc

ls *.doc

Hidden Files

An invisible file is one, the first character of which is the dot or the period character (.). Unix programs (including the shell) use most of these files to store configuration information.

To list the invisible files, specify the -a option to ls

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
  • .profile − The Bourne shell ( sh) initialization script
  • .kshrc − The Korn shell ( ksh) initialization script
  • .cshrc − The C shell ( csh) initialization script
  • .rhosts − The remote shell configuration file
  • Single dot (.) − This represents the current directory.
  • Double dot (..) − This represents the parent directory.

Creating Files

Use the vi editor to create ordinary files on any Unix system.

vi filename

The above command will open a file with the given filename. Now, press the key i to come into the edit mode. Once you are in the edit mode, you can start writing your content in the file as in the following program.

This is unix file....I created it for the first time.....
I'm going to save this content in this file.

Once you are done 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.

Editing Files

vi filename

Once the file is opened, you can come in the edit mode by pressing the key i and then you can proceed by editing the file. If you want to move here and there inside a file, then first you need to come out of the edit mode by pressing the key Esc. After this, you can use the following keys to move 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.

Once you are positioned, 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 finally two keys Shift + ZZ together to come out of the file completely.

Display Content of a File

Use the cat command to see the content of a file.

cat filename

This is unix file....I created it for the first time.....
I'm going to save this content in this file.

Display the line numbers by using the -b option along with 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.

Counting Words in a File

Use the wc command to get a count of the total number of lines, words, and characters contained in a file.

wc filename

2  19 103 filename
First Column Second Column Third Column Fourth Column
Total num of lines Total num of words Total num of bytes (Actual size of file) File name

Give multiple files and get information about those files at a time. Following is simple syntax.

wc filename1 filename2 filename3

Copying Files

To make a copy of a file, Use the cp command.

cp source_file destination_file

Following is the example to create a copy of the existing file filename.

cp filename copyfile

Renaming Files

To change the name of a file, use the mv command.

mv old_file new_file

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

mv filename newfile

Deleting Files

To delete an existing file, use the rm command.

rm filename

Remove multiple files at a time with the command given below.

rm filename1 filename2 filename3

Standard Unix Streams

Under normal circumstances, every Unix program has three streams (files) opened for it when it starts up.

  • stdin
    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.
  • stdout
    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
  • stderr
    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.




Back to Top

See other articles in Category Unix-Linux

Leave a comment