Linux recursively remove all files

Delete files & folders recursively

All log file on the Qnap data storage and it is in the another location. So when I want to delete files & folders which are older than 180 days I cannot use find command cause of the slowness. So I wrote a script like this .

SixMonthAgo=$(date --date='190 day ago' "+%Y/%m/%d/%H") = 2011/06/12/12 Hosts="hosta hostb" maxDay=181 qnapFolder="/opt/logs/qnap" for host in $(echo "$"); do maxDayAgo=$(date --date="$ day ago" "+%Y/%m/%d") countCharacters=$(echo $ | wc -c) if [ $ -ge 10 ]; then rm -rf $/$/$ #output of this just like 2012/03/12 fi done 

But sometimes i got an error. This soluiton is not working correctly what I want. I want to delete all files and folders before 180 days. How can I do this ? That must delete before all files and directories before 2012/03/23 for example . Thanks in advance

2 Answers 2

The find command is slow? Are you certain? The only drawback is that it fails to remove the directory structure.

find /opt/logs/qnap -type f -mtime +180 -delete

If the directories were indeed created at the same time then maybe removing -type f could work for you.

find /opt/logs/qnap -mtime +180 -delete

find is very fast mind you . but if you really want to do it with a shell script .

CUTOFF=$(date --date='190 day ago' "+%s) HOSTS="hosta hostb" DIRECTORY="/opt/logs/qnap" for HOST in $(ls -1 $DIRECTORY); do if [ -d $DIRECTORY$HOST ]; then for DATE in $(ls -1 $DIRECTORY$HOST); do if [ -d $DIRECTORY$HOST$DATE ]; then if [ "$CUTOFF" < "$(stat --format="%Y" $DIRECTORY$HOST$DATE)" ]; then echo "Deleting $DIRECTORY$HOST$DATE" rm -rf $DIRECTORY$HOST$DATE; fi fi done fi 

I believe that the original poster has a remote file system (NFS maybe), which is indeed slow. Then, to go faster, he could run that find on the file server if possible.

the find command is not an exact solution for me cause of slowness. Even When I use ls command, I have to wait 40 seconds to list. So how can I loop recursive . Do you have any recommends?

No, find is probably the fastest you can get. You might write a C program using linux.die.net/man/3/ftw which would first collect a list of paths to remove, then remove them.

I have found the solution just like below. For instance lets say that the day older than 180 days ago is 2012/04/17 . That just deletes all directories starting from 2012/04/17 until 2012/04/01. And secondly it deletes every month before 2012/04 . The deletes ones are 2012/03, 2012/02 and 2012/01.

# SixMonthAgo=$(date --date='190 day ago' "+%Y/%m/%d/%H") = 2011/06/12/12 maxDay=104 qnapFolder="/opt/logs/qnap" for host in $(echo "$"); do maxDayAgo=$(date --date="$ day ago" "+%Y/%m/%d") countCharacters=$(echo $ | wc -c) if [ $ -ge 10 ]; then year=$(echo $ |cut -d '/' -f 1) month=$(echo $ |cut -d '/' -f 2) #2012/06 day=$(echo $ |cut -d '/' -f 3) #2012/06/04 minday=00 minmonth=00 until [ $month -le $minmonth ] do until [ $day -le $minday ] do rm -rf $/$/2012/$month/$day day=$(printf "%02d" $(expr $day - 01)) done month=$(printf "%02d" $(expr $month - 01)) rm -rf $/$/2012/$month done fi done 

Источник

Читайте также:  Посмотреть объем файла linux

Bash script to recursively step through folders and delete files

Can anyone give me a bash script or one line command i can run on linux to recursively go through each folder from the current folder and delete all files or directories starting with '._'?

Just FYI: the ._ files are called AppleDouble files. On Mac systems, files have a data fork and a resource fork. The resource fork typically holds information such as icons, the file's spatial position in the folder (in Finder), and other metadata. The data fork (the actual file) contains the actual important data, so discarding the AppleDouble file shouldn't be problematic.

6 Answers 6

Change directory to the root directory you want (or change . to the directory) and execute:

find . -name "._*" -print0 | xargs -0 rm -rf 

xargs allows you to pass several parameters to a single command, so it will be faster than using the find -exec syntax. Also, you can run this once without the | to view the files it will delete, make sure it is safe.

This will be confused by filenames with spaces, which are common if you're dealing with files from a Mac environment. Use the null-delimiter options to find and xargs ( find . -name "._*" -print0 | xargs -0 rm -rf ) to avoid this problem.

I've had a similar problem a while ago (I assume you are trying to clean up a drive that was connected to a Mac which saves a lot of these files), so I wrote a simple python script which deletes these and other useless files; maybe it will be useful to you:

Yep, u assumed correctly. Supposedly its something apple are looking to fix to make macs more 'network friendly'.

find /path -name "._*" -exec rm -fr "<>" +; 

Instead of deleting the AppleDouble files, you could merge them with the corresponding files. You can use dot_clean .

dot_clean -- Merge ._* files with corresponding native files.

For each dir, dot_clean recursively merges all ._* files with their corresponding native files according to the rules specified with the given arguments. By default, if there is an attribute on the native file that is also present in the ._ file, the most recent attribute will be used.

If no operands are given, a usage message is output. If more than one directory is given, directories are merged in the order in which they are specified.

Because dot_clean works recursively by default, use:

If you want to turn off the recursively merge, use -f for flat merge.

Источник

How To Remove Files or Directories Recursively in Linux

Sue Wayne

No worries. Wondershare Recoverit can get back your lost files within 3 steps. The ultimate and professional Linux data recovery software is totally reliable and 100% safe.

A directory can have many subdirectories and files within it. Removing all files and subdirectories in a directory may be necessary when working with the Linux filesystem. It is referred to as recursive deletion. Use the rm command to recursively remove files or directories (also known as folders in Windows) in Linux. The rmdir command only removes empty directories. This guide will present the steps to recursively remove files and directories in Linux.

Читайте также:  Parallels windows mac linux

In this article

Part 1. How To Remove Directory Recursively in Linux Using rm Command

A summary of the rm command syntax for removing directories/folders recursively:

Command Syntax
Description

In Linux and Unix-like systems, everything is considered a file. In other words, "files" include photos, documents, directories/folders, SSD/hard drives, NIC, USB devices, keyboards, printers, and network communications.

Examples Of How To Delete A Folder Recursively

In this example, we will delete the data folder in the current home directory recursively:

Before removing the data directory, the specified /home/vivek/data/ directory will be emptied of all subdirectories, including their subdirectories and files. Unless the -f (force) option is specified on the command line, the user is prompted to remove any write-protected files in the directories:

Alternatively, you can use the command that follows:

To remove a folder whose name begins with a “ - ,” such as “ -dsaatia ,” use one of the following commands:

You can also run the following command:

To see verbose outputs, we can use the -v option. In other words, the rm command on Linux will explain what happens to our files and folders. As an example:

rm -rfv /path/to/dir1
rm -r -f -v /home/vivek/oldpartpics

Delete Folders With Unique Name Characters

Your folders and files may contain while spaces, semicolons, backslashes, and other characters in Linux. As an example:

Assume we have a folder called "Our Sales Data" and " baddir# " or " dir2 ;# " in it. So, how do we get rid of those directories with unusual names? The solution is straightforward. We will enclose the problematic filename or folder name in quotation marks. As an example:

rm 'Our Sales Data'
rm -rfv '/path/to/Dir 1 ;'
rm -r -f -v "baddir#"
rm a\ long \dir1 \name

Sometimes a backslash ( \ ) is required before the meta-character in your filename or folder name:

remove a directory or folder recursively

Part 2. How To Delete Files Recursively in Linux Using rm Command

Delete All Files Recursively

When used with the -r flag, the rm command will remove the contents of all types of files.

But first, using the ls command, let's look at the home directories. In this example, there are five directories: Desktop, dir2, Documents, Downloads, and removerecurdir.

use the ls command

Assume we want to delete the removecurdir and its contents, including all files and subdirectories. Run the syntax below.

The ls output shows the directory and its contents were successfully removed.

delete files recursively in linux

Remove Files by Size Recursively

Here are the steps to recursively delete files (

The find syntax is as follows:

You can use sudo to access the protected files in the example below.

sudo find /var/log -type f -size -10M -exec rm <> +

The minus sign ( - ) must be replaced with a plus sign ( + ).

In the following example, I will use the previous syntax to remove files larger than 1 GB.

find /var/log -type f -size +1G -exec rm <> +

Remove Files by Extension Recursively (File type)

The following section describes how to delete files recursively by extension or file type.

Look for the content of the home directory, which is called testhint with the tree command.

Читайте также:  Hp m227sdn драйвер linux

The parent directory testhint has a file (file1.txt) and two subdirectories: testhint2 has file3.txt, and testhint3 has file3 and something.txt.

parent directory testhint

Assume you want to remove all txt files recursively. The syntax is as follows:

Then, run this command to remove all text files recursively within the parent directory testhint.

find ~/testhint -type f -name '*.txt' -print -delete

All text files were deleted, leaving only file3 without an extension.

remove all txt files recursively

You can also delete files by extension by the find together with exec commands.

find together with exec commands

The syntax for removing files by extension with -exec is as follows:

Run the command below to remove the .log files.

find ~/testhint -type f -name '*.log' -exec rm -f <> \;

The xargs command provides a similar solution. The distinction between xargs and exec is that exec executes the rm function whenever a file matches the condition. The command xargs run the rm command once for each file found that matches the condition.

To remove all files by extension using find and xargs , use the following syntax:

remove extension using find and xargs

Enter the command shown below to remove all .c files with xargs.

find . -name "*.c" -print0 | xargs -0 rm

Once again, the selected extension files were successfully deleted.

successfully deleted selected extension files

Recursively Deleting All Files Based On Permissions

Let's take a look at the new content in the testhint directory. Four files have full access (file2, file3.c, file6.c, and file7).

content in the testhint directory

Assume you want to locate and delete all files with full permissions for everyone. The syntax is as follows:

Run the command below to remove all files with full access for all users.

find ~/testhint -perm 777 -print0 | xargs -0 rm

remove all files based on permissions

Delete Files Recursively Based on Their Modification Or Creation Time

Execute the syntax that follows:

find ~/testhint -perm 777 -print0 | xargs -0 rm

If you want to delete files created or modified on the last day (last 24 hours), use the following command, where 1 denotes the number of days, and the minus ( - ) symbol denotes files created or modified before the defined number of days.

find -type f -mtime -1 -delete

Replace the minus symbol with a plus symbol to remove files created or modified before a day, before 24 hours.

find -type f -mtime +1 -delete

Part 3. What To Do If You've Accidentally Deleted a File or Folder in Linux?

In Linux, unintentionally deleting a file or folder can be a real hassle, but there are actions you can take to improve your chances of successful recovery:

  • Stop using the computer: To prevent further data loss, stop using the computer immediately and avoid writing any new data to the disk where the deleted file was located.
  • Act quickly: The longer you wait to start the recovery process, the greater the risk of permanent data loss.
  • Create backups regularly: Regular backups can ensure that you have a recent copy of your important data in case of accidental deletion.
  • Keep your file system in good condition: Regular disk maintenance, such as checking for errors and fixing them, can help keep your file system in good condition and make recovery easier.

The best Linux data recovery tools can also help you recover lost or deleted files. Following these tips and using the correct data recovery tool can increase your chances of successfully recovering your deleted files and folders in Linux.

Источник

Оцените статью
Adblock
detector