Linux Terminal - Files and Directories and Getting Around

From Computer Science
Jump to: navigation, search

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. You should start reading through this page before watching the video.

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.

Sample Quiz

An example quiz over this material. After watching the video and trying the commands out yourself to make sure you understand.

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 HW

The following might be used by your instructor as part of a HW assignment to verify that you are able to use the terminal to create and copy files and view the contents of directories.

Create a directory to store files for this HW. If you are given this assignment in one of your courses you will be told which directory to use. We refer to this directory as HW_dir.

  1. Copy /u1/junk/shakespeare.txt into your HW_dir.
  2. Copy /proc/cpuinfo into your HW_dir, but make the file name cpuinfo-HW.txt
  3. Copy /proc/version into your HW_dir, but make the file name version-HW.txt
  4. Create a file info.txt in your HW_dir. Inside the file fill in the following information.
    • Name (what you want to be called):
    • Why you are taking this course:
    • Anything you are worried about (wrt this course):
    • Most excited about (wrt this course):
  5. In the terminal, browse to the directory /net/web, run the command ls -l cs151, and copy the output of that command. Go back to your HW_dir, edit your info.txt file, and paste into the file.
  6. Copy ~cs151/hello.txt into your HW_dir

Sample Session

The following is a transcript of the commands and screen output from the youtube video linked at the top of this page. Blank lines have been put in after each command. You can read through the transcript and think about what you think the output will be after each command is entered, and check that this is how it shows in the transcript.

Note that a few text files have been edited using the nano text editor. When the nano text editor was used the editing of the file itself is not part of the transcript. Note that the transcript is for the user cs15100 logged in to the CS server.

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

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

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 

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

cs15100@cs:/u1/class/cs15100> nano "hello there.txt"

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/