Question 3.10: You want to delete a file or directory using the Terminal....

You want to delete a file or directory using the Terminal.

Step-by-Step
The 'Blue Check Mark' means that this solution was answered by an expert.
Learn more on how do we answer questions.

The rm (remove) command will delete a file or directory and its contents. It should be used with extreme caution.

Discussion

Deleting a single file is simple and safe. The following example will delete the file my_file.txt from the home directory:

$ cd ~
$ rm my_file.txt
$ ls

You need to have write permission in the directory within which you are trying to carry out the deletion.

You can also use the * wildcard when deleting files. This example will delete all the files starting with my_file. in the current directory:

$ rm my_file.*

You could also delete all the files in the directory by typing:

$ rm *

If you want to recursively delete a directory and all its contents, including any directories that it contains, you can use the -r option:

$ rm -r mydir

When deleting files from a Terminal window, remember that you do not have the safety net of a recycle bin from which files can be undeleted. Also, generally speaking, you won’t be given the option to confirm, the files will just be immediately deleted. This can be totally devastating if you combine it with the command sudo (Recipe 3.11).

See Also

See also Recipe 3.3

If you are concerned about accidentally deleting files or folders, you can force the rm command to confirm by setting up a command alias (Recipe 3.32).

Related Answered Questions

Question: 3.34

Verified Answer:

Use the Linux df command: $ df -h Filesystem   ...
Question: 3.33

Verified Answer:

Use the Linux date command. The date and time form...
Question: 3.32

Verified Answer:

Edit the file ~/.bashrc using nano (Recipe 3.6), a...
Question: 3.31

Verified Answer:

Run the program or command in the background using...
Question: 3.30

Verified Answer:

Redirect the output to /dev/null using >. For e...
Question: 3.24

Verified Answer:

Use the Task Manager utility, which you’ll find on...
Question: 3.29

Verified Answer:

Use the pipe command, which is the bar symbol (|) ...
Question: 3.27

Verified Answer:

Use the > command to redirect output that would...
Question: 3.28

Verified Answer:

Use the cat command to concatenate a number of fil...
Question: 3.26

Verified Answer:

Use the lsusb command. This will list all the devi...