Delete file with linux terminal

How to Delete and Remove Files on Ubuntu Linux Terminal

Delete and remove files on ubuntu linux using terminal. In this tutorial, you will learn how to delete and remove a file on Ubuntu Linux based system using terminal or command prompt.

This tutorial will use the rm command. It tries to remove the files specified on the command line. Use the rm command to delete files and directories on Ubuntu Linux. This tutorial will guide you on how to delete and remove files on Ubuntu Linux with a terminal using rm command.

  • -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.

This tutorial will explain all the options for the rm command one by one below.

Commands to delete and remove files on Ubuntu Linux

  1. Open the Ubuntu terminal
  2. Type any one of the following command to delete a file named hello.txt in the current directory
  3. rm hello.txt
    OR
    unlink 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!

Command to delete multiple files on Ubuntu Linux

Use the following command to delete the file named hello.txt, my.txt, and abc.jpg placed in the current directory:

You can specify path too. If a file named hello.txt placed in /tmp/ directory, you can run:

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

To delete a file and prompt before every removal in Ubuntu Linux

To get confirmation before attempting to remove each file pass the -i option to the rm command on Ubuntu Linux:

rm -i fileNameHere rm -i hello.txt

Force rm command on Ubuntu Linux to explain what is being done with file

Pass the -v option as follows to get verbose output on Ubuntu Linux box:

rm -v fileNameHere rm -v cake-day.jpg

To delete all files in folder or directory in Ubuntu Linux

Use the following command with following options to delete all files in folder or directory in Ubuntu Linux:

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

The above given commands will remove all files and subdirectories from a directory. So be careful. Always keep backups of all important data on Ubuntu Linux.

Читайте также:  Launch terminal in linux

Ubuntu Linux delete file begins with a dash or hyphen

If the name of a file or directory or folder starts with a dash ( — or hyphen — ), use the following syntax:

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

Do not run ‘rm -rf /‘ command as an administrator/root or normal Ubuntu Linux user

rm -rf (variously, rm -rf /, rm -rf *, and others) is frequently used in jokes and anecdotes about Ubuntu Linux disasters. The rm -rf / variant of the command, if run by an administrator, would cause the contents of every writable mounted filesystem on the computer to be deleted. Do not try these commands on Ubuntu Linux:

Conclusion

Delete and remove files on ubuntu linux using terminal. In this tutorial, you have learned how to delete and remove a file on Ubuntu Linux based system using terminal or command prompt.

Источник

Introduction

This page describes how to delete files through terminal.

IconsPage/important.png

It is possible, though difficult, to recover files deleted through rm. See DataRecovery. If you want to permanently delete a file use shred.

Commands for deleting files

The terminal command for deleting file(s) is rm. The general format of this command is rm [-f|i|I|q|R|r|v] file.

rm removes a file if you specify a correct path for it and if you don’t, then it displays an error message and move on to the next file. Sometimes you may not have the write permissions for a file, in that case it asks you for confirmation. Type yes if you want to delete it.

Options

  1. -f — deletes read-only files immediately without any confirmation.If both -f and -i are used then the one which appears last in the terminal is used by rm.
  2. -i — prompts for confirmation before deleting every file beforing entering a sub-directory if used with -R or -r. If both -f and -i are used then the one which appears last in the terminal is used by rm.
  3. -q — suppresses all the warning messages however error messages are still displayed. However the exit status is modified in case of any errors.
  4. -R — means delete recursively and is used to delete the directory tree starting at the directory specified i.e. it deletes the specified directory along with its sub-directory and files.
  5. -r — same as -R.
  6. -v — displays the file names on the output as they are being processed.
  7. -I — prompts everytime when an attempt is made to delete for than 3 files at a time or while removing recursively.

Precautions

IconsPage/stop.png

