Posted: . At: 11:31 AM. This was 1 year ago. Post ID: 17835
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


How to print ASCII art with Linux and other useful Linux tips.


Printing ASCII art with the bash shell is very useful. This is actually very easy.

goku.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash
 
cat << "EOF"
 
⣿⣿⠿⠿⠿⠿⣿⣷⣂⠄⠄⠄⠄⠄⠄⠈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣷⡾⠯⠉⠉⠉⠉⠚⠑⠄⡀⠄⠄⠄⠄⠄⠈⠻⠿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⡀⠄⠄⠄⠄⠄⠄⠄⠄⠉⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿
⠄⠄⠄⠄⠄⠄⠄⠄⠄⢀⠎⠄⠄⣀⡀⠄⠄⠄⠄⠄⠄⠄⠘⠋⠉⠉⠉⠉⠭⠿⣿
⡀⠄⠄⠄⠄⠄⠄⠄⠄⡇⠄⣠⣾⣳⠁⠄⠄⢺⡆⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄
⣿⣷⡦⠄⠄⠄⠄⠄⢠⠃⢰⣿⣯⣿⡁⢔⡒⣶⣯⡄⢀⢄⡄⠄⠄⠄⠄⠄⣀⣤⣶
⠓⠄⠄⠄⠄⠄⠸⠄⢀⣤⢘⣿⣿⣷⣷⣿⠛⣾⣿⣿⣆⠾⣷⠄⠄⠄⠄⢀⣀⣼⣿
⠄⠄⠄⠄⠄⠄⠄⠑⢘⣿⢰⡟⣿⣿⣷⣫⣭⣿⣾⣿⣿⣴⠏⠄⠄⢀⣺⣿⣿⣿⣿
⣿⣿⣿⣿⣷⠶⠄⠄⠄⠹⣮⣹⡘⠛⠿⣫⣾⣿⣿⣿⡇⠑⢤⣶⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣯⣤⣤⣤⣤⣀⣀⡹⣿⣿⣷⣯⣽⣿⣿⡿⣋⣴⡀⠈⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣾⣝⡻⢿⣿⡿⠋⡒⣾⣿⣧⢰⢿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⣏⣟⣼⢋⡾⣿⣿⣿⣘⣔⠙⠿⠿⠿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠿⣛⡵⣻⠿⠟⠁⠛⠰⠿⢿⠿⡛⠉⠄⠄⢀⠄⠉⠉⢉
⣿⣿⣿⣿⡿⢟⠩⠉⣠⣴⣶⢆⣴⡶⠿⠟⠛⠋⠉⠩⠄⠉⢀⠠⠂⠈⠄⠐⠄⠄⠄
 
EOF

This is how to print preformatted text in the terminal.

This is the easiest way to do this and this may be used to dress up your Linux prompt.

Here is a nice example.

┗━━━━━━━━━━┓ john@localhost ~/Documents
           ┗━━━━━━━━━━━━━╾ ╍▷ bash face.sh 
⣿⣿⡿⣫⣾⠏⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢀⣀⣀⣀⣀⠄⠄⠄⠄⠄⠄
⣿⡇⠱⠉⠁⠄⠄⠄⠄⠄⠄⢀⣀⣤⣶⣶⣿⣿⣿⣿⣿⣿⣿⣦⠄⠄⠄⠄⠄
⣿⡇⠄⠄⠄⠄⠄⢀⣠⣛⡩⣩⣭⡹⣿⣿⣿⣿⠞⣛⣛⣛⡲⣿⡇⠄⠄⠄⠄
⣿⡇⠄⠄⠄⡾⣡⣾⣿⣷⣹⣿⣿⡿⣪⡻⠟⣱⣿⣿⣿⣿⣿⣷⡹⠄⠄⠄⠄
⣿⡇⠄⠄⣼⡇⣿⣻⣿⠟⡛⢿⣿⣾⣿⡇⢰⣍⢻⡿⠛⢿⣿⡭⣿⣷⠄⠄⠄
⣿⣧⣄⡀⣿⡇⣮⣽⣿⣮⣉⣾⣿⣿⣿⣇⡸⣿⣿⣆⠛⣰⣿⣾⡿⣿⠄⠄⠄
⣿⣇⡼⣄⣿⣿⡄⠙⢿⣏⣿⣿⡮⠁⣉⣾⣷⡈⠃⢿⣿⣬⡭⠝⣀⣿⠄⠄⠐
⡆⡇⣹⣿⣿⣿⣿⡿⠓⠛⣉⣉⣉⣉⣙⣛⠓⠾⣟⢿⣿⣿⣿⣿⣿⣿⣿⠇⠄⠙
⠁⡇⣞⣿⡿⠋⠁⠄⠄⠈⠉⠙⠛⠛⠻⠿⠿⠿⣶⣌⠻⣿⣿⣿⣿⣿⢗⢴⣆⢣
⠸⣇⡻⠈⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠈⢻⣷⡌⢿⣿⣿⣿⢸⠼⣣⣾
⣦⡀⠄⠄⠄⠄⠄⠄⠄⠄⠄⢀⠄⠄⠄⠄⠄⠄⠄⠄⠙⠛⠈⣿⡫⡼⢠⣾⣿⣿
⣿⣇⠄⣀⣠⡀⠄⠄⠴⠾⠿⠿⠶⠶⣦⣤⡀⠄⠄⠄⠄⠄⠄⢨⠯⢁⣿⣿⣿⣿
⣿⣿⣦⢒⠤⣅⡶⣶⣶⣾⣿⣿⣿⣷⣶⣮⣍⠢⠄⠄⠄⠄⠄⠐⢠⣾⣿⣿⣿⣿
⣿⣿⣿⣧⡐⠫⣉⡿⣬⡞⢿⣿⢯⠽⣶⡽⢟⣛⢖⣨⣛⠛⢃⣴⣿⣿⣿⣿⣿⣿

This would look very nice in a ~/.bashrc file.

Install the neofetch package to enable a lovely display of distribution information in the terminal.

┗━━━━━━━━━━┓ john@localhost ~
           ┗━━━━━━━━━━━━━╾ ╍▷ neofetch 
        #####           john@localhost 
       #######          -------------- 
       ##O#O##          OS: Rocky Linux 9.1 (Blue Onyx) x86_64 
       #######          Kernel: 5.14.0-162.22.2.el9_1.x86_64 
     ###########        Uptime: 2 hours, 50 mins 
    #############       Packages: 1753 (rpm) 
   ###############      Shell: bash 5.1.8 
   ################     Resolution: 1920x1080 
  #################     DE: GNOME 40.10 
#####################   WM: Mutter 
#####################   WM Theme: Adwaita 
  #################     Theme: Adwaita [GTK2/3] 
                        Icons: Adwaita [GTK2/3] 
                        Terminal: gnome-terminal 
                        CPU: Intel i5-4670K (4) @ 3.800GHz 
                        GPU: NVIDIA GeForce GTX 1050 
                        Memory: 1365MiB / 15652MiB 

                                                
                                                

This looks very nice when seen in color.

To get a nice process tree listing all of the processes owned by your user, use the pstree command.

┗━━━━━━━━━━┓ john@localhost ~
           ┗━━━━━━━━━━━━━╾ ╍▷ pstree -U john
gdm-x-session─┬─Xorg───{Xorg}
              ├─gnome-session-b─┬─ssh-agent
              │                 └─3*[{gnome-session-b}]
              └─2*[{gdm-x-session}]
 
gnome-keyring-d───3*[{gnome-keyring-d}]
 
sshd───bash
 
systemd─┬─(sd-pam)
        ├─at-spi-bus-laun─┬─dbus-broker-lau───dbus-broker
        │                 └─3*[{at-spi-bus-laun}]
        ├─at-spi2-registr───2*[{at-spi2-registr}]
        ├─chrome-gnome-sh───2*[{chrome-gnome-sh}]
        ├─dbus-broker-lau───dbus-broker
        ├─dconf-service───2*[{dconf-service}]
        ├─evolution-addre───5*[{evolution-addre}]
        ├─evolution-calen───8*[{evolution-calen}]
        ├─evolution-sourc───3*[{evolution-sourc}]
        ├─firefox─┬─3*[Isolated Web Co───21*[{Isolated Web Co}]]
        │         ├─Privileged Cont───19*[{Privileged Cont}]
        │         ├─Socket Process───5*[{Socket Process}]
        │         ├─3*[Web Content───14*[{Web Content}]]
        │         ├─WebExtensions───21*[{WebExtensions}]
        │         ├─chrome-gnome-sh───2*[{chrome-gnome-sh}]
        │         └─102*[{firefox}]
        ├─2*[gjs───6*[{gjs}]]
        ├─gnome-session-b─┬─evolution-alarm───5*[{evolution-alarm}]
        │                 ├─gnome-software───3*[{gnome-software}]
        │                 ├─gsd-disk-utilit───2*[{gsd-disk-utilit}]
        │                 └─3*[{gnome-session-b}]
        ├─gnome-session-c───{gnome-session-c}
        ├─gnome-shell─┬─audacious───5*[{audacious}]
        │             ├─gjs─┬─bash
        │             │     └─7*[{gjs}]
        │             ├─ibus-daemon─┬─ibus-dconf───3*[{ibus-dconf}]
        │             │             ├─ibus-engine-sim───2*[{ibus-engine-sim}]
        │             │             ├─ibus-extension-───3*[{ibus-extension-}]
        │             │             └─2*[{ibus-daemon}]
        │             └─13*[{gnome-shell}]
        ├─gnome-shell-cal───5*[{gnome-shell-cal}]
        ├─gnome-terminal-─┬─bash───pstree
        │                 └─3*[{gnome-terminal-}]
        ├─goa-daemon───3*[{goa-daemon}]
        ├─goa-identity-se───3*[{goa-identity-se}]
        ├─gsd-a11y-settin───3*[{gsd-a11y-settin}]
        ├─gsd-color───3*[{gsd-color}]
        ├─gsd-datetime───3*[{gsd-datetime}]
        ├─gsd-housekeepin───3*[{gsd-housekeepin}]
        ├─gsd-keyboard───3*[{gsd-keyboard}]
        ├─gsd-media-keys───3*[{gsd-media-keys}]
        ├─gsd-power───3*[{gsd-power}]
        ├─gsd-print-notif───2*[{gsd-print-notif}]
        ├─gsd-printer───2*[{gsd-printer}]
        ├─gsd-rfkill───2*[{gsd-rfkill}]
        ├─gsd-screensaver───2*[{gsd-screensaver}]
        ├─gsd-sharing───3*[{gsd-sharing}]
        ├─gsd-smartcard───5*[{gsd-smartcard}]
        ├─gsd-sound───3*[{gsd-sound}]
        ├─gsd-usb-protect───3*[{gsd-usb-protect}]
        ├─gsd-wacom───2*[{gsd-wacom}]
        ├─gsd-xsettings───3*[{gsd-xsettings}]
        ├─gvfs-goa-volume───2*[{gvfs-goa-volume}]
        ├─gvfs-gphoto2-vo───2*[{gvfs-gphoto2-vo}]
        ├─gvfs-mtp-volume───2*[{gvfs-mtp-volume}]
        ├─gvfs-udisks2-vo───3*[{gvfs-udisks2-vo}]
        ├─gvfsd─┬─gvfsd-trash───2*[{gvfsd-trash}]
        │       └─2*[{gvfsd}]
        ├─gvfsd-fuse───5*[{gvfsd-fuse}]
        ├─gvfsd-metadata───2*[{gvfsd-metadata}]
        ├─ibus-portal───2*[{ibus-portal}]
        ├─ibus-x11───2*[{ibus-x11}]
        ├─pipewire───{pipewire}
        ├─pipewire-pulse───{pipewire-pulse}
        ├─wireplumber───3*[{wireplumber}]
        ├─xdg-desktop-por───5*[{xdg-desktop-por}]
        ├─2*[xdg-desktop-por───3*[{xdg-desktop-por}]]
        ├─xdg-document-po─┬─fusermount
        │                 └─5*[{xdg-document-po}]
        └─xdg-permission-───2*[{xdg-permission-}]

Another very useful Linux tip.

get output like the ls -l command using C.

ls.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
 
/********************************************************************
* Description: A version of LS written in C.
* Author: John Cartwright <>
* Created at: Mon Apr  3 10:38:18 AEST 2023
* Computer: localhost.localdomain
* System: Linux 5.14.0-162.18.1.el9_1.x86_64 on x86_64
*
* Copyright (c) 2023 John Cartwright  All rights reserved.
*
********************************************************************/
 
#include <dirent.h>
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <limits.h>
 
void list_files(const char *path) {
    struct dirent *entry;
    struct stat file_stat;
    char full_path[PATH_MAX];
 
    DIR *dir = opendir(path);
 
    if (dir == NULL) {
        perror("opendir");
        return;
    }
 
    while ((entry = readdir(dir)) != NULL) {
        if (strcmp(entry->d_name, ".") == 0) {
            continue;
        }
 
        snprintf(full_path, sizeof(full_path), "%s/%s", path, entry->d_name);
 
        if (stat(full_path, &file_stat) < 0) {
            perror("stat");
            continue;
        }
 
        printf("%s", S_ISDIR(file_stat.st_mode) ? "d" : "-");
        printf("%s", (file_stat.st_mode & S_IRUSR) ? "r" : "-");
        printf("%s", (file_stat.st_mode & S_IWUSR) ? "w" : "-");
        printf("%s", (file_stat.st_mode & S_IXUSR) ? "x" : "-");
        printf("%s", (file_stat.st_mode & S_IRGRP) ? "r" : "-");
        printf("%s", (file_stat.st_mode & S_IWGRP) ? "w" : "-");
        printf("%s", (file_stat.st_mode & S_IXGRP) ? "x" : "-");
        printf("%s", (file_stat.st_mode & S_IROTH) ? "r" : "-");
        printf("%s", (file_stat.st_mode & S_IWOTH) ? "w" : "-");
        printf("%s", (file_stat.st_mode & S_IXOTH) ? "x" : "-");
        printf(" %ld ", file_stat.st_size);
        printf("%s\n", entry->d_name);
    }
 
    closedir(dir);
}
 
int main(int argc, char *argv[]) {
    if (argc == 1) {
        list_files(".");
    } else {
        printf("%s", argv[1]);
        list_files(argv[1]);
    }
 
    return 0;
}

This works very well indeed. The output is shown below.

┗━━━━━━━━━━┓ john@localhost ~/Documents
           ┗━━━━━━━━━━━━━╾ ╍▷ ./myls Books/
Books/drwxr-xr-x 4096 ..
-rwxrwxrwx 42240960 Chaos Daemons 6th Edition Codex - 2013.pdf
-rwxrwxrwx 3490574 DF30-TheVillageWithNoName.pdf
-rwxrwxrwx 49368906 Doctor Who Annual 1966.pdf
-rwxrwxrwx 49154799 Doctor Who Annual 1967.pdf
-rwxrwxrwx 54796484 Doctor Who Annual 1968.pdf
-rwxrwxrwx 48382732 Doctor Who Annual 1969.pdf
-rwxrwxrwx 51775051 Doctor Who Annual 1970.pdf
-rwxrwxrwx 27765053 Doctor Who Annual 1971.pdf
-rwxrwxrwx 38119593 Doctor Who Annual 1973.pdf
-rwxrwxrwx 39884575 Doctor Who Annual 1974.pdf
-rwxrwxrwx 39421292 Doctor Who Annual 1975.pdf
-rwxrwxrwx 30370876 Doctor Who Annual 1976.pdf
-rwxrwxrwx 44198182 Doctor Who Annual 1977.pdf
-rwxrwxrwx 31837591 Doctor Who Annual 1978.pdf
-rwxrwxrwx 31853626 Doctor Who Annual 1979.pdf
-rwxrwxrwx 28880923 Doctor Who Annual 1980.pdf
-rwxrwxrwx 29533024 Doctor Who Annual 1981.pdf
-rwxrwxrwx 7344144 Doctor Who Annual 1982.pdf
-rwxrwxrwx 35240338 Doctor Who Annual 1983.pdf
-rwxrwxrwx 29588831 Doctor Who Annual 1984.pdf
-rwxrwxrwx 29462308 Doctor Who Annual 1985.pdf
-rwxrwxrwx 30411007 Doctor Who Annual 1986.pdf
-rwxrwxrwx 54853920 Warhammer 40k - Codex - Chaos Daemons 6th.pdf
-rwxrwxrwx 165494 Bond 10 - The Spy Who Loved Me - Fleming_ Ian.epub
-rwxrwxrwx 247538 Bond 11 - On Her Majesty's Secret Servic - Fleming_ Ian.epub
-rwxrwxrwx 216078 Bond 12 - You Only Live Twice - Fleming_ Ian.epub
-rwxrwxrwx 169942 Bond 13 - The Man With The Golden Gun - Fleming_ Ian.epub
-rwxrwxrwx 192072 Bond 1 - Casino Royale - Fleming_ Ian.epub
-rwxrwxrwx 229788 Bond 2 - Live and Let Die - Fleming_ Ian.epub
-rwxrwxrwx 240786 Bond 3 - Moonraker - Fleming_ Ian.epub
-rwxrwxrwx 220069 Bond 4 - Diamonds Are Forever - Fleming_ Ian.epub
-rwxrwxrwx 255741 Bond 5 - From Russia with Love - Fleming_ Ian.epub
-rwxrwxrwx 225979 Bond 6 - Dr. No - Fleming_ Ian.epub
-rwxrwxrwx 266261 Bond 7 - Goldfinger - Fleming_ Ian.epub
-rwxrwxrwx 173711 Bond 8 - For Your Eyes Only - Fleming_ Ian.epub
-rwxrwxrwx 251915 Bond 9 - Thunderball - Fleming_ Ian.epub
-rwxrwxrwx 457582 Diamond Age - Stephenson_ Neal.epub
-rwxrwxrwx 627495 Harry Potter 7 - Harry Potter and the De - Rowling_ J.K_.epub
-rwxrwxrwx 279654 The Fountains of Paradise - Clarke_ Arthur C_.epub
-rwxrwxrwx 289435 Zodiac - Stephenson_ Neal.epub

That little program should be very useful to anyone who wants to code their own version of ls.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.