0% found this document useful (0 votes)
33 views25 pages

Module 1 Part2

Uploaded by

vinutha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
33 views25 pages

Module 1 Part2

Uploaded by

vinutha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 25

Unix files

1.11 Naming Files


File names can consist up to 255 characters
Files names may or, may not have extensions
Can consist any ASCII character except the / and NULL

It is recommended that only the following character be used in filenames:


. Alphabetic characters and numerals
. The period (.), Hyphen (-) and underscore (_)
Eg: last_name, Prog1.c, a.d.c.d, prog1.c

1.12 Basic file types/categories


1) Ordinary or Regular Files
Ordinary File can be divided into 2 types: Text file, Binary File
2) Directories

A directory is a binary file used to track and locate other files and directories. The binary
format is used so that directories containing large numbers of filenames can be search
quickly.
3) Device (Special) Files

Device or special files are used for device I/O on UNIX systems. They appear in a file
system just like an ordinary file or a directory. On UNIX systems there are two flavors of
special files for each device, character special files and block special files. When a
character special file is used for device I/O, data is transferred one character at a time.
This type of access is called raw device access. When a block special file is used for
device I/O, data is transferred in large fixed-size blocks. This type of access is called
block device access.

1.13 Organization of files


Unix file system is a logical method of organizing and storing large amounts of information in
a way that makes it easy to manage. A file is a smallest unit in which the information is stored.
Unix file system has several important features. 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
file system.
Files in Unix System are organized into multi-level hierarchy structure known as a directory tree.
At the very top of the file system is a directory called “root” which is represented by a “/”. All
other files are “descendants” of root.

Directories or Files and their description –


 / : The slash / character alone denotes the root of the filesystem tree.
 /bin : Stands for “binaries” and contains certain fundamental utilities, such as ls or cp,
which are generally needed by all users.
 /boot : Contains all the files that are required for successful booting process.
 /dev : Stands for “devices”. Contains file representations of peripheral devices and pseudo-
devices.
 /etc : Contains system-wide configuration files and system databases. Originally also
contained “dangerous maintenance utilities” such as init,but these have typically been
moved to /sbin or elsewhere.
 /home : Contains the home directories for the users.
 /lib : Contains system libraries, and some critical files such as kernel modules or device
drivers.
 /media : Default mount point for removable devices, such as USB sticks, media players,
etc.
 /mnt : Stands for “mount”. Contains filesystem mount points. These are used, for example,
if the system uses multiple hard disks or hard disk partitions. It is also often used for
remote (network) filesystems, CD-ROM/DVD drives, and so on.
 /proc : procfs virtual filesystem showing information about processes as files.
 /root : The home directory for the superuser “root” – that is, the system administrator. This
account’s home directory is usually on the initial filesystem, and hence not in /home (which
may be a mount point for another filesystem) in case specific maintenance needs to be
performed, during which other filesystems are not available. Such a case could occur, for
example, if a hard disk drive suffers physical failures and cannot be properly mounted.
 /tmp : A place for temporary files. Many systems clear this directory upon startup; it might
have tmpfs mounted atop it, in which case its contents do not survive a reboot, or it might
be explicitly cleared by a startup script at boot time.
 /usr : Originally the directory holding user home directories,its use has changed. It now
holds executables, libraries, and shared resources that are not system critical, like the X
Window System, KDE, Perl, etc. However, on some Unix systems, some user accounts
may still have a home directory that is a direct subdirectory of /usr, such as the default as in
Minix. (on modern systems, these user accounts are often related to server or system use,
and not directly used by a person).
 /usr/bin : This directory stores all binary programs distributed with the operating system
not residing in /bin, /sbin or (rarely) /etc.
 /usr/include : Stores the development headers used throughout the system. Header files are
mostly used by the #include directive in C/C++ programming language.
 /usr/lib : Stores the required libraries and data files for programs stored within /usr or
elsewhere.
 /var : A short for “variable.” A place for files that may change often – especially in size,
for example e-mail sent to users on the system, or process-ID lock files.
 /var/log : Contains system log files.
 /var/mail : The place where all the incoming mails are stored. Users (other than root) can
access their own mail only. Often, this directory is a symbolic link to /var/spool/mail.
 /var/spool : Spool directory. Contains print jobs, mail spools and other queued tasks.
 /var/tmp : A place for temporary files which should be preserved between system reboots.
1.14 Hidden Files
A “hidden” file in Unix is simply one that has a dot (″.”) as the first character of its name. This
applies to directories (and other things) since they are fundamentally files too.

In fact, such a thing is not really very hidden…the ls command simply does not show it by
default.
One can readily see it by specifying “ls -a”
“ls -d .*” (note that without the “-d” one would see the content of any hidden directories as
well).

1.15 Standard Directories


/ – The Root Directory

Everything on your Unix system is located under the / directory, known as the root directory..

bin – Essential User Binaries

The /bin directory contains the essential user binaries (programs) that must be present when the
system is mounted in single-user mode. Applications such as Firefox are stored in /usr/bin, while
important system programs and utilities such as the bash shell are located in /bin. The /usr
directory may be stored on another partition – placing these files in the /bin directory ensures the
system will have these important utilities even if no other file systems are mounted. The /sbin
directory is similar – it contains essential system administration binaries.

/boot – Static Boot Files

The /boot directory contains the files needed to boot the system –

/dev – Device Files

The /dev directory contains a number of special files that represent devices. These are not actual
files as we know them, but they appear as files – for example, /dev/sda represents the first SATA
drive in the system. If you wanted to partition it, you could start a partition editor and tell it to
edit /dev/sda. This directory also contains pseudo-devices, which are virtual devices that don’t
actually correspond to hardware. For example, /dev/random produces random numbers. /dev/null
is a special device that produces no output and automatically discards all input – when you pipe
the output of a command to /dev/null, you discard it.

/etc – Configuration Files

The /etc directory contains configuration files, which can generally be edited by hand in a text
editor. Note that the /etc/ directory contains system-wide configuration files – user-specific
configuration files are located in each user’s home directory.

/home – Home Folders

The /home directory contains a home folder for each user.

/lib – Essential Shared Libraries

The /lib directory contains libraries needed by the essential binaries in the /bin and /sbin folder.
Libraries needed by the binaries in the /usr/bin folder are located in /usr/lib.

/root – Root Home Directory

The /root directory is the home directory of the root user. Instead of being located at /home/root,
it’s located at /root. This is distinct from /, which is the system root directory.

1.16 Parent Child Relationship


The Unix file system has a hierarchical (or tree-like) structure with its highest level directory
called root (denoted by /, pronounced slash). Immediately below the root level directory are
several subdirectories, most of which contain system files. Below this can exist system files,
application files, and/or user data files. Similar to the concept of the process parent-child
relationship, all files on a Unix system are related to one another. That is, files also have a
parent-child existence. Thus, all files (except one) share a common parental link, the top-most
file (i.e. /) being the exception.

Every file, apart from root must have a parent. Home directory is the parent of mthomas,
mthomas is the parent of bin, class_stuff, .profile. In these parent-child relationships, the parent
is always a directory.
1.17 The Home Directory and The HOME variable
When you log on to the system, UNIX automatically places you in a directory called HOME
directory. It is created by the system when a user account is opened
The shell variable HOME knows your home directory.

$ echo $HOME
/home/kumar

1.18 Reaching required files – The PATH Variable


On Unix-like operating systems, the $PATH variable is specified as a list of one or more
directory names separated by colon ( : ) characters. Directories in the PATH -string are not
meant to be escaped, making it impossible to have directories with : in their name.

The /bin , /usr/bin , and /usr/local/bin directories are typically included in most
users' $PATH setting (although this varies from implementation to implementation).
The superuser also typically has /sbin and /usr/sbin entries for easily executing system
administration commands. The current directory ( . ) is sometimes included by users as well,
allowing programs residing in the current working directory to be executed directly. System
administrators as a rule do notinclude it in $PATH in order to prevent the accidental execution
of scripts residing in the current directory, such as may be placed there by a malicious tarbomb.
In that case, executing such a program requires specifying an absolute
( /home/userjoe/bin/script.sh ) or relative path ( ./script.sh ) on the command line.

When a command name is specified by the user or an exec call is made from a program, the
system searches through $PATH , examining each directory from left to right in the list, looking
for a filename that matches the command name. Once found, the program is executed as a child
process of the command shell or program that issued the command.
To find out what your path is, at the Unix shell prompt, enter:

PATH echo $

Your path will look something like the following.

/usr2/username/bin:/usr/local/bin:/usr/bin:.

1.19 Manipulating the PATH


If you are using csh or tcsh, at the shell prompt, enter:

setenv PATH $PATH\:/dir/path

If you are using sh, ksh, or bash, at the shell prompt, enter:

PATH=$PATH\:/dir/path ; export PATH

1.20 Relative and Absolute pathnames


A path is a unique location to a file or a folder in a file system of an OS.A path to a file is a
combination of / and alpha-numeric characters.

Absolute Path-name
An absolute path is defined as the specifying the location of a file or directory from the root
directory(/).
To write an absolute path-name:
 Start at the root directory ( / ) and work down.
 Write a slash ( / ) after every directory name (last one is optional)
For Example :
$cat abc.sql
will work only if the file “abc.sql” exists in your current directory. However, if this file is not
present in your working directory and is present somewhere else say in /home/kt , then this
command will work only if you will use it like shown below:
cat /home/kt/abc.sql
In the above example, if the first character of a pathname is /, the file’s location must be
determined with respect to root. When you have more than one / in a pathname, for each such /,
you have to descend one level in the file system like in the above kt is one level below home, and
thus two levels below root.
An absolute path is defined as specifying the location of a file or directory from the root
directory(/). In other words,we can say that an absolute path is a complete path from start of
actual file system from / directory.
Relative path

Relative path is defined as the path related to the present working directly(pwd). It starts at your
current directory and never starts with a / .
To be more specific let’s take a look on the below figure in which if we are looking for photos
then absolute path for it will be provided as /home/jono/photos but assuming that we are
already present in jono directory then the relative path for the same can be written as
simple photos.
UNIX offers a shortcut in the relative pathname– that uses either the current or parent directory
as reference and specifies the path relative to it. A relative path-name uses one of these cryptic
symbols:
.(a single dot) - this represents the current directory.
..(two dots) - this represents the parent directory.
1.21 Directory Commands
1) Pwd: As the name states, command 'pwd' prints the current working directory or simply
the directory user is, at present. It prints the current directory name with the complete
path starting from root (/). This command is built in shell command
2) Cd: The cd command is used to change the current directory (i.e., the directory in which
the user is currently working)
3) Mkdir: the mkdir command in Linux/Unix allows users to create or make new
directories. mkdir stands for “make directory
4) Rmdir: rmdir command is used remove empty directories from the filesystem
in Linux. The rmdir command removes each and every directory specified in
the command line only if these directories are empty. So if the specified directory has
some directories or files in it then this cannot be removed by rmdir command.

1.22 Dot (.) and double dots(..) notations to represent present and
parent directories and their usage in relative path names.

.(a single dot) - this represents the current directory.


..(two dots) - this represents the parent directory.

Eg:
$ pwd
/home/kumar/progs/data/text
$cd ..
$pwd
/home/kumar/progs/data
$ cd ../..
$pwd
/home/kumar
Dot (.)
Eg:
$cp ../sharma/profile .
Copies the file profile to the current working directory
1.23 File related commands
1) cat: The cat (short for “concatenate“) command is one of the most frequently used command
in Linux/Unix like operating systems. cat command allows us to create single or multiple files,
view contain of file, concatenate files and redirect output in terminal or files.
General Syntax

cat [OPTION] [FILE]...

1. Display Contents of File

In the below example, it will show contents of /etc/passwd file.

# cat /etc/passwd

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
narad:x:500:500::/home/narad:/bin/bash

2. View Contents of Multiple Files in terminal

In below example, it will display contents of test and test1 file in terminal.

# cat test test1

Hello everybody
Hi world,

3. Create a File with Cat Command

We will create a file called test2 file with below command.

# cat >test2

Awaits input from user, type desired text and press CTRL+D (hold down Ctrl Key and type ‘d‘)
to exit. The text will be written in test2 file. You can see content of file with
following cat command.
# cat test2

