TLCL: 3 - Exploring the System

This chapter introduces command options and arguments, along with the file and less commands.

Typically a command will have one or more options, which are generally a single character with a - prefix (-a). It is important to remember that you can combine multiple short options (-abc). Commands also usually support long options, which is a word with a -- prefix (--all).

To see the options a command has, you can add --help to the end of the command and it will output something like the following (ls --help)

BusyBox v1.33.1 () multi-call binary.

Usage: ls [-1AaCxdLHRFplinshrSXvctu] [-w WIDTH] [FILE]...

List directory contents

    -1  One column output
    -a  Include entries which start with .
    -A  Like -a, but exclude . and ..
    -x  List by lines
    -d  List directory entries instead of contents
    -L  Follow symlinks
    -H  Follow symlinks on command line
    -R  Recurse
    -p  Append / to dir entries
    -F  Append indicator (one of */=@|) to entries
    -l  Long listing format
    -i  List inode numbers
    -n  List numeric UIDs and GIDs instead of names
    -s  List allocated blocks
    -lc List ctime
    -lu List atime
    --full-time List full date and time
    -h  Human readable sizes (1K 243M 2G)
    -S  Sort by size
    -X  Sort by extension
    -v  Sort by version
    -t  Sort by mtime
    -tc Sort by ctime
    -tu Sort by atime
    -r  Reverse sort order
    -w N    Format N columns wide
    --color[={always,never,auto}]   Control coloring

This is actually not the typically output of ls --help, and this is because the bash:5.1 docker image actually uses BusyBox under the hood. The only thing to take away from that statement is that BusyBox is another shell like BASH, though very minimal. Fortunately the BusyBox ls command contains examples of both short and long options. The following is the result of running ls -lh, which is the combination of the -l (displays long format) and the -h (in long format listings, display file sizes in human readable format) options.

total 56K    
drwxr-xr-x    1 root     root        4.0K Jan  6 20:20 bin
drwxr-xr-x    5 root     root         340 Jan 11 18:36 dev
drwxr-xr-x    1 root     root        4.0K Jan 11 18:36 etc
drwxr-xr-x    2 root     root        4.0K Nov 12 09:18 home
drwxr-xr-x    1 root     root        4.0K Jan  6 20:20 lib
drwxr-xr-x    5 root     root        4.0K Nov 12 09:18 media
drwxr-xr-x    2 root     root        4.0K Nov 12 09:18 mnt
drwxr-xr-x    2 root     root        4.0K Nov 12 09:18 opt
dr-xr-xr-x  313 root     root           0 Jan 11 18:36 proc
drwx------    2 root     root        4.0K Nov 12 09:18 root
drwxr-xr-x    2 root     root        4.0K Nov 12 09:18 run
drwxr-xr-x    2 root     root        4.0K Nov 12 09:18 sbin
drwxr-xr-x    2 root     root        4.0K Nov 12 09:18 srv
dr-xr-xr-x   13 root     root           0 Jan 11 18:36 sys
drwxrwxrwt    1 root     root        4.0K Jan  6 20:20 tmp
drwxr-xr-x    1 root     root        4.0K Jan  6 20:20 usr
drwxr-xr-x    1 root     root        4.0K Jan  6 20:20 var

When looking at a long listing format, it can be broken down like the following.

At first, file doesn’t seem like that useful of a command, since all it does is “Determine type of FILEs.” For a programmer though, this information is very useful, as it tells you things like if the program is statically or dynamically linked. It also indicates things, like which C library was used (/lib/, what CPU architecture the program was compiled for (x86-64), and if debugging was removed (stripped).

/usr/bin/file: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/, stripped

The less command is one of a few that are used to output the contents of a file. The following is the output of less /etc/passwd.

xfs:x:33:33:X Font Server:/etc/X11/fs:/sbin/nologin

Though vim has not been covered yet, less uses similar hotkeys (like j/k to move up/down, or / to search.

Though the filesystem of the bash:5.1 docker image is different, it still contains most of the core directories that are found on any other Unix-like system.

The last thing covered in this chapter are links. If we run the command ls -l /bin/sh, we get the following.

lrwxrwxrwx    1 root     root            12 Nov 12 09:18 /bin/sh -> /bin/busybox

The key thing to note here is /bin/sh -> /bin/busybox. This is a special kind of file called a symbolic link (also referred to as soft link or symlink). This allows a file to be referenced by multiple names, which comes in handy when you have different versions of the same program. There is another type of link, called a hard link, which is similar to a symbolic link, but they operate in different ways. Links are covered more in the next chapter.



#tlcl #shell #unix