shuf command in Linux
What is the shuf
command in Linux?
What is the shuf
command?
The shuf
command in Linux is a utility for shuffling lines of input files. It allows users to shuffle contents of a file or produce random output based on supplied arguments.
How is the shuf
command used?
Given a file named quotes.txt
with the following contents:
1"The only way to do great work is to love what you do." - Steve Jobs
2"In the end, it's not the years in your life that count. It's the life in your years." - Abraham Lincoln
3"Success is not final, failure is not fatal: It is the courage to continue that counts." - Winston Churchill
4"Believe you can and you're halfway there." - Theodore Roosevelt
5"Be yourself; everyone else is already taken." - Oscar Wilde
the shuf
command allows for a random permutation of all the lines.
1% shuf quotes.txt
2"Success is not final, failure is not fatal: It is the courage to continue that counts." - Winston Churchill
3"In the end, it's not the years in your life that count. It's the life in your years." - Abraham Lincoln
4"The only way to do great work is to love what you do." - Steve Jobs
5"Believe you can and you're halfway there." - Theodore Roosevelt
6"Be yourself; everyone else is already taken." - Oscar Wilde
In addition, it can be used in the following scenarios as well:
List 1 random quote:
1% shuf -n 1 quotes.txt
2"In the end, it's not the years in your life that count. It's the life in your years." - Abraham Lincoln
List random numbers between a range of numbers:
1% shuf -i 0-5
24
31
40
52
63
75
Give shuf
a list of options using -e
and outputing 1 chosen line using -n
:
1% shuf -e option1 option2 option3 -n 1
2option3
Standard input works as well:
1% seq 5 | shuf
25
32
44
51
63