Navigation Using the Command-Line COP-3402 (2024)

Table of Contents

  • Getting started
    • Using ssh
    • Logging in to eustis
    • Ctrl-L to clear your screen
  • Navigating the File System
    • Running commands
    • Where am i?
    • What's here?
    • Running commands with arguments
    • Go to a new directory
    • Go the parent directory
    • Go to a subdirectory
  • Making the command-line fast
    • Tab-completion
    • Editing commands
    • Rerunning commands
  • Viewing files
    • cat command
    • less command
  • Downloading and untarring files
  • Editing the file tree
    • Removing non-empty directories?
  • Review
    • Orienting yourself
    • Moving around
    • Tab completion
    • History
    • Modifying the file tree

Getting started

Using ssh

See OS-specific guides in the syllabus

Logging in to eustis

ssh NID@eustis.eecs.ucf.edu

Then enter your password

You can create an ssh key and add it to eustis's .ssh/authorized_keys to login without a password.

# if you haven't already generated a key, run ssh-keygenssh-keygen# accept the default location (as long as you haven't already generated# now let's tell eustis about your public key (be sure to use .pub!)# linux/maccat ~/.ssh/id_rsa.pub | ssh NID@eustis.cecs.ucf.edu 'cat >> .ssh/authorized_keys'# windows (https://superuser.com/questions/1747549/alternative-to-ssh-copy-id-on-windows)type %USERPROFILE%\.ssh\id_rsa.pub | ssh NID@eustis.cecs.ucf.edu "cat >> .ssh/authorized_keys"# windows alternative (rsa_id.pub instad of id_rsa.pub)type %USERPROFILE%\.ssh\rsa_id.pub | ssh NID@eustis.cecs.ucf.edu "cat >> .ssh/authorized_keys"

Ctrl-L to clear your screen

Navigating the File System

Running commands

Type its name and hit <enter>

Where am i?

Print Working Directory: pwd

What's here?

LiSt the directory: ls

Running commands with arguments

Type its name, a space, then the argument.

Arguments are separate by spaces.

What if the argument has a space in it?

  • escape the space with a backslash ~\ ~
  • Use double-quotes (evaluates the contents, e.g., for bash variables and escape sequences)
  • Use single-quotes (does not evaluate contents or escape sequences)

Go to a new directory

Change (working) directory: cd path

cd /usr/include

What kind of path is this, relative or absolute?

Use eustis's /usr/include as an example file hierarchy

These are where system-wide C header files are installed

Go the parent directory

cd ..

What is the .. directory?

Where are we now? How do we find out?

Get used to running pwd and ls to orient yourself

Go to a subdirectory

cd include

What kind of path is this, relative or absolute?

What if we had done cd ./include instead? What's the difference? Why do we have the . folder?

Making the command-line fast

NavigationUsing the Command-Line COP-3402 (1)

At this point, you are typing a lot, and retyping some of the same paths over and over again.

Tab-completion

One of these most useful features for speed on the command-line

Works for files and directories, command-line arguments, and commands themselves.

You can even create completions for your own software.

Impatience

Don't spend time typing when you can use tab completion

Laziness

Don't try to remember yourself file names, let the shell do it

Example

  • Be sure we are in /usr/include
  • Type the following, but don't hit <enter>

    ls gnum
  • Hit <tab>
  • The shell completes the file name for you!

    ls gnumake.h

Multiple completions

  • Type the following, but don't hit <enter>

    ls gnu
  • Hit <tab>. Nothing seems to happen!
  • Hit <tab> again to see all completions.

    /usr/include$ ls gnugnu/ gnumake.h gnu-versions.h /usr/include$ ls gnu
  • Add the hyphen and hit <tab> to complete.

    ls gnu-

Completion happens only when there is a unique option. But hitting <tab> again will show all possibilities with the prefix written so far. Continue typing until you have a unique prefix, then hit <tab> again to complete the unique possibility.

Make it a habit to hit tab

  • Confirming existence of a file
  • Avoiding long typing
  • Narrowing down results (tab twice)

Editing commands

KeyboardDescriptionMnemonic
Ctrl-aBeginning of command (home)a is beginning of the alphabet
Ctrl-eEnd of commande for end
<backspace>Delete a character behind youit's the backspace key
Alt-<backspace>Delete a whole word behind youalt for alternative version
Alt-bGo back a wordb for back, alt for alternative version
Alt-fGo forward a wordf for forward, alt for alternative version

Additional commands

KeyboardDescriptionMnemonic
Ctrl-dDelete a character in frontd for delete
Ctrl-bMove backb for back
Ctrl-fGo forwardf for forward
Alt-dDelete a word in frontd for delete, alt for alternative version

Kill (cut) and yank (paste)

KeyboardDescriptionMnemonic
Ctrl-kKill (cut) until the endk for kill
Alt-<backspace>Kill (cut) a word to the backalt for alternative version
Alt-dKill (cut) a word in frontd for delete, alt for alternative version
Ctrl-yYank (paste)y for yank

Rerunning commands

  • History saves all commands your run
  • history will print them all
  • Last command added to bottom of the list

Navigating the command history

KeyboardMnemonicDescription
Ctrl-p or <up>p for previousPrevious command
Ctrl-n or <down>n for nextNext command

Searching for a previous command

  • Ctrl-r (r for reverse lookup)
  • CAREFUL: hitting <enter> will immediately run
    • Use ctrl-a or ctrl-e to start editing the command
  • Ctrl-g to cancel search

"Save" commands by commenting them out

  • Ctrl-a, #, <enter>
  • Adds "saved" command to last one in history

Viewing files

CommandMnemonicDescription
catconcatenatewrite file(s) to terminal (stdout)
moreask user to show morewrite file out, pausing after each screenful
less"less is more"interactive command-line file viewer

cat command

cat /usr/include/stdio.h

less command

less /usr/include/stdio.h

q to quit

Navigating less

  • q to quit (try esc then q if not working, you might have hit other keys)
  • pg-up, pg-down, up, down, left, right
  • ctrl-v, alt-v, j, k, h, l
  • / type pattern, then enter
    • n for next
    • N for previous
  • type a number then G to go to line
  • ctrl-g to show current command-line

The less command has vim-like commands.

Downloading and untarring files

CommandDescription
wget '<url>'Download a file
tar -xvf file.tarUnpack a tarball (x for eXpand, v for Verbose, f for Filename)
cd ~/wget 'https://www.cs.ucf.edu/~gazzillo/teaching/cop3402fall24/files/hw3.tar'tar -xvf hw3.tarcd hw3

Editing the file tree

CommandDescription
touchcreate file
rmReMove
mvMoVe
cpCoPy
mkdirMaKe DIRectory
rmdirReMove (an empty) DIRectory

Removing non-empty directories?

  • Remove contents of the directory first, then use rmdir
  • What's an algorithm to remove a whole directory tree?

You can ask rm to remove non-empty directories and their contents recursively, but it's a bit dangerous.

Review

Orienting yourself

pwd # to show your working directoryls # to list current directory contents

Moving around

cd /usr/include # to change working directorycd ../ # can use relative paths to, this puts you in /usrcd ~ # to go to home directorycd - # to go to last directorycd # also to go to the home directory

Tab completion

  • Hit tab to auto-complete file and directory names
  • Hit tab again to show all files with a matching prefix
  • Use tab to quickly find paths

History

  • Hit up (or Ctrl-p) to repeat the prior command
  • Go up and down (or Ctrl-n) to step through the command history
  • Hit enter on a command to rerun it

Modifying the file tree

touch filename # to create an empty regular filerm filename # to remove a (non-directory) filemv filename newname # to rename a filemkdir subdir # to make a new directorymv filename subdir/ # to move a filecp subdir/filename copiedname # to copy a filerm subdir/filename # to remove a filermdir subdir # to remove an empty directory

Author: Paul Gazzillo

Created: 2024-08-28 Wed 05:36

Validate

Navigation

Using the Command-Line  COP-3402 (2024)
Top Articles
Latest Posts
Article information

Author: Francesca Jacobs Ret

Last Updated:

Views: 5516

Rating: 4.8 / 5 (48 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Francesca Jacobs Ret

Birthday: 1996-12-09

Address: Apt. 141 1406 Mitch Summit, New Teganshire, UT 82655-0699

Phone: +2296092334654

Job: Technology Architect

Hobby: Snowboarding, Scouting, Foreign language learning, Dowsing, Baton twirling, Sculpting, Cabaret

Introduction: My name is Francesca Jacobs Ret, I am a innocent, super, beautiful, charming, lucky, gentle, clever person who loves writing and wants to share my knowledge and understanding with you.