Avatar of msryhajw
msryhajw

asked on 

Multiple Folder Contents Deletion & Drive Space Issue

I am trying to delete the content of two folders on a dedicated linux server that I access remotely via SSH as the HD is 97% full.

The folders are called 'cache' and 'tcache' and contain cached contents that are egenerated on the fly. These folder exist on over 100 domains hosted on a cpanel server so what I am looking for is a way to recursively delete all of the files contained in these folders. It should also be noted that rm gives the following error due to file number limitations just in one of the folders "-bash: /bin/rm: Argument list too long"

The question is if there is an easier way of see where the space is being used on the server and how to remove the cache files all in one without having to manually go through each folder.

TIA

Linux

Avatar of undefined
Last Comment
Duncan Roe
Avatar of Nick Upson
Nick Upson
Flag of United Kingdom of Great Britain and Northern Ireland image

cd /cache

for file in `ls`
do
  rm $file
done

# make sure the chars around the ls are ` and not '
Avatar of msryhajw
msryhajw

ASKER

Does this proposal not require me to manually go through each folder to delete the files i.e. cd directory

Structure is:

/home2/test1234/public_html/tcache/     First cache folder
/home2/test1234/public_html/cache/      Second cache folder
/home2/anottest/public_html/cache/       Third cache folder
/home2/anottest/public_html/tcache/      Forth Cache Folder etc ....

There are around 400 cache folders in the same structure as above and I am trying to delete the files within these 'cache' folders all at once not one by one.

Also if there are 200 folders in /home2/ how do I find out how much space each folder is using (as per my question previously)

TIA

Avatar of Nick Upson
Nick Upson
Flag of United Kingdom of Great Britain and Northern Ireland image

cd /home2

for file in `ls */public_html/cache`
do
  rm $file
done

part 2

du /home/*
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

cd home2/test1234/public_html/tcache
find . -type f -print|xargs -n 32 -r rm

-n32 limits to 32 files at a time (should eliminate command line length problems
-r says to to run if no args (i.e. there was a multiple of 32 files to rm in the first place)

This will leave all directories in place. If you want to remove these, and "rm -r *" still breaks command line length, do:

find . -maxdepth 1 -type d -print|xargs -n 32 -r rm -r
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

How much space each folder is using (in current folder)

find . -mount -type d -mindepth 1 -maxdepth 1 -exec du -s "{}" \;|sort -n
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

Post before last should have -mindepth 1 -maxdepth 1 instead of just -maxdepth 1, otherwise it'll try to rm . (and fail to do so). -print is optional with gnu find in the absence of any other actions
ASKER CERTIFIED SOLUTION
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of msryhajw
msryhajw

ASKER

I tried to look at this solution but:

find . -mount -type d -mindepth 1 -maxdepth 1 -print|xargs -n 1 -r ./todo.sh

gives:

xargs: todo.sh: No such file or directory

Which I assume create the file list for deletion by ./todo.sh ?

TIA
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

You need to create the file todo.sh first. That is part of the solution. You must make it executable (chmod a+x todo.sh).
The contents of todo.sh should be as per my previous post, except you don't need to include the comments (words following '#'), but they will do no harm if included..
Linux
Linux

Linux is a UNIX-like open source operating system with hundreds of distinct distributions, including: Fedora, openSUSE, Ubuntu, Debian, Slackware, Gentoo, CentOS, and Arch Linux. Linux is generally associated with web and database servers, but has become popular in many niche industries and applications.

71K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo