Linux-Ubuntu/C2/Working-with-Regular-Files/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of the script: Working with Regular Files in Linux.

Authors: EduPyramids

Keywords: Linux, regular files, cp command, mv command, rm command, cmp command, wc command, EduPyramids, video tutorial.


Visual Cue Narration
Slide 1

Title Slide

Welcome to this Spoken tutorial on Working with Regular Files in Linux.
Slide 2

Learning Objectives

  • Copy, move, and delete regular files in Linux
  • Use file-handling commands such as cp, mv, and rm
  • Compare files and count lines, words, and characters using cmp and wc
  • Use important command options to manage files safely.


In this tutorial, we will learn how to:
  • Copy, move, and delete regular files in Linux
  • Use file-handling commands such as c p, m v, and r m
  • Compare files and count lines, words, and characters using c m p and w c
  • Use important command options to manage files safely.
Slide 3

System Requirements

To record this tutorial, I am using

Ubuntu OS version 24 point 04.

Slide 4

Pre-requisites

https://EduPyramids.org

To follow this tutorial,

Learners should have Ubuntu version 24 point 04.

For the prerequisite of Linux tutorials please visit this website.

Slide 5

Code files

The following code file is required to practice this tutorial

  1. wrf-commands.txt

This file is provided in the Code Files link of this tutorial page.

The following code file is required to practice this tutorial.

This file is provided in the Code Files link of this tutorial page.

Let’s get started with the c p command.

Let’s see what a c p command does?

Slide 6

cp command

  • The cp command is used to copy files and directories in Linux.
  • It creates a duplicate of a file or directory at a specified location.
  • It does not change or delete the original file
  • The c p command is used to copy files and directories in Linux.
  • It creates a duplicate of a file or directory at a specified location.
  • It does not change or delete the original file
Slide 7

cp command

  • cp SOURCE DESTINATION

copies a file to another location

  • cp SOURCE DIRECTORY

copies one or more files into a directory

  • The command c p SOURCE DESTINATION, copies a file to another location and
  • The c p SOURCE DIRECTORY, copies one or more files into a directory.
Let’s see how to copy a file using the c p command.
Press Ctrl+Alt+T keys together.Type: mkdir testdir press Enter

Point to testdir directory.

Type: cd testdirHighlight the changed directory

Type: pwd press Enter

Highlight the /home/spoken/testdir

Let us open the terminal.

Let us make a directory named test d i r in the home folder.

Type m k d i r test d i r press Enter.

The directory test d i r is created in the home directory.

Let us change the directory from home to test d i r.

Type c d space test d i r and press Enter.

We can see that the directory has changed to test d i r.

Now type p w d and press Enter.

My current working directory is slash home slash spoken slash test d i r.

Type cat >test1

Type: I am using Linux. and press Enter.Press Ctrl + D keys together

Type cat test1

Type cp test1 test2 and press Enter.

Point to the files test1 and test2

Let’s create a file by typing cat space greater than test1.

After pressing Enter, the command waits for input from the user.

Now I will type, I am using Linux. and press Enter.

Press Ctrl and D keys to come out of input mode.

Let’s type cat space test1 to see the content of the file test1.

Now type c p space test1 space test2 and press Enter.

This copies the content of test1 into a new file test2 in the same directory.

If the file test2 does not exist, it will be created.

If it exists, its content is overwritten.

Type cat test2 and press Enter. To check type cat space test2 and press Enter.

The original file test1 remains unchanged.

test1 is the source file and test2 is the destination file.

Type cp /home/spoken/testdir/test1 /home/spoken/test2 and press Enter.

Show in side by side manner

Type ls /home/spoken and press Enter.

We can also copy files between directories.

Type c p space slash home slash spoken slash test d i r slash test1 space slash home slash spoken slash test2 and press Enter.

This will copy the file test1 from test d i r directory to home directory as test2.

Type l s space slash home slash spoken and press Enter.

The output lists the contents of the directory.

We can see that file test2 is in the home directory.

Type cp /home/spoken/testdir/test1 /home/spoken/ and press Enter To keep the same file name, we need not specify its name at the destination.

Let’s type c p space slash home slash spoken slash test d i r slash test1 space slash home slash spoken and press Enter.

This will copy the file test1 from slash home slash spoken slash test dir slash to the home directory.

The destination file will also be named as test1.

Type cp test1 test3 press Enter

Type cp test1 test4 press Enter

Type ls press Enter

Highlight test1, test2, test3 and test4

Type: cp test3 test4 /home/spoken and press Enter.

Point to test3 and test4 in the home directory.

While copying multiple files, we need not specify the destination file name.

First, let us create more files by copying from the existing file.

Type c p space test1 space test3 and press Enter.

Again type c p space test1 space test4 and press Enter.

To list the files type l s and press Enter.

Output shows that test1, test2, test3 and test4 are in the current directory.

Now let us copy multiple files to the parent directory.

Type c p space test3 space test4 space slash home slash spoken.

Now test3 and test4 are also copied to the home directory.

Notice that all files are copied to the home directory with the same names.

Type cd .. and press Enter.

Highlight the home directoryType cp testdir/ test and press Enter.

Highlight cp: -r not specified; omitting directory 'testdir/'

Type cp -R testdir/ test and press Enter.

Point to test folder

Type ls and press Enter.

Type ls test and press Enter.

Let’s type c d space dot dot and press Enter to move to the parent directory.

Let’s type c p space test d i r slash test and press Enter.

Here we get an error;

hyphen r not specified; omitting directory test dir slash.

Normally, the c p command cannot copy directories with files.

Using the hyphen R option with c p allows us to copy them.

Type c p space hyphen R space test dir slash space test and press Enter.

