Linux delete all empty files

Linux / UNIX: How To Empty Directory

How To Empty Directory In Linux and Unix

  1. rm command – Delete one or more files or directories.
  2. find command – Find and delete all files from a specific directory.

Linux Empty Directory Using the rm Command

First, consider the following directory structure displayed using the tree command

To delete all files from /tmp/foo/ directory (i.e. empty /tmp/foo/ directory), enter:
$ cd /tmp/foo/
$ rm *
OR
$ rm /tmp/foo/*

Delete All Files Using the Find Command

Consider the following directory structure:

To delete all files from /tmp/bar/ directory (including all files from sub-directories such as /tmp/bar/dir1), enter:
$ cd /tmp/bar/
$ find . -type f -delete
OR
$ find /tmp/bar/ -type f -delete
The above find command will delete all files from /tmp/bar/ directory. It will not delete any sub-directories. To remove both files and directories, try:
find /path/to/target/dir/ -delete
The find commands 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

  • -type f : Delete on files only.
  • -type d : Remove folders only.
  • -delete : Delete all files from given directory name.

How to remove a full directory and all files in Linux

To remove a directory that contains other files or sub-directories, use the following rm command command. In the example, I am going to empty directory named “docs” using the rm -rf command as follows:
rm -rf /tmp/docs/*
Get verbose outputs:
rm -rfv /tmp/docs/*

The rm command options are as follows:

  • -r : Delete directories and their contents recursively on Linux or Unix-like systems.
  • -f : Forceful removal. In other words, ignore nonexistent files and delete whatever found.
  • -v : Verbose outputs. For example, explain what is being done on screen.

Conclusion

You learned how to use the rm and find command to delete all files and sub-directories on Linux/macOS/*BSD and Unix-like systems. In other words, this is useful to empty folders on Linux. For more information see rm command help page here.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Читайте также:  Current mac os version

Источник

Linux / Unix: Find and Delete All Empty Directories & Files

H ow do I find out all empty files and directories on a Linux / Apple OS X / BSD / Unix-like operating systems and delete them in a single pass?

You need to use the combination of find and rm command. [donotprint]

Tutorial details
Difficulty level Easy
Root privileges No
Requirements find command
Est. reading time 5m

[/donotprint]GNU/find has an option to delete files with -delete option. Please note that Unix / Linux filenames can contain blanks and newlines, this default behaviour is often problematic; filenames containing blanks and/or newlines are incorrectly processed by many utilities including rm command. To avoid problems you need to pass the -print0 option to find command and pass the -0 option to xargs command, which prevents such problems.

Method # 1: Find and delete everything with find command only

The syntax is as follows to find and delete all empty directories using BSD or GNU find command:

Find and delete all empty files:

Delete empty directories

In this example, delete empty directories from

  • 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

Delete empty files

In this example, delete empty files from

Fig.01: Delete empty directories and files.

How to count all empty files or directories?

The syntax is as follows:

  • -empty : Only find empty files and make sure it is a regular file or a directory.
  • -type d : Only match directories.
  • -type f : Only match files.
  • -delete : Delete files. Always put -delete option at the end of find command as find command line is evaluated as an expression, so putting -delete first will make find try to delete everything below the starting points you specified.

This is useful when you need to clean up empty directories and files in a single command.

Method # 2: Find and delete everything using xargs and rm/rmdir command

The syntax is as follows to find and delete all empty directories using xargs command:

Источник

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.

Источник

How to Delete Empty Files and Directories in Unix or Linux Server — find Command Example

Creating Empty files and directories in Unix

//This will create an empty directory inside the current directory
test@localhost:

/unix mkdir empty_dir

//This the command will find all empty files and directories in Unix
test@localhost:

/unix find . -empty
. / empty.txt
. / empty_dir

Searching Empty Files and Directory in Unix/Linux

/ unix find . -type f -empty
. / empty.txt

/ unix find . -type d -empty
. / empty_dir

/ unix ls -ltr | grep ‘\ ‘
drwxr-xr-x+ 1 test Domain Users 0 Jun 15 11 : 43 empty_dir /
-rw-r—r— 1 test Domain Users 0 Jun 15 11 : 44 empty.txt

/ unix find . -maxdepth 1 -size 0 -ls
90353467524120775 0 drwxr-xr-x 1 test Domain Users 0 Jun 15 11 : 43 .
9007199255261921 0 -rw-r—r— 1 test Domain Users 0 Jun 15 11 : 44 . / empty.txt
19421773393554899 0 drwxr-xr-x 1 test Domain Users 0 Jun 15 11 : 43 . / empty_dir

Deleting Empty Files and Directories in Unix Linux

/ unix find . -empty -delete

/ unix find . -empty -delete

/ unix ls -lrt
total 1.0K
-rw-r—r— 1 test Domain Users 118 Aug 4 2011 contacts.txt

/ unix find . -empty -type d -exec rm -r <> \;
find : ` . / empty_dir ‘: Not a directory

3 comments :

Very nice article..very helpful for interviews..
As you mentioned there are lot of ways to delete files and directories, just wanted to add on one more way:

rm -rf `find . -empty`

August 28, 2012 at 1:25 AM Anonymous said.

I’ve been using find for ages but this is the first time to hear about -empty and -delete. (Later GNU additions?) Thanks!

But the premise in this fine article is incorrect. Why would «find . -empty -delete» or «find . -empty -exec rm -r <> \;» not be counted as a «single command»?

Second, «find . -empty -delete» or its -exec equivalent is better than xargs and backquote when you have many to delete. The problem with a second command is that each command can only take this many arguments in shell. So when find finds too many empty files, the second command will bail with error message «Too many arguments.» Now the rm from within -exec actually receives only a single argument each time find finds a file or directory, so there is no fuss about too many of them.

Источник

How to remove files and directories quickly via terminal (bash shell) [closed]

Want to improve this question? Update the question so it’s on-topic for Stack Overflow.

Closed 6 years ago .

From terminal window:

When I use the rm command it can only remove files.
When I use the rmdir command it only removes empty folders.

If I have a directory nested with files and folders within folders with files and so on, is there any way to delete all the files and folders without all the strenuous command typing?

If it makes a difference, I am using the mac bash shell from terminal, not Microsoft DOS or linux.

4 Answers 4

-r «recursive» -f «force» (suppress confirmation messages)

Would remove everything (folders & files) in the current directory.

But be careful! Only execute this command if you are absolutely sure, that you are in the right directory.

Yes, there is. The -r option tells rm to be recursive, and remove the entire file hierarchy rooted at its arguments; in other words, if given a directory, it will remove all of its contents and then perform what is effectively an rmdir .

The other two options you should know are -i and -f . -i stands for interactive; it makes rm prompt you before deleting each and every file. -f stands for force; it goes ahead and deletes everything without asking. -i is safer, but -f is faster; only use it if you’re absolutely sure you’re deleting the right thing. You can specify these with -r or not; it’s an independent setting.

And as usual, you can combine switches: rm -r -i is just rm -ri , and rm -r -f is rm -rf .

Also note that what you’re learning applies to bash on every Unix OS: OS X, Linux, FreeBSD, etc. In fact, rm ‘s syntax is the same in pretty much every shell on every Unix OS. OS X, under the hood, is really a BSD Unix system.

Источник

Читайте также:  Простой ftp server linux
Оцените статью