File Commands with Examples | WC command with Example | CP Command with Example | CMP Command with Example | LS command with Example | RM command with Example

Unix File Commands with Examples:

In my previous article i have explained the Unix Directory commands with real world example and how to create file in multiple ways in unix.In this article i will explain Unix File Commands  with examples.File is nothing but collection of information.The files are very much useful in any operating system.In Unix  operating system everything is considered as file ; unix directory is also considered as one type of file.There are following types of files in unix:

1.Regular/ Ordinary FilesUnix Files

2.Directories

3.Device Specific Files

4.Links

5.Named Pipes

6.Sockets

For working on such files we need to use following different Unix File Commands:

1.WC command with Example :

WC command is very useful and most use File Commands in unix which is used to count the words,total characters in file,total byte count.WC stands for ‘Word Count’ which counts total number of lines,words or characters in the specific file.

Generic Syntax of WC Command :

. $ wc File_Name option

1.1. To count No of lines :

$ wc – l File_Name

The above command is used to calculate the no of lines in specific file.

Example :

$ wc – l Amit.txt

100

1.2. To count No of words in file :

$ wc – w File_Name

The above command is used to calculate the no of lines in specific file.

Example :

$ wc – w Amit.txt

1000

1.3. To count No of characters in file :

$ wc – c File_Name

The above command is used to calculate the no of characters in file.

Example :

$ wc – c Amit.txt

10000

1.4. To count No of lines and words in file :

$ wc –lw  File_Name

The above command is used to calculate line and word for specific file.

Example :

$ wc –lw Amit.txt

100

1000

1.5. To count No of words and characters in file :

$ wc –lw  File_Name

The above command is used to calculate line and word for specific file.

Example :

$ wc –lw Amit.txt

1000

10000

1.6. To count No of words and characters in file :

$ wc –lc  File_Name

The above command is used to calculate line and character for specific file.

Example :

$ wc –lc Amit.txt

100

10000

2.CP Command with Example :

CP command in unix is used to copy the file from source path to target path.CP command is used to copy the file content from source file  to target file.Source file must be existing file and target file may be a new file/existing file.

Syntax:

$ cp Source_File Target_File

Example:

$ cp unix.txt linux.txt

This command will copy all contents in file unix.txt  to linux.txt.

3.CMP Command with Example :

It compares 2 files character by character, if no output then files are same otherwise the files are not same & output is shown.

Syntax :

$ cmp File_1 File_2

Example :

 $cmp a1 a2 (If character are same no result)

Step 1 :

$ cat>A1

Hello

Ctrl+d

 

$ cat>A2

Hello

Ctrl+d

 

$ cat> A3

Helo

Ctrl+d

Step 2:

$ cmp A2 A3

4.LS command with Example:

Ls command is most commonly used unix command to show the list of files in that working directory.

Syntax:

$ls [option]

To list all Files and Directories :

It list current directory all files & sub – directories in ascending order based on ASCII Values.

Example :

$ls

Amit pradnya.txt unix.txt

To list all files with hidden files/directories :

Following option is used to show the files with hidden files and folders in specific directory.

$ ls -a

To list all files in reverse order(Descending) :

$ ls -r

To list all files recursively :

$ ls – R

To list all files based on date and time of creation :

$ ls – t

5.RM command to remove files :

RM command is used to remove file on specific path of unix.

Syntax :

$rm File_Name

Example :

$rm Amit.txt

If file is removed properly it will produce no output on prompt.

5.1.To remove File with permission :

If user wants to remove file with its permissions use -i option.

Syntax :

$ rm –I file_name

Example :

$ rm –I Amit.txt

5.2.To remove File forcefully:

If user wants to remove file forcefully use -f option. IF using simple method file is not removing then try this method.

Syntax :

$ rm –f file_name

Example :

$ rm –f Amit.txt

5.3.To remove multiple files:

User can remove multiple files at a same time.

Syntax :

$ rm file1 file2 ……File_N

Example :

$ rm Amit.txt unix.tx Linux.txt

The above statement will remove 3 different files Amit.txt,unix.txt and Linux.txt.

Hope this article is useful to users who are working on unix.If you like this article dont forget to comment in comment section.

 

 

 

Leave a Reply