Linux sorting "ls -al" output by date - linux

I want to sort the output of the "ls -al" command according to date. I am able to easily do that for one column with command:
$ ls -al | sort -k6 -M -r
But how to do it for both collumn 6 and 7 simultaneously? The command:
$ ls -al | sort -k6 -M -r | sort -k7 -r
prints out results I do not understand.
The final goal would be to see all the files from the most recently modified (or v.v.).
Here is the attached example for the data to be sorted and the command used:

With sort, if you specify -k6, the key starts at field 6 and extends to the end of the line. To truncate it and only use field 6, you should specify -k6,6. To sort on multiple keys, just specify -k multiple times. Also, you need to apply the M modifier only to the month, and the n modifier to the day. So:
ls -al | sort -k 6,6M -k 7,7n -r
Do note Charles' comment about abusing ls though. Its output cannot be reliably parsed. A good demonstration of this is that the image you've posted shows the month/date in columns 4 and 5, so it's not clear why you want to sort on columns 6 and 7.

The final goal would be to see all the files from the most recently modified
ls -t
or (for reverse, most recent at bottom):
ls -tr
The ls man page describes this in more details, and lists other options.

You could try ls -lsa -it -r
sample
enter image description here

Related

how do I access or sort the files of a directory in another order that the alphabetical order?

I run the following commands in linux on a pdf file to convert its pages to image files. However, it runs twice over the pdf file
pdftoppm -H 700 -f 30 -l 40 -png rl.pdf top
pdftoppm -y 700 -f 30 -l 40 -png rl.pdf bottom
output would be (the list of output files):
bottom-001.png
bottom-002.png
top-001.png
top-002.png
However, I want to access and process them in the following order (for ffmpeg):
top-001.png
bottom-001.png
top-002.png
bottom-002.png
To reach this goal you may suggest another way for naming the output files or run another script on the output files to sort them out.
sort -n -t- -s -k2
Sort numerically using - as separator on the second field. Stable sort so that top is on top.
Alternatively sort the first field in reverse:
sort -t- -k2n -k1r
For example the following command:
echo 'bottom-001.png
bottom-002.png
top-001.png
top-002.png' | sort -t- -k2n -k1r
outputs:
top-001.png
bottom-001.png
top-002.png
bottom-002.png
Another solution in this case is adding a suffix (in alphabetical order) to the output files of each command and move them to a new directory:
pdftoppm -H 450 -f 30 -l 40 -png rl.pdf page
for file in *.png; do
mv "$file" "out/${file%.png}_a.png"
done
pdftoppm -y 700 -f 30 -l 40 -png rl.pdf page
for file in *.png; do
mv "$file" "out/${file%.png}_b.png"
done
Variants of sorting with ls command
$ ls --help
...
-r, --reverse reverse order while sorting
-S sort by file size, largest first
--sort=WORD sort by WORD instead of name: none (-U), size (-S),
time (-t), version (-v), extension (-X)
-t sort by modification time, newest first
-u with -lt: sort by, and show, access time;
with -l: show access time and sort by name;
otherwise: sort by access time, newest first
-U do not sort; list entries in directory order
-v natural sort of (version) numbers within text
-X sort alphabetically by entry extension

What does the following command do ls -la | sort | wc -l

When I run this command in terminal it displays a number.
The number changes when I mount another folder using cd command.
ls -la // List files in your current directory (hidden files are included thanks to the -a)
sort // Sort entries returned by the previous command
wc -l // Returns the number of lines returned by the previous command
«|» send the output of one command to the input of the next one

How to change ls -a in terminal

I did a lot of customization to bash recently, and I've noticed a small but frustrating problem. When I enter $ ls -a into my home directory it lists all the files and directories one per line rather than the normal two or three per line. What is particularly strange is that this does not happen with $ ls or $ ls -a into any other directory the output is standard.
It only happens with the -a flag when in my home directory. Has anyone else encountered this problem, and have any tips on solving it?
It sounds like you have a particularly long dotfile in your home dir.
ls shows files in columns, but if one filename is exceptionally long, it can only fit one column.
Here's a command that will show the longest filenames in your ls output:
ls -a | awk '{print length($0), $0}' | sort -n

first two results from ls command

I am using ls -l -t to get a list of files in a directory ordered by time.
I would like to limit the search result to the top 2 files in the list.
Is this possible?
I've tried with grep and I struggled.
You can pipe it into head:
ls -l -t | head -3
Will give you top 3 lines (2 files and the total).
This will just give you the first 2 lines of files, skipping the size line:
ls -l -t | tail -n +2 | head -2
tail strips the first line, then head outputs the next 2 lines.
To avoid dealing with the top output line you can reverse the sort and get the last two lines
ls -ltr | tail -2
This is pretty safe, but depending what you'll do with those two file entries after you find them, you should read Parsing ls on the problems with using ls to get files and file information.
Or you could try just this
ls -1 -t | head -2
The -1 switch skips the title line.
You can use the head command to grab only the first two lines of output:
ls -l -t | head -2
You have to pipe through head.
ls -l -t | head -n 3
will output the two first results.
Try this:
ls -td -- * | head -n 2

Linux: cat matching files in date order?

I have a few files in a directory with names similar to
_system1.log
_system2.log
_system3.log
other.log
but they are not created in that order.
Is there a simple, non-hardcoded, way to cat the files starting with the underscore in date order?
Quick 'n' dirty:
cat `ls -t _system*.log`
Safer:
ls -1t _system*.log | xargs -d'\n' cat
Use ls:
ls -1t | xargs cat
ls -1 | xargs cat
You can concatenate and also store them in a single file according to their time of creation and also you can specify the files which you want to concatenate. Here, I find it very useful. The following command will concatenate the files which are arranged according to their time of creaction and have common string 'xyz' in their file name and store all of them in outputfile.
cat $(ls -t |grep xyz)>outputfile

Resources