How to delete file in linux terminal
How to delete file in linux terminal
How can I delete files in terminal fluently?
For example, I do
how do I delete, say, files qweerkrtrkgljdjfkdjfdkf.mp3 and blabla.mp3 with least effort?
UPD: Key idea is that file names can be long so I actually dont want to type them.
7 Answers 7
Try this: rm qweer*.mp3 bla*.mp3
Caution: If there is a file name which is started with these letters,this command will detete that.
CAUTION: This will cause permanent deletion!
1234.MP3 1345.MP3 1234.MP4
* is anything even blank that is why it still deletes the second one.
What I would do is put the files you didn’t want to delete in a folder before running these commands if there were less of them than you wanted to get rid of. But ultimately a combination of Wildcards («*» and «?») and defining statements would get the job done easily. Just depends on what files you have that you want to keep, or get rid of.
Ever tried dragging the files to delete into terminal app window? It may clone the directory as well as the file-name path style for you. However, if you are only deleting 2 or 3 files I hope it is simply for learning purposes otherwise I would type the paths and file-name in manually using the «TAB» on my keyboard it should auto-complete file-names given in the current directory that start with what you already typed.
Tab completion is the way. It’s enabled for bash in Ubuntu by default. It works like this:
will automatically be changed to
If the file name is ambiguous, you need to press tab once more to see a list of possible file names:
If you’re interested in an even more sophisticated tab completion, check out zsh with the Grml config, it’s mind-blowing.
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.
This tutorial will explain all the options for the rm command one by one below.
Commands to delete and remove files on Ubuntu Linux
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:
To delete a file and prompt before every removal in Ubuntu Linux
Force rm command on Ubuntu Linux to explain what is being done with file
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:
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.
Ubuntu Linux delete file begins with a dash or hyphen
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.
Delete a File Linux – Secrets that you didn’t know about it [Complete Guide]
Delete a File Linux: This article will explain how to delete files in terminal using the rm, unlink, and rmdir commands to delete files and directories in Linux. Those commands are critical and you should use them carefully.
On Linux, the rmdir and rm functionality are to delete directories and files, like any Unix-based operating system, including macOS.
They’re comparable to the commands del and deltree commands in Windows and DOS. These commands are mighty and have quite a few parameters.
It is essential to note that files and directories deleted using rm and rmdir are not transferred to the Trash. They are instantly excluded from your computer. If you unintentionally delete files applying these commands, the only method you’ll be capable of restoring them is from a backup.
However, you will see that we could create our own solution, to create trash and send the deleted files to there.
Besides learning how to use the commands rm, rmdir, and unlink, you will learn:
What is rm command?
The rm means “remove,” so this command allows us to delete a file Linux. Also, this command is available on any Unix-based system, including the Mac OS.
How to delete a file in Linux?
First, let’s see how to remove files with rm:
The most straightforward example is deleting a single file in the current directory. Let me show you an example:
But, If the file is not in the current working directory, you can type the full path from that file, for example:
Deleting multiple files in Linux
We can pass more than one file to the rm command to delete all files at once. Let’s see how easy it is:
Delete files with specific extension Linux
But, sometimes, we have a list with several files, and we don’t want to specify all of them on the command. So, instead, we can use Wildcards to filter a collection of files that we want to delete.
For example, we can use the filename to have a clear pattern on their names or extensions. Let’ see a sample?
Easy, right? You also can do it:
In some scenarios, a file is write-protected; in these cases, you have to answer a prompt before deleting the file. You must reply with y or n and hit “Enter.”
So, How delete file in Linux with prompt?
How to remove files in Linux without prompt
How to force delete a file on Linux?
If you’re afraid of removing files, you should substitute your rm command with a different one that asks you for confirmation before permanently removing files. You can use an alias for this goal:
Create alias for rm command
Add this command above on your
/.bashrc, so you will not lose the alias when you restart your computer. And, every time that you execute rm, the prompt for confirmation will be shown for you.
Delete a File Linux: How to restrict rm command in Linux
Also, there is an excellent tool to prevent accidental deletion of important files: safe-rm
Safe-rm is a security tool designed to interrupt the unintentional deletion of critical files by replacing /bin/rm with a wrapper version, which examines the presented arguments against a configurable blocklist of files and directories that you should never remove.
Users who try to delete one of these protected files or directories from the block list will not be able to delete so and will be displayed a notification message alternately.
The rm command also provides the options –one-file-system, –no-preserve-root, and –preserve-root, but those are only suggested for advanced users. If you get something wrong, you could unintentionally delete all your system files. Look at the command’s manual page for further information.
How to install tree command in Linux?
You can use apt-get to install this package onto your system using Ubuntu or different Debian-based distribution. On other Linux distributions, utilize your Linux distribution’s package command tool instead.
Running the tree command gives a simple picture of the directory structure and files under the directory from which it is run.
You can also provide a path to the tree command to start the tree from different directories in the file system.
Delete only files in a directory on Linux
For example, let’s suppose that you have a folder with several files and subdirectories. And you would like to delete only files from the first level.
Although this won’t prompt you for confirmation or output what it just deleted, consequently, most experts run it without the -delete action first to make sure that the files are correct.
However, if you would like to delete all content from the directory and not the directory itself:
This command above will delete all files and subdirectories inside of the /path/to/directory.
Delete files using find command
Remove multiple files Linux using a native command like find, requires also apply the grep and xargs, but it’s very straightforward, let show you how easy it is:
See this example, delete files with find:
In my opinion, xargs and find command on Linux are the most helpful command ever! You can resolve any complex problem very efficiently.
If you would like to learn more about how delete files with find, check our post: find command with examples.
Also, I recommend reading our Complete Guide about xargs with examples. And discover everything that you can do with Xargs.
How to remove a directory in Linux
We will learn two commands that allow us to remove directories: the rm and rmdir.
How to Remove Directories with rm
You will see that deleting directories that are empty and not empty could be handled differently.
To delete an empty directory, apply the -d (means directory) option. Also, you can utilize wildcards (* and ?) in directory names precisely as you can with files.
For example, if you would like to delete your Download directory (it should be empty).
Like files, providing more than one directory name deletes all of the specified empty directories.
See this example:
Another example is to delete directories that are NOT empty and use the -r (means recursive) option. So, this deletes the directories, and all files and sub-directories included within them.
How to delete the download directory?
But, the command above deletes the Downloads directory itself. So, to remove only files and sub-directories from that folder, as we saw before:
Tip:
Use the temporary folder /tmp to store the download files that you are sure you will not need later. Because, when you restart your computer, all files and directories inside the /tmp will be deleted automatically. So, you don’t need to worry about cleaning up files that you use at once. Nowadays, with high-speed internet, it doesn’t make sense to store every file on our computer.
Also, you can configure your web browser to use the /tmp as the default Download folder, so you don’t need to select the folder for each download.
How to Remove Directories with rmdir
Delete a File Linux: What is rmdir in Linux?
The rmdir command allows you to delete directories too.
You may be asking yourself, why a second command to delete directories?
The rmdir command can only delete empty directories. So, it means that It will never delete files. It could be more secure in some scenarios.
The most simplistic case is deleting a single empty directory. As with rm, you can provide various directories names to the command rmdir or the full path to a directory.
Delete just one directory in the current directory by giving its name to rmdir :
Delete various directories by giving a list of names to rmdir :
If you need to delete a directory not in the current directory by defining the full path to that directory:
if you got an error message like this one:
Because you try to delete a not empty folder, rmdir will present you with this error message. In the following sample, rmdir successfully and quietly deletes the directory but declines to delete the projects directory because it contains files. Thus, the project’s directory is left exactly as it was, and its files are untouched.
When rmdir shows a “Directory not empty” error, it stops processing the directories that were passed to it on the command line. So, for example, if you’ve asked it to delete two folders and the first one had files in it, rmdir would show you the error message and do nothing more. You can apply it to overlook these errors with the –ignore-fail-on-non-empty option to process other directories.
Le’s see in the following example, two folders have been given to rmdir. These are directory-A and directory-B. The –ignore-fail-on-non-empty option has been introduced in the command. The directory-A folder has files in it, so rmdir cannot delete it. The –ignore-fail-on-non-empty option forces rmdir to overlook the error and move on to the following folder it needs to process: directory-B, which is an empty folder, and rmdir deletes it.
The command that covers this scenario:
To make your life easier, you can create an alias on your
/.bashrc that automatically adds the –ignore-fail-on-non-empty, so you don’t need to type it every time. For example:
You can apply the -p (parents) option to delete a directory and delete its parent directories. This method works because rmdir begins with the target directory and then back-steps to the parent. Therefore, that directory should now be empty so that it can be deleted by rmdir, and the process reoccurs, stepping back up the path that was given to the command rmdir.
Let me show you the command that is given to rmdir is:
Both the bitslovers and the posts directories will be deleted, as requested.
Linux provides manageable and robust commands for you to delete directories and files right from the terminal command line whether you’re using bash or any other shell. Some people prefer to work with terminal. Others may have no alternative in the matter. They may be running on servers without a GUI placed or on a remote session onto a headless system, for example, a Raspberry Pi device. These commands are excellent for that group of people.
Delete a File Linux: When should you use the rmdir?
When you get started on Linux, I recommend using rmdir for the scenarios where you don’t know if you have files in the directory you want to delete. For example, if you believe that the directory is empty, always use rmdir. Otherwise, if the directory has files and you forget about them, you will be protected against the mistake of deleting files that you don’t want.
Caution! These samples will delete all files on your computer if execute it.
Unlink command in Linux
The unlink command doesn’t get as much awareness as other GNU Core Utilities, like the rm command. However, it has a straightforward function to remove a file or symbolic link. Let’s see some of the differences between unlink and the rm command.
Unlink vs. rm command
The unlink command contrasts in many ways from the rm command. Behind the scenes, it uses the related unlink() system call. However, there are practical distinctions that users will notice.
Unlink command in Linux with examples
The unlink command goal is to remove a single file only, is not allowed to passing more than one file name like the rm command. It has no extra arguments options, only the –help and –version. The syntax is simplistic, use the command and pass a single filename, for example:
If we use a wildcard to unlink, you will get an extra operand error, and the reason for that is because the wildcard denotes more than one file.
Let’s one example:
You will see an error message like this one:
Try ‘unlink –help’ for more information.
Delete a File Linux: How to undo rm in Linux
I have bad and good news for you: it’s not possible to undo the rm command. However, let me explain the bad one first and why. Later I will present a solution for it:
Where do files go when the rm command is executed?
Nowhere, gone, disappeared. Well, more accurately, the file becomes unlinked. The files are still sitting there on the disk, but the link to it is removed. So it’s possible to recover the files, but the metadata is lost now, and nothing’s recoverable. That’s is the reason that we can’t undo the rm operations. Some software tries to recover files but not necessarily will, and it’s not a straightforward process. It demands time.
There is no Trash can when you execute the rm command, nor should there be. If you necessitate a Trash can, you should use an interface. There is a command utility in trash-cli for Ubuntu, but most of the time, the GUI file browsers like Nautilus or Dolphin already provide a standard Trash can. So, for example, files deleted in Dolphin will be visible in the Trash from Nautilus.
Deleted files are usually located somewhere like
/.local/share/Trash/files/ when trashed. The rm command on UNIX/Linux is equivalent to del on DOS/Windows, which also deletes and does not move files to the Recycle Bin. Also, I would like to highlight that moving a file across filesystems like a USB disk from your hard disk drive involves two operations:
First, a new copy of the file is done. And, second, unlinking the original file from your hard disk. You wouldn’t desire your Trash to have with these extra copies.
Now let’s see the good news?
It’s incredible how Linux provides us the ability to customize our system and our daily tasks. When you face a limitation on the usability of Linux, you need to stop for seconds and realize that maybe it is not a limitation.
Sometimes we do not note that some limitations were designed to provide a generic structure on the system that allows us to build our solution and highly customization. For example, the fact that we don’t have Trash to move your files when you execute the rm command doesn’t mean you have accepted that reality. However, we can build one solution that can handle that!
Great, right? But, how? Let me show you.
One of the first ideas that come to my mind was creating an alias that can override the rm command by moving instead of deleting the files right the way. However, the alias command doesn’t allow us to provide arguments.
So, how to resolve that? Let’s see:
Delete a File Linux: How to prevent a mistake with the rm command?
The most simplistic way is to create a function called “rm” that will override the original rm command on your
/.bashrc. Let’s how to do it:
With this new rm above, you will be able to recover files in Linux after you have executed rm.
Easy, right? Add those lines above at the end of your file (
Note: You will need to type bash on your terminal or open a new terminal to make the changes available to use.
Moving the files to /tmp, it’s the best approach, in my opinion, because all files will be deleted when you shut down your computer. So, you still have time to recover your files if you deleted any files by mistake and don’t need to clean up the files, and this approach will automatically release space when you turn on your computer again. However, you can specify any folder you want. Remember that you need to clean up the folder manually or create a cron job (a utility on Linux to automate tasks according to a schedule you define) that automatically deletes that folder or files for you.
And if you need to clean up the oldest files that you didn’t access within 30 days or more:
You can execute the command above to remove the old files from your trash folder automatically.
Delete a File Linux: Securely remove files in Linux
We have learned that when you delete files using the command line, the operating system unlinks the files from our disk. So they got lost, but behind the scenes, the files were still physically on the disk. Also, it’s possible to use appropriate software to recover them.
For example, this could be a security problem if you decide to sell your computer to another person.
How can we delete the files that could be impossible to recover?
Disk wipe for Linux
A disk wipe may be the best solution if we need to overwrite and delete every individual byte from a computer’s hard disk. The term disk wipe relates to the action of cleaning a computer’s hard disk. The term also points to a class of computer software products created to achieve this task more quickly and efficiently, with some variants of disk wipe software.
The process is not only easier than reformatting a hard drive; it is also considered by many to be more secure. But, you may be asking yourself, why is it more secure?
Because it guarantees that all data, including licensed software and files that may hold passwords or other personal information, is overwritten.
Now, let’s see an example of software that we can use for free that achieve that goal.
Shred for Linux
The shred command allows us to overwrite the specified files repeatedly and make it hard for software to recover the data. The syntax is as follows:
The shred command also overwrites data in a file or a whole device with random bits, making it nearly unachievable to recover. Finally, this command permanently deletes the data. Also, the command has some options which you can use to wipe a disk as:
So, to wipe the partition, you can apply.
Change the /dev/sda1 for your desire disk.
How to install Shred on Ubuntu
The shred it should already be installed on your computer, but if you need to install it, follow the command:
Conclusion
Delete files and directories are the firsts actions that maybe we do when we get started with Linux, and it’s considered an easy task. However, you have noticed that it could get complicated on specifics scenarios that require attention from us.
So, rm and rmdir delete directory Linux command.
We also learned about the unlink command. I never used that command to delete files daily, but it is good to be aware of all our alternatives. In addition, there are specific scenarios where the unlink could be better and useful for scripts and automation tasks. For example, you could use it to provide more security for the automatic tasks done by Shell Scripts to delete and limit the number of files safely.
Whatever command you like to delete files and directories, these commands lend themselves very well to being added in shell scripts. For example, a cron job triggers a script that can help automate routine housekeeping jobs, such as cleaning undesired log files. Only remember of the potential of these commands, test everything delicately, and always keep an up-to-date backup.
If you already have some experience and acknowledged with Linux, before. I hope that you have learned something new.
Please leave a comment below, and give us feedback. It’s crucial for us.
Also, take some minutes and follow us on social media.
How to Delete Files and Directories in Linux?
Linux comes with several tools that can assist us in removing files. We always need to delete many files and folders based on a set of requirements. To complete our mission quickly, knowing a few basic commands and their variations is beneficial.
1. Remove file by using “unlink”:
Not so well-liked. We may use the unlink command to permanently delete a single file.
2. Delete a single file:
The rm command, which facilitates deleting one or more files simultaneously, is a more widely used command for removing files.
If the file is write-protected, rm will ask you to validate its deletion; otherwise, it will delete it without prompting. Using the “-i” flag to force rm to prompt for confirmation before removing a file:
The rm command deletes files without showing any messages. Using the rm command with the -v flag to see what the rm command is currently doing.
Using the -f flag to remove write-protected files without asking for clarification.
3. Multiple files can be deleted:
Bypassing multiple filenames as arguments to rm, you can delete multiple files.
Regular expressions are also supported by rm. If you want to delete all files with the name file-name-*, type:
Regular expressions may also be used to define different directories. We can use something like to delete three files that fit file-name-1, file-name-2, and file-name-3.
4. Delete the archive:
The rm command with the -d flag can be used to remove an empty directory.
Supported options for file deletion can also be combined with deleting the directory with the -d flag.
If you do not want a prompt before deleting the directory and its contents, use the -rf flag. This will remove everything inside the directory, including the directory itself, without any confirmation. Use caution especially when using as a root.
5. Locate and delete files:
We can use the locate command with various choices for more complicated specifications. To delete all files in a path specified by that follow a pattern .
Example:
We may slightly change the above command to delete everything that fits the sequence
Internally, modern implementations of the find command support the delete feature. The -delete flag is used to override the rm instruction, while the –depth flag tells find to process the contents of the directory before the directory itself:
6. Empty files should be found and deleted:
You may use the following command to remove all empty directories within a given path dir-to-search:
Instead, use the following command to remove all empty files within a given path dir-to-search:
7. Permissions are used to locate and delete files:
We can now remove files based on special permissions, such as:
Consider the following scenario:
Easy (unlink), (rm), and (rmdir) commands are available in Linux, and they can be quickly expanded with regular expressions. For more specialized needs, you should use a variety of techniques such as (find) to accomplish your goals. Aside from the examples in this post, you can configure your quest by using find with any of the available flags.
Often run find commands without the rm or -delete flags and examine the output to determine which files or folders may be affected by the execution of a program. Backup setup and procedure are beneficial not just in the event of unintentional deletions, but also in the event of hardware errors and cyber-attacks.
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
3. Remove multiple files
To remove multiple files at once, you can provide all the filenames.
4. Remove files interactively
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 in the terminal. Let’s see how to remove directories 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 to delete folders as well. Remembering the rm command is a lot more useful than rmdir which in my opinion is not worth the trouble.
1. Remove an empty directory
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.
Awesome! So now you know how to remove directory in Linux terminal.
Summary
Here’s a summary of the rm command and its usage for a quick reference.
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.