Observe that it copied the entire testdir directory recursively.

All files and subdirectories were copied into a new directory named test.

Type l s and press Enter.

We can see a directory named test.

Now type l s space test and press Enter.

The output lists the files present in the test directory.

If test does not exist, it is created as a copy of test dir.

If the test exists, test dir is copied inside it as a subdirectory.

Slide 8

mv command

  • The mv command moves files and directories.
  • It is used to rename files or directory or move them to another directory.
  • Unlike the cp command, mv does not create a copy of the file.
The m v command moves files and directories.

It is used to rename files or directory or move them to another directory.

Unlike the c p command, m v does not create a copy of the file.

Type mv test1 test2 and press Enter.

Type cat >test1 and press Enter.

Type the prompt: I am using Linux. and press Enter.Press ctrl and D keys together.

Type mv -i test2 test3 press Enter

Prompt asks: overwrite test3?

Type no press Enter

In the terminal, type m v space test1 space test2 and press Enter.

This renames the file test1 to test2.

If test2 already exists, it is overwritten without warning.

Let us again create test1 file by using the cat command.

Now type m v space hyphen i space test2 space test3 and press Enter.

The hyphen i option asks for confirmation before overwriting the files.

Prompt asks: overwrite test3 question mark

Type no and press Enter.

This will not overwrite the test3 file.

Type mv test2 test3 test4 /home/spoken/testdir and press Enter.

Type ls /home/spoken/testdir press Enter

Like c p, m v can move multiple files, but the destination must be a directory.

Type m v space test2 space test3 space test4 space slash home slash spoken slash test dir and press Enter.

This command moves the files test2, test3, and test4 to the specified directory.

Press Enter.

Files are moved from the current directory to slash home slash spoken slash test dir directory.

To check, type l s space slash home slash spoken slash test dir and press Enter.

The output shows that the files have been moved.

The next command is r m.
Slide 9

rm command

  • The rm command in Linux is used to remove files and directories.
  • It permanently deletes the specified files from the system
The r m command in Linux is used to remove files and directories.

It permanently deletes the specified files from the system.

Show Terminal and Files side by side. Type rm testdir/test1 and press Enter.

Type rm testdir/test2 testdir/test3 press Enter

Highlight test4 file.

Type r m space test dir slash test1 and press Enter.

This removes the file test1 from the test dir directory.

We can use it with multiple files also.

Type r m space test dir slash test2 space test dir slash test3 press Enter.

This will remove the files test2 and test3.

Only test4 is available in the test dir directory.

Slide 10

-f and -r option

  • Use the -f option to force deletion.
  • The -r option is commonly used to remove directories recursively.
  • rm deletes files.
  • rmdir deletes only empty directories.
  • To delete a directory containing files and subdirectories, use both options together: rm -rf.
Sometimes a file is write-protected, and rm cannot delete it.

The hyphen f option forces deletion, even for write-protected items.

The hyphen r option removes directories and their contents recursively.

The r m command removes files.

The r m dir command removes empty directories only.

To delete a directory with files and subfolders, use r m hyphen r f together.

Highlight testdir folderType rm -rf testdir press EnterHighlight it is removed from filesType ls press Enter Type r m space hyphen r f space test dir and press Enter.

This deletes test dir and all its files and subdirectories recursively.

Type: l s and press Enter.

Observe that the test dir directory is deleted.

Sometimes we need to check whether two files are identical.

If they are the same, we can delete one of them.

For this, we use the c m p command.

But first let’s learn what cmp command is?

Slide 11

cmp commands

  • cmp compares two files byte by byte.
  • If the files are identical, it shows no output, only the prompt.
  • If there are differences, cmp shows the location of the first mismatch.
  • c m p compares two files byte by byte.
  • If the files are identical, it shows no output, only the prompt.
  • If there are differences, c m p shows the location of the first mismatch.
Type cat >sample1 press EnterType: This is a Linux file.

Press Ctrl and D keys together Type cat >sample2 press EnterType: This is a Unix file.

Press Ctrl and D keys together.

Let us see how the c m p command works.

We will create two sample files sample 1 and sample 2 using the cat command.


  • sample1: This is a Linux file.
  • sample2: This is a Unix file.
Type cmp sample1 sample2 press Enter.

Highlight:sample1 sample2 differ: byte 11, line 1

Type c m p space sample1 space sample2 press Enter.

We can see how the differences between the first and second file are highlighted.

Output is shown as sample1 sample2 differ: byte 11, line 1.

Type cat sample2 press Enter

Type wc sample2 press Enter

Highlight output

Next we will see the wc command.

w c counts the number of lines, words, and characters in a file.

For example, Type cat space sample2 and press Enter.

The file sample2 contains some text.

Now Type w c space sample2 and press Enter.

Using w c, we see it has 1 line, 5 words, and 21 characters.

With this we come to the end of this tutorial.
Slide 12

Summary

In this tutorial, we have learnt to:

  • Copy, move, and delete regular files in Linux
  • Use file-handling commands such as cp, mv, and rm
  • Compare files and count lines, words, and characters using cmp and wc
  • Use important command options to manage files safely
Let us summarise.
Slide 13

As an assignment,

  1. Copy one file to another directory using cp and verify the contents.
  2. Use cp -i and cp -b to observe how Linux prevents accidental overwriting.
  3. Compare two files using cmp and note the position of the first difference.
  4. Use the wc command to find the number of lines, words, and characters in a file.
As an assignment, please do the following.
Slide 14

Thank you

This Spoken Tutorial is brought to you by EduPyramids Educational Services Private Limited SINE IIT Bombay. Thank you.

Contributors and Content Editors

Ketkinaina, Madhurig