hello everyone, how do you do?

4. Use Cat Command with More & Less Options

If file having large number of content that won’t fit in output terminal and screen scrolls up very
fast, we can use parameters more and less with cat command as show above.

# cat song.txt | more


# cat song.txt | less

5. Display Line Numbers in File

With -n option you could see the line numbers of a file song.txt in the output terminal.

# cat -n song.txt

1 "Heal The World"


2 There's A Place In
3 Your Heart
4 And I Know That It Is Love
5 And This Place Could
6 Be Much
7 Brighter Than Tomorrow
8 And If You Really Try
9 You'll Find There's No Need
10 To Cry
11 In This Place You'll Feel
12 There's No Hurt Or Sorrow

2) mv command:
mv stands for move. mv is used to move one or more files or directories from one place to
another in file system like UNIX. It has two distinct functions:
(i) It rename a file or folder.
(ii) It moves group of files to different directory.
No additional space is consumed on a disk during renaming. This command normally works
silently means no prompt for confirmation.
Syntax:
mv [Option] source destination
Let us consider 5 files having name a.txt, b.txt and so on till e.txt.
To rename the file a.txt to geek.txt(not exist):
$ ls
a.txt b.txt c.txt d.txt

$ mv a.txt geek.txt

$ ls
b.txt c.txt d.txt geek.txt
If the destination file doesn’t exist, it will be created. In the above command mv simply replaces
the source filename in the directory with the destination filename(new name). If the destination
file exist, then it will be overwrite and the source file will be deleted. By default, mv doesn’t
prompt for overwriting the existing file
Options:
1. -i (Interactive): Like in cp, the -i option makes the command ask the user for confirmation
before moving a file that would overwrite an existing file, you have to press y for confirm
moving, any other key leaves the file as it is. This option doesn’t work if the file doesn’t exist, it
simply rename it or move it to new location.
$ ls
b.txt c.txt d.txt geek.txt

$ cat geek.txt
India

$ cat b.txt
geeksforgeeks

$ mv -i geek.txt b.txt
mv: overwrite 'b.txt'? y

$ ls
b.txt c.txt d.txt

$ cat b.txt
India
2. -f (Force): mv prompts for confirmation overwriting the destination file if a file is write
protected. The -f option overrides this minor protection and overwrite the destination file
forcefully and delete the source file.
$ ls
b.txt c.txt d.txt geek.txt
$ cat b.txt
geeksforgeeks

$ ls -l b.txt
-r--r--r--+ 1 User User 13 Jan 9 13:37 b.txt

$ mv geek.txt b.txt
mv: replace 'b.txt', overriding mode 0444 (r--r--r--)? n

$ ls
b.txt c.txt d.txt geek.txt

$ mv -f geek.txt b.txt

$ ls
b.txt c.txt d.txt

$ cat b.txt
India
3. -n (no-clobber): With -n option, mv prevent an existing file from being overwritten.
In the following example the effect is for nothing to happen as a file would be overwritten.
$ ls
b.txt c.txt d.txt geek.txt

$ cat b.txt
geeksforgeeks

$ mv -n geek.txt b.txt

$ ls
b.txt c.txt d.txt geek.txt

$ cat b.txt
geeksforgeeks
4. –b (backup): With this option it is easier to take a backup of an existing file that will be
overwritten as a result of the mv command. This will create a backup file with the tilde
character(~) appended to it.
$ ls
b.txt c.txt d.txt geek.txt

$ mv -b geek.txt b.txt
$ ls
b.txt b.txt~ c.txt d.txt
5. –version: This option is used to display the version of mv which is currently running on your
system.
$ mv --version
mv (GNU coreutils) 8.26
Packaged by Cygwin (8.26-2)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
3) rm command:
rm stands for remove here. rm command is used to remove objects such as files, directories,
symbolic links and so on from the file system like UNIX. To be more precise, rm removes
references to objects from the filesystem, where those objects might have had multiple references
(for example, a file with two different names). By default, it does not remove directories.
This command normally works silently and you should be very careful while
running rm command because once you delete the files then you are not able to recover the
contents of files and directories.
Syntax:
rm [OPTION]... FILE...
Let us consider 5 files having name a.txt, b.txt and so on till e.txt.
$ ls
a.txt b.txt c.txt d.txt e.txt
Removing one file at a time
$ rm a.txt

