How to delete a file using the terminal (2024)

Trusted answers to developer questions

Free System Design Interview Course

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2024 with this popular free course.

In this shot, you will learn how to remove a file (or files) from your system using Shell (bash, zsh, etc.) In case you’ve forgotten, Shell (or terminal) is the (black) interface that allows you to “instruct” a computer through a text command.

To perform a deletion task, we use the rm command.

Let’s see it in more detail below.

Remove a single file

Let’s start with a very simple scenario: removing one file from the current directory. For the demo, let’s create our files to work, as shown below:

touch cv_salimas.pdf cv_yves.doc songs_val.txt readme.txt unkis.doc projet_wmdrc.pdf

Type or copy the above command in the terminal (below) to create six empty files.

Terminal 1

Terminal

Loading...

Now, we can securely perform our deletion task on these files. The syntax is:

rm <filename>

rm is short for remove. To delete the file cv_yves.doc, we’ll type the command rm cv_yves.doc.

Beware: This rm command will permanently remove the file from your system. So, use it with caution. It’s even better to use it with the -i (interactive) flag, which will prompt you to confirm the deletion.How to delete a file using the terminal (1)To confirm the deletion, hit the Y button or type yes.

You can practice in Shell below:

In the first part of this section, we discussed how to remove a file from the current directory. Now, lets see how to remove a file located in another directory. The syntax is:

rm path/to/file

First, type the following command to create files in a new directory:

mkdir test-rm && touch test-rm/file1.txt test-rm/file2.txt test-rm/file3.txt

Terminal 1

Terminal

Loading...

To delete the file1.txt file, write:

rm test-rm/file1.txt

Note:

  • If the file is protected, you can force it to delete with the -f (force) flag:
rm -f <filename>
  • You can also delete a file with the unlink command:
unlink cv_salimas.pdf

Do you want to delete more files at once? Then, let’s move to the next section.

Remove multiple files

To delete multiple files, just pass their names, with spaces between them, after the rm command:

rm <file1> <file2> <file3> <etc>

Let’s try to delete these files (cv_salimas.pdf, songs_val.txt, unkis.doc) in in the terminal window below:

Terminal 1

Terminal

Loading...

To make sure you’ve successfully deleted all the files above, type ls. Now, you won’t see them in the list of files.

We can do even better using the wildcard character (*). This character means “any”. Let’s do some exercises so that you can better understand how to use this character.

(i) delete any file starting with CV_ character.

rm CV_*

(ii) delete all pdf files

rm *.pdf

(iii) delete all files

rm *

Note:

  • If the files are located in another directory, don’t forget to pass their path(s) after rm.
  • You can use all the flags (-i, -f) we learned about in the first section for the deletion of multiple files
  • unlink can’t be used to delete multiple files

Summary

The rm command is used to delete one or more files located in the current directory – this operation is permanent. For that reason, you may need to use rm with the -i flag so that you can be prompted for confirmation.

That’s all for today – thank you for learning with me! Before you go, here are some exercises for you.

Exercises

In the Terminal below:

  • delete all .txt files located in the exercise-rm directory
  • delete the protected.md file from the current directory

Terminal 1

Terminal

Loading...

RELATED TAGS

general

Keep Exploring

Related Courses

Did you find this helpful?

How to delete a file using the terminal (2024)

FAQs

How to delete a file using the terminal? ›

Select “Open in Terminal”.

You can use the command “rm -r” (short for “remove”) followed by the name of the directory to delete the whole folder and all of its contents.

How do I delete a folder in Terminal command line? ›

Select “Open in Terminal”.

You can use the command “rm -r” (short for “remove”) followed by the name of the directory to delete the whole folder and all of its contents.

How do I delete a file on my Mac? ›

Delete an item from your Mac or iCloud Drive

On your Mac, drag the item to the Trash in the Dock or select the item, then press Command-Delete. If an item is locked, click Continue to confirm you want to move the locked item to the Trash.

How do I delete a file in use Command Prompt? ›

In the command window, type the DEL /F file name command and press Enter to force delete the file that is in use. Note: In the above command, the file name must be replaced by the name of the file along with its extension that you want to delete. For example del /f TestFile. txt.

How do I delete a file using Terminal? ›

Use the delete command

The syntax for deleting a file is: del “filename"For instance, if you want to delete Math_file. txt, run the following command:C:\Users\Reyansh\Documents> del “Math_file. txt”If you receive a prompt to delete the file, type 'Y' and press 'Enter'.

How do I delete text in Terminal? ›

Erase unwanted characters: Use the Delete key or press Control-H, then type the new characters. Erase the line: Press Control-U. Erase from cursor position to the end of the line: Press Control-K.

How to delete files immediately in Mac? ›

