[email protected]Office hours: Mon – Fri 9:00AM – 5:00PMLinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedJuly 3, 2023

How to remove files and directories.

How To Remove Files And Directories.

This tutorial, will show you how to use the rm, unlink, and rmdir commands to remove files and directories in Linux.

 

How to Remove Files.

To remove (or delete) a file in Linux from the command line, use either the rm (remove) or unlink command.

The unlink command allows you to remove only a single file, while with rm, you can remove multiple files at once.

Be extra careful when removing files or directories, because once the file is deleted it cannot be easily recovered.

  • To delete a single file, use the rm or unlink command followed by the file name:

    unlink filename
    rm filename

    If the file is write-protected, you will be prompted for confirmation, as shown below. To remove the file type y, and hit Enter. Otherwise, if the file is not write-protected, it will be deleted without prompting.

    Output:
    rm: remove write-protected regular empty file 'filename'?
  • To delete multiple files at once, use the rm command followed by the file names separated by space.

    rm filename1 filename2 filename3

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

    rm *.pdf

    When using regular expansions, first list the files with the ls command so that you can see what files will be deleted before running the rm command.

  • Use the rm with the -i option to confirm each file before deleting it:

    rm -i filename(s)
  • To remove files without prompting, even if the files are write-protected, pass the -f (force) option to the rm command:

    rm -f filename(s)
  • You can also combine rm options. For example, to remove all .txt files in the current directory without a prompt in verbose mode, use the following command:

    rm -fv *.txt

 

How to Remove Directories (Folders)

In Linux, you can remove/delete directories with the rmdir and rm.

rmdir is a command-line utility for deleting empty directories, while with rm you can remove directories and their contents recursively.

  • To remove an empty directory, use either rmdir or rm -d followed by the directory name:

    rm -d dirname
    rmdir dirname
  • To remove non-empty directories and all the files within them, use the rm command with the-r (recursive) option:

    rm -r dirname

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

  • To remove non-empty directories and all the files without being prompted, use rm with the -r (recursive) and -f options:

    rm -rf dirname
  • To remove multiple directories at once, use the rm -r command followed by the directory names separated by space.

    rm -r dirname1 dirname2 dirname3

    Same as with files, you can also use a wildcard (*) and regular expansions to match multiple directories.

 

Conclusion

By now you should have a good understanding of how to use the Linux rm, rmdir and unlink commands and you should be able to safely remove files and directories from the command line.

Keep reading
How to Implement Network Segmentation and Resource Isolation in Linux.
February 6, 2024

How to Implement Network Segmentation and Resource Isolation in Linux.

How to Implement Network Segmentation and Resource Isolation in Linux. Securing and managing networks in a Linux environment involves advanced practices like network segmentation and resource isolation. This guide explores these concepts, providing detailed instructions and examples for…

Read article
How to open or close ports in Linux.
February 6, 2024

How to open or close ports in Linux.

How to open or close ports in Linux. Effectively managing ports on your Linux system is crucial for both security and service accessibility. This comprehensive guide will provide a detailed walkthrough on opening and closing ports using the robust tools iptables and ufw. Understanding these…

Read article
How to use ypdomainname command.
January 23, 2024

How to use ypdomainname command.

How to use ypdomainname command. In the realm of Linux networking, the ypdomainname command plays a crucial role in the context of the Network Information Service (NIS). NIS, formerly known as Yellow Pages, is a distributed database service that facilitates the sharing of network configuration and…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, colocation, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Office hours: Mon – Fri 9:00AM – 5:00PM