$ ls
b.txt c.txt d.txt e.txt

Removing more than one file at a time


$ rm b.txt c.txt

$ ls
d.txt e.txt
Options:
1. -i (Interactive Deletion): Like in cp, the -i option makes the command ask the user for
confirmation before removing each file, you have to press y for confirm deletion, any other key
leaves the file un-deleted.
$ rm -i d.txt
rm: remove regular empty file 'd.txt'? y

$ ls
e.txt

2. -f (Force Deletion): rm prompts for confirmation removal if a file is write protected. The -
f option overrides this minor protection and removes the file forcefully.
$ ls -l
total 0
-r--r--r--+ 1 User User 0 Jan 2 22:56 e.txt
$ rm e.txt
rm: remove write-protected regular empty file 'e.txt'? n

$ ls
e.txt

$ rm -f e.txt

$ ls

Note: -f option of rm command will not work for write-protect directories.

3. -r (Recursive Deletion): With -r(or -R) option rm command performs a tree-walk and will
delete all the files and sub-directories recursively of the parent directory. At each stage it deletes
everything it finds. Normally, rm wouldn’t delete the directories but when used with this option,
it will delete.
Below is the tree of directories and files:
$ ls
A

$ cd A

$ ls
B C

$ ls B
a.txt b.txt

$ ls C
c.txt d.txt

Now, deletion from A directory(as parent directory) will be done as:


$ rm *
rm: cannot remove 'B': Is a directory
rm: cannot remove 'C': Is a directory

$ rm -r *

$ ls
Every directory and file inside A directory is deleted.
4. –version: This option is used to display the version of rm which is currently running on your
system.
$ rm --version
rm (GNU coreutils) 8.26
Packaged by Cygwin (8.26-2)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Paul Rubin, David MacKenzie, Richard M. Stallman,


and Jim Meyering.
Applications of wc Command
Delete file whose name starting with a hyphen symbol (-): To remove a file whose name
begins with a dash (“-“), you can specify a double dash (“–“) separately before the file name.
This extra dash is necessary so that rm does not misinterpret the file name as an option. Let say
their is a file name -file.txt, to delete this file write command as:
$ ls
-file.txt

$ rm -file.txt
rm: unknown option -- l
Try 'rm ./-file.txt' to remove the file '-file.txt'.
Try 'rm --help' for more information.

$ rm -- -file.txt

$ ls
3) cp command
cp stands for copy. This command is used to copy files or group of files or directory. It creates
an exact image of a file on a disk with different file name. cp command require at least two
filenames in its arguments.
Syntax:
cp [OPTION] Source Destination
cp [OPTION] Source Directory
cp [OPTION] Source-1 Source-2 Source-3 Source-n Directory

First and second syntax is used to copy Source file to Destination file or Directory.
Third syntax is used to copy multiple Sources(files) to Directory.
cp command works on three principal modes of operation and these operations depend
upon number and type of arguments passed in cp command :
1. Two file names : If the command contains two file names, then it copy the contents of 1st
file to the 2nd file. If the 2nd file doesn’t exist, then first it creates one and content is
copied to it. But if it existed then it is simply overwritten without any warning. So be
careful when you choose destination file name.
2. cp Src_file Dest_file
Suppose there is a directory named geeksforgeeks having a text file a.txt.
Example:
$ ls
a.txt

$ cp a.txt b.txt

$ ls
a.txt b.txt
3. One or more arguments : If the command has one or more arguments, specifying file
names and following those arguments, an argument specifying directory name then this
command copies each source file to the destination directory with the same name, created if
not existed but if already existed then it will be overwritten, so be careful !!.
4. cp Src_file1 Src_file2 Src_file3 Dest_directory
Suppose there is a directory named geeksforgeeks having a text file a.txt, b.txt and a
directory name new in which we are going to copy all files.
Example:
1. $ ls
2. a.txt b.txt new
3.
4. Initially new is empty
5. $ ls new
6.
7. $ cp a.txt b.txt new
8.
9. $ ls new
10. a.txt b.txt
11. Note: For this case last argument must be a directory name. For the above command to
work, Dest_directory must exist because cp command won’t create it.
12. Two directory names : If the command contains two directory names, cp copies all files
of the source directory to the destination directory, creating any files or directories needed.
This mode of operation requires an additional option, typically R, to indicate the recursive
copying of directories.
13. cp -R Src_directory Dest_directory
In the above command, cp behavior depend upon whether Dest_directory is exist or not. If
the Dest_directory doesn’t exist, cp creates it and copies content
of Src_directory recursively as it is. But if Dest_directory exists then copy
of Src_directorybecomes sub-directory under Dest_directory.
Options:
There are many options of cp command, here we will discuss some of the useful options:
Suppose a directory named geeksforgeeks contains two files having some content named
as a.txt and b.txt. This scenario is useful in understanding the following options.
$ ls geeksforgeeks
a.txt b.txt

$ cat a.txt
GFG

$ cat b.txt
GeeksforGeeks
1. -i(interactive): i stands for Interactive copying. With this option system first warns the user
before overwriting the destination file. cp prompts for a response, if you press y then it
overwrites the file and with any other option leave it uncopied.
$ cp -i a.txt b.txt
cp: overwrite 'b.txt'? y

$ cat b.txt
GFG
2. -b(backup): With this option cp command creates the backup of the destination file in the
same folder with the different name and in different format.
$ ls
a.txt b.txt
$ cp -b a.txt b.txt

$ ls
a.txt b.txt b.txt~
3. -f(force): If the system is unable to open destination file for writing operation because the user
doesn’t have writing permission for this file then by using -f option with cp command,
destination file is deleted first and then copying of content is done from source to destination file.
$ ls -l b.txt
-r-xr-xr-x+ 1 User User 3 Nov 24 08:45 b.txt

User, group and others doesn't have writing permission.

Without -f option, command not executed


$ cp a.txt b.txt
cp: cannot create regular file 'b.txt': Permission denied

With -f option, command executed successfully


$ cp -f a.txt b.txt
4. -r or -R: Copying directory structure. With this option cp command shows its recursive
behavior by copying the entire directory structure recursively.
Suppose we want to copy geeksforgeeks directory containing many files, directories
into gfg directory(not exist).
$ ls geeksforgeeks/
a.txt b.txt b.txt~ Folder1 Folder2

Without -r option, error


$ cp geeksforgeeks gfg
cp: -r not specified; omitting directory 'geeksforgeeks'

With -r, execute successfully


$ cp -r geeksforgeeks gfg

$ ls gfg/
a.txt b.txt b.txt~ Folder1 Folder2

5. -p(preserve): With -p option cp preserves the following characteristics of each source file in
the corresponding destination file: the time of the last data modification and the time of the last
access, the ownership (only if it has permissions to do this), and the file permission-bits.
Note: For the preservation of characteristics you must be the root user of the system, otherwise
characteristics changes.
$ ls -l a.txt
-rwxr-xr-x+ 1 User User 3 Nov 24 08:13 a.txt

$ cp -p a.txt c.txt

$ ls -l c.txt
-rwxr-xr-x+ 1 User User 3 Nov 24 08:13 c.txt
As we can see above both a.txt and c.txt(created by copying) have same characteristics.
Examples:
Copying using * wildcard: The star wildcard represents anything i.e. all files and directories.
Suppose we have many text document in a directory and wants to copy it another directory, it
takes lots of time if we copy files 1 by 1 or command becomes too long if specify all these file
names as the argument, but by using * wildcard it becomes simple.
Initially Folder1 is empty
$ ls
a.txt b.txt c.txt d.txt e.txt Folder1

$ cp *.txt Folder1

$ ls Folder1
a.txt b.txt c.txt d.txt e.txt
5) wc command
wc stands for word count. As the name implies, it is mainly used for counting purpose.
 It is used to find out number of lines, word count, byte and characters count in the files
specified in the file arguments.
 By default it displays four-columnar output.
 First column shows number of lines present in a file specified, second column shows
number of words present in the file, third column shows number of characters present in
file and fourth column itself is the file name which are given as argument.
Syntax:
wc [OPTION]... [FILE]...
Let us consider two files having name state.txt and capital.txt containing 5 names of the Indian
states and capitals respectively.
$ cat state.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh

$ cat capital.txt
Hyderabad
Itanagar
Dispur
Patna
Raipur
Passing only one file name in the argument.
$ wc state.txt
5 7 63 state.txt
OR
$ wc capital.txt
5 5 45 capital.txt
Passing more than one file name in the argument.
$ wc state.txt capital.txt
5 7 63 state.txt
5 5 45 capital.txt
10 12 108 total
Note : When more than file name is specified in argument then command will display four-
columnar output for all individual files plus one extra row displaying total number of lines,
words and characters of all the files specified in argument, followed by keyword total.
Options:
1. -l: This option prints the number of lines present in a file. With this option wc command
displays two-columnar output, 1st column shows number of lines present in a file and 2nd itself
represent the file name.
With one file name
$ wc -l state.txt
5 state.txt

With more than one file name


$ wc -l state.txt capital.txt
5 state.txt
5 capital.txt
10 total
2. -w: This option prints the number of words present in a file. With this option wc command
displays two-columnar output, 1st column shows number of words present in a file and 2nd is the
file name.
With one file name
$ wc -w state.txt
7 state.txt

With more than one file name


$ wc -w state.txt capital.txt
7 state.txt
5 capital.txt
12 total
3. -c: This option displays count of bytes present in a file. With this option it display two-
columnar output, 1st column shows number of bytes present in a file and 2nd is the file name.
With one file name
$ wc -c state.txt
63 state.txt

With more than one file name


$ wc -c state.txt capital.txt
63 state.txt
45 capital.txt
108 total
4. -m: Using -m option ‘wc’ command displays count of characters from a file.
With one file name
$ wc -m state.txt
63 state.txt

With more than one file name


$ wc -m state.txt capital.txt
63 state.txt
45 capital.txt
108 total
5. -L: The ‘wc’ command allow an argument -L, it can be used to print out the length of longest
(number of characters) line in a file. So, we have the longest character line Arunachal Pradesh in
a file state.txt and Hyderabad in the file capital.txt. But with this option if more than one file
name is specified then the last row i.e. the extra row, doesn’t display total but it display the
maximum of all values displaying in the first column of individual files.
Note: A character is the smallest unit of information that includes space, tab and newline.
With one file name
$ wc -L state.txt
17 state.txt

With more than one file name


$ wc -L state.txt capital.txt
17 state.txt
10 capital.txt
17 total
6. –version: This option is used to display the version of wc which is currently running on your
system.
$ wc --version
wc (GNU coreutils) 8.26
Packaged by Cygwin (8.26-1)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
6) od command
od command in Linux is used to convert the content of input in different formats with octal
format as the default format.This command is especially useful when debugging Linux scripts
for unwanted changes or characters. If more than one file is specified, od command concatenates
them in the listed order to form the input.It can display output in a variety of other formats,
including hexadecimal, decimal, and ASCII. It is useful for visualizing data that is not in a
human-readable format, like the executable code of a program.
Syntax :
od [OPTION]... [FILE]...
Options of od command
1. -b Option :It displays the contents of input in octal format.
SYNTAX :
$ od -b input.txt

The first column in the output of od represents the byte offset in file.
2. -c Option :It displays the contents of input in character format.
SYNTAX :
$ od -c input.txt

3. -An Option :It displays the contents of input in character format but with no offset
information.
SYNTAX :

$ od -An -c input.txt

4. -A Option :It displays the contents of input in different format by concatenation some special
character with -A.
For example:
1. -Ax for Hexadecimal format(we concatenate x with -A)
2. -Ao for Hexadecimal format(we concatenate o with -A)
3. -Ad for Hexadecimal format(we concatenate d with -A)
SYNTAX :
$ od -Ax input.txt
$ od -Ao input.txt
$ od -Ad input.txt

5. – Option :Accept input from command line.


SYNTAX :
$ od -c -

You might also like