Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 25 Next »

Goals: Introduction to the Linux File System, its structure and how to navigate around it, as well as creating, moving and copying files and folders.

  • What the file system is, and a typical organization / hierarchy.

  • Some high-level comparison to that of Windows.

  • Absolute vs relative paths.

  • Commands: pwd, cd, ls, mv, cp, mkdir, rmdir, rm

  • History: history

  • File Ownership and Permissions.



Some high-level comparison to that of Windows

 

Windows

Linux

Structure

Uses (data) drives C:, D:, E:…

Uses a tree hierarchy starting at “/

Known as the root directory.

 

<drive>\<folder01>\<folder02> 

/<folder01>/<folder02> 

Syntax

Uses the back slash: “\”

Uses the forward slash: “/”

Home folder

C:\Users\<username>

/home/<username>

Application Install

C:\Program Files\

/usr/

Folder and Filenames

Case insensitive: FoLdEr = FOLDER

Case sensitive: FoLdEr ≠ FOLDER

 

 

Wherever you are within the hierarchy is known as your current working directory (cwd)


Linux OS General Structure


Linux Hierarchical Structure: Example

image-20240522-180610.png

Absolute Path: /home/arcc-t05/

Path starts with a “/”

image-20240522-180711.png

Absolute Path: /home/arcc-t05/workshop/projects/p01/etc/

image-20240522-180809.png

Relative Path: workshop/projects/p01/etc/

Path does not start with a “/”

image-20240522-180856.png

Relative Path: p01/etc/

image-20240522-180952.png

Ex: Starting at / (root), what is the absolute path to the bits folder?

image-20240522-181029.png

Answer:

 Answer

/usr/include/bits/

image-20240522-181112.png

Ex: Starting in the home folder what is the relative path to the Jan folder?

image-20240522-181139.png

Answer:

 Answer

arcc-t05/workshop/data/2023/Jan/

image-20240522-181215.png

If you are following along as part of a scheduled training or bootcamp, please replace the <project-name> directory with the project directory you’ve been provided for your specific training/bootcamp. If you are training independently, please use your own project folder, but contact arcc-help@uwyo.edu if you would like a copy of the files and directories used in our examples.

Commands:

Used to perform certain operating system tasks through the Command Line Interface, as directed by the interpreter (as opposed to a Graphical Interface Interpreter we would usually use).


<command --help> 

Command

Description

pwd

pwd: pwd [-LP]
Print the name of the current working directory.

cd

cd: cd [-L|[-P [-e]] [-@]] [dir]
    Change the shell working directory.

ls

Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default)

mkdir

Usage: mkdir [OPTION]... DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.

mv

Usage: mv [OPTION]... [-T] SOURCE DEST
  or:  mv [OPTION]... SOURCE... DIRECTORY
  or:  mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY

Commands: <command --help> 

Command

Description

cp

Usage: cp [OPTION]... [-T] SOURCE DEST
  or:  cp [OPTION]... SOURCE... DIRECTORY
  or:  cp [OPTION]... -t DIRECTORY SOURCE...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

rmdir

Usage: rmdir [OPTION]... DIRECTORY...
Remove the DIRECTORY(ies), if they are empty.

rm

Usage: rm [OPTION]... [FILE]...
Remove (unlink) the FILE(s).

pwd: Print the name of the current working directory

If you get lost, then you can jump back to the home folder.

[<username>@blog1 ???]$ cd
[<username>@blog1 ~]$

The ~ “tilda” character represents your home directory.

Use the pwd to confirm your current working directory, which after the above command will be home.

[<username>@blog1 ~]$ pwd
/home/<username>

cd: Change the shell working directory

Start by navigating back to home.

[<username>@blog1 ???]$ cd
[<username>@blog1 ~]$

Move up one level, into the current folder’s parent.

[<username>@blog1 ~]$ cd ..
[<username>@blog1 ~]$ pwd
/home

Move up another level into the root folder.

[<username>@blog1 home]$ cd ..
[<username>@blog1 /]$ pwd
/

cd: Change working directory (cont)

Navigate into the opt folder.

[<username>@blog1 /]$ cd opt
[<username>@blog1 opt]$ pwd
/opt

Question: Did we define an an absolute or relative path?


Answer:

Did we define an an absolute or relative path?

 Answer

When we print out the working directory, we are presented with a '/' in front of opt, however when we changed directories to opt, our cd command did not have a '/'. Therefore, we defined the relative path to the opt directory.


cd: Change the shell working directory

First reset back to home.

[<username>@blog1 opt]$ cd
[<username>@blog1 ~]$

Navigate into the /usr/include/asm folder.

