How To Delete File Linux Terminal

Many commands are available to delete files in a Linux terminal window or command line, but the most frequently used is the rm command with its options. The rm command deletes files and directories and their contents. To delete a file, Just type rm filename on terminal and press enter, this will delete it. For example, if you want to delete the helloworld.txt file, you just type rm helloworld.txt on the terminal window.

You can also delete multiple files without a confirmation prompt, just type the command rm -rf with file names on the Terminal window, for example, to delete file1, file2, file3, just type rm -rf file1 file2 file3.

Here are some approaches to remove or delete files from Linux Ubuntu terminal window or command line:

Approach 1: Remove file using rm command ubuntu linux using terminal

To delete the file very fast, you can type the rm filename command on the Linux terminal window and hit Enter, You can use it like this:

rm hello.txt

WARNING: Do not type sudo rm -R / or sudo rm -r / or sudo rm -f /* or sudo rm --no-preserve-root -rf / as it removes all the data in the root directory. Avoid data loss and you should not execute them!

Approach 2: Delete file using the unlink Ubuntu Linux command line

To remove a file on the linux Ubuntu terminal, you can use the unlink filename command, you can use:

  1. Open the Ubuntu terminal
  2. Type unlink hello.txt to delete a file named hello.txt in the current directory

Approach 3: Delete Multiple Files Ubuntu linux Terminal

To delete multiple files in Ubuntu Linux terminal, you have to use the command rm filename filename1, you can use it like this:

rm hello.txt my.txt abc.jpg

Also, you can specify path in rm command. here is an example:

rm /tmp/hello.txt
rm /tmp/hello.txt /home/html/my.txt/home/html/data/abc.jpg

Approach 4: Remove the file with confirmation in Ubuntu terminal

To delete a file with confirmation prompts in Linux by using the rm command with the -i option. For example, if you want to delete a file named “file1. txt” with confirmation prompts, you would type rm -i file1. txt in the terminal.

 rm -i fileNameHere
 rm -i hello.txt

Approach 5: Delete File in linux without Prompt

On Linux Ubuntu command line, you can force delete the file completely without prompt, for this you have to use rm -f command, you would type rm -r file1 in the terminal:

 rm -f fileNameHere
 rm -f cake-day.jpg

Approach 6: To Remove All Files and Subdirectories in a Directory Linux Ubuntu

You can delete all the files, subdirectories, and other contents in a directory with the rm -rf directory_name command, for this you have to use this command like this:

 rm -rf dir1
 rm -rf /path/to/dir/
 rm -rf /home/html/oldimages/

This command will remove all files and subdirectories from a directory. So be careful. Always keep backups of all important data on Ubuntu Linux.

Approach 7: Linux deletes a file that starts with a dash

If the name of a file or directory or folder starts with a , use the following syntax:

If you have a file whose name starts with a dash (-), hyphen (--), and you want to delete the file or folder, you need to use the command:

 rm -- -fileNameHere
 rm -- --fileNameHere
 rm -rf --DirectoryNameHere
 rm ./-file
 rm -rf ./--DirectoryNameHere

The rm command removes files on Ubuntu Linux. The rm command options are as follows:

  • -f : Remove read-only files immediately without any confirmation.
  • -i : Prompts end-users for confirmation before deleting every file.
  • -v : Shows the file names on the screen as they are being processed/removed from the filesystem.
  • -R OR -r : Removes the given directory along with its sub-directory/folders and all files.
  • -I : Prompts users everytime when an attempt is made to delete for than three files at a time. Also works when deleting files recursively.

Here is the video guide on how to delete file in linux using terminal:

Conclusion

That’s it; In this tutorial, you have learned how to delete and remove a file on Ubuntu Linux-based system using a terminal or command prompt.

Recommended Posts

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *