Forum

How to use the Linu...
 
Notifications
Clear all

How to use the Linux command line to remove directories and files?

 
(@administrator)
Member Admin

We will guide you through the usage of the rm, unlink, and rmdir commands in Linux in this article.

 

The method to find and remove files

You may use the rm (remove) or unlink (delete) command to remove a file in Linux from the command line.

With rm, you may delete several files, but with the unlink command, you can remove just one file.

 

Remove files and directories with extreme caution, since once the files and directories are removed, they cannot be readily restored.

 

# sudo unlink filename

 

# sudo rm filename

 

When using the rm command followed by the names of the files separated by space, you may remove several files at once.

 

# sudo rm yourfilename1 yourfilename2 yourfilename3

 

The following command removes all .pdf files in the current directory:

 

# sudo rm *.pdf

 

You may also use the -r option together with other rm options. As an example, in verbose mode, the following command removes all .txt files in the current directory:

 

# sudo rm -fv *.txt

 

Here are instructions for removing directories: (Folders)

 

With rm, you may remove directories and their contents recursively, whereas rmdir deletes empty directories.

 

Use rmdir or rm -d to delete empty directories:

 

# sudo rm -d yourdirname

 

# sudo rmdir yourdirname

 

Persoinally I use like this, 

 

Login into your server as root user via putty.exe and run

 

# sudo rm -r /var/www/html/arnlweb.com/cache

( it will remove cache directory)

# sudo rm -r /var/www/html/https:/arnlweb.com/cache/*

( it will remove all files inside the cache directory)

 

Your prior learning of the Linux rm, rmdir, and unlink commands should give you a solid knowledge of how to use them securely.

This topic was modified 3 years ago 2 times by Administrator
Quote
Topic starter Posted : 14/07/2021 12:18 pm
(@administrator)
Member Admin

To remove directories and files using the Linux command line, you can use the rm command. Here are some examples:

  1. Remove a file:
rm filename

This command will remove the file named filename in the current directory.

  1. Remove a directory:

rm -r directoryname

This command will remove the directory named directoryname and all its contents (subdirectories and files) in the current directory. The -r option stands for "recursive" and is necessary to remove a directory and its contents.

  1. Remove multiple files:

rm filename1 filename2 filename3

This command will remove the files named filename1, filename2, and filename3 in the current directory.

  1. Remove all files in a directory:

rm *

This command will remove all files in the current directory.

It is important to use the rm command with caution as it permanently deletes files and directories without confirmation. It is recommended to double-check the path and filenames before executing the command.

ReplyQuote
Topic starter Posted : 17/03/2023 3:52 am
Share:
×