Linux sort Command Examples | Sort files or directory in Linux

Blogs ❯❯ Linux

Image could not load

Imabe by pixabay

what is sort Command in Linux ?

Linux में sort Command का use files को sort करने में , या file content को किसी particular order में arrange करने के लिए किया जाता है। By default sort command file content को characters wise ascending order में sort करती है, लेकिन आप number या size के according भी sort कर सकते हैं।

sort Command Options

by default , आप sort filename command run कर सकते हैं , लेकिन जैसा अभी आपने पढ़ा कि इसे हम कई तरह से use कर सकते हैं , So different - different task के किए लिए different options भी हैं। sort command के सभी options देखने के लिए आप sort --help command run कर सकते हैं।

$ sort --help

sort Command Example

Example के लिए हम एक test.txt name की file बनाकर उसमे content insert करेंगे , फिर sort command का use करके अलग अलग तरीके से content को sort करेंगे।

$ touch test.txt
$ cat > test.txt
rahul
ravi
mohit
john
tom
press ctrl+d to save and exit.

Example में use की गयी touch Command का use file create करने के लिए और cat Command का use file में content write और concatenate करने के लिए किया जाता है।

अब , sort command का use करके file को sort करते हैं -

$ sort test.txt 
john
mohit
rahul
ravi
tom

Linux reverse sorting

reverse (descending) order में sort करने के लिए -r option का use किया जाता है।

$ sort -r test.txt 
tom
ravi
rahul
mohit
john

Write output to new file

-o , option का use करके आप किसी existing file content को sort करके new file में write कर सकते हैं।

$ sort -o test.txt > newfile.txt
$ cat newfile.txt
john
mohit
rahul
ravi
tom

Linux sort numerically

file को numeric sort करने के लिए -n command का use किया जाता है। Example के लिए हम फिर से test.txt में numeric data enter करके sort करेंगे।

$ cat > test.txt
200
3
56
23
90
press ctrl+d to save and exit.
sort numerically
$ sort -n  test.txt
3
23
56
90
200

sort numeric data in reverse order

पिछले example में use किया गया -r option का use -n के साथ numeric data को reverse order में print करने के लिए -rn का use करेंगे।

$ sort -rn  test.txt
200
90
56
23
3
Recent Blogs

Loading ...

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook