ls fails to order files in desired way - sorting

I have a script that creates files with output_#.root where # is a number. When I do ls in the directory, it chooses to order the files in a weird way:
output_1.root
output_10.root
output_100.root
output_11.root
output_2.root
etc.
How do I make it order the files in the logical order 1, 2, 3, etc.

Your files are sorted by alphabetical order. It's normal behavior. If you want to sort them by numerical order, you can try this:
ls *.root | sort -k2 -t_ -n
This will split your result using _ as a separator, and order by numerical order -n based on the second field -k2.

If you are using ls from GNU coreutils you can use the version-sort switch:
ls -v
Create example files:
touch output_1.root output_10.root output_100.root output_11.root output_2.root
List them:
ls -1v
Output:
output_1.root
output_2.root
output_10.root
output_11.root
output_100.root

Related

Order by numbers with bash wildcards

I have a directory that contains
frame0.png
frame1.png
frame2.png
...
frame20.png
I want to use wildcards such that ls -l shows me the files ordered by the number. I tried
ls -l frame?.png frame??.png
because I thought it would first seach for the items with just one digit, order them and then do the same with two digits but, the output is
frame0.png
frame10.png
frame11.png
...
frame1.png
frame20.png
frame2.png
...
frame9.png
How can I circumvent that bash orders them like that?
If you have gnu utilities then use -v option to get natural sort:
ls -lv frame*.png
If you don't have gnu ls then try this find + sort:
find . -maxdepth 1 -name 'frame*.png' | sort -V

ls -S / ls -r Sorting on the command line (Bash) doesn't make sense

I'm going through this tutorial on the command line. http://lifehacker.com/5633909/who-needs-a-mouse-learn-to-use-the-command-line-for-almost-anything
It mentioned I could type ls -S to sort the results by file size (I assumed it meant from largest to smallest) and that I could type ls -r to sort the results the other way (smallest to largest)
I immediately tried those commands on my home directory, but in both cases the same file tweets.csv was the first file listed.
Since tweets.csv can't possibly be both the largest file and the smallest file I'm very confused.
In case it helps in my home directory I have a bunch of other folders, and other file types such as another .csv, .txt, .py, .bash and .sh. I'm also running OSX El Captain version 10.11.1
So why don't ls -S and ls -r give me opposite results? Did I misunderstand them?
I'd appreciate any help. Thanks!
Two options need two letters:
ls -rS
To which you can add long to get:
ls -lrS
The order is usually of no impotance, so -lrS and -rSl mean the same, as using them split in separate values:
ls -r -S -l
That is, each option is activated by its presence.
You could sort by date, and use the -r to reverse the output:
ls -lc
ls -rlc
-r reverses the sort order specified. If you don't specify any order, the files are sorted by name. ls -r sorts them by name, Z to A. To sort them by size from the largest to the smallest, use
ls -Sr
Adding -l will display file sizes, too, so you can verify the files are sorted correctly.

Stable sorting two files into one with the duplicates

I've been trying to sort two files and get the output.
say for file 1:
102310863||7097881||6845123||271640||06007709532577||||
102310875||7092992||6840818||023740||10034500635650||||
and file 2:
102310863||7097881||6845193||271640||06007709532577||||
102310875||7092992||6840808||023740||10034500635650||||
The desired output is:
102310863||7097881||6845123||271640||06007709532577||||
102310863||7097881||6845193||271640||06007709532577||||
102310875||7092992||6840818||023740||10034500635650||||
102310875||7092992||6840808||023740||10034500635650||||
I've been trying to use the sort command
sort -t \| -n -k1,1 t1.txt t2.txt
but it is giving me the output
102310863||7097881||6845123||271640||06007709532577||||
102310863||7097881||6845193||271640||06007709532577||||
102310875||7092992||6840808||023740||10034500635650||||
102310875||7092992||6840818||023740||10034500635650||||
which is not what I want because original file order is not preserved.
Is there any other way of doing it to get the desired output?
Using the -s flag performs a stable sort.
sort -s -t \| -k1,1 t1.txt t2.txt
From man sort:
-s, --stable
stabilize sort by disabling last-resort comparison

sorting file names ascending where names have a dash in bash

I have a list of files in a folder.
The names are:
1-a
100-a
2-b
20-b
3-x
and I want to sort them like
1-a
2-b
3-x
20-b
100-a
The files are always a number, followed by a dash, followed by anything.
I tried a ls with a col and sort and it works, but I wanted to know if there's a simpler solution.
Forgot to mention: This is bash running on a Mac OS X.
Some ls implementations, GNU coreutils' ls is one of them, support the -v (natural sort of (version) numbers within text) option:
% ls -v
1-a 2-b 3-x 20-b 100-a
or:
% ls -v1
1-a
2-b
3-x
20-b
100-a
Use sort to define the fields.
sort -s -t- -k1,1n -k2 filenames.txt
The -t tells sort to treat - as the field separator in input items. -k1,1n instructs sort to first sort on the first field numerically; -k2 sorts using the remaining fields as the second key in cade the first fields are equal. -s keeps the sort stable (although you could omit it since the entire input string is being used in one field or another).
(Note: I'm assuming the file names do not contain newlines, so that something like ls > filenames.txt is guaranteed to produce a file with one name per line. You could also use ls | sort ... in that case.)

How to sort the output obtained with grep -c?

I use the following 'grep' command to get the count of the string alert in each of my files at the given path:
grep 'alert' -F /usr/local/snort/rules/* -c
How do I sort the resulting output in desired order- say ascending order, descending order, ordered by name, etc. An answer specific to these cases is sufficient.
You may freely suggest a command other than grep as well.
Pipe it into sort. Assuming your filenames have no colons, use the "-t" option to specify the colon as field saparator. Use -n for numerical sorting.
Example:
grep 'alert' -F /usr/local/snort/rules/* -c | sort -t: -n -k2
should split lines into fields separated by ":", use the second field for sorting, and treat this as numbers (so 21 is actually later than 3).

Resources