Reputation: 3446
I have the following command in my backup rotate script to remove old backups. It works as it ensure the latest backup is retained.
ls -t | sed -e '1,1d' | xargs -d '\n' rm
I'm unsure of the sed -e
argument is doing though. When I check man sed
I get:
-e script, --expression=script
add the script to the commands to be executed
I can't find details of what my current script argument '1,1d'
is attempting to do. Can anyone help?
Upvotes: 3
Views: 832
Reputation: 999
-e script
is adding the script to the sed-command-list. script
is used here as synonym for string, in difference to script-file in the option -f script-file
. That's why the argument is usually put into quotes (which could be omitted here). The script/string may inlcude a sequence of commands.
You may use more then one -e '<sed-cmd>'
, which are then applied to the input line in sequence. For example sed -e 's/kitty/cat/g' -e 's/cat/lion/g'
. Sometimes it is easier to understand where all the lions came from.
d
is one of the commands which accept address ranges. 1,1
is the address range, here from line 1 to line1, which could be shortened to 1d
Upvotes: 4
Reputation: 59506
d
elete lines 1
to 1
. It just removes the first line.
For testing, type:
seq 10
vs.
seq 10 | sed -e '1,1d'
Upvotes: 4