Difference between revisions of "Linux-Ubuntu/C2/Working-with-Regular-Files/English"
Ketkinaina (Talk | contribs) (Created page with "'''Title of the script: Working with Regular Files in Linux. '''Authors:''' '''EduPyramids''' '''Keywords:''' Linux, regular files, cp command, mv command, rm command, cmp c...") |
|||
| Line 16: | Line 16: | ||
'''Title Slide''' | '''Title Slide''' | ||
| − | || Welcome to this Spoken tutorial on '''Working with Regular Files | + | || Welcome to this Spoken tutorial on '''Working with Regular Files in Linux.''' |
|- | |- | ||
|| '''Slide 2 ''' | || '''Slide 2 ''' | ||
| Line 25: | Line 25: | ||
* Compare files and count lines, words, and characters using '''cmp''' and '''wc''' | * Compare files and count lines, words, and characters using '''cmp''' and '''wc''' | ||
* Use important command options to manage files safely. | * Use important command options to manage files safely. | ||
| + | |||
| + | |||
|| In this tutorial, we will learn how to: | || In this tutorial, we will learn how to: | ||
* Copy, move, and delete regular files in '''Linux''' | * Copy, move, and delete regular files in '''Linux''' | ||
| Line 73: | Line 75: | ||
* It creates a duplicate of a file or directory at a specified location. | * It creates a duplicate of a file or directory at a specified location. | ||
* It does not change or delete the original file | * 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''' | * '''cp SOURCE DESTINATION''' | ||
copies a file to another location | copies a file to another location | ||
* '''cp SOURCE DIRECTORY''' | * '''cp SOURCE DIRECTORY''' | ||
| − | |||
copies one or more files into a directory | copies one or more files into a directory | ||
| − | || | + | || |
| − | + | ||
| − | + | ||
* The command '''c p SOURCE DESTINATION''', copies a file to another location and | * 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. | * The''' c p SOURCE DIRECTORY, '''copies one or more files into a directory. | ||
| Line 128: | Line 138: | ||
After pressing '''Enter''', the command waits for input from the user. | After pressing '''Enter''', the command waits for input from the user. | ||
| − | Now I will type, '''I am using Linux. '''and press '''Enter'''. | + | Now I will type, '''I am using Linux. ''' and press '''Enter'''. |
Press''' Ctrl''' and''' D '''keys to come out of input mode. | Press''' Ctrl''' and''' D '''keys to come out of input mode. | ||
| Line 134: | Line 144: | ||
Let’s type '''cat space test1''' to see the content of the file '''test1'''. | Let’s type '''cat space test1''' to see the content of the file '''test1'''. | ||
| − | Now type '''c p space | + | 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. | This copies the content of '''test1''' into a new file '''test2''' in the same directory. | ||
| Line 145: | Line 155: | ||
|| To check type '''cat space test2''' and press''' Enter'''. | || To check type '''cat space test2''' and press''' Enter'''. | ||
| − | The original file '''test1 '''remains unchanged. | + | The original file '''test1 ''' remains unchanged. |
'''test1''' is the '''source file''' and '''test2''' is the '''destination file'''. | '''test1''' is the '''source file''' and '''test2''' is the '''destination file'''. | ||
| Line 156: | Line 166: | ||
|| We can also copy files between directories. | || 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'''. | + | 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'''. | + | 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'''. | Type '''l s''' space '''slash home slash spoken '''and press''' Enter'''. | ||
| Line 169: | Line 179: | ||
|| To keep the same file name, we need not specify its name at the destination. | || 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'''. | + | 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. | This will copy the file '''test1''' from '''slash home slash spoken slash test dir slash''' to the home directory. | ||
| Line 190: | Line 200: | ||
First, let us create more files by copying from the existing file. | First, let us create more files by copying from the existing file. | ||
| − | Type '''c p space test1 space test3 '''and press '''Enter'''. | + | Type '''c p space test1 space test3 ''' and press '''Enter'''. |
Again type '''c p space test1 space test4 '''and press''' Enter'''. | Again type '''c p space test1 space test4 '''and press''' Enter'''. | ||
| Line 219: | Line 229: | ||
Type''' ls test '''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 d space dot dot ''' and press''' Enter''' to move to the parent directory. |
| − | Let’s type''' c p space test d i | + | Let’s type ''' c p space test d i r slash test ''' and press ''' Enter'''. |
Here we get an error; | Here we get an error; | ||
| − | '''hyphen r not specified; omitting directory test dir slash | + | '''hyphen r not specified; omitting directory test dir slash'''. |
Normally, the '''c p''' command cannot copy directories with files. | Normally, the '''c p''' command cannot copy directories with files. | ||
| − | Using the '''hyphen R''' option with '''c p '''allows us to copy them. | + | 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'''. | Type '''c p space hyphen R space test dir slash space test''' and press '''Enter'''. | ||
| − | Observe that it copied the entire''' testdir directory '''recursively. | + | Observe that it copied the entire ''' testdir directory ''' recursively. |
| − | All files and subdirectories were copied into a new directory named''' test | + | All files and subdirectories were copied into a new directory named''' test'''. |
Type '''l s''' and press '''Enter'''. | Type '''l s''' and press '''Enter'''. | ||
| Line 241: | Line 251: | ||
We can see a directory named''' test'''. | We can see a directory named''' test'''. | ||
| − | Now type '''l s | + | Now type '''l s space test''' and press '''Enter'''. |
The output lists the files present in the '''test''' directory. | 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. | + | 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 | + | || '''Slide 8''' |
'''mv command''' | '''mv command''' | ||
| Line 274: | Line 286: | ||
This renames the file '''test1''' to '''test2'''. | 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. | + | 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''' | Prompt asks: '''overwrite test3 question mark''' | ||
| − | Type '''no '''and press''' Enter'''. | + | Type '''no ''' and press''' Enter'''. |
This will not overwrite the '''test3''' file. | This will not overwrite the '''test3''' file. | ||
| Line 289: | Line 305: | ||
|| Like '''c p''',''' m v''' can move multiple files, but the destination must be a directory. | || 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'''. | + | 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. | This command moves the files '''test2''', '''test3''', and '''test4''' to the specified directory. | ||
| Line 297: | Line 313: | ||
Files are moved from the current directory to '''slash home slash spoken slash test dir '''directory. | 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 | + | 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 output shows that the files have been moved. | ||
| Line 304: | Line 320: | ||
|| The next command is '''r m'''. | || The next command is '''r m'''. | ||
|- | |- | ||
| − | || '''Slide''' ''' | + | || '''Slide''' '''9''' |
'''rm command''' | '''rm command''' | ||
| Line 318: | Line 334: | ||
'''Highlight test4 file.''' | '''Highlight test4 file.''' | ||
| − | || Type '''r m space test dir slash test1 '''and | + | || Type '''r m space test dir slash test1 '''and press '''Enter.''' |
This removes the file '''test1''' from the '''test dir''' directory. | This removes the file '''test1''' from the '''test dir''' directory. | ||
| Line 324: | Line 340: | ||
We can use it with multiple files also. | 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'''. | + | 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. | Only '''test4''' is available in the '''test dir''' directory. | ||
|- | |- | ||
| − | || '''Slide | + | || '''Slide 10''' |
'''-f and -r option''' | '''-f and -r option''' | ||
| Line 349: | Line 367: | ||
|- | |- | ||
|| Highlight '''testdir folder'''Type''' rm -rf testdir '''press''' EnterHighlight it is removed from files'''Type '''ls '''press '''Enter''' | || Highlight '''testdir folder'''Type''' rm -rf testdir '''press''' EnterHighlight it is removed from files'''Type '''ls '''press '''Enter''' | ||
| − | || Type''' r m space hyphen r f space test dir '''and | + | || Type''' r m space hyphen r f space test dir '''and press''' Enter'''. |
This deletes '''test dir''' and all its files and subdirectories recursively. | This deletes '''test dir''' and all its files and subdirectories recursively. | ||
| Line 355: | Line 373: | ||
Type: '''l s''' and press '''Enter'''. | Type: '''l s''' and press '''Enter'''. | ||
| − | Observe that the''' test dir''' directory is deleted. | + | Observe that the ''' test dir''' directory is deleted. |
|- | |- | ||
|| | || | ||
| Line 367: | Line 385: | ||
But first let’s learn what '''cmp''' command is? | But first let’s learn what '''cmp''' command is? | ||
|- | |- | ||
| − | || '''Slide | + | || '''Slide 11''' |
'''cmp commands''' | '''cmp commands''' | ||
| Line 387: | Line 405: | ||
We will create two sample files '''sample 1''' and '''sample 2''' using the '''cat''' command. | We will create two sample files '''sample 1''' and '''sample 2''' using the '''cat''' command. | ||
| − | + | ||
* '''sample1''': This is a Linux file. | * '''sample1''': This is a Linux file. | ||
* '''sample2''': This is a Unix file. | * '''sample2''': This is a Unix file. | ||
| Line 406: | Line 424: | ||
'''Highlight output''' | '''Highlight output''' | ||
| − | || Next we will see the '''wc''' command.'''w c''' counts the number of lines, words, and characters in a file. | + | || 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'''. | For example, Type '''cat''' '''space''' '''sample2''' and press '''Enter'''. | ||
| Line 419: | Line 439: | ||
|| With this we come to the end of this tutorial. | || With this we come to the end of this tutorial. | ||
|- | |- | ||
| − | || '''Slide | + | || '''Slide 12''' |
'''Summary''' | '''Summary''' | ||
| Line 430: | Line 450: | ||
|| Let us summarise. | || Let us summarise. | ||
|- | |- | ||
| − | || '''Slide | + | || '''Slide 13''' |
As an assignment, | As an assignment, | ||
| Line 439: | Line 459: | ||
|| As an assignment, please do the following. | || As an assignment, please do the following. | ||
|- | |- | ||
| − | || '''Slide | + | || '''Slide 14''' |
'''Thank you''' | '''Thank you''' | ||
Latest revision as of 18:23, 23 January 2026
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
|
In this tutorial, we will learn how to:
|
| Slide 3
System Requirements |
To record this tutorial, I am using
Ubuntu OS version 24 point 04. |
| Slide 4
Pre-requisites |
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
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
|
|
| Slide 7
cp command
copies a file to another location
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 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 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
|
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
|
|
| 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.
|
| 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:
|
Let us summarise. |
| Slide 13
As an assignment,
|
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. |