Safely remove files and directories in Linux Operating System through SSH or Command Line Interface

In this post, we will show you how to remove files and directories in Linux based Operating Systems through SSH or Command Line Interface. Removing the files or folders using commands may leave you in trouble sometimes. Here we have elaborated each commands with a short explanation for better understanding.

Directories & Files:

In a storage system, normally the data will be in the form of files & folders. In Linux System & Linux based groups, people used to mentioned the folders as directories. Normally, Folders will be comprised of files & other sub-folders whereas files will have the data.

How To Remove Files:

In Linux, to remove a file or directory(folder) from the command line, we must use “rm” command. It is one of the basic Linux Commands that you should know.

rm” is a common command to remove the files and directories(folders).

  • To delete a single file in your linux operating system use “rm” command
    Example: In the below example, I have deleted the files “dir1” & “files2”.
    $rm filename

Remove single file

  • To remove multiple files at the same time, you can use “rm” command followed by consecutive files names with a space in between.
    For example: If my files are names as “filename1”, “filename2” and “filename3” then the linux command to delete all the files will be as mentioned below
    $rm filename1 filename2 filename3

Remove multiple files

  • You can also use a wildcard (*) and regular expansions to match multiple files.
    For example to remove all “.pdf” files in the current directory, you can use the following command
    $rm *.pdf

 

  • Use the “-i” option to confirm each file before deleting. If you have one or more important files & you want to make sure that the unwanted files alone getting deleted, you can use this command. It may not be helpful when you delete a single file. It will effective to use this option when you use a wildcard option.
    $rm –i  *.pdf

 

  • To remove files without prompting even if the files are write-protected use the -f(force) option. You can use this option to complete the process without any interruption. It is not a recommended operation but you can use this while deleting unimportant files.
    $rm –f filename

 

How To Remove The Directories:

In this section, I have explained how to remove the directories in Linux based systems using SSH Shell Access or Command Line Interface. The commands what you use to delete the files will not work on directories but moreover it will similar & easy to understand.

  • You should use “-d” command for removing the empty directory. If the directory doesn’t have any files, then you can use this command to delete it.
    For example: If the directory name is “dirname” then the command will be,
    $rm –d dirname

If a directory or a file within the directory is write-protected, you will be prompted to confirm the deletion.

remove empty directory

  • To delete a non-empty directory and all the files within that, then you should use “-r” option which is a recursive function.
    For example: If the directory names as “dirname” is an non-empty directory then the command will be,
    $rm –r dirname

remove non-empty directory

  • To delete non-empty directories and all the files without being prompted, use the “r and f” option. It will delete all the files within the directory & finally the directory as well.
    $rm –rf dirname

 

  • To remove the multiple directories at once use the “rm” & “-r” command followed by the directory name separated by space.
    $rm –r dirname1 dirname2 dirname3

 

  • You can use wildcard(*) and regular expansions to match multiple directories same as using multiple files removing command. If you don’t need any files or directories within a directory or a partition, you can use this option.
    $rm -rf *

Also, check the 70 Basic Linux Commands that makes you to work comfortably in Linux based operating systems.