How to Remove Directories and Files in Linux using Command Line

1. To remove a directory, use the “rm” command followed by the directory name. For example, to remove a directory named “mydir”, type the following command:

rm -r mydir

2. To remove a file, use the “rm” command followed by the file name. For example, to remove a file named “myfile.txt”, type the following command:

rm myfile.txt

3. To remove multiple files or directories at once, use the “rm” command followed by the names of the files or directories separated by spaces. For example, to remove two files named “myfile1.txt” and “myfile2.txt”, type the following command:

rm myfile1.txt myfile2.txt

4. To remove all files and directories in a directory, use the “rm” command followed by the “-r” option and the directory name. For example, to remove all files and directories in a directory named “mydir”, type the following command:

rm -r mydir
[ad_1]

In this remove or delete directories and files linux tutorial guide, you will learn how to remove empty directory and non empty directory linux using command line. And as well as how to remove/file files linux using command line.

If you work with Linux then you will need the following:

  • how to remove empty directory in linux,
  • how to remove non empty directory,
  • how to remove directory without confirmation linux
  • how to remove files with and without confirmation in linux.

So, this tutorial guide will show you you how to use the rmunlink, and rmdir commands to remove or delete files and directories in Linux with and without confirmation.

How to Remove Files and Directories Using Linux Command Line

In this tutorial, you will learn the following:

  1. How to Remove Files Linux
  2. How to Delete Directory in Linux
    1. Remove empty directory linux
    2. Remove non empty directory linux
    3. Remove multiple directories linux

1 – How to Remove Files Linux

If you want to remove (or delete) a file in Linux from the command line. So, you can use the rm (remove) or unlink command.

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

1 :- How to remove a single file in linux

You can use the rm and unlink command to delete single file in linux. See the uses of rm and unlink command given below:

unlink filename
rm filename

If the file is write-protected, you will be prompted for confirmation on your terminal or command prompt, as shown below.

rm: remove write-protected regular empty file 'filename'?

Note that, Otherwise, if the file is not write-protected, it will be deleted without prompting.

2 :- How to remove multiple files at once in linux

You can use the rm command to remove multiple file in linux. See the uses of rm command with multiple options, as shown below:

  • To delete multiple files at once using rm command:
rm filename1 filename2 filename3
  • Wildcard (*) and regular expansions to delete files

If you want to remove all .txt files in the current directory, use the following command:

rm *.pdf
  • Delete file with confirmation

You can use the rm with the -i option to confirm each file before deleting it:

rm -i filename(s)
  • Delete files in linux without confirmation

You can use the rm with the -f option to without prompting even:

rm -f filename(s)

2 – How to Delete Directory in Linux

If you want to remove (or delete) a direcotories in Linux from the command line. So, you can use the rmdir and rm command.

1 :- Remove empty directory linux

If you want to delete an empty directory in linux. So, you can use rmdir or rm -d command, as shown below:

rm -d dirname

rmdir dirname

2 :- Remove non empty directory linux

If you want to remove non empty directory in linux. So, you can use with the-r (recursive) option, as shown below:

rm -r dirname
  • Delete non empty directory linux without prompt

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

rm -rf dirname
  • Delete non empty directory linux without prompt

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

rm -rf dirname

3 :- Remove multiple directories linux

If you want to delete multiple directories at once in linux. So, you can use the rm -r command, as shown below:

rm -r dirname1 dirname2 dirname3

Conclusion

In this tutorial guide, you have learned how to use the Linux rmrmdir and unlink commands to remove or delete files and directories in linux. And as well as how to remove or delete empty and non empty directories in linux.

Recommended Posts

[ad_2]

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00