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
2
3
4
5
"The only way to do great work is to love what you do." - Steve Jobs
"In the end, it's not the years in your life that count. It's the life in your years." - Abraham Lincoln
"Success is not final, failure is not fatal: It is the courage to continue that counts." - Winston Churchill
"Believe you can and you're halfway there." - Theodore Roosevelt
"Be yourself; everyone else is already taken." - Oscar Wilde
the shuf command allows for a random permutation of all the lines.
1
2
3
4
5
6
% shuf quotes.txt
"Success is not final, failure is not fatal: It is the courage to continue that counts." - Winston Churchill
"In the end, it's not the years in your life that count. It's the life in your years." - Abraham Lincoln
"The only way to do great work is to love what you do." - Steve Jobs
"Believe you can and you're halfway there." - Theodore Roosevelt
"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
2
% shuf -n 1 quotes.txt
"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
2
3
4
5
6
7
% shuf -i 0-5
410235
Give shuf a list of options using -e and outputing 1 chosen line using -n: