I found 5 last core files.
I need to delete all core files except these 5 files.
ls -t /u01/1/bin/core.siebprocmw.* |head -n 5
command to find 5 last files by time.
ls -t /u01/1/bin/core.siebprocmw.* |head -n 5 |xargs rm -r
command remove found last 5 files.
I need to delete all files except these last 5 files. Any ideas?
You could use sed to exclude the first five newest files, then delete the rest:
ls -t /u01/1/bin/core.siebprocmw.* | sed '1,5d' | xargs rm -r
You could also try
ls -t /u01/1/bin/core.siebprocmw.* | head -n -5 | xargs rm -r
head -n -5 selects everything except the last 5 lines in the output of ls.
Related
I am trying to monitor the progress of the newest five files and I want to see the last few lines of the each of them.
I was able to get the newest five files using the command:
ls *.log -lt | head -5
but I want to iterate through these five files and display the last 10 lines of each file. I was wondering if it can be done in a single bash command instead of a loop. But if it can't be done, I would appreciate a bash loop implementation too
tail can take multiple file names.
tail -n 10 $(ls -t *.log | head -5)
Add -F to monitor them continuously for changes.
If the file names might have spaces xargs will be more robust:
ls -t *.log | head -5 | xargs -d '\n' tail -n 10
Assuming the file names and path names do not contain special characters such as TAB or newline, how about:
while true; do
find . -type f -name "*.log" -printf "%T#\t%p\n" | sort -n | tail -5 | cut -f2 | xargs tail -10
sleep 1
done
ls *.log -1t | head -5 | while IFS= read -r file; do tail -10 "$file"; done
I would like to pre-process a directory of .gz files before submitting them to Hadoop/Spark. This is to avoid issues, such as these ones. The following bash pipeline almost does what I need, except that xargs rm doesn't seem to delete the files that fail the gunzip -t test.
gunzip -t *.gz 2>&1 | cut -f 2 -d: - | xargs rm
The pipeline works silently. Yet when gunzip -t *.gz is called again, it prints out
gzip: unhappy.gz: unexpected end of file
or similar.
For some reason, it looks as though this only deletes one file, then finishes. A (more complicated) pipeline that invokes xargs twice seems to work much more reliably:
ls *.gz | xargs -n 1 gunzip -t 2>&1 | cut -f 2 -d: - | xargs -t -n 1 rm
Decomposed, this pipeline says:
ls *.gz: list all .gz files
xargs -n 1 gunzip -t 2>&1: send that list one at a time (-n 1) to gunzip -t, to test the input
cut -f 2 -d: -: extract the filename from the output of gunzip, which is the second field (-f 2) of the line delimited by : character
xargs -t -n 1 rm: send the output of cut to rm one filename at a time, printing out progress (-t) as it operates
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
this command allows me to login to a server, to a specific directory from my pc
ssh -t xxx.xxx.xxx.xxx "cd /directory_wanted ; bash"
How can I then do this operation in that directory. I want to be able to basically delete all files except the N most newest.
find ./tmp/ -maxdepth 1 -type f -iname *.tgz | sort -n | head -n -10 | xargs rm -f
This command should work:
ls -t *.tgz | tail -n +11 | xargs rm -f
Warning: Before doing rm -f, confirm that the files being listed by ls -t *.tgz | tail -n +11 are as expected.
How it works:
ls lists the contents of the directory.-t flag sorts by
modification time (newest first). See the man page of ls
tail -n +11 outputs starting from line 11. Please refer the man page of
tail for more
detials.
If the system is a Mac OS X then you can delete based on creation time too. Use ls with -Ut flag. This will sort the contents based on the creation time.
You can use this command,
ssh -t xxx.xxx.xxx.xxx "cd /directory_wanted; ls -t *.tgz | tail -n
+11 | xargs rm -f; bash"
In side quotes, we can add what ever the operations to be performed in remote machine. But every command should be terminated with semicolon (;)
Note: Included the same command suggested by silentMonk. It is simple and it is working. But verify it once before performing the operation.
A coworker showed me a nifty way of using rm and xargs for deleting filenames listed in a .txt - but I can't remember what he did.
I ran
echo | xargs -a file.txt
where file.txt contained
1
2
3
4
And it printed
1 2 3 4
My logic says that
rm | xargs -a file.txt
should delete the files I created titled 1 and 2 and 3 and 4.
But that is not the behavior I get.
How do I form this simple command?
I believe you want:
xargs -a file.txt rm
The last argument to xargs should be the command you want it to run on all of the items in the file.
The solution proposed by Lynch is also valid and equivalent to this one.
Try this command:
xargs rm < file.txt
xargs take every line in input and append it to the command you specify.
so if file.txt contains:
a
b
then xargs will execute rm a b
Unless file.txt is really large, xargs is unnecessary and this is equivalent:
rm $(<file.txt)
and portable (POSIX) too.