These precautions are to help you avoid dangerous commands. You should not execute them!

  1. Never type sudo rm -R / or sudo rm -r / as it deletes all the data in the root directory and will delete the data of all the mounted volumes until you want to wipe of everything from your system.
  2. sudo rm -f /* also does blunders with your system.
Читайте также:  Офисный пакет в линукс

See Also

DeletingFiles (последним исправлял пользователь ckimes 2017-09-03 16:40:24)

The material on this wiki is available under a free license, see Copyright / License for details
You can contribute to this wiki, see Wiki Guide for details

Источник

How to Delete a File or Directory in Linux – Command to Remove a Folder and its Contents

Shittu Olumide

Shittu Olumide

How to Delete a File or Directory in Linux – Command to Remove a Folder and its Contents

In Linux, deleting files or directories is a fundamental operation that every user must know. Although it may seem like a straightforward task, there are different methods to delete files or directories, each with its specific use case.

This tutorial will provide a step-by-step guide on how to delete files or directories in Linux. We will also walk through the commands you can use to remove files and folders along with their content.

How to Delete a File in Linux

Deleting a file involves removing the reference to the file from the file system. The file itself is not immediately removed from the storage device, but its space is marked as available for reuse.

There are several ways to delete a file in Linux. Here are some of the most common methods:

Using the GUI file manager

Most Linux distributions come with a GUI file manager that allows you to delete files using a graphical interface. Simply navigate to the file you want to delete, right-click it, and select «Delete» or «Move to Trash.»

Using the rm command

You can also use the rm (remove) command to delete files and directories in Linux. To delete a file using the rm command, type the following command in the terminal:

Make sure you replace filename with the name of the file you want to delete. If the file is write-protected or you don’t have sufficient permissions to delete it, you will be prompted to confirm the deletion.

Using the shred command

The shred command is a more secure way to delete files by overwriting the file’s contents multiple times before deleting it. This makes it difficult for anyone to recover the deleted file.

To use the shred command, type the following command in the terminal:

Make sure to replace filename with the name of the file you want to delete. The -u option tells shred to delete the file after overwriting it.

Using the trash-cli command

The trash-cli command provides a safer way to delete files by moving them to the trash instead of immediately deleting them. To be able to use the trash-cli command, you install it first:

sudo apt-get install trash-cli 

After installation, you can delete a file using the following command:

How to Delete a Directory in Linux

To delete a directory in Linux, you can use the rmdir or rm command. You use the rmdir command to remove an empty directory, while the rm command removes a directory and all its contents.

Using the rm command

Here are the steps to delete a directory in Linux using the rm command:

  1. Open the terminal: To delete a directory in Linux, you need to use the command line. Open the terminal by pressing «Ctrl+Alt+T» on your keyboard or by searching for «terminal» in your system’s application launcher.
  2. Navigate to the directory you want to delete: Use the cd command to navigate to the directory you want to delete. For example, if the directory you want to delete is called my_directory and is located in your home folder, type cd ~/my_directory and press «Enter».
  3. Check the contents of the directory: Before deleting the directory, it is a good idea to check its contents to make sure you are deleting the right directory. Use the ls command to list the contents of the directory. For example, type ls and press «Enter» to see the files and folders inside the my_directory folder.
  4. Delete the directory and its contents: To delete the directory and all its contents, use the rm command with the -r option, which stands for recursive. Type rm -r my_directory and press «Enter». You will be prompted to confirm the deletion. Type y and press «Enter» to confirm.
  5. Verify that the directory has been deleted: To verify that the directory has been deleted, use the ls command to list the contents of the parent directory. For example, if the my_directory folder was located in your home folder, type ls ~/ and press «Enter». The my_directory folder should no longer be listed.
Читайте также:  List all linux os version

Note: Be very careful when using the rm -r command, as it can delete files and directories irreversibly.

Using the rmdir command

Here are the steps to delete a directory in Linux using the rmdir command:

  1. Open the terminal: Open the terminal by pressing «Ctrl+Alt+T» on your keyboard or by searching for «terminal» in your system’s application launcher.
  2. Navigate to the directory you want to delete: Use the cd command to navigate to the directory you want to delete. For example, if the directory you want to delete is called my_directory and is located in your home folder, type cd ~/my_directory and press «Enter».
  3. Delete the directory: To delete the directory, use the rmdir command followed by the name of the directory. Type rmdir my_directory and press «Enter». If the directory is not empty, you will receive an error message and the directory will not be deleted.
  4. Verify that the directory has been deleted: To verify that the directory has been deleted, use the ls command to list the contents of the parent directory. For example, if the my_directory folder was located in your home folder, type ls ~/ and press «Enter». The my_directory folder should no longer be listed.

Conclusion

The rm command is the most commonly used command for deleting files, while the rmdir and rm commands with the -r or -R options are used for deleting directories. By following this step-by-step guide, you can now effectively delete files or directories in Linux.

  1. Be careful when using the rm command with the -r or -R option as it can delete files and directories irreversibly.
  2. Always double-check the file or directory name before deleting to avoid accidentally deleting the wrong file or directory.
  3. Use the shred command only when necessary, as it can take longer to delete files than other methods.
  4. Be mindful of file permissions when deleting files or directories, as some files or directories may require root access to delete.

Let’s connect on Twitter and on LinkedIn. You can also subscribe to my YouTube channel.

Источник

Оцените статью
Adblock
detector