[<username>@blog1 ~]$ cd /usr/include/asm
[<username>@blog1 asm]$ pwd
/usr/include/asm

Question: Did we define an absolute or relative path?


Answer:

 Answer

Relative path, since we specified the full path, starting with root: /


Navigate up two levels:

[<username>@blog1 asm]$ cd ../..
[<username>@blog1 usr]$ pwd
/usr

Navigate back home:

[<username>@blog1 usr]$ cd
[<username>@blog1 ~]

ls: List information about the FILEs (cwd by default)

Take a look at the ls command and some of the options it provides.

List files in the user’s home folder.

[<username>@blog1 ~]$ ls

List long format that includes ownership and permission details.

[<username>@blog1 ~]$ ls -l

List all files, including hidden files and folders start with “.”.

Notice how ‘short-name’ options are grouped.

[<username>@blog1 ~]$ ls –a

List all files with long format.

[<username>@blog1 ~]$ ls –al

List all files with long format, in reverse order.

[<username>@blog1 ~]$ ls –alr

List all files with long format, in reverse order, in human readable form.

<username>@blog1 ~]$ ls –alrh

Note how we can use multiple options together.

Demonstrate how to ls to a folder outside cwd using an absolute path.

# Demonstrate how to ‘ls’ to a folder outside cwd
[~]$ ls /project/<project-name>/arcc-t01
folder01  myfile.txt

mkdir: Create the DIRECTORY(ies), if they do not already exist

Navigating to you /home and make a folder called folder01.

$ cd
[~]$ ls
Desktop  Documents  Downloads
[~]$ mkdir folder01
[~]$ ls
Desktop  Documents  Downloads  folder01

Try creating that folder again. What happens?

[~]$ mkdir folder01
mkdir: cannot create directory ‘folder01’: File exists

If a folder already exists, you can not make it again.

Navigate into this folder and print the current working directory.

[~]$ cd folder01/
[folder01]$ pwd
/home/<username>/folder01

mkdir: Create the DIRECTORY(ies), if they do not already exist

Within folder01, try creating two new folders called folder02 and folder03.

Navigate into folder02 and print the current working directory.

[folder01]$ mkdir folder02 folder03
[folder01]$ ls
folder02 folder03
[folder01]$ cd folder02/
[folder02]$ pwd
/home/<username>/folder01/folder02

Notice: You can create multiple folders at the same time.


mv: Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY

Renaming a file is the same as moving it to a new name.

Navigate to your home and create a file called myfil.txt

[]$ $ cd
[~]$ touch myfil.txt
[~]$ ls
Desktop  Documents  Downloads  folder01  myfil.txt

Move this file from myfil.txt to myfile.txt.

[~]$ mv myfil.txt myfile.txt 
[~]$ ls
Desktop  Documents  Downloads  folder01  myfile.txt

Notice how we have moved the file from one name to a new name - essentially renaming it.


mv: Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY

Move the myfile.txt into folder01.

[~]$ mv myfile.txt folder01/
[~]$ ls
Desktop  Documents  Downloads  folder01
[~]$ ls folder01/
folder02  myfile.txt

Notice how we used ls to see what is in a relative folder.


cp: Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY

Navigate back home and create a file called myfile02.txt.

[]$ cd
[~]$ touch myfile02.txt
[~]$ ls
Desktop  Documents  Downloads  folder01  myfile02.txt

Copy (duplicate) this file to a new file called myfile02b.txt.

[~]$ cp myfile02.txt myfile02b.txt 
[~]$ ls
Desktop  Documents  Downloads  folder01  myfile02b.txt  myfile02.txt

Copy file myfile02b.txt into the existing folder02 folder.

[~]$ cp myfile02b.txt folder01/
[~]$ ls folder01/
folder02  folder03  myfile02b.txt  myfile.txt

cp: folders

[~]$ cp folder01
cp: missing destination file operand after 'folder01'
Try 'cp --help' for more information.
[~]$ ls folder01
folder02  myfile02b.txt  myfile.txt
[~]$ cp folder01 folder04
cp: -r not specified; omitting directory 'folder01’

You can not use the cp command with no options to copy a folder that has files within it.

Look at the options available. cp -h

In this specifically -r that recursively copy a folder and all its contents.

[~]$ cp –r folder01 folder04
[~]$ ls
Desktop  Documents  Downloads  folder01  folder04  myfile02b.txt  myfile02.txt
[~]$ ls folder04
folder02  myfile02b.txt  myfile.txt

rmdir: Remove the DIRECTORY(ies), if they are empty

[~]$ ls 
Desktop  Documents  Downloads  folder01  folder04  myfile02b.txt  myfile02.txt
[~]$ mkdir folder05
[~]$ ls
Desktop  Documents  Downloads  folder01  folder04  folder05  myfile02b.txt  
myfile02.txt

