- How To Delete File In Linux?
- rm Command Syntax
- rm Command Help
- Delete Single File with rm Command
- Delete Multiple Files with rm Command
- Delete Files According To Their Extensions/Types with rm Command
- Delete Files Recursively
- Delete File with Prompt Before Every Removal
- Print Verbose Output About Delete Operation
- Delete empty Directories or Folders with rmdir Command
- Read File Names From Text File For Delete or Removal
- Delete File Names Starts with Dash —
- Delete Files By Reading Their Names From A File/List
- Delete Files By Finding them with find Command
- How to Delete Files and Folders in Linux
- The rmdir Command
- Remove Folders in Linux Using the rmdir Command
- The rm Command
- Remove Files in Linux Using the rm Command
- Wrap Up
- How to Remove Files and Directories in Linux Command Line [Beginner’s Tutorial]
- How to delete files in Linux
- 1. Delete a single file
- 2. Force delete a file
- 3. Remove multiple files
- 4. Remove files interactively
- How to remove directories in Linux
- 1. Remove an empty directory
- 2. Remove directory with content
- 3. Force remove a directory and its content
- 4. Remove multiple directories
- Summary
- How to Remove (Delete) a File or Directory in Linux
- How To Remove or Delete Linux Files
- rm Command Options
- How to Delete a Directory in Linux
- Remove Directory Linux with rm Command
- Remove Directories in Linux with rmdir Command
How To Delete File In Linux?
Deleting files in Linux can be sometimes tricky. We have a tool named rm which is the shortcut for the word remove. In this tutorial, we will look at how to remove or delete a file in Linux with different examples and ways.
rm Command Syntax
rm command syntax is the same as the most the Linux command. We can provide options before specifying the file and directories we cant to operate or delete.
- OPTINS states the behavior of the rm command as we will see below in detail.
- FILENAME is the file or directory name we want to delete or operate.
rm Command Help
rm command help information can be printed to the console with the —help command which is like below. Help information will provide some popular options and usages.
Delete Single File with rm Command
We will start with simple steps just deleting a single file. We will just specify the file name we want to delete. In order to remove the file successfully, we should have privileges to modify a file. For example, if we try to remove the file owned by root with a regular user we will get an error and would not delete the file. In this example, we will delete the file named foo.txt
Delete Multiple Files with rm Command
We have the ability to delete multiple files in a single rm command. We will just put file names we want to delete by separating them with space. In this example, we will delete file names foo.txt and bar.txt but we can add more if we need it.
Delete Files According To Their Extensions/Types with rm Command
Linux bahs provides the glob or asterisk in order to specify the files with a specific name or extension. We can use glob * in order to specify a specific extension like *.txt , *.pdf , *.tmp etc. We can use this extension or name specification in order to delete specific files. In this example, we will delete all *.deb extensions.
We can also specify names like deleting all files which name starts with pof like below.
Delete Files Recursively
rm command provides the ability to delete or remove files recursively. Recursive removal will check subdirectories for files to remove with the directories. We will remove the directory name ndiff with all sub-directories and files in this example. We will use -R option for the recursive operation.
Delete File with Prompt Before Every Removal
While removing files and directories we may need approval for each file to delete. In this case, we can use -i option which will prompt to accept or deny deletion of the given file.
Print Verbose Output About Delete Operation
While deleting files and directories we may want to see details of the removal operation. rm command provides a verbose option which will list information about each deletion of file or directory. We will use -v option for this.
Delete empty Directories or Folders with rmdir Command
In some cases, we need to delete empty folders. rm without options will not work in this case as we can see this in the following screenshot. We case use rmdir command to remove an empty directory or folder.
Read File Names From Text File For Delete or Removal
Another interesting use case for rm command is providing file or directory names from a list like a text file. We will use xargs command to-read the list and redirect to the rm command.
Delete File Names Starts with Dash —
Another interesting case is dash or — problem where file or directory names starting with dash . As we know Linux commands options are specified with dash . So how can rm recognize file name from option? We will use — or double dash were to specify the file or directory name start. For example we have a file named -file.txt and we want to remove. We will use the following command. As we can see the file name is specified after double dash. Options are specified before the double dash.
Delete Files By Reading Their Names From A File/List
In some cases, we may need to read a list that contains the file names we want to delete. This is generally a simple text file where each file name with their path is specified line by line. We can use xargs command to redirect the list contents to the rm command which will delete them one by one. In this example, we will read the list file names file-list.txt .
Delete Files By Finding them with find Command
find is a very useful command which is used to find files and folders. find command also provides some options like running commands on the search results. We can also remove or delete files found by the find command. In this example, we will delete files that are older than 3 days.
Источник
How to Delete Files and Folders in Linux
Linux command line fundamentals are absolutely essential for every future system administrator and advanced Linux user. Today we’ll cover another basic function – deleting files and directories in Linux using the command line.
The rmdir Command
The command used to delete empty directories in Linux is rmdir.
The basic syntax of this command is easy to grasp. here’s an example:
- rmdir is the command
- [option] is an optional modifier that changes the way the command behaves
- DirectoryName is the directory you want removed
If no option is provided, rmdir simply deletes the directory whose name is provided as the destination. Before using this command, you’ll have to log into your VPS server using SSH. Here’s an article to help you out.
Remove Folders in Linux Using the rmdir Command
Before using the rmdir command we suggest you check the files present in a directory with the ls command. In our case, we have a directory named Dir1.
This command will delete the empty directory named Dir1. Simple enough, right?
You can also delete multiple directories by separating their names by spaces. For instance:
After executing this command, the directories named Dir1, Dir2 and Dir3 will be deleted.
Let say we have a directory named Dir3. Dir3 has subdirectories and files inside of it. Now if we use following command:
We will receive an error like this:
As you may have guessed from the output, rmdir only works with empty directories.
The rmdir is a smart utility, and only allows you to delete empty directories as a built-in safeguard to prevent accidental loss of data. Remember it’s almost impossible to recover deleted data on any Linux distribution.
The -p option lets you delete the directory as well as its parent directories.
This above command will delete Dir3 and its parent directories Dir2 and Dir1.
The -v option outputs a diagnostic text for every directory processed. Using this option will output a confirmation listing all the directories that were deleted.
The rm Command
The rmdir command is great for safely removing unused and empty directories. If you want to remove files or directories containing files, you would have to use the rm command.
The basic syntax of this command is similar to rmdir:
Remove Files in Linux Using the rm Command
Use the rm command to remove the file named article.txt:
If we have a directory with the name Dir1 containing subdirectories and files we will have to attach the -r modifier. The command would look like this:
The -r option recursively removes directories and their content.
Another helpful option is -i. This will ask you to confirm the files that will be deleted individually. That way you can avoid any unpleasant mistakes.
You can also delete empty directories using -d option. The following command will delete an empty directory with name Dir1:
You can use a wildcard (*) and a regular expansions to match multiple files. For instance, the following command will delete all pdf files placed in the current directory.
You can use variation of all the commands listed above to delete files with other extensions like .txt, .doc, .odt, etc.
The -f option lets you forcefully delete everything placed in a directory. The command would look like this:
The above command will delete everything recursively and forcefully, residing under the Dir1 directory without prompting anything on the terminal.
You can also delete more than one directory at a time. The following command will delete three directories Dir1, Dir2 and Dir3 in a single command.
Congratulations, you successfully mastered all the basic functions of the rm and rmdir commands!
Wrap Up
In Linux deleting a single file by accident can lead to major issues. That’s why it’s essential to master the two main commands for file and directory removal – rm and rmdir. In this article we cover these two commands and the various options that can be used with them.
We hope you found this article useful! Remember, once you delete a file or directory from Linux, you can’t recover it, so be extra careful! Good luck.
Edward is an expert communicator with years of experience in IT as a writer, marketer, and Linux enthusiast. IT is a core pillar of his life, personal and professional. Edward’s goal is to encourage millions to achieve an impactful online presence. He also really loves dogs, guitars, and everything related to space.
Источник
How to Remove Files and Directories in Linux Command Line [Beginner’s Tutorial]
How to delete a file in Linux? How to delete a directory in Linux? Let’s see how to do both of these tasks with one magical command called rm.
How to delete files in Linux
Let me show you various cases of removing files.
1. Delete a single file
If you want to remove a single file, simply use the rm command with the file name. You may need to add the path if the file is not in your current directory.
If the file is write protected i.e. you don’t have write permission to the file, you’ll be asked to confirm the deletion of the write-protected file.
You can type yes or y and press enter key to confirm the deletion. Read this article to know more about Linux file permissions.
2. Force delete a file
If you want to remove files without any prompts (like the one you saw above), you can use the force removal option -f.
3. Remove multiple files
To remove multiple files at once, you can provide all the filenames.
You can also use wildcard (*) and regex instead of providing all the files individually to the rm command. For example, if you want to remove all the files ending in .hpp in the current directory, you can use rm command in the following way:
4. Remove files interactively
Of course, removing all the matching files at once could be a risky business. This is why rm command has the interactive mode. You can use the interactive mode with the option -i.
It will ask for confirmation for each of the file. You can enter y to delete the file and n for skipping the deletion.
You just learned to delete files. Let’s see how to remove directory in Linux.
How to remove directories in Linux
There is a command called rmdir which is short for remove directory. However, this rmdir command can only be used for deleting empty directories.
If you try to delete a non-empty directory with rmdir, you’ll see an error message:
There is no rmdir force. You cannot force rmdir to delete non-empty directory.
This is why I am going to use the same rm command for deleting folders as well. Remembering rm command is a lot more useful than rmdir which in my opinion is not worth the trouble.
1. Remove an empty directory
To remove an empty directory, you can use the -d option. This is equivalent to the rmdir command and helps you ensure that the directory is empty before deleting it.
2. Remove directory with content
To remove directory with contents, you can use the recursive option with rm command.
This will delete all the contents of the directory including its sub-directories. If there are write-protected files and directories, you’ll be asked to confirm the deletion.
3. Force remove a directory and its content
If you want to avoid the confirmation prompt, you can force delete.
4. Remove multiple directories
You can also delete multiple directories at once with rm command.
Summary
Here’s a summary of the rm command and its usage for a quick reference.
Purpose | Command |
---|---|
Delete a single file | rm filename |
Delete multiple files | rm file1 file2 file3 |
Force remove files | rm -f file1 file2 file3 |
Remove files interactively | rm -i *.txt |
Remove an empty directory | rm -d dir |
Remove a directory with its contents | rm -r dir |
Remove multiple directories | rm -r dir1 dir 2 dir3 |
I hope you like this tutorial and learned to delete files and remove directories in Linux command line. If you have any questions or suggestions, please leave a comment below.
Источник
How to Remove (Delete) a File or Directory in Linux
Home » SysAdmin » How to Remove (Delete) a File or Directory in Linux
How do I delete a file in Linux using the command line option? How can I remove a Linux directory?
Deleting files and directories is a necessary task when working with Linux. In this guide, learn how to remove files and directories from the command line in Linux using the RM Command.
- A command line / terminal window (Ctrl-Alt-T or Ctrl-Alt-F2)
- A user account with sudo privileges (optional)
Note: If you feel that a directory is misplaced and you do not want to remove it, try moving it to a different place. To learn how, visit our post How to Move Directories in Linux.
How To Remove or Delete Linux Files
The rm command deletes files in a Linux. The command unlinks the data from the file name, allowing the user to overwrite on that particular storage space.
To delete a single file, entering the following in the command line:
The rm command can be used to delete more than one file at a time:
Wildcards can be used with this command.
For example, to delete all files with the .bmp filename, enter:
This method is also used to delete all files that contain a string of characters:
This will erase any file that has the word sample in the name.
The system will search the current directory for the file you want to remove.
To delete a file in a different directory, either switch to that directory first:
Or you can specify the file location in a single command directly:
Note: Once the rm command has deleted a file, you will not be able to access it. The only way to retrieve a file would be to restore it from a backup (if one is available).
rm Command Options
You can adjust the way the rm command works by adding options. An option is a hyphen, followed by one or more letters that stand for commands.
If you’re deleting multiple files, add a confirmation prompt. Use the –i option to use an interactive dialog:
Confirm the deletion of files by typing ‘yes’ or ‘no.’
To display the progress of the deletion with the v or verbose command:
The output confirms that the file test.txt has been successfully removed.
To force the removal of a file that’s write-protected, use the –f option:
To use sudo privileges for a file that says Access denied and delete it:
How to Delete a Directory in Linux
A linux directory (or folder) can be empty, or it can contain files. If To delete a directory in Linux, use one of the following two commands:
- rmdir command – removes empty directories/folders
- rm command – removes a directory/folder along with all the files and sub-directories in it
Remove Directory Linux with rm Command
By adding the -r (-R) option to the rm command, you can remove a directory along with all its contents.
To remove a directory (and everything inside of it) use the –r option as in the command:
This will prompt you for confirmation before deleting.
To remove a directory without confirmation:
Also, you can delete more than one directory or folder at a time:
Remove Directories in Linux with rmdir Command
Remember, the rmdir command is used only when deleting empty folders and directories in Linux. If a specified directory is not empty, the output displays an error.
The basic syntax used for removing empty Linux folders/directories is:
Additionally, you can delete multiple empty directories at once by typing:
If the command finds content in one of the listed directories, it will skip it and move on to the next one.
Note: To permanently delete a file in Linux by overwriting it, use the shred command.
With this tutorial, deleting files and directories in Linux was made easy. The rm and rmdir commands are flexible with many options available.
Источник