Blog

15 Articles

Linux Bash/Terminal – Advanced – Redirect command output

Redirection Reference (PAL Series)

Update: These were written for my students while my role was a PAL (Peer Assisted Learning) Leader for my course, I intended to keep all the basic/intermediate commands necessary for terminal use in one place to ease the students into using Linux efficiently. I’ve kept them up just in case they’re of use to someone.

Linux makes use of 3 standard streams

  • Standard input – stdin
  • Standard output – stdout
  • Standard error – stderr

By default these stream operate with the terminal; writing to stdout or stderr will display text in the terminal for example, and stdin will read from the terminal by default too.

However, it is often very useful to redirect these streams. For example, what if we wanted to run a program and store all it’s output in a text file?

$ ls > listOutput.txt     # Redirect stdout to listOutput.txt
$ cat listOutput.txt      # Print the contents
include
Makefile
README.md
shaders
src
tracer.pro

Using > to redirect will completely overwrite whatever data was in listOutput.txt.

If we wanted to append to the file instead, we can use the >> operator.

$ echo "I'm at the end" >> listOutput.txt     # Append the result of the echo command
$ cat listOutput.txt                          # Print the contents
include
Makefile
README.md
shaders
src
tracer.pro
I'm at the end

What if we wanted to ignore errors from a program? (assuming errors are being printed to stderr, as they should. Some programs don’t always adhere though)

$ goMaya 2> /dev/null   # Run Maya and redirect stderr to the special device /dev/null, which silently ignores all input

By redirecting to /dev/null, we’re no longer printing stderr to the terminal and it is basically a way of silencing a stream.

This also applies to the other streams, a common technique to completely silence a program and run it in the background is

$ dropbox >/dev/null 2>/dev/null &;
# or
$ dropbox >/dev/null 2>&1 &    # This achieves the same effect and basically says, redirect stdout to /dev/null, and redirect stderr to stdout

Or perhaps you want to run a program silently, but append all of it’s errors to a log file

$ renderManager >/dev/null 2>>logfile.txt

Piping

An alternative to redirection is piping, which works better with joining multiple commands together. A good explanation is here

$ cat Makefile | wc -l    # Pipe the output of the cat command into wc, using it to count the number of lines in the file
508

Here’s a really good example.

Summary

Stream Redirect symbol File Descriptor
stdin < &0
stdout > &1
stderr 2> &2

In the shell what is 2>&1?

Linux Bash/Terminal – Basics – Files/Folders

File/Folder Quick Reference (PAL Series)

Update: These were written for my students while my role was a PAL (Peer Assisted Learning) Leader for my course, I intended to keep all the basic/intermediate commands necessary for terminal use in one place to ease the students into using Linux efficiently. I’ve kept them up just in case they’re of use to someone.

Making directories

Create a directory :

$ mkdir myDir

Create more than one directory at once :

$ mkdir dir1 dir2

Create subdirectory :

$ mkdir myDir/mySubdir

Create a subdirectory even if it’s parent does not exist :

$ mkdir -p i/don\'t/exist/yet/subdir

Creating files

Create an empty file

$ touch empty.txt

Create multiple empty files

$ touch hello.c world.c Makefile

Updating the timestamp for an existing file

$ ls -l output.exr 
-rw-r--r--. 1 tom tom 3291 Oct 30 21:23 output.exr
$ touch output.exr 
$ ls -l output.exr 
-rw-r--r--. 1 tom tom 3291 Nov 25 22:28 output.exr

Reading files

cat – Echo to terminal

$ cat hello.c # Echo the contents to the terminal
hello
world
hello
world
hello
world
hello
world
$

cat’s main purpose is actually to combine files into new files, but we can use it to print the contents too.
More info (the example uses redirection, covered here)

more – Scroll through large file (old utility)

$ more hello.c
hello
world
hello
world
--More-- 50%   # We can only move forward by pressing Enter, no scrolling back

less – Scroll through large file (modern utility, behaves like more but can scroll backwards too, use this because less is more )

$ less hello.c
hello
world
hello
world
hello.c       # We can move forward and back

What’s the difference?

Linux Bash/Terminal – Basics – Filesystem

Linux Filesystem Reference (PAL Series)

Update: These were written for my students while my role was a PAL (Peer Assisted Learning) Leader for my course, I intended to keep all the basic/intermediate commands necessary for terminal use in one place to ease the students into using Linux efficiently. I’ve kept them up just in case they’re of use to someone.