Command + Delete is the fastest way to delete files. It sends the selected file to Trash. Option + Command + Delete. This shortcut deletes the file permanently and immediately, meaning that it is not sent to Trash but removed from Mac right away.

How do I Delete unwanted files on my MacBook? ›

To delete a file, select it, then click Delete; click Show in Finder to view the file in the Finder.

Which command can be used to delete a file? ›

The rm command is used to delete files. rm -i will ask before deleting each file. Some people will have rm aliased to do this automatically (type "alias" to check). Consider using rm -I instead, which will only ask once and only if you are trying to delete three or more files.

How do I delete files in a folder? ›

To delete a computer file or folder:
  1. Locate the file or folder by using Windows Explorer. ...
  2. In Windows Explorer, right-click the file or folder that you want to delete and then choose Delete. ...
  3. Click Yes to delete the file.
Jun 19, 2017

How do I force delete a corrupted file? ›

Boot your PC or Mac into Safe Mode, then delete the files normally. On Windows, you can also open Command Prompt as an administrator and use del /F /Q /A <filepath>. On a Mac, delete corrupted files in Terminal using the command sudo rm -R filename.

How do I clear Terminal data on Mac? ›

How to empty Terminal on Mac using keyboard shortcut. When you use Terminal on Mac, typing a command is one of the most common ways to clear your screen. Type Command + K and tap on your Mac's Enter key. If you have an older Mac, use ⌘ + K > Enter instead.

How do you end a file in Terminal Mac? ›

You have to press Ctrl + D + D . Hold the control down and press D twice. I have to say what a weird key sequence. In macOS Monterey, ctrl + D is all I need to fire an EOF.

How do I force Mac to delete an app in Terminal? ›

Go to Applications > Utilities and open Terminal. Enter the following command: sudo uninstall file:// Drag the app icon into the Terminal window — you will see the path to the app. Press Return and enter your admin credentials.

Top Articles
How Is My Buying Power Calculated? - PersonalFinanceLab
10 Basic Safety Rules for Freedivers
Global Foods Trading GmbH, Biebesheim a. Rhein
Washu Parking
Jonathon Kinchen Net Worth
Nfr Daysheet
oklahoma city for sale "new tulsa" - craigslist
Hertz Car Rental Partnership | Uber
7.2: Introduction to the Endocrine System
Riegler &amp; Partner Holding GmbH auf LinkedIn: Wie schätzen Sie die Entwicklung der Wohnraumschaffung und Bauwirtschaft…
Sinai Web Scheduler
The Many Faces of the Craigslist Killer
Anki Fsrs
Saw X | Rotten Tomatoes
Turning the System On or Off
Think Up Elar Level 5 Answer Key Pdf
Elizabethtown Mesothelioma Legal Question
Most McDonald's by Country 2024
Las 12 mejores subastas de carros en Los Ángeles, California - Gossip Vehiculos
Team C Lakewood
Evil Dead Rise Showtimes Near Pelican Cinemas
yuba-sutter apartments / housing for rent - craigslist
Filthy Rich Boys (Rich Boys Of Burberry Prep #1) - C.M. Stunich [PDF] | Online Book Share
Anotherdeadfairy
Costco Gas Hours St Cloud Mn
Chime Ssi Payment 2023
At 25 Years, Understanding The Longevity Of Craigslist
Impact-Messung für bessere Ergebnisse « impact investing magazin
4Oxfun
Mcclendon's Near Me
Restored Republic
Kaiser Infozone
Metro By T Mobile Sign In
Royal Caribbean Luggage Tags Pending
Tamilyogi Ponniyin Selvan
Junee Warehouse | Imamother
CVS Near Me | Somersworth, NH
Cl Bellingham
Pinellas Fire Active Calls
Emerge Ortho Kronos
Philadelphia Inquirer Obituaries This Week
Vision Source: Premier Network of Independent Optometrists
Infinite Campus Farmingdale
Traumasoft Butler
Dwc Qme Database
Silicone Spray Advance Auto
Reli Stocktwits
Europa Universalis 4: Army Composition Guide
Unblocked Games - Gun Mayhem
Willkommen an der Uni Würzburg | WueStart
Benjamin Franklin - Printer, Junto, Experiments on Electricity
sin city jili
Latest Posts
Article information

Author: Jonah Leffler

Last Updated:

Views: 5386

Rating: 4.4 / 5 (45 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Jonah Leffler

Birthday: 1997-10-27

Address: 8987 Kieth Ports, Luettgenland, CT 54657-9808

Phone: +2611128251586

Job: Mining Supervisor

Hobby: Worldbuilding, Electronics, Amateur radio, Skiing, Cycling, Jogging, Taxidermy

Introduction: My name is Jonah Leffler, I am a determined, faithful, outstanding, inexpensive, cheerful, determined, smiling person who loves writing and wants to share my knowledge and understanding with you.