System & Meta

System

uname -a (display all info about the system like OS, CPU arch, etc…)

$ uname -a
Linux fedora 5.14.0-60.fc35.aarch64 #1 SMP Mon Aug 30 16:30:42 UTC 2021 aarch64 aarch64 aarch64 GNU/Linux

uptime (shows how long the system is running for)

date (Mon Sep 26 18:59:15 IST 2022)

cal (prints the calendar of current month highlighting the current date)

$ cal -y		print cal for full current year
$ cal -3		print cal for prev, current, and next month

whoami (prints current user’s username)

groups (list current user’s groups)

id (show current user’s UID and all GIDs)

ps (disaply all running processes)

whereis <command> (lists all command executables for a command on the system)

which <command> (shows path to the specified command’s default executable)

whatis <command> (shows a small description of specified command from man pages)

$ whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz

$ which ls
ls: /usr/bin/ls

$ whatis ls
ls (1) - lists directory contents

Meta

env (show environment valriable; shows all if no parameter is supplied)

$ env $HOME

$ TEST=foo 		creating a variable (only for current shell) (session-bound lifetime)
$ echo $TEST	prints "foo"

export TEST=foobar (create an env variable and we can use it in all the currently runnning programs/shell) (session-bound lifetime)

alias unalias (set alias for commonly used commands; erased after terminal restart (session-bound lifetime)

$ alias GOFOO="cd /a/b/c/foo"

$ unalias GOFOO

logout exit

clear (clears terminal)

history <n> (list last n used commands)

Run last used command with !!, run command number 69 with !69 (command number can be found with history command)

reverse-i-search (Ctrl+R) : Hit Ctrl+R to go one step back in the search results; forward control varies by system. Use Ctrl+O to run command the command found in the reverse search.

Help

man <command> (opens man1 page of the command; often as less pager)

help <command>

command --help

Redirection and Pipelines

Redirection

Read from/write to files other than /dev/stdin or /dev/stdout

  • Input redirection (<): read from a specified file instead of stdin
  • Output redirection (>): write to a specified file insted of stdout
$ echo "Hello" > file.txt

$ echo "World" > file.txt		(> will overwrite everything in file)

$ echo "World" >> file.txt		(>> appends to file) 


$ cat < foo.txt > bar.txt 		(data goes from "foo" to cat command and then to "bar")

								(LR associative since leftmost must be a command and it needs parameters to run)

stderr: whenever a program produces an error message, it is expected that it writes to /dev/stderr.

In the terminal, commands write to stderr upon error, and to the “finalstdout directly (skipping to the end of any pipeline that exists).

Only stdout of current command is written to stdin of the next command in the pipeline, not the stderr.

We need to use file descriptor (2) in order to interact with stderr.

stdin	0
stdout 	1
stderr	2
# write stderr to a file
$ cd /nosuchdir 2>myerr.txt

# write stderr to next stdout (piping stderr)
$ cd /nosuchdir 2>&1

# write both stdout and stderr to a file
$ cd /nosuchdir > all-output.txt 2>&1

In the third command above, redirection is processed from left to right, stdout is redirected to all-output.txt and then stderr is redirected to stdout, but the latter is already going into our file so both stdout and stderr go into file. Nothing is printed on the screen.

Piping

Use | to channel the output of one command as the input to another command

$ ls | less			output of ls becomes input of less command

$ ls | file.txt		won't work since we are trying to channel to a file
					(piping differs from redirection in this way)

Tee-ing: pipe to tee command and we can write to stdout (implicit) as well as another file that we specify (explicit) simultaneously

$ ls | tee file.txt

xargs used to pass output of one command to the input parameter of another, used along with pipe (|)

# filenames from ls's output will goto grep as a parameter, and 'foo' will be searched INSIDE those files
$ ls | xargs grep 'foo'

# filenames from ls's output will goto stdin, and 'foo' will be searched in text (which are filenames themselves)
$ ls | grep 'foo'

Control Characters

Ctrl + C 		interrupt execution of currently running command
Ctrl + D 		end of transmission (terminate input)
Ctrl + V 		reserved (^V is printed on using it twice)
Ctrl + A 		jump to beginning of line
Ctrl + E 		jump to end of line
Ctrl + W 		erase word to the left

Use Shift+Ctrl+C/V to copy/paste text in terminal. Some terminals also use Shift+Home/Insert


  1. Several vendors document man pages, like https://www.kernel.org/doc/man-pages or https://linux.die.net/man ↩︎