Questions tagged [sort]
Sorting text files or other data, for example with the sort utility.
1,036 questions
4
votes
1
answer
196
views
Choice of field separator affects sort's ordering
Suppose we have a script named test_sort in our $PATH with the following contents:
#!/bin/bash
function echo_text () {
printf -- "%s\n" "$fc$oc$fs$lc"
printf -- "%s\n&...
4
votes
3
answers
301
views
Sort output using "--key" and "--field-separator"
I am trying to sort the output of ls by a certain part of the file names. The files are named like this:
file1-2025-09-30.tgz
file1-2025-10-01.tgz
file1-2025-10-15.tgz
file2-2025-09-30.tgz
file2-2025-...
5
votes
0
answers
144
views
Why does Nautilus sort these files in this order?
In Nautilus (Debian 12, en_US.UTF-8, GNOME Files 43.2), the following eight files are sorted in the following order:
-
-1
--
--1
1
--a
-a
a
To answer the question (Why does Nautilus sort these files ...
1
vote
1
answer
123
views
sort result of fd
I use fd to find file names containing only zeros. I want adopt this code to work with all files in folders, not with the only one file... This works not bad
$ fdfind -tf -x bash -c '[ -s {} ] &&...
4
votes
2
answers
615
views
Unix sort doesn't sort string correctly [duplicate]
Here is my simple test file:
# cat test
grp_pt cmg
grp_pt compsc
grp_pt ffre
grp_pt grp_pt
grp_pt hpcedt
grp_ptix dsnlp
grp_ptix exinspect
grp_ptix ptix
grp_ptix sciml
grp_ptix vers
grp_ptix ...
2
votes
0
answers
90
views
How can I find multiple duplicates of media files,sort, backup them and delete the rest?
I have a 4 TB hard drive containing pictures, sounds, and videos from the last 15 years.
These files were copied onto this drive from various sources, including hard drives, cameras, phones, CD-ROMs, ...
3
votes
1
answer
259
views
How are leading spaces handled in sort -k?
I have a file named test with the following content:
12288
4096
505
Note that there's a single space before 12288, two spaces before 4096 and three spaces before 505.
When I run GNU sort -k1.1,...
5
votes
2
answers
403
views
How to show directory sizes in tree format with correct values and depth control
I can't solve what I want using the tree, du, or df commands.
I'm using:
du version 9.6
tree version 2.2.1
What I want:
For the current directory, list all directories recursively to a depth of 3, ...
0
votes
1
answer
56
views
Sort of different column using different types, works, only strange thing with spaces
I have this file
1 you
3 me
2 their
4 us
this pipe works fine (it order first column by number, second by letters) is a pipe of another user found here
paste <(awk '{print $1}' file.txt | sort ...
9
votes
2
answers
1k
views
How do I make `ls` sort the same way Thunar sorts files?
The title is a summary of what I'm trying to achieve, but I'll give an example to illustrate what my problem is and how I've been trying to solve it.
Example folder
Let's say I've got a folder on a ...
0
votes
3
answers
138
views
Searching unique entries in a file
I am using Arch Linux/Debian Linux and want a list of unique "identifiers" in an ASCII txt file. Here is a snippet of the data I want to whittle down:
... (Received from VRW): wind ...
... (...
0
votes
1
answer
54
views
How to sort correctly the processes hungry of ram?
With this cmd line I see processes and use of ram in mb
ps aux | awk '{print $6/1024 " MB\t\t" $11}' | sort -rn|less
The problem is the sort
9.95703 MB /usr/lib/systemd/systemd-...
4
votes
1
answer
119
views
Why does command 'sort' behave differently for those 2 files
Those 2 sequences of lines are not sorted the same by the Linux shell command sort (gnu-coreutils 8.22 under RHEL 7.8), even though they begin with the same distinct prefixes. Why ?
$ echo "f'*.5....
-5
votes
1
answer
74
views
After wrote prompt $ sort [closed]
After I wrote prompt $ sort
display doesn't shown anything what the next step?
1
vote
1
answer
116
views
How can I find duplicate lines among files?
I have a software module which contains some files with same pattern.
<whitespaces>private static final long serialVersionUID = \dL;
How can I find files with the same value?
$ grep -R ...
2
votes
1
answer
544
views
linux sort float numbers starting with point
I would expect that the -g flag tells sort to treat the fields as floating point numbers, but:
cat << EOF | sort -g -k1,2
0 0
0 1.0
0 .5
0 5.0
EOF
gives
0 0
0 1.0
0 .5
0 5.0
Therefore the .5 ...
0
votes
2
answers
265
views
Sort is not sorting?
I seem to be having the same issue as described in The "join" utility reports: file is not sorted, but in fact it is sorted however I have piped BOTH files through sort before attempting to ...
-2
votes
2
answers
177
views
How to diff two files in bash efficiently?
I have two files containing the similar pattern:
cmd1 [cmd2 {xx/xx[7] x/x[0] ...}] cmd3 [cmd4 {xx/x[12] ...}]
cmd5 [cmd6 {x/x[1] xx ...}]
I don't need to consider all the cmds in two files. Only need ...
10
votes
3
answers
2k
views
GNU sort command does not sort words of different lengths with common prefixes correctly when using field delimiter
The GNU sort command is not sorting words of different lengths with common prefixes correctly for me, but only when using a field delimiter to sort on one of multiple fields.
Here is the correct, ...
0
votes
0
answers
12
views
cat/sort/uniq temporary storage? [duplicate]
I'm wondering where this data is being stored. I am running these commands from a different file system, but / seems to increase and I'm not sure where the data is.
cd /different_fs
cat a b c | sort | ...
1
vote
1
answer
198
views
Why is sorted uniq -c command showing duplicates
I am trying to count how many times I use a certain version of a library on my computer.
For some reason, uniq -c is outputing duplicates, despite sorting it, and despite the sort order seeming in ...
-1
votes
1
answer
103
views
Sorting Bash History for Redundancy Removal
I'm working with my .bash_history file and want to identify repetitive commands for cleanup.
Here's a sample snippet:
...
#1713251479
sh lowbat.sh
#1713251495
nvim lowbat.sh
#1713252186
sh lowbat.sh
#...
1
vote
3
answers
862
views
How to sort files by size in bash script?
I want to loop over all files in directory on macOS and perform some operations on each file, and do this based on their size, going from big to small. To create a list I can loop over I do this:
...
6
votes
2
answers
827
views
Sort a file by lines, regardless of their content
I have a very large file which I want to sort alphabetically. It is a tab separated file, but I really need to be sure that the file is sorted starting by the first character in the line regardless it ...
6
votes
2
answers
2k
views
sort all arrays in a JSON file depth-first to get a consistent order
I'm tracking the configuration of some system in JSON format in a revision control system.
Unfortunately that configuration is retrieved using some closed-source proprietary command, and the output ...
5
votes
5
answers
470
views
Is it possible to sort the letters in a word in a line of text?
So I have a file full of test commands that I like to run against some of my functions to make sure they are handling all possible situations correctly. No point in having duplicate commands tho. ...
3
votes
1
answer
125
views
Write header as first line at end of data
Having a read loop that manipulate data from another function.
One of the tasks in the loop is to record widest text in "some columns".
At the end of the loop the width is printed out, but ...
2
votes
1
answer
202
views
Why does sort on Linux and on Cygwin return different results?
Scenario:
# on Linux
$ cat r456.txt
e+e+l
e+e-c
$ cat r456.txt | sort
e+e-c
e+e+l
$ sort --version
sort (GNU coreutils) 8.30
# on Cygwin
$ cat r456.txt
e+e+l
e+e-c
$ cat r456.txt | sort
e+e+l
e+e-...
6
votes
3
answers
1k
views
awk: sort by first column then second; output unique 1st column once but all 2nd column
I have a two-column file that you can create as follows
cat > twocol << EOF
007 03
001 03
003 01
137 12
001 11
002 01
002 02
002 03
001 02
002 04
137 94
010 21
001 01
EOF
The resultant file, ...
0
votes
1
answer
903
views
List all files in a directory, recursively, sorted by modification date
The main answer of Sort the files in the directory recursively based on last modified date gives a method to list all files in a directory, recursively, sorted by modification date:
find -printf "...
0
votes
2
answers
57
views
Multilevel numeric path sort
I am on Ubuntu 22, and I have a large tree of files with annoying names that include numeric values in a variety of positions. Something like:
./2
./2/9
./2/10
./2/8
./1
./1/2
./1/1
./1/11
./11
./11/...
1
vote
4
answers
93
views
Reverse sorting of 2 textblocks in larger set of textblocks
I have a list of data, separated into blocks by headlines. The headlines are marked by 6 '=' signs at beginning and end.
There is a limited set of headlines that may appear, but not all of them appear ...
6
votes
3
answers
1k
views
How does sort -n command know what it needs to sort by in a file that contains both numbers and text?
I'm reading this blog at the moment: https://www.baeldung.com/linux/dir-find-largest-file-recursively
And I am asking about this command:
find /usr/lib -type f -printf "%s\t%p\n" | sort -n | ...
4
votes
4
answers
425
views
Awk- Compare Numbers from Two Files and write Differences in New File
I have two lists with item numbers and want to mark the difference between these lists by writing the numbers wich aren't in both files in a new file.
Both Files have the item number in column 2 and ...
0
votes
1
answer
173
views
Display the results in an alphabetical order, independently of their case [duplicate]
I am using ls on MacOS 11.6.1. In the output, the Upper cases are displayed before the lower case. For example, I get the following output:
$ ls
Blabla.txt
GAGAS.txt
asdf.txt
blabla.txt
and would ...
5
votes
2
answers
254
views
Why is ls sorting Chinese filenames by length?
I've run into a bit of a weird behaviour that I don't fully understand with ls and Chinese filenames. I'm running macOS 13.6.1 with SIP enabled (no core OS modifications), MacPorts installed, and US ...
4
votes
4
answers
577
views
How to sort the list of positional parameters in POSIX sh
Is there a way to sort the list of positional parameters in POSIX sh? Each positional parameter could contain arbitrary characters (e.g. spaces, newlines, tabs, etc.). The sort algorithm should be ...
0
votes
1
answer
163
views
Shell script to find and copy files from a painfully large unsorted folder
I have a folder is populated each day with a series of files, for example content1_20231110.csv, content2_20231110.csv, etc. For reasons out of my control, this folder holds an entire year's worth of ...
1
vote
1
answer
791
views
How exactly does Nautilus sort files by name?
I noticed that Nautilus(GNOME Files) does not fully sort alphabetically.
$ touch 1
$ touch 2
$ touch 3
$ touch '!2'
Then the files are displayed in the following order: !2 1 2 3. This is fully ...
0
votes
2
answers
163
views
Load files in sequential order
I need to load multiple files in my shell script with the same filename but appended in the front of each file name is YYMMDDPERSONNEL
Examples: 231102PERSONNEL and 230103PERSONNEL
There are many ...
-2
votes
2
answers
184
views
Sorting a text file based on numerical values present in each line [closed]
I have a text file containing paths of various wav files. But when they are created they are sorted based on alphabetical order. Hence the paths are like this
/home/wav_path/...
0
votes
3
answers
90
views
Does a command exist that lists all the directories where a word appears in a file or directory name?
When I don't remember where a file or a folder is, I sometime use the locate command
(that finds more occurrences, allow more candidates than a find, in my mind. But maybe I'm mistaking).
But then ...
1
vote
4
answers
292
views
How to sort and filter images by modification time and view them in nsxiv on FreeBSD
I am using nsxiv to view JPEG and PNG images on FreeBSD. However, I am unable to find a way to sort and filter the images by modification time to view them in nsxiv on FreeBSD. On Linux with GNU ...
0
votes
3
answers
221
views
How can I sort the lines in this text file?
I have a text file with book name and chapter in the bible. I would like to sort the lines so that
lines are first sorted according to the order between their books, the lexical order between the ...
0
votes
0
answers
19
views
reading into variables from "piped" data [duplicate]
I have a csv (Fieldseparator is the ',') file with data in it and I would have to prepare the csv before reading it (mainly just sort it) into variables. How can I read the sorted stream into ...
1
vote
3
answers
123
views
ls starting at particular file
How do I ls files in a directory beginning with a particular filename?
For example, if my CWD has these files:
$ ls -1
alpha.mp3
bar.xls
file1.txt
file2.txt
foo.odt
testing3.txt
How do I print out ...
0
votes
1
answer
44
views
Pipe null terminated file paths twice to same output, but second time sorted by basename
So, let's say I have a script that uses find to print paths with null-terminated filenames.
I also want to print another version of the output where each path is sorted by its basename. I want to ...
1
vote
1
answer
213
views
Group IP addresses belonging to the same /24 network
I have a file containing a list of both IPv4 addresses and /24 networks.
The IP addresses are already unique and sorted by all four octets.
I would like to use bash to group the addresses that belong ...
3
votes
3
answers
415
views
MAC OSX / Bash Problem - Sorting strings as a date
I have file output in a CSV file I want to sort by date time but the date time appears as below, with the time and year appearing in a different order.
Using this command to produce the file listing:
...
0
votes
3
answers
830
views
listing existing users sorted by date of creation using cut command
ASK: to use cut command to list all users to confirm a successful useradd.
ISSUE: The list was very long and I had to scroll through output to find user.
NEED: Sort by the date the user was created. ...