- 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
- Linux / Unix: Find And Remove Files With One Command On Fly
- Find And Remove Files With One Command On Fly
- Examples of find command
- Conclusion
- 5 Ways to Empty or Delete a Large File Content in Linux
- 1. Empty File Content by Redirecting to Null
- 2. Empty File Using ‘true’ Command Redirection
- 3. Empty File Using cat/cp/dd utilities with /dev/null
- 4. Empty File Using echo Command
- 5. Empty File Using truncate Command
- If You Appreciate What We Do Here On TecMint, You Should Consider:
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.
Источник
Linux / Unix: Find And Remove Files With One Command On Fly
However, the rm command does not support search criteria. For example, find all “*.bak” files and delete them. For such necessities, you need to use the find command to search for files in a directory and remove them on the fly. You can combine find and rm command together. This page explains how to find and remove files with one command on fly.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | find command |
Est. reading time | 4 minutes |
Find And Remove Files With One Command On Fly
The basic find command syntax is as follows:
find dir-name criteria action
Where,
- dir-name : – Defines the working directory such as look into /tmp/
- criteria : Use to select files such as “*.sh”
- action : The find action (what-to-do on file) such as delete the file.
You want to remove multiple files such as ‘*.jpg’ or ‘*.sh’ with one command find, try:
find . -name «FILE-TO-FIND» -exec rm -rf <> \;
OR
find /dir/to/search/ -type f -name «FILE-TO-FIND-Regex» -exec rm -f <> \;
The only difference between above two syntax is that the first command remove directories as well where second command only removes files. Where, options are as follows:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
- -name «FILE-TO-FIND» : File pattern.
- -exec rm -rf <> \; : Delete all files matched by file pattern.
- -type f : Only match files and do not include directory names.
- -type d : Only match dirs and do not include files names.
Modern version of find command has -delete option too. Instead of using the -exec rm -rf <> \; , use the -delete to delete all matched files. We can also explicitly pass the -depth option to the find to process each directory’s contents before the directory itself. It is also possible to use the -maxdepth option to control descend at most levels of directories below the starting-points. For example, -maxdepth 0 means only apply the tests and actions to the starting-points themselves. Similary, we can pass the -mindepth to the find. It means do not apply any tests or actions at levels less than levels (a non-negative integer). For exampe, -mindepth 1 means process all files except the starting-points. So here is a simplied syntax:
find /dir/to/search/ -type f -name «FILES-TO-FIND» -delete
find /dir/to/search/ -type f -name «FILES-TO-FIND» -depth -delete
find /dir/to/search/ -maxdepth 2 -type f -name «FILES-TO-FIND» -depth -delete
Examples of find command
Find all files having .bak (*.bak) extension in the current directory and remove them:
find . -type f -name «*.bak» -exec rm -f <> \;
OR
find . -type f -name «*.bak» -delete
Find all core files in the / (root) directory and remove them (be careful with this command):
# find / -name core -exec rm -f <> \;
### OR ###
# find / -name core -delete
Find all *.bak files in the current directory and removes them with confirmation from user:
$ find . -type f -name «*.bak» -exec rm -i <> \;
Sample outputs:
The -delete always works better because it doesn’t have to spawn an external process such as rm for every matched file. However, the -delete option may not available on all find versions. Hence, we can use the xargs command as follows too:
find . -type f -name «*.err» -print0 | xargs -I <> -0 rm -v «<>»
Where the find command option is as follows:
- -print0 – Force find command to print the full file name on the standard output, followed by a null character (instead of the newline character that -print uses). This allows file names that contain newlines or other types of white space to be correctly interpreted by programs that process the find output. This option corresponds to the -0 option of xargs.
And the xargs command options are as follows:
- -I <> : Replace occurrences of <> in the initial-arguments with names read from standard input. We pass <> as arg to the rm command.
- -0 : Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally). Disables the end of file string, which is treated like any other argument. Useful when input items might contain white space, quote marks, or backslashes. The GNU find -print0 option produces input suitable for this mode.
- rm -v «<>« : Run rm command on matched files.
Conclusion
Read the following man pages using the man command:
man find
man xargs
man rm
For detailed information on find command please see finding/locating files with find command part # 1, Part # 2.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
5 Ways to Empty or Delete a Large File Content in Linux
Occasionally, while dealing with files in Linux terminal, you may want to clear the content of a file without necessarily opening it using any Linux command line editors. How can this be achieved? In this article, we will go through several different ways of emptying file content with the help of some useful commands.
Caution: Before we proceed to looking at the various ways, note that because in Linux everything is a file, you must always make sure that the file(s) you are emptying are not important user or system files. Clearing the content of a critical system or configuration file could lead to a fatal application/system error or failure.
With that said, below are means of clearing file content from the command line.
Important: For the purpose of this article, we’ve used file access.log in the following examples.
1. Empty File Content by Redirecting to Null
A easiest way to empty or blank a file content using shell redirect null (non-existent object) to the file as below:
Empty Large File Using Null Redirect in Linux
2. Empty File Using ‘true’ Command Redirection
Here we will use a symbol : is a shell built-in command that is essence equivalent to the true command and it can be used as a no-op (no operation).
Another method is to redirect the output of : or true built-in command to the file like so:
Empty Large File Using Linux Commands
3. Empty File Using cat/cp/dd utilities with /dev/null
In Linux, the null device is basically utilized for discarding of unwanted output streams of a process, or else as a suitable empty file for input streams. This is normally done by redirection mechanism.
And the /dev/null device file is therefore a special file that writes-off (removes) any input sent to it or its output is same as that of an empty file.
Additionally, you can empty contents of a file by redirecting output of /dev/null to it (file) as input using cat command:
Empty File Using cat Command
Next, we will use cp command to blank a file content as shown.
Empty File Content Using cp Command
In the following command, if means the input file and of refers to the output file.
Empty File Content Using dd Command
4. Empty File Using echo Command
Here, you can use an echo command with an empty string and redirect it to the file as follows:
Empty File Using echo Command
Note: You should keep in mind that an empty string is not the same as null. A string is already an object much as it may be empty while null simply means non-existence of an object.
For this reason, when you redirect the out of the echo command above into the file, and view the file contents using the cat command, is prints an empty line (empty string).
To send a null output to the file, use the flag -n which tells echo to not output the trailing newline that leads to the empty line produced in the previous command.
Empty File Using Null Redirect
5. Empty File Using truncate Command
The truncate command helps to shrink or extend the size of a file to a defined size.
You can employ it with the -s option that specifies the file size. To empty a file content, use a size of 0 (zero) as in the next command:
Truncate File Content in Linux
That’s it for now, in this article we have covered multiple methods of clearing or emptying file content using simple command line utilities and shell redirection mechanism.
These are not probably the only available practical ways of doing this, so you can also tell us about any other methods not mentioned in this guide via the feedback section below.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник