Linux Command to Empty All Files of a Directory

linux command to empty all files of a directory

You can't use redirection (>) within find -exec directly because it happens before the command runs and creates a file called {}. To get around this you need to do it in a new shell by using sh -c.

Also, note that you don't need to cat /dev/null > file in order to clobber a file. You can simply use > file.

Try this:

find . -type f -exec sh -c '>"{}"' \;

Linux is it possible to empty the contents of all files in a directory

Use truncate:

truncate -s 0 directory/* &> /dev/null

How to remove all files that are empty in a directory?

The easiest way is to run find with -empty test and -delete action, e.g.:

find -type f -empty -delete

The command finds all files (-type f) in the current directory and its subdirectories, tests if the matched files are empty, and applies -delete action, if -empty returns true.

If you want to restrict the operation to specific levels of depth, use -mindepth and -maxdepth global options.

Linux -How to delete all files in a directory without using find

Use the following command:

rm -f XYZ/*

If you want to delete also subdirectories, use:

rm -fr XYZ/*

If you also want to delete the directory, use

rm -fr XYZ

Command to empty many files recursively

Thanks to @chepner for showing me the better way to protect against double quotes in the file names:

You can use find to do it

find start_dir -type f -exec sh -c '> "$1"' _ {} \;

And you could add extra restrictions if you don't want all files, like if you want only files ending in .log you could do

find start_dir -type f -name '*.log' -exec sh -c '> "$1"' _ {} \;

How delete all the empty files in linux irrespective of their directory using rm command

Well, rm(1) command only deletes the files whose names you pass to it on the command line. There's no code in rm(1) to allow you to filter those files, based on some condition or criteria. The old UNIX filosophy mandates here, write simple tools and couple them on pipes as neccessary to construct complex commands. In this case, find(1) is the answer... this is a tool to select files based on quite arbitrary criteria (like the one you ask for) and generate the actual file names or simply call commands based on that. On this respect

find dir1 dir2 ... -type f -size 0 -print | xargs rm

would be the solution (batching the filenames with xargs(1) command to do less fork(2) and exec(2) calls with less process fork overhead) to your problem, allowing to specify several dirs, selecting only files of size 0 and passing them to the batch command xargs(1) to erase them in groups. you can even filter the filenames based on some regular expression with

find dir1 dir2 ... -type f -size 0 -print | grep 'someRegularExpression' | xargs rm

and you'll get erased only the files that match the regular expression (and the other two predicates you expressed in find(1)) You can even get a list of the erased files with

find dir1 dir2 ... -type f -size 0 -print | grep 'someRegularExpression' | tee erased.txt | xargs rm

See find(1), grep(1), tee(1), xargs(1) and rm(1) for reference.

Unix Command to Delete all files in a directory but preserve the directory

rm -i <directory>/*

this should do the trick

EDIT: added -i just in case (safety first). directory should be a full or relative path (e.g. /tmp/foo or ../trash/stuffs)

What is the command to remove files in Linux from a particular directory which are owned by a particular user?

find /home/username -maxdepth 1 -type f -user "dev-user" -delete

Use the user flag to specify files owner by a specific user and use -delete to remove the files.

Set maxdepth 1 to search for files within /home/username only and not child directories.

How can I delete all files starting with ._ from the shell in Linux?

Try something like:

cd /path/to/directory; \rm -rf ._*

OR if there are recursive files with in subfolders then try:

find /path/to/directory -name "._*" -type f -print0| xargs -0 \rm -rf


Related Topics



Leave a reply



Submit