
- Kali Linux Tutorial
- Kali Linux - Home
- Installation & Configuration
- Information Gathering Tools
- Vulnerability Analyses Tools
- Kali Linux - Wireless Attacks
- Website Penetration Testing
- Kali Linux - Exploitation Tools
- Kali Linux - Forensics Tools
- Kali Linux - Social Engineering
- Kali Linux - Stressing Tools
- Kali Linux - Sniffing & Spoofing
- Kali Linux - Password Cracking Tools
- Kali Linux - Maintaining Access
- Kali Linux - Reverse Engineering
- Kali Linux - Reporting Tools
- Kali Linux Useful Resources
- Kali Linux - Quick Guide
- Kali Linux - Useful Resources
- Kali Linux - Discussion
What's the difference between nohup and ampersand (&) on Linux?
Linux provides us with different utility commands that we can make use of to print a random line from any files in the Unix command line. Mostly we make use of either the shuf command or the sort command, and in this article I’ll explain both the commands and which one is better and why.
Shuf Command
The shuf command in Linux is used to write random permutations of the input lines to the standard output. The idea of randomizing the input is the same as one does when the cards are shuffled. On most of the Linux operating systems it is present by default, as it is part of the GNU Coreutils and in case of the BSD like Mac OS X, we need to explicitly install the Coreutils first, then we can make use of it.
Syntax
shuf [OPTION] [FILE]
On Mac OS X, the syntax looks something like this −
gshuf [OPTION] [FILE]
The most basic case of making use of the shuf command is to use it without any options and file.
Example
Consider the example shown below −
immukul@192 d1 % gshuf apple sample orange
Now press CTRL+D and you will get the inputs you entered in a random order printed to the screen.
Output
sample apple orange
Now let’s make use of the shuf/gshuf command to print a random line from a .txt file.
Consider the example shown below
Example
gshuf -n 1 file.txt
Output
unsalutary
If I run the same command again, we will get a different output.
Output
typhlopexia
Sort Command
The sort command in Linux is used to sort the contents of a file and we can use the head command along with the sort command to get a random line from a .txt file.
Consider the example shown below −
Example
sort --random-sort file.txt | head -n 1
Output
podiatry
It should be noted that it is better to make use of the gshuf command as the sort command usually takes more time.
- Related Articles
- Linux Job Control &, disown, and nohup
- Difference between Linux and Windows
- Difference between Linux and Unix
- What's the difference between sizeof and alignof?
- What's the difference between window.location and document.location?
- What's the difference between Matplotlib.pyplot and Matplotlib.figure?
- What's the difference between "!!" and "?" in Kotlin?
- What's the Difference between Skills and Competencies?
- What's the difference between a context switch, a process switch and a thread switch in Linux?
- What's the difference between Tkinter's Tk and Toplevel classes?
- Difference Between Linux and Windows Operating System
- Difference Between sh and Bash in Linux?
- What's the difference between "STL" and "C++ Standard Library"?
- What is the difference between Python's re.search and re.match?
- What's the difference between lists and tuples in Python?
