Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Last updated: Thursday 25 September 2025 @ 18:06:06

Shell Commands

As said in the lecture shell commands....

To successfully make use of a terminal and to become competent, knowing the commonly used posix and gnu-core-utils packages are paramount. It is a tall order to learn every package, out there, for instance the nixos package manager has over 80,000 packages!

So below is a sample of useful packages to become familar with.


1. Navigating the File System

1.1 pwd (Print Working Directory)

Description: Displays the current directory.

pwd

Example:

  1. Open your terminal.
  2. Type pwd and press Enter.
  3. You will see the full path of the directory you’re currently in, like /home/user.

1.2 cd (Change Directory)

Description: Changes the working directory.

cd [directory]

Examples:

  1. To move to the /home directory:
    cd /home
    
  2. To go back to the previous directory:
    cd -
    

1.3 ls (List Directory Contents)

Description: Lists files and directories.

ls [options] [directory]

Examples:

  1. List all files in the current directory:
    ls
    
  2. List hidden files:
    ls -a
    
  3. Show detailed information (file size, permissions, etc.):
    ls -l
    

2. File Operations

2.1 cp (Copy Files)

Description: Copies files or directories.

cp [source] [destination]

Examples:

  1. Copy a file:
    cp file.txt /backup/file.txt
    
  2. Copy a directory:
    cp -r /source/dir /backup/dir
    

2.2 mv (Move/Rename Files)

Description: Moves or renames files.

mv [source] [destination]

Examples:

  1. Move a file:
    mv file.txt /backup/file.txt
    
  2. Rename a file:
    mv oldname.txt newname.txt
    

2.3 rm (Remove Files)

Description: Deletes files or directories.

rm [options] [file/directory]

Examples:

  1. Remove a file:
    rm file.txt
    
  2. Remove a directory:
    rm -r /path/to/directory
    

3. File Permissions

3.1 chmod (Change File Permissions)

Description: Changes permissions of a file or directory.

chmod [options] [permissions] [file/directory]

Examples:

  1. Make a file executable:
    chmod +x script.sh
    
  2. Set specific permissions (e.g., read-write for owner, read for others):
    chmod 644 file.txt
    

3.2 chown (Change File Ownership)

Description: Changes the owner of a file or directory.

chown [owner][:group] [file/directory]

Examples:

  1. Change owner:
    chown newuser file.txt
    
  2. Change owner and group:
    chown newuser:newgroup file.txt
    

4. Searching and Finding Files

4.1 find (Search for Files)

Description: Searches for files in a directory hierarchy.

find [path] [options]

Examples:

  1. Find all .txt files in the current directory:
    find . -name "*.txt"
    
  2. Find files modified in the last 7 days:
    find /path -mtime -7
    

4.2 grep (Search Text in Files)

Description: Searches for a pattern in files.

grep [options] [pattern] [file]

Examples:

  1. Search for "hello" in a file:
    grep "hello" file.txt
    
  2. Search recursively in a directory:
    grep -r "hello" /path/to/directory
    

5. Disk Usage

5.1 df (Disk Free)

Description: Displays disk space usage.

df [options]

Example:

  1. Check disk usage in human-readable format:
    df -h
    

5.2 du (Disk Usage)

Description: Shows the size of files and directories.

du [options] [file/directory]

Example:

  1. Check the size of a directory:
    du -sh /path/to/directory
    

6. Process Management

6.1 ps (Process Status)

Description: Shows information about active processes.

ps [options]

Example:

  1. Show all running processes:
    ps aux
    

6.2 kill (Terminate Processes)

Description: Sends a signal to a process (usually to terminate it).

kill [options] [process_id]

Example:

  1. Kill a process with PID 1234:
    kill 1234
    

7. Networking

7.1 ping (Check Network Connectivity)

Description: Sends ICMP echo requests to test network connectivity.

ping [host]

Example:

  1. Ping Google to check internet connectivity:
    ping google.com
    

7.2 ifconfig / ip (Network Interface Configuration)

Description: Displays or configures network interfaces.

ifconfig [interface]

or

ip a

Example:

  1. Check the network interface configuration:
    ifconfig
    

or

ip a

8. Archiving and Compression

8.1 tar (Create and Extract Archives)

Description: Creates or extracts archives.

tar [options] [archive] [file/directory]

Examples:

  1. Create a .tar.gz archive:
    tar -czvf archive.tar.gz /path/to/directory
    
  2. Extract a .tar.gz archive:
    tar -xzvf archive.tar.gz
    

8.2 gzip / gunzip (Compress/Decompress Files)

Description: Compresses or decompresses files using gzip.

gzip [file]
gunzip [file.gz]

Examples:

  1. Compress a file:
    gzip file.txt
    
  2. Decompress a file:
    gunzip file.txt.gz
    

9. Text Processing

9.1 cat (Concatenate and Display Files)

Description: Displays file contents.

cat [file]

Example:

  1. Display the contents of a file:
    cat file.txt
    

9.2 head / tail (View Beginning or End of Files)

Description: Displays the first or last lines of a file.

head [file]
tail [file]

Example:

  1. Display the first 10 lines of a file:
    head file.txt
    
  2. Display the last 10 lines of a file:
    tail file.txt
    

10. Dates

  1. First run the date command in the terminal.

    $ date
    

    Output: will be something like this...

    Mon 27 Feb 13:58:41 GMT 2023
    
  2. So how do we get different formats?

    Well if in doubt you can check the man pages or use the commands --help flag

    $ date --help
    
  3. Looking at the output workout how to produce this format:

    > 20230227
    
    Answer
    date +"%Y%m%d"
    
  4. Now modify this output to display:

    > Year: 2023, Month: 02, Day: 27
    
    Answer
    date +"Year: %Y, Month: %m, Day: %d"
    
  5. The most common formatting characters:

    • %D – Display date as mm/dd/yy
    • %Y – Year (e.g., 2020)
    • %m – Month (01-12)
    • %B – Long month name (e.g., November)
    • %b – Short month name (e.g., Nov)
    • %d – Day of month (e.g., 01)
    • %j – Day of year (001-366)
    • %u – Day of week (1-7)
    • %A – Full weekday name (e.g., Friday)
    • %a – Short weekday name (e.g., Fri)
    • %H – Hour (00-23)
    • %I – Hour (01-12)
    • %M – Minute (00-59)
    • %S – Second (00-60)
  6. Using the above information and what previous steps reproduce the ISO 8601 standard for datetime.

    > 2023-02-27T12:08:45Z
    
    Answer
    $ date +"%Y-%m-%dT%H:%M:%S:%NZ"
    
    • %Y – Year (e.g., 2020)
    • %m – Month (01-12)
    • %d – Day of month (e.g., 01)
    • T – self delimeter
    • %H – Hour (00-23)
    • : – self delimeter
    • %M – Minute (00-59)
    • : – self delimeter
    • %S – Second (00-60)
    • Z – self delimeter
  7. Amend the last command so that after seconds you get nano seconds.

    > 2023-02-27T12:14:03:281999558Z
    
    Answer
    $ date +"%Y-%m-%dT%H:%M:%S:%NZ"
    
    • %Y – Year (e.g., 2020)
    • %m – Month (01-12)
    • %d – Day of month (e.g., 01)
    • T – self delimeter
    • %H – Hour (00-23)
    • : – self delimeter
    • %M – Minute (00-59)
    • : – self delimeter
    • %S – Second (00-60)
    • : – self delimeter
    • %N – nanoseconds (000000000..999999999)
    • Z – self delimeter

11 Manipulating inputs and outputs

  1. working with the previous command lets strip out the T,:s and Z so that we are left with only numnbers.

  2. Using awk we can manipulate the output by piping | the output of date into awk - first run this command

    $ date +"%Y-%m-%dT%H:%M:%S:%NZ" | awk "{print $1}" 
    > 2023-02-27T12:20:40:005619503Z   
    
    • awk with the argument print $1 will print the first argument... ie the output of date
  3. Now lets remove the T

    $ date +"%Y-%m-%dT%H:%M:%S:%NZ" | awk -F 'T' '{print $1}'
    > 2023-02-27
    
  4. What happens when you do $2 on the same command as above?

    Answer
    > 12:50:48:341942600Z
    

    This is because the input has been split and now

    • $1 = 2023-02-27
    • $2 = 12:20:40:005619503Z
  5. Change the delimeter -F 'T' to -F ':' and repeat both steps as before:

    Answer
    $  date +"%Y-%m-%dT%H:%M:%S:%NZ" | awk -F ':' '{print $1}'
    >  2023-02-27T12
    
    $ date +"%Y-%m-%dT%H:%M:%S:%NZ" | awk -F ':' '{print $2}'
      55
    $ date +"%Y-%m-%dT%H:%M:%S:%NZ" | awk -F ':' '{print $3}'
      32
    $ date +"%Y-%m-%dT%H:%M:%S:%NZ" | awk -F ':' '{print $4}'
      473072761Z
    
  6. You can put these together using the print {$2$3$4$}, try. sh date +"%Y-%m-%dT%H:%M:%S:%NZ" | awk -F ':' '{print $2$3$4}' 5719707652042Z

  7. Experiment with this and consider piping with awk to see if you can get:

    20230227130026355420256
    
    Answer
    $ date +"%Y-%m-%dT%H:%M:%S:%NZ" | awk -F '[-T:Z]' '{print $1$2$3$4$5$6$7$8$9}'
    20230227130026355420256