# Can remove folder05 since it is empty.
[~]$ rmdir folder05
[~]$ ls
Desktop  Documents  Downloads  folder01  folder03  myfile02b.txt  myfile02.txt

[~]$ rmdir folder04/
rmdir: failed to remove 'folder04/': Directory not empty

You can not use rmdir to remove a directory that has files still within it. The folder must be empty.


rm: Remove (unlink) the FILE(s)

[~]$ cd
[~]$ cd folder04
[folder04]$ ls
folder02  myfile02b.txt  myfile.txt

[folder04]$ ls folder02/
[folder04]$
# ‘folder02’ is empty.
[folder04]$ rmdir folder02/

[folder04]$ ls
myfile02b.txt  myfile.txt
[folder04]$ rm myfile.txt 
[folder04]$ ls
myfile02b.txt
[folder04]$ rm myfile02b.txt

rm: Remove (unlink) the FILE(s)

[folder04]$ ls
[folder04]$
# ‘folder04’ is now empty.
[folder04]$ cd ..
[~]$ rmdir folder04/
[~]$ ls
Desktop  Documents  Downloads  folder01  myfile02b.txt  myfile02.txt
# This has taken a lot of individual steps.
# Can we do this quicker?

rm: folders and file(s)

[~]$ cd
[~]$ rm folder01/
rm: cannot remove 'folder01/': Is a directory

You can not use the rm command with no options to remove a folder.

[~]$ rm --help
Usage: rm [OPTION]... [FILE]...
Remove (unlink) the FILE(s).
...
  -r, -R, --recursive   remove directories and their contents recursively
...

Using help we found we can use the -r option to remove a folder and all its contents.

[~]$ rm -r folder01/
[~]$ ls
Desktop  Documents  Downloads  myfile02b.txt  myfile02.txt
# Can remove multiple files.
[~]$ rm myfile02b.txt myfile02.txt 
[~]$ ls
Desktop  Documents  Downloads
# Alternatively we could have removed above 2 files with: rm myfile* 
# * is a wildcard, so the rm myfile* will remove all starting with the characters "myfile"

rm: WARNING

From the command-line there is NO trash bin!

Using rm/rmdir is FINAL!

spiderman.png

history

[~]$ history --help
history: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]
    Display or manipulate the history list.
    ...
[~]$ history 
  ...	
  219  rm -f folder01/
  220  rm -r folder01/
  221  ls
  222  rm myfile02b.txt myfile02.txt 
  223  ls
  224  history
# Repeat command ‘223’
[<username>@blog1 ~]$ !223
ls
Desktop  Documents  Downloads

Exercises

Questions:

  1. How can you return to your home folder?

  2. What command do you use if you’ve forgotten where you are in the folder hierarchy?

  3. How can you list what is in a folder as well as any subfolders?

  4. Go back through the command related slides are try for yourself.


Answers

1: How can you return to your home folder?

 Answer
  • Use: cd or cd ~ as a shortcut to return to your home folder instead of using a cd /home/$USER or cd /home/<insert_your_username>/

  • cd stands for: change directory

2: What command do you use if you’ve forgotten where you are in the folder hierarchy?

 Answer
  • Use: pwd

  • This stands for: print working directory.

3: How can you list what is a folder as well as any subfolders?

 Answer
  • ls command lists contents, which flag do we use to get it’s subfolders as well?

[]$ man ls
-R, --recursive
              list subdirectories recursively
[]$ ls -R
  • -R is for recursive, which relates to repeated application of the rule over successive executions. We continue applying the listings for all subfolders that exist within the top level parent directory.


File Ownership and Permissions

What does the output of ls –l mean?

[<username>@blog1 ~]$ cd /project/<project-name>/intro_to_linux
[<username>@blog1 intro_to_linux]$ ls -al
total 54
drwxrwsr-x  4 <someuser> <project-name>  4096 Oct  6 08:09 .
drwxrws--- 40 root       <project-name>  4096 Oct  6 08:09 ..
drwxrwsr-x  2 <someuser> <project-name>  4096 Oct  5 11:19 clusters
drwxrwsr-x  6 <someuser> <project-name>  4096 Oct  5 14:56 data
-rw-rw-r--  1 <someuser> <project-name>   874 Oct  5 15:30 fruits.txt
-rw-rw-r--  1 <someuser> <project-name> 34472 Oct  5 10:57 software.csv
-rw-rw-r--  1 <someuser> <project-name>  1603 Oct  6 08:08 vegatables.txt
-rw-rw-r--  1 <someuser> <project-name>    26 Oct  5 07:20 workshop_all.txt
-rw-------  1 <someuser> <project-name>    23 Oct  5 07:20 workshop_me.txt

