Department of Computer Science
Subject Name : Linux & Shell Programming
Subject Code : 43A
Class : II B.Sc. CS
Semester : IV
Prepared by : Dr. P. Selvi
Assistant Professor
Department of Computer Science
KG College of Arts and Science
Unit - II
File Management
File management in Unix/Linux
• All data in Unix is organized into files.
• All files are organized into directories.
• Directories are organized into a tree-like structure called the filesystem.
• When you work with Unix/Linux, one way or another, you spend most of your
time working with files.
In Unix/Linux, there are three basic types of
files −
• 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 stored in the current directory, use the following
command −
• $ls
• The command ls supports the -l option which would help you to get more
information about the listed files −
• $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
Metacharacters
• Metacharacters have a special meaning in Unix. For example, * and ? are
metacharacters. We use * to match 0 or more characters, a question mark (?)
matches with a single character.
• For Example −
• $ls ch*.doc
• Displays all the files, the names of which 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
Creating Files
• You can use the vi editor to create ordinary files on any Unix system. You simply
need to give the following command −
• $ vi filename
Editing Files
• You can edit an existing file using the vi editor. We will discuss in short how to
open an existing file
• $ 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.
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.
• Some common examples of the hidden files include the files −
• .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
• 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
• $
• .
• Single dot (.) − This represents the current directory.
• Double dot (..) − This represents the parent directory
Managing Files and Directories
Unix / Linux - Directory Management
Home Directory
Absolute/Relative Pathnames
Following are some examples of absolute
filenames
Listing Directories
Creating Directories
Removing Directories
Changing Directories
Renaming Directories
Unit - II
File management in Unix/Linux
(Display)
Display Content of a File
Counting Words in a File
Copying Files
Deleting Files
Unit - II
Shell Types
• Bourne shell
• C shell
• Korn shell
• Restricted shell
• Bash shell
• Tcsh shell
• A shell
• Z shell
1) Bourne shell
2) C Shell
3) Korn Shell
4) Restricted shell
5) Bash shell
6) Tcsh shell
7) A shell
8) Z shell
Beginning a Linux session : logging on
Unit - II
Wildcard
The * Wildcard
The ? Wildcard
The [ ] Wildcard
Example-1: