The Command Line Interface Shell - clizarraga-UAD7/Workshops GitHub Wiki

The Command Line Interface Shell


The Command Line Interface (CLI)

The Command Line Interface is the standard way of interacting thru a shell (command line interpreter) with an Operating System (UNIX, Linux, MacOS,and others). There are several shell options in the Unix/Linux systems environments, which are used in diverse applications:

Screen Shot 2022-02-24 at 16 46 50

Many Linux systems users use /bin/bash, that comes configured by default. To find out which shell you are using in a terminal session type: echo $SHELL.

The Shell or command line interpreter, is the intermediary between the user and the operating system. It interprets what we type. If the typed command is not recognized by the interpreter it will generate a warning or help us complete the posible commands.

Screen Shot 2022-02-24 at 17 28 25

It's possible to change the Shell program, simply we can run a Shell on top of another. To finish a user session in that Shell, we type exit.

Most shells are equivalent, they have many characteristics in common. The differences are noted when we try to write Sell command scripts, called Shell Scripting which is done to automate repetitive tasks.


CLI Options

There are several options to access a CLI on a local computer:

  • Linux Terminal that is included in every Linux system.
  • MacOS Terminal included in every MacOS system, under Applications/Utilities.

If the previous local options are not available, there are cloud accessible CLI options:


Github Codespaces

Codespaces terminal within VSCode is a pre-configured, cloud-based, efficient, secure, and easy-to-use environment that is integrated with VSCode and uses a virtual machine, making it accessible from anywhere and requiring less resources on the user's computer.


Google Cloud Shell

Google Cloud Shell

(Image credit: Author, CC)

Google offers a Shell for a Command Line Interface. We can connect to it using a Google Account via a Web Browser. Recommended browsers are Firefox, Google Chrome, but does not exclude others (?).

The URL for launching a Google Cloud Shell is: https://shell.cloud.google.com

(You can watch an Introduction to Google Cloud Shell in this 6 min video by Diane K. Patterson).

Also, it is important that you read this Basic Tutorial about Google Cloud Shell Setup by Dianne K. Patterson.


CyVerse CLI

Cyverse VICE CLI

(Image credit: Author, CC)

If you are a CyVerse user, you can launch a CLI Cloud Shell instance, via the Discovery Environment Dashboard, under Apps, and then access it thru the Analyses option. This is an Ubuntu Linux terminal.

When you are done, type exit in the terminal, and then go to Analyses, select the running Cloud Shell and go to More actions and Terminate the Shell.


UA HPC Interactive Desktop

UA HPC Interactive Desktop

(Image credit: Author, CC)

If you are a UA HPC user, then you can access via web the ood.hpc.arizona.edu dashboard and request an Interactive Desktop, that offers you a virtual Centos 7 Linux system. Use the default values, specify your HPC group and launch the instance. You may need to wait in the queue, until the Desktop is ready and open it.

When you are done, you can select System/Shutdown the Interactive Desktop.


UNIX Shell

Unix Shell

The command line interface (CLI) or Shell, is inherited from the UNIX shell, and it includes a family of user and system administration commands.

In the following table we present a minimum set of commands to get started.

Command Description
man command1 Show manual entry for command1
command1 --help Will show the syntax on how to use command1
apropos keyword Search the manual pages and descriptions that include keyword
pwd Present working directory
cd Change to directory
cd or cd ~ Change to Home directory
cd .. Change to parent directory
cd - Change to previous visited directory
ls -al List all files in directory
ls -alR List all files recursively
cat file1 Concatenate file1 and display contents to screen
cat file1 file2 > file3 Concatenates files1 and file2 and writes to file3
cat file4 >> file3 Appends file4 contents to existing file3
cp Copy files and directories
cp file1 dir1 Make a copy of file1 into directory dir1
cp -r dir1 dir2 Copy recursively directory dir1 into directory dir2
mv Rename files
mv file1 new_file Renames file1 to new_file
rm Remove or delete files (Use with Caution)
rm file1 Removes file1 permanently
rm -r dir1 Removes the directory dir1 and all its contents permanently
rmdir Removes an empty directory
history Show command line history
clear Clear the terminal screen
touch newfile Create an new empty file if it does not exist
head -n 5 filename Show the first 5 lines of a file
tail -n 5 filename Show the last 5 lines of a file
more filename Show the contents of a file, one page at a time in the forward direction (SPACE BAR). Type 'q' to quit.
less filename less is opposite of more. Show the contents of a file, one page at a time, forward (SPACE BAR) and backward ('u'). To end of file ('G'), and beginning of file ('p'). Type 'q' to quit. The manages uses less`to show the manual contents.
wc filename Prints number of lines, words and characters in a text file
find ./ -name 'a*.txt' -print To search for files in the file hierarchy

Other useful commands

Command Description
echo Display a line of text
echo abcd > file1 Echo abcd and write to file1
echo $SHELL Display the value of the SHELL variable
echo $PATH Display PATH environment variable
file Return the type of file
tar cvf dir1.tar dir1 Archive a whole directory and contents
tar xvf dir.tar Will unarchive tar file and restore its contents and directory structure
gzip file.tar Will produce compressed file.tar.gz
gunzip file.tar.gz Will uncompress file and return file.tar

Special characters

The UNIX/Linux Shell handles special symbols:

 `~`  the Home directory
 `.`  the current working directory
 `..` the parent directory
 `*`  wildcard that matches any character
 `?`  wildcard that matches exactly one character 
 TAB  will autocomplete command/filename, given initial sequence of characters

Taking advantages of cursor motion in terminal

One of the advantages of working with these Shells is that, they inherit the some editing commands from the GNU Emacs Editor. This allows us to move our cursor to move and "edit" the command line text.

For completeness we summarize these commands, because we will be using them extensively:

Command Action
Positioning
Ctrl+f Moves the cursor one character forward
ESC, f Moves the cursor one word Forward
Ctrl+b Moves the cursor one character backward
ESC, b Moves the cursor one word Backward
Ctrl+a Moves the cursor to the beginning of line
Ctrl+e Moves the cursor to the end of line
Ctrl+p Moves the cursor to the previous line in commands history
Ctrl+n Moves the cursor to the next line in commands history
Memory buffer
Ctrl+k Sends contents of right region after cursor to memory (a.k.a. Kill. Memory keeps only last contents if overwritten)
Ctrl+y Flushes the contents in memory into cursor position (a.k.a. Yank)

Having these commands in mind, will ease our job of command line editing when needed.

Also, take advantage of the word completion capability offered by the Shell, for example in the case of long file/directory names.


File ownership & permissions.

File permissions
Command Description
chown username filename Change ownership of a file
chgrp group name filename Change group name of a file
chmod +x filename Making a file executable
or chmod 755 filename
📝 More on file permissions: Click to expand!

File permissions

When executing a list file command: ls -al, a list of 10 characters (octal bits) will appear at the beginning of a line.

drwxr-xr-x   5 user  group       160 Oct 11 09:37 dir1
-rw-r--r--   1 user  group      7080 Apr 15 19:20 TheRaven.txt
-rwxr-xr-x   1 user  group        92 Oct 11 09:54 script.sh

they are related with file type and permissions.

  • The first character defines if it is a directory ('d'), or an ordinary file ('-').
  • The next 3 characters (2nd thru 4th), defines if the user has permissions to read it ('r'), write or modify it ('w'), and execute it ('x'). A missing permission is denoted by the empty character ('-').
  • The following 3 characters starting at the 5th one, defines the permissions to the group the user belongs to.
  • The last 3 characters starting at the 8th place defines the permissions to other (the rest of the world).

To turn on/off the permission octal bits, use the chmod command.

Numeric octal bits (0-7):

7 = 2^2 + 2^1 + 2^0 = 4 + 2 + 1 -> ('rwx')
6 = 2^2 + 2^1 +   0 = 4 + 2 + 0 -> ('rw-')
5 = 2^2 +   0 + 2^0 = 4 + 0 + 1 -> ('r-x')
4 = 2^2 +   0 +   0 = 4 + 0 + 0 -> ('r--')
0 =   0 +   0 +   0 = 0 + 0 + 0 -> ('---')

Examples

  1. Text files: chmod 644 filename, gives 'rw' permission to owner, 'r' permissions to the group and other ('-rw-r--r--')
  2. Text files: chmod 600 filename, the owner has 'rw' permissions. No one else can read, write or execute that file.
  3. Executable file: chmod 755 filename, gives 'rwx' permissions to owner, 'r-x' permissions to the group and other ('-rwxr-xr-x')
  4. Executable file: chmod 700 filename, the owner has 'rwx' permissions. No one else can read, write or execute that file.

In UNIX/Linux files and directories are equivalent. In the case of a directory: 'r' means you can read the contents of a directory, 'w' means you can create/delete files in that directory and 'x' means you can enter into a directory.

By default, the system will create directories with '755' permissions and regular files with '644' permissions.

⚠️ WHAT NOT TO DO: chmod 000 filename. You will deny all 'rwx' permissions to yourself and everyone else.

Process/Job control

Command Description
exit Finish user session in terminal
Ctrl-C Abort job execution
Ctrl-Z Suspend job execution
jobs -l List jobs of current session
fg %Number Run job in foreground
ps ax List system processes
ps u List user processes
program & Execute program in background
`kill -9 PID Terminate process with ID number
kill %1 Will kill the first process in background mode (use jobs -l to list jobs)
w Show who is connected and what process is running, uptime, CPU load
who Show who is logged in
top Show top processes running
printenv Prints user environment variables

Other utilities

Command Description
df -h Available system disk space, human readable
du -h Approximate disk use of current directory, human readable
cal 10 2022 Displays a calendar for October 2022
date Displays system date and time
bc -il (ENTER) Arbitrary precision calculator

Text editors.

If you need to create/edit/modify a text file or source code, most systems include a family of basic editors.

Please explore the possible text editors included in your system:


Resources

Online Help

Other resources


Created: 02/24/2022 (C. Lizárraga); Updated: 05/14/2023 (C. Lizárraga).

CC BY-NC-SA

⚠️ **GitHub.com Fallback** ⚠️