Why i can not delete folder with du? - sorting

I can't understand how to delete several folder by size with 'du'
sudo du -h --apparent-size /folder-to-delete/. | sort -h | head -n 65 | sudo rm -rf
Thanks for helpful ;)

Related

Find last created tar.gz and extract it

I need to find last created tar.gz file and extract it to some directory, something like this:
ls -t $(pwd)/Backup_db/ | head -1 | xargs tar xf -C /somedirectory
How to do it the right way in CentOS 7?
You can find out the most recently edited file in a subshell, and then use that in place of a filename. The new directory can be created, and then the tar file can be extracted to it.
new_dir="path/to/new/dir"
mkdir -p $new_dir
tar -zxvf $(ls -t *.tar.gz | head -1) -C $new_dir
Note that ls -t <dir> will not show the full <dir>/<filename> path for the files, but ls -t <dir>/* will, so after also reordering xargs flags (and forcing -n1 for safety), below should work for you:
ls -t $(pwd)/Backup_db/*.tar.gz | head -1 | xargs -n1 tar -C /somedirectory -xf

Remove last 2 lines of 'ls' output

I want to delete some revisions of docker images. I want to delete the last 2 lines. I'm able to print the last 2 lines with:
ls -lt | tail -n 2
gives my the 2 last lines.
drwxr-xr-x 2 root root 4096 Nov 9 10:56 541a303d3c82785293f89a401038ac33ef2b54b6aeb09efd3d3bda7xxxx
drwxr-xr-x 2 root root 4096 Oct 25 12:07 c74e1399c99de0c23517abc95bc9b16d09df5c4d518776e77d9ae67xxxx
Now is my question. How do I have to delete them?
I tried ls -lt | tail -n 2 | rm -r * but than I deleted everything (the whole output of ls)
You could get that to work. I would probably use something like rm -rf $(ls -t | tail -n2), but parsing ls is really not recommended.
A cleaner way to do this would be to use find. You can use that to delete everything before a certain time. Something like this: find . -mtime -180 -exec rm -f {} \; would delete everything newer than 180 days ago.
I would highly recommend testing whatever you are planning to run before you actually do the delete!
You have the right idea; however, whatever comes after 'rm' gets deleted, which is why * deleted all instead of what you were trying to pipe into it.
This is a pretty clean way to do it though
rm `ls | tail -n 2`
Just for test :
ls -t | tail -n 2 | xargs -i -t echo {}
-t, --verbose
Print the command line on the standard error output before executing it.
After the test, you can delete them with :
ls -t | tail -n 2 | xargs -i -t rm -fr {}
rm -fr 541a303d3c82785293f89a401038ac33ef2b54b6aeb09efd3d3bda7xxxx
rm -fr c74e1399c99de0c23517abc95bc9b16d09df5c4d518776e77d9ae67xxxx

How to cd into the first directory after sorting

I have a folder in which there are directories
ABC_1
ABC_2
ABC_3
ABC_4
ABC_5
Test
XYZ
I want to sort them by date,remove the directories which do not contain ABC in their name and cd into the first directory.
I tried
cd $(/bin/ls -t1 | head -n 1)
This is not working.
Any help would be much appreciated
Thanks.
This will list only directories and filter out any folder that does not start with ABC_:
cd "$(ls -t1 -d */ |grep "^ABC_" |head -n1)"
UPDATE:
You actually do not need grep
cd "$(ls -t1 -d ABC_*/ | head -n1)"
cd "$(/bin/ls -t1 | grep ABC | head -n 1)"
The poster is wanting the first result after the list not the first result according to modification time so the "-t" option is not needed.
You also have to make sure that you're pulling just directories and not files.
This will do what you want:
cd $(ls -d [^ABC]*/ | head -n 1)
$() run command
ls -d search for directories
[^ABC]/ do not include any directories that start with ABC
head -n 1 return the first entry
cd change to the directory

getting list of top space consuming files and remove it

I am using du -hsx * | sort -rh | head -10 to get top 10 space consuming files in directory. So I would like to know how to pass output of above command and delete those files. I know about xargs but I don't know how to incorporate it in my command, so any help would be appreciated ?
Thanks
You can do something like this:
du -sxh * | sort -rh | head -10 | xargs rm -fr $1
You can do,
du -sxh * | sort -rh | head -10 > out
cat out | xargs rm -fr $1

i want to move only temp files from those top 60 file to some folder

du -mx --exclude='.snapshot' | sort -n -r|head -n 60
after the execution of this command I am getting list of 60 files out of 1000 files which are present in that folder, where I want to move only temp files from those top 60 file to some folder. I tried with below command but its not working.
du -mx --exclude='.snapshot' | sort -n -r|head -n 60 | cp *temp /mnt/TEST/
Try the following
du -mx --exclude='.snapshot' | sort -n -r | head -n 60 | grep temp | xargs cp {} /mnt/TEST/
I'm not what your files names look like, so I put in a grep on temp. You should modify that as needed.
Once it works, you will need to replace cp with mv.

Resources