Explanation of the Linux file system (PAL Series)

It is important that you understand the basics of where Linux stores certain directories, especially when dealing with the terminal.

Filepath Description
/ Root directory (equivalent to C:\ on Windows)
/root Home directory of the root user
/home The location of all personal user account files
/home/<username> Your home directory, contains your personal files ( /home/tom, /home/i7245143, etc )
/bin, /usr/bin, usr/local/bin, /opt Contains most executable files/tools, in-depth explanation. These directories (and others) are often referenced in the PATH variable.
/lib, /usr/lib Contains system libraries
/net Network drives are mounted here on the NCCA (Bournemouth University) Linux machines (e.g. /net/w32305/transfer to access the transfer drive of lab machine 05 in room w323 )
/tmp Stores temporary files, these are cleared on every single boot (by default)
/var Contains variable contents that the operating system always expects to be writable, such as log files, cache, etc
/mnt Where devices get mounted (such as memory sticks or external drives)
/dev Devices can be accessed from here (Everything is a file)
/proc Contains kernel information like hardware temperatures (this is a virtual filesystem)

Linux Bash/Terminal – Basics – Navigation

Bash Navigation Reference (PAL Series)

Update: These were written for my students while my role was a PAL (Peer Assisted Learning) Leader for my course, I intended to keep all the basic/intermediate commands necessary for terminal use in one place to ease the students into using Linux efficiently. I’ve kept them up just in case they’re of use to someone.

Change directory

Navigate to directory in the current directory ( . represents the current directory )

$ cd subDir
#or
$ cd ./subDir

Navigate to a directory in the parent directory ( .. represents the parent directory )

$ pwd
 /home/tom/folder
$ ls
parentDir emptyDir
$ cd emptyDir
 
$ cd ..           #Change directory to parent
$ cd parentDir
#or
$ cd ../parentDir #Do it all in one go

Navigate to a subdirectory within your home directory ( ~ represents the home directory, the environment variable $HOME also works )

$ cd ~/Downloads
#or
$ cd $HOME/Downloads # You will see this more often in scripts,

Why use $HOME over ~/ in a shell script?

Print current directory

$ pwd             # Print working directory

List contents of the directory

$ ls              # List contents
$ ls -a           # List hidden files
$ ls -l           # Show more info
$ ls ~/Downloads  # Show contents of specific folder

Manual Pages ( very useful )

If you want to find out more arguments/information about the commands we use, look at the man command.
It works with every single linux utility and is often faster to use than Googling it.

$ man ls          # Load the manual page for 'ls', see what other options are available

Setting up SDL2 ( and SDL2image )

by tomminor 0 Comments

Update: These were written for my students while my role was a PAL (Peer Assisted Learning) Leader for my course, it is meant to be a simple how to on how to setup SDL2. I’m not sure if it teaches the ‘correct’ way, but I had trouble figuring this stuff out when I started so I decided to help them out. I’ve decided to keep it up just incase someone else will find it handy. It only covers Linux installation as it is only required that the class can setup their development environment on the university lab machines.

Linux

Install SDL2:

Download the .tar.gz file from the SDL2 download page.
$ mkdir -p ~/src   # It's good to keep all your source code in a consistent place
$ mv ~/Downloads/SDL2-2.0.3.tar.gz ~/src
$ cd ~/src
$ tar xvf SDL2-2-0.3.tar.gz
$ cd SDL2-2-0.3/"
$ ./configure           # Check dependencies and generate Makefile
$ make                  # Compile the library            
$ sudo make install     # Install the library

Install SDL2 image:

Download the .tar.gz file from the SDL2 image download page.
$ mv ~/Downloads/SDL2_image-2.0.0.tar.gz ~/src
$ cd ~/src
$ tar xvf SDL2_image-2-0.0.tar.gz
$ cd SDL2_image-2-0.0/
$ ./autogen.sh   # This may not be necessary, but it is sometimes required 
$ ./configure
$ make
$ sudo make install

Command Line Usage:

$ gcc mysdlprog.c -o sdlprog -lSDL2 -lSDL2_image $(sdl2-config --cflags --libs)

Note for 1st years
Jon has already setup your assignment .pro file to check sdl2-config, you don’t need to modify it once SDL is installed.