The first character on the left indicates if it is a directory “d” or a file “-”.

drwxrwsr-x    clusters          # A folder.
-rw-rw-r--    workshop_all.txt  # A file.

File Ownership and Permissions

-rw-rw-r--    1 <username> <project-name>     26 Oct  5 07:20 workshop_all.txt
  • User: This is the owner of the file/folder. By default, the person who created it becomes its owner.

    • <username> is the owner

  • Group: A group is a collection of users. The primary purpose of the group is to define a set of privileges for a given resource that can be shared among the users within the group.

    • <project-name> is the group.

    • In general, for a workshop, all attending users / the arcc-txx users (if being used) have been setup to be within this group.

  • Other: This is any other user who has access to the file/folder. This person has neither created the file, nor do they belong to a user group.


Permission Denied

This demonstrates how permissions work. Bare in mind this assumes you’re logged in as user <username>.

[]$ cd /project/<project-name>/
[]$ ls -al
...
# drwxr-sr-x    2 <username> <project-name>   4096 May 16 16:26 <username>
...

The middle set of permissions is “drwxr-sr-x” means no one other than <username> has permission to write within this folder.

Try navigating into a <different-username> folder within <project-name> and creating a file.

[<project-name>]$ cd <different-username>/
# Can <username> create (write) a file within this folder?
[<different-username]$ touch text.txt
touch: cannot touch 'text.txt': Permission denied

No one, other than <different-username> can create (write) a file within this folder.


If we change directories, and go to /project/<project-name>/intro_to_linux, what permissions do the contents of this directory have?

[<username>]$ cd ../intro_to_linux
[<username>]$ ls -al
# -rw-rw-r--    1 <arcc-username> <project-name>     26 Oct  5 07:20 workshop_all.txt

Any user within the <project-name> group can read/write the file workshop_all.txt.

Everybody can read it. Do you want anyone outside of this project to be able to read this file?

[] ls -al
# -rw-------    1 <arcc-username> <project-name>     23 Oct  5 07:20 workshop_me.txt
[intro_to_linux]$ cat workshop_me.txt
cat: workshop_me.txt: Permission denied

Only user <arcc-usernameusername> can read/write this file. No one else, not even anyone within the <project-name> group, can view this file.


Exercise: Try it

Questions: In all cases be able to justify your answer.

  1. Can you create a folder under /project/<project-name>/username>/?

  2. Can you /project/<project-name>/intro_to_linux/ and view workshop_all.txt?

  3. Can you /project/<project-name>/intro_to_linux/ and view workshop_me.txt?

  4. Can you cd into the /opt folder?

  5. Can you cd into the /root folder?


Answers

1. Can you create a folder under /project/<project-name>/username>/?

 Answer
[]$ cd /project/<project-name>/
[<project-name>]$ cd <username>
[<username>]$ touch test.txt
# Do you get a “Permission denied”?

Yes. You should have write permissions as owner for this folder.

# drwxr-sr-x    2 <username> <project-name>   4096 May 16 16:26 <username>

2. Can you /project/<project-name>/intro_to_linux/ and view workshop_all.txt?

 Answer
[<username>]$ cd ../intro_to_linux
[intro_to_linux]$ cat workshop_all.txt
# Do you get a “Permission denied”?

Yes. Everybody can read this.

-rw-r--r-- 1 <arcc-username> <project-name>    26 Sep  4 10:38 workshop_all.txt

3. Can you /project/<project-name>/intro_to_linux/ and view workshop_me.txt?

 Answer
[intro_to_linux]$ cat workshop_me.txt
# Do you get a “Permission denied”?

No. Only <arcc-username> can read/write this file.

-rw------- 1 <arcc-username> <project-name>    23 Sep  4 10:38 workshop_me.txt

4. Can you cd into the /opt folder?

 Answer
[<username>]$ cd /opt
[<username> opt]$

# “other” has read permissions
drwxr-xr-x.    5 root root    43 Jun 26 11:47 opt

Yes. The other has read permissions.

drwxr-xr-x.    5 root root    43 Jun 26 11:47 opt

5. Can you cd into the /root folder?

 Answer
[<username>]$ cd /root
-bash: cd: /root: Permission denied
[<username>]$ ls -l /
...
# No permission set for other read permissions
dr-xr-x---.   17 root root  4096 Oct  4 12:58 root

No. There are no permissions set for other read permissions.

dr-xr-x---.   17 root root  4096 Oct  4 12:58 root

Next Steps

  • No labels