A bit out of box and assuming that you want to retain the most recently modified file, rename the file you want to keep by giving it a leading dot, then remove everything else and rename the file back:-
Would that work for you?
Perhaps also, list the files, stripping out the most recent:-
A bit out of box and assuming that you want to retain the most recently modified file, rename the file you want to keep by giving it a leading dot, then remove everything else and rename the file back:-
Would that work for you?
Perhaps also, list the files, stripping out the most recent:-
I hope that these help.
Robin
Hello Robin,
IMHO I think directly giving rm * could be dangerous in case we are not running the script in the same path where user actually wants to perform the actions. So I believe it will more good if we could add path here and check the condition if path exist too(my fav. Bakunin style )
Thanks,
R. Singh
Given the small number of files that you want to remove:
would suffice. Alternatively you could remove the write-attribute of the file(s) you wish to preserve. This would cause rm to prompt for their removal whilst quietly removing all other files. Just remember to put it back afterwards.
If you have a varying list of files whose names all begin with MM_ and you want to remove all but the last name sorted alphanumerically, none of the filenames contain any IFS characters (by default <space>, <tab>, and <newline>), the list of files you're removing will never give you a command length that would exceed ARG_MAX, and you're using a shell that meets POSIX shell requirements (with or without extensions like shopt and setopt). You could also try:
which is similar to what Corona688 suggested, but only invokes rm once instead of once for each file to be removed. (But his suggestion works without the limitations noted above except the 1st and last.)
Note: The problem noted by MadeInGermany in post #13 in this thread has been fixed above.
Last edited by Don Cragun; 05-09-2017 at 05:38 AM..
Reason: Fix bug noted by MadeInGermany.
This User Gave Thanks to Don Cragun For This Post:
Don Cragun's method using bash4 arrays:
Same caveats as above, of course.
Andrew
Not quite. If you run my (corrected) script twice, the 2nd execution will silently do nothing (since only one matching file remains). The code above will run rm with no file operands the 2nd time you run it (or any time that there are no files or only one file matching the pattern MM_6000*). And MadeInGermany's code has this same issue.
The above can be fixed using:
and I think MadeInGermany's suggested code can be fixed with:
again, replacing echo with rm if the resulting list of files produced is what you expect.
A slightly modified version of this also works with a recent Korn shell (tested with ksh version 93u+ 2012-08-01):
Both of the above suggestions work even with filenames that contain whitespace characters, but are still susceptible to ARG_MAX limits.
This User Gave Thanks to Don Cragun For This Post:
Hi there, shameful Linux Newbie here :p
I was wondering if you could help with my problem...
I have plenty of files I'd like to concatenate. I know how to basically use cat command but that won't be enough from what I need : excluding the last xx bytes from files before assembling since there's... (4 Replies)
hi,
I need to find files that have been created less than 3 days ago. However, I need to only search specific directories.
I've searched about the net and found some useful commands such as :
find . -type d -name 'dir_to_exclude' -prune -o -print -mtime -3
however I cannot get it... (2 Replies)
Hi ,
I have a file like below.I want all the content in a single line excluding the XML.How can i proceed?
t=21
y=23
rg=xyz
.....
<xmlstarts>
.
.
<xmlends>
lk=99
lo=09 (3 Replies)
Hello i am using HP-UX rapdb2 B.11.23 U ia64 1068321383 unlimited-user license.
I am tryiyng to exclude for tar all files that start with TOT* but i doues not work
I am using:
tar -cvf /ODS/prepaid/CDR_FLOW/WORK/backup.tar --exclude='TOT*'
and i get the error:
tar: cannot stat... (3 Replies)
The title is not as easy as it sounds.... I am trying to exclude and file while ssh and untaring the file on the fly.
The command I am using is... The command typically works but recently I've add the X option along with the exclude file. Essentially, the exclude file is being ignored when run... (2 Replies)
Hello,
Under one directory, I can use below command to find out the file names with string "Export terminated successfully without warnings"
grep -i -l "Export terminated successfully without warnings" *.*
My question is : how I find out the file names without including string
"Export... (5 Replies)
Hi,
I have a log file which might have certain paragraphs.
Switch not possible Error code 1234
Process number 678
Log not available Error code 567
Process number 874
.....
......
......
Now I create an exception file like this.
cat text.exp
Error code 1234
Process number 874 (3 Replies)
Hi
I have some 5 folders and two files in the current directory.
I want to delete all, expect one folder(files in the folder too should not be deleted) and a file in the current directory. Lets say the folder and file that should not be deleted as 'a'(folder name) and 'b'(file name). Can you... (1 Reply)
How to get the name of the file by excluding the extention of file name?
For example, my filename is 'test.txt'. I want to get only the name 'test' but not the extention .txt. (2 Replies)
:confused:
I'm trying to archive a site excluding all PDF files in order to keep the size down. I have manged to do this in the past using the exclude argument in the tar command and something along the lines of *.pdf. Unfortunately, I failed at the time to note the syntax that I used and have... (1 Reply)