I need to catalog my installed applications prior to backing up and upgrading and encountered this issue trying to figure out how to trim ls
output, but the awk
applications suggested in other Ask Ubuntu questions didn't work at all.
What worked for me were suggestions to use cut
: I redirected test ls output to a text file and used a "ruler" in Sumblime Text to find the character column numbers
total 2060
1 2 3 4 5
123456789012345678901234567890123456789012345678901234567890
-rw-r--r-- 1 root root 291 2023-04-12 13:53 apport-gtk.desktop
-rw-r--r-- 1 root root 277 2023-04-12 13:53 apport-kde-mime.desktop
-rw-r--r-- 1 root root 3417 2018-04-14 03:38 assistant-qt5.desktop
-rw-r--r-- 1 root root 1066 2022-03-07 19:03 atom.desktop
-rw-r--r-- 1 root root 2683 2018-05-01 04:10 audio-recorder.desktop
...
and then filtered with cut -c30-, where the trailing hyphen extends rendering to the end of (variable length) lines.
$ dir -l --time-style=long-iso /usr/share/applications | cut -c30-
291 2023-04-12 13:53 apport-gtk.desktop
277 2023-04-12 13:53 apport-kde-mime.desktop
3417 2018-04-14 03:38 assistant-qt5.desktop
1066 2022-03-07 19:03 atom.desktop
2683 2018-05-01 04:10 audio-recorder.desktop
...
This "ruler check" is needed since ls
's field alignment is relative after the first access permissions field. For the specific issue here (isolating the day of month) we can identify columns 38 and 39 as our targets.
$ ls -l >> cutcheck.txt
$ cat cutcheck.txt
1 2 3 4 5
123456789012345678901234567890123456789012345678901234567890
total 480
-rwxrwx--- 1 root plugdev 112 Mar 12 17:36 cutcheck.txt
-rwxrwx--- 1 root plugdev 112 Mar 12 17:34 cutruler.txt
drwxrwx--- 3 root plugdev 32768 Mar 10 17:53 KDEAppLaucherConfig
-rwxrwx--- 1 root plugdev 195385 Mar 2 18:02 KDEAppLaucherMenuEditorHandbk.pdf
-rwxrwx--- 1 root plugdev 2202 Mar 11 18:30 KDEAppLauncherFiles.txt
-rwxrwx--- 1 root plugdev 40346 Mar 5 21:10 KDEApps.ods
-rwxrwx--- 1 root plugdev 90993 Mar 5 20:12 KDEMenuAppsDesktopFiles.html
$ ls -l | cut -c38-39
12
12
10
2
11
5
5
Once you know the "ballpark" character column values for your particular file system, you can issue succesive cut
commands to isolate what you need to extract.
The stat
command allows specifying particular ls
/dir
output fields, but the neat ls
column alignment is lost because of the variable field lengths. The application of awk
given in @Corey Whitaker's answer did work, but with the same loss of column alignment.
I think stat
is the better choice if the intention is direct import into a spreadsheet catalog because the field codes can be separated with commas for redirection to a CSV file.