4 UNIX Basics

UNIX is a powerful, multitasking, multi-user operating system originally developed in the 1970s. It has since evolved into various flavors and derivatives, including popular Linux distributions and macOS. Here are some UNIX basics to get you started.

4.1 Installing a Unix-based Terminal

Here are some basic steps to install a Unix-based terminal or Unix-like environment on your computer.

  1. Determine your operating system: Unix-based terminals are available for multiple operating systems, such as Linux, macOS, and Windows. Choose the one that matches your operating system.

4.1.1 Install a Unix-like environment on Windows

  1. If you are using Windows, you can install a Unix-like environment by using Windows Subsystem for Linux (WSL). To install WSL, follow these steps:

    1. Open the Windows Start menu and search for Windows Features.
    2. Select Turn Windows features on or off.
    3. Scroll down and check the box for Windows Subsystem for Linux.
    4. Click OK and restart your computer when prompted.

4.1.2 Install a Unix-based terminal on MacOS

macOS comes with a built-in terminal application called Terminal. To open the Terminal app, go to the Applications folder, then the Utilities folder, and double-click on Terminal. You can also download other terminal applications, such as iTerm2 or Terminator.

4.1.3 Install a Unix-based terminal on Linux

Most Linux distributions come with a terminal application pre-installed. If not, you can use your package manager to install one. For example, on Ubuntu, you can open the terminal and run the following command to install the default terminal:

 sudo apt-get install gnome-terminal 
  1. Configure your Unix-like environment.
    Once you have installed a Unix-like environment, you may need to configure it to suit your preferences. This could include setting up your shell, configuring your prompt, installing additional tools, and more.

Note: It’s important to note that these steps may vary depending on your specific operating system and terminal application. Additionally, it’s important to follow any additional instructions provided by the terminal application’s documentation or installation guide.

4.2 Basic Operations

4.2.1 File System Hierarchy

Hierarchy Description
/ The top-level directory in the UNIX file system.
/home Contains user-specific files and directories.
/bin and /usr/bin Store essential system binaries (executable programs).
/sbin and /usr/sbin Store system administration binaries.
/etc Contains system-wide configuration files.
/var Stores variable data files, such as logs and databases.
/tmp Stores temporary files that are usually deleted upon system reboot.
/usr Contains user-installed applications and files shared among users.
/lib Store shared libraries and kernel modules.

4.2.2 Basic Commands

Command Description
pwd Print the current working directory.
ls List the contents of a directory.
cd Change the current working directory.
mkdir Create a new directory.
rmdir Remove an empty directory.
touch Create a new, empty file.
rm Remove files or directories.
cp Copy files or directories.
mv Move or rename files or directories.
cat Print the contents of a file to the console.
more and less View the contents of a file, one screen at a time.
grep Search for a pattern in files.
find Find files or directories based on various criteria.
man Display the manual page for a command.

4.2.3 Copying a file in the same directory

To create a copy of a file in the same directory in UNIX, you can use the cp command. Here’s an example:

 cp original_file.extension copy_of_file.extension 

4.2.4 Renaming a file in the same directory

To rename a file, you can use the mv command. Here’s an example:

 mv new_file.md virtual_env.md 

4.2.5 Redirection and Pipes

Operator Description
> Redirect the output of a command to a file, overwriting the file if it exists.
>> Append the output of a command to a file, creating the file if it doesn’t exist.
< Redirect the contents of a file to be used as input for a command.
\| Pipe the output of one command to be used as input for another command.

4.2.6 Permissions and Ownership

Permission/Ownership Description
chmod Change the access permissions of a file or directory.
chown Change the owner of a file or directory.
chgrp Change the group of a file or directory.
umask Set default permissions for newly created files and directories.

4.2.7 Process Management

Process Command Description
ps List currently running processes.
top or htop Display a dynamic view of system processes.
kill Terminate a process by its process ID (PID).
killall Terminate all instances of a specified process.
bg Send a process to the background.
fg Bring a background process to the foreground.

4.2.8 Networking

Command Description
ifconfig or ip Display network interface information.
ping Check network connectivity between two hosts.
traceroute Trace the route that packets take to reach a destination.
ssh Connect to a remote system via a secure shell.
scp Copy files between local and remote systems using a secure shell.
wget or curl Download files from the internet.

4.2.9 Archiving and Compression

Command Description
tar Create or extract archive files.
gzip and gunzip Compress and decompress files using the gzip format.
bzip2 and bunzip2 Compress and decompress files using the bzip2 format.

Here are some examples:

  1. Creating a .tar archive:

     tar -cvf archive_name.tar directory_to_archive 
  2. Compressing an archive with gzip:

     gzip archive_name.tar 

This will result in a compressed archive with the .tar.gz extension (also referred to as a “tarball”).

  1. Compressing an archive with bzip2:

     bzip2 archive_name.tar 

This will result in a compressed archive with the .tar.bz2 extension.

  1. Creating a compressed archive in one step using tar with gzip:

     tar -czvf archive_name.tar.gz directory_to_archive 
  2. Creating a compressed archive in one step using tar with bzip2:

     bzip2 archive_name.tar 

This will result in a compressed archive with the .tar.bz2 extension.

  1. Creating a compressed archive in one step using tar with gzip:

     tar -czvf archive_name.tar.gz directory_to_archive 
  2. Creating a compressed archive in one step using tar with bzip2:

     tar -cjvf archive_name.tar.bz2 directory_to_archive 
  3. Extracting a .tar archive:

     tar -xvf archive_name.tar 
  4. Decompressing a gzip compressed archive and extracting its contents:

     gunzip -c archive_name.tar.gz | tar -xvf - 
  5. Decompressing a bzip2 compressed archive and extracting its contents:

     bunzip2 -c archive_name.tar.bz2 | tar -xvf - 
  6. Extracting a gzip compressed archive in one step:

     tar -xzvf archive_name.tar.gz 
  7. Extracting a bzip2 compressed archive in one step:

     tar -xjvf archive_name.tar.bz2 

These archiving and compression commands allow you to create, compress, and extract archive files on UNIX-like systems. Make sure to consult the man pages or online resources for more information on available options and best practices for each command.

4.2.10 Text Editing

Text Command Description
nano A beginner-friendly, easy-to-use text editor.
vi or vim A powerful and feature-rich text editor with a steeper learning curve.
emacs Another powerful and extensible text editor, often used by experienced users.

4.2.11 Users and Groups

User and/or Group Command Description
useradd or adduser Create a new user account.
userdel Delete a user account.
passwd Change a user’s password.
groupadd Create a new group.
groupdel Delete a group.
gpasswd Add or remove users from a group.

4.2.12 Environment Variables

Command Description
export Set an environment variable for the current session.
unset Remove an environment variable from the current session.
env List all environment variables in the current session.
echo $VARIABLE Display the value of a specific environment variable.

4.3 Data Analytics Use Cases

The Chronic Kidney Disease (CKD) dataset contains data and information about 400 patients.

4.3.1 Example 1

What is the normal red blood cell (RBC) count?

$ cat CKD.csv | cut -d "," -f6 | grep "normal" | wc -l
248

4.3.2 Example 2

What is the red blood cell (RBC) count when bacteria is present?

$ cat CKD.csv | cut -d "," -f6,9 | grep "normal,present" | wc -l
18

4.3.3 Example 3

Copy entire records of this data to a new text file where the records are organized by age in descending order.

$ cat CKD.csv | sort -t "," -k1 -n -r  > ckd_sort_age_desc.txt

The new text file is located here.