LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-24-2020, 03:18 AM   #1
chrisguk
LQ Newbie
 
Registered: Aug 2014
Posts: 14

Rep: Reputation: Disabled
Deleting directories except last two


I am trying to delete the directories inside another directory with the following command but for me on Debian 10 its not working:

Code:
ls -ltr $APPVER | awk -vvar=echo $APPVER/ '/^d/ {A++;X[++j]=$NF} END{for(i=1;i<=A-5;i++){print "rm -rf " var X[i]}}' | sh
I get this result:

Code:
sh: 1: 34mAPP-2.15/: not found
APP-2.15 being the directory name.

Can anyone assist?
 
Old 06-24-2020, 05:00 AM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
First, do not just copy paste and execute some code that you do not understand, especially if it involves deleting files/directories.

Second, do not parse the output from ls.

Third, your example lacks important information.
What is the value of $APPVER? How does it get assigned? What does the directory structure look like? What do you mean by "last" two directories? Directories at that are at depth < 2? Or the last two directories that are listed by ls?
 
Old 06-24-2020, 05:15 AM   #3
chrisguk
LQ Newbie
 
Registered: Aug 2014
Posts: 14

Original Poster
Rep: Reputation: Disabled
I resolved it myself by using a different solution. This can be deleted/closed
 
Old 06-24-2020, 05:24 AM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Please post your solution for others who may have a similiar problem.

Quote:
Originally Posted by chrisguk View Post
I resolved it myself by using a different solution. This can be deleted/closed
You can mark the thread as "SOLVED" by clicking on "Thread Tools" -> "Mark this thread as SOLVED".
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Get first day of last month and last day of last month in bash xowl Linux - Software 18 02-09-2017 09:49 AM
Comparing two directories and deleting files dadams982 Linux - General 4 05-09-2013 08:50 AM
[SOLVED] Deleting all files from a directory except for two nonshatter Linux - Newbie 2 11-25-2010 08:57 AM
Copying files and sub-directories of a directory except the directories named ".abc" sri1025 Linux - General 2 08-24-2010 08:53 AM
bash: PWD minus last two directories wonderfullyrich Programming 4 09-11-2009 05:32 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration