Difference between revisions of "Linux Terminal - Files and Directories and Getting Around"

From Computer Science
Jump to: navigation, search
Line 3: Line 3:
 
'''Basic idea''' The linux terminal is used to type commands to control your computer and files.  Everything that you can do graphically on your computer (in Windows with File Explorer, on MacOS with Finder) you can do by typing commands instead.
 
'''Basic idea''' The linux terminal is used to type commands to control your computer and files.  Everything that you can do graphically on your computer (in Windows with File Explorer, on MacOS with Finder) you can do by typing commands instead.
  
'''Demo''' See [https://www.youtube.com/watch?v=ynETRS3KGGs&feature=youtu.be youtube link] for a video demonstrating the basics of getting around in the linux terminal.
+
'''Demo''' See [https://www.youtube.com/watch?v=ynETRS3KGGs&feature=youtu.be this demo video] for a video demonstration of the topics on this page, including running the commands on the ISU CS server.
  
 
=Basic Commands=
 
=Basic Commands=

Revision as of 17:22, 10 January 2020

This page is a part of the Linux and CS Systems - Getting Started. This page assumes you have your computer setup to connect to the CS server, or have the appropriate software installed on your computer to run commands. Go back to the Linux and CS Systems Getting Started main page if you don't have our system setup yet.

Basic idea The linux terminal is used to type commands to control your computer and files. Everything that you can do graphically on your computer (in Windows with File Explorer, on MacOS with Finder) you can do by typing commands instead.

Demo See this demo video for a video demonstration of the topics on this page, including running the commands on the ISU CS server.

Basic Commands

With the terminal you can create, copy, move, and delete files and directories. The following are the most important commands to do this.

  • cd - change directory
  • mkdir - make a new directory
  • ls - list directory contents (use option -l to see details)
  • cp - copy files
  • rm - remove files (be careful, there is no recycle bin or trash - once you delete, it's gone)
  • rmdir - remove directory
  • mv - move or rename a file or directory
  • nano - simple text editor

Getting Around

To "get around" in the terminal, use the following keys.

  • enter - a command is only executed when you press enter.
  • up and down arrows - lets you run a previously typed command again.
  • tab - if you are typing a filename, you can press tab to let the terminal auto-complete some part of it. This also works for command names (e.g., type mkdi and then tab).

The following are shortcuts for directories.

  • . - current directory
  • .. - directory one level up from where you are currently
  • ~ - your home directory
  • / - the top of the entire file system

The following are special notes about directory or file names.

  • "" - if you have a file name with spaces in it, then you put the file name within "". So you could do mkdir "Some Directory"
  • * - called a wildcard, used to specify all file names that match some pattern. To list all files that end in ".pdf" you could type ls *.pdf.

Quiz

An example quiz over this material. After watching the video

Fill in the blank, what is the shortcut or key used in the linux terminal for each of the following.

  • Press this key to execute a command:
  • Keys that let you run a previously typed command again:
  • Key to press to auto-complete a filename or command:
  • Shortcut for current directory (one character):
  • Shortcut for directory one level up from current:
  • Shortcut for your home directory (one character):
  • Shortcut for the top of the entire file system (one character):
  • Characters to put around a filename that has spaces:
  • Wildcard character:

Fill in the blank, what is the linux command to do each of the following.

  • Change directory:
  • Make a new directory:
  • List directory contents:
  • Flag/option to use with previous command to list details of files:
  • Copy file(s):
  • Remove files:
  • Remove directory:
  • Move or rename a file or directory:
  • A terminal text editor:

Give the complete linux terminal command to complete each of the following tasks. Tasks such as these could be asked about.

  • Create a new directory HELLO in your home directory:
  • Copy the contents of jkinne's GREAT directory into your home directory:
  • Change directory to your home directory:
  • Check to see who is logged in:
  • List the contest of the directory ~cs151/ including the sizes of the files:

Sample Session

In just a minute...

cs15100@cs:/u1/class/cs15100> ls
Desktop/  proto/

cs15100@cs:/u1/class/cs15100> ls -l
total 8
drwxr-xr-x 2 cs15100 class 4096 Jan  8 21:17 Desktop/
drwxr-xr-x 2 cs15100 class 4096 Jan  9 12:35 proto/

cs15100@cs:/u1/class/cs15100> cd Desktop

cs15100@cs:/u1/class/cs15100/Desktop> ls

cs15100@cs:/u1/class/cs15100/Desktop> cd ..

cs15100@cs:/u1/class/cs15100> mkdir hello

cs15100@cs:/u1/class/cs15100> ls
Desktop/  hello/  proto/

cs15100@cs:/u1/class/cs15100> ls
Desktop/  hello/  proto/

cs15100@cs:/u1/class/cs15100> nano hello.txt
Option morespace is ignored; it is the default
Option smooth is ignored; it is the default

cs15100@cs:/u1/class/cs15100> ls
Desktop/  hello/  hello.txt  proto/

cs15100@cs:/u1/class/cs15100> ls -l
total 16
drwxr-xr-x 2 cs15100 class 4096 Jan  8 21:17 Desktop/
drwxr-xr-x 2 cs15100 class 4096 Jan 10 11:37 hello/
-rw-r--r-- 1 cs15100 class   60 Jan 10 11:43 hello.txt
drwxr-xr-x 2 cs15100 class 4096 Jan  9 12:35 proto/

cs15100@cs:/u1/class/cs15100> mv hello.txt Desktop

cs15100@cs:/u1/class/cs15100> ls
Desktop/  hello/  proto/

cs15100@cs:/u1/class/cs15100> ls Desktop
hello.txt

cs15100@cs:/u1/class/cs15100> ls
Desktop/  hello/  proto/

cs15100@cs:/u1/class/cs15100> ls /
bin/   dev/  home/  lib64/       media/  net/  proc/  run/   srv/    swap  tmp/  u2/   var/
boot/  etc/  lib/   lost+found/  mnt/    opt/  root/  sbin/  store/  sys/  u1/   usr/  xsys/

cs15100@cs:/u1/class/cs15100> cd Desktop

cs15100@cs:/u1/class/cs15100/Desktop> ls /u1/class/cs15100
Desktop/  hello/  proto/

cs15100@cs:/u1/class/cs15100/Desktop> ls ~
Desktop/  hello/  proto/

cs15100@cs:/u1/class/cs15100/Desktop> cd /u1/junk/kinne

cs15100@cs:/u1/junk/kinne> ls
cs151Hello            Indianapolis-Weather-Station-USW00093819-1950-2018.csv  shakespeare_1000_lines.txt
GRCh38.p12.genome.fa  interview-questions/                                    shakespeare.txt
hello.txt             shakespeare_1000_lines_tail.txt                         Terre_Haute_airport_93823-03868.csv

cs15100@cs:/u1/junk/kinne> ls ~
Desktop/  hello/  proto/

cs15100@cs:/u1/junk/kinne> ls
cs151Hello            Indianapolis-Weather-Station-USW00093819-1950-2018.csv  shakespeare_1000_lines.txt
GRCh38.p12.genome.fa  interview-questions/                                    shakespeare.txt
hello.txt             shakespeare_1000_lines_tail.txt                         Terre_Haute_airport_93823-03868.csv

cs15100@cs:/u1/junk/kinne> cp cs151Hello ~

cs15100@cs:/u1/junk/kinne> cd ~

cs15100@cs:/u1/class/cs15100> ls
cs151Hello  Desktop/  hello/  proto/

cs15100@cs:/u1/class/cs15100> ls -l
total 16
-rw-r--r-- 1 cs15100 class   16 Jan 10 11:46 cs151Hello
drwxr-xr-x 2 cs15100 class 4096 Jan 10 11:44 Desktop/
drwxr-xr-x 2 cs15100 class 4096 Jan 10 11:37 hello/
drwxr-xr-x 2 cs15100 class 4096 Jan  9 12:35 proto/

cs15100@cs:/u1/class/cs15100> nano cs151Hello
Option morespace is ignored; it is the default
Option smooth is ignored; it is the default

cs15100@cs:/u1/class/cs15100> ls
cs151Hello  Desktop/  hello/  proto/

cs15100@cs:/u1/class/cs15100> rm cs151Hello
rm: remove regular file 'cs151Hello'? y

cs15100@cs:/u1/class/cs15100> ls
Desktop/  hello/  proto/

cs15100@cs:/u1/class/cs15100> rmdir hello

cs15100@cs:/u1/class/cs15100> ls
Desktop/  proto/

cs15100@cs:/u1/class/cs15100> ls
Desktop/  proto/

cs15100@cs:/u1/class/cs15100> cd Desktop/

cs15100@cs:/u1/class/cs15100/Desktop> ls
hello.txt

cs15100@cs:/u1/class/cs15100/Desktop> mv hello.txt ..

cs15100@cs:/u1/class/cs15100/Desktop> ls

cs15100@cs:/u1/class/cs15100/Desktop> cd ..

cs15100@cs:/u1/class/cs15100> ls
Desktop/  hello.txt  proto/

cs15100@cs:/u1/class/cs15100> mv hello.txt helloThere.txt

cs15100@cs:/u1/class/cs15100> ls
Desktop/  helloThere.txt  proto/

cs15100@cs:/u1/class/cs15100> ls
Desktop/  helloThere.txt  proto/

cs15100@cs:/u1/class/cs15100> mv hello.txt helloThere.txt
mv: cannot stat 'hello.txt': No such file or directory

cs15100@cs:/u1/class/cs15100> ls
Desktop/  helloThere.txt  proto/

cs15100@cs:/u1/class/cs15100> cp helloThere.txt hello.txt

cs15100@cs:/u1/class/cs15100> ls
Desktop/  helloThere.txt  hello.txt  proto/

cs15100@cs:/u1/class/cs15100> nano helloThere.txt 
Option morespace is ignored; it is the default
Option smooth is ignored; it is the default

cs15100@cs:/u1/class/cs15100> cd Desktop

cs15100@cs:/u1/class/cs15100/Desktop> ls

cs15100@cs:/u1/class/cs15100/Desktop> ls .

cs15100@cs:/u1/class/cs15100/Desktop> cp ../helloThere.txt .

cs15100@cs:/u1/class/cs15100/Desktop> ls
helloThere.txt

cs15100@cs:/u1/class/cs15100/Desktop> cp /u1/class/cs15100/helloThere.txt /u1/class/cs15100/Desktop/
cp: overwrite '/u1/class/cs15100/Desktop/helloThere.txt'? n

cs15100@cs:/u1/class/cs15100/Desktop> cd ..

cs15100@cs:/u1/class/cs15100> nano hello there.txt
Option morespace is ignored; it is the default
Option smooth is ignored; it is the default

cs15100@cs:/u1/class/cs15100> nano "hello there.txt"
Option morespace is ignored; it is the default
Option smooth is ignored; it is the default

cs15100@cs:/u1/class/cs15100> ls -l
total 20
drwxr-xr-x 2 cs15100 class 4096 Jan 10 11:51 Desktop/
-rw-r--r-- 1 cs15100 class    9 Jan 10 11:52 hello\ there.txt
-rw-r--r-- 1 cs15100 class   60 Jan 10 11:43 helloThere.txt
-rw-r--r-- 1 cs15100 class   60 Jan 10 11:50 hello.txt
drwxr-xr-x 2 cs15100 class 4096 Jan  9 12:35 proto/