HomeLinuxDelete Recordsdata and Folders in Linux Command Line

Delete Recordsdata and Folders in Linux Command Line


Within the earlier chapters of the Terminal Fundamentals sequence, you discovered to create new information and directories (folders).

Let’s now see how one can delete information and folders within the Linux terminal.

Deleting information

To take away information, you should use the rm command within the following style:

rm filename_or_path

You will not see any output if the file is efficiently deleted.

This is an instance the place I eliminated one of many information named new_file. Once I record the listing contents, you’ll be able to see that new_file not exists.

Removing files in Linux terminal
Eradicating a single file

You can too take away a number of information in the identical command:

rm file1 file2 file3

Let me present an instance of deleting two information in a single command.

Deleting multiple files in single rm command
Eradicating a number of information

🏋️Train file deletion

Let’s observe what you simply discovered. Create a listing named practice_delete and change to it:

mkdir practice_delete && cd practice_delete

Now create just a few empty information:

contact file1 file2 file3

Delete the file3:

rm file3

Now, let’s do one thing further. Run this command and alter the permission on file2:

chmod u-w file1 file2

Attempt deleting file2 now:

rm file2

Do you see a message ‘take away write protected file‘? That is since you eliminated the write permission (for modification) from this file.

You may press Y or enter key to substantiate the deletion or N to disclaim the removing.

If you happen to do not need to see this message and nonetheless delete it, you should use the drive delete choice -f. Attempt it by deleting file1:

rm -f file1

This is a replay of all of the above examples that can assist you:

Deleting files in Linux terminal

🚧

There is no such thing as a trash bin within the Linux command line. As soon as the file is deleted, you can not undo the motion to convey it again from the trash bin as you do within the graphical file supervisor. Because of this, be further cautious whereas deleting the information.

Take away however with warning

The shortage of trash bin makes the deletion a everlasting jobs of kind. For this reason try to be cautious about what information are you deleting.

There’s an interactive mode with choice -i. With this, you may be requested to substantiate the deletion.

rm -i filename

That is useful if you end up deleting a number of information based mostly on a sure sample.

This is an instance the place I’m interactively deleting all of the information that match file_ sample of their identify. I delete some and preserve some within the interactive mode.

Deleting files in interactive mode

💡

I counsel switching to the listing the place the information are positioned after which eradicating them. This helps in lowering any potential brought on by a typo in file path.

Deleting directories

There’s a devoted rmdir command to take away directories in Linux.

rmdir dir_name

Nonetheless, it will possibly solely delete empty directories. If the listing has any information or subdirectories in it, the rmdir command will throw error.

[email protected]:~/practice_delete$ rmdir dir2
rmdir: did not take away 'dir2': Listing not empty

And that makes it much less helpful most often.

So, how do you delete a non-empty folder then? Effectively, you utilize the identical rm command that you just used earlier for eradicating information.

Sure, the identical rm command however with the recursive choice -r:

rm -r dir_name

🏋️Train folder deletion

Let’s observe what you discovered.

Change to practice_delete folder if you’re not already there. Now, create two directories dir1 and dir2.

mkdir dir1 dir2

Create a file in dir2:

contact dir2/file

Now attempt deleting the directories utilizing the rmdir command:

rmdir dir1
rmdir dir2

For the reason that dir2 will not be empty, rmdir command will fail. As an alternative, use the rm command with recursive choice:

rm -r dir2

This is a replay of all of the above command examples that can assist you out:

Deleting folders in Linux

💡

The interactive deletion mode is much more useful whereas deleting a listing with the recursive choice of the rm command: rm-ri dir_name

So, you discovered to delete information and folders each utilizing Linux instructions. It is time to observe some extra.

Take a look at your data

Put together a listing tree that appears like this:

.
├── dir1
│   ├── file1
│   ├── file2
│   └── file3
├── dir2
├── dir3
└── file

Mainly, you create a file named file and three directories dir1, dir2 and dir3 within the present listing (practice_delete). And you then create information file1, file2 and file3 in dir1.

Now do the next:

  • Delete file2.
  • Change to the dir3 and drive delete the file named file within the higher listing.
  • Delete all of the contents of dir1 however not the listing itself.
  • Checklist the contents of the dir.

I encourage you to debate the observe questions within the It is FOSS group discussion board.

That is going good. You will have discovered a number of staple items like switching directories, checking contents of listing, creating and deleting information and directories. Within the subsequent chapter, you may find out about copying information and folders within the terminal. Keep tuned!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments