Lecture 2A : Linux File Commands

If you are familiar with how files and directories work in Windows, then you’ll be happy to know that it works very similarly in Linux.

When you start up File Explorer in Windows, you see an empty folder for your user, and inside there are folders called ‘Documents’, ‘Music’, ‘Desktop’, and so on.

When you start up the File Explorer in Knoppix or any Linux distribution with a GUI, you will see almost exactly the same thing. Each user has a home directory located here: /home/. (Please note the direction of the slashes, they are the opposite of the slashes in Windows!)


Terminology Revisited

nautilus screenshot

Use ls -a to view the contents of your Matrix home. You might see something like this:

.   .bash_history  .bashrc  public_html  .viminfo
..  .bash_logout

.. = parent directory (one level up).


More Navigation

Try to use cd .. to enter /home. Use ls to view all of the home directories of students and professors (Be prepared to wait!). Ask a friend for their myseneca username, and use cd <username> to enter their home directory. Try to use ls. Does it let you?


Creating Files

We can use a command called touch to make a file called test1. touch test1

This file is empty, it has nothing in it and its size is zero. You can confirm this by typing in ls -l.


total 8
drwxrwxr-x 2 student student 4096 Sep  7 14:33 Desktop
drwxrwxr-x 2 student student 4096 Sep  7 15:51 Documents
-rw-rw-r-- 1 student student    0 Sep  9 21:24 test1

See the 0 in the fifth column? That is file size.


Copying Files

Now, let’s take a look at copy and move.

cp = copy

cp test1 test2 what did that do?


total 8
drwxrwxr-x 2 student student 4096 Sep  7 14:33 Desktop
drwxrwxr-x 2 student student 4096 Sep  7 15:51 Documents
-rw-rw-r-- 1 student student    0 Sep  9 21:24 test1
-rw-rw-r-- 1 student student    0 Sep  9 21:24 test2

cp takes two arguments. The first is the target. This is a file or directory that already exists. The second argument is what you want to create. (Tell the shell what you have, and then tell it what you want).


Renaming Files

mv = move/rename.

mv test2 test3 what did that do?


total 8
drwxrwxr-x 2 student student 4096 Sep  7 14:33 Desktop
drwxrwxr-x 2 student student 4096 Sep  7 15:51 Documents
-rw-rw-r-- 1 student student    0 Sep  9 21:24 test1
-rw-rw-r-- 1 student student    0 Sep  9 21:24 test3

mv is used to move files and directories around, but we also use it to rename files and directories. This is an example of renaming. Again, tell the shell what you have (I have a file called test2), and then tell the shell what you want (I want this to become a file called test3).


Creating Directories

Working with directories requires some different commands in Linux.

In Documents, type this command: mkdir level1. Then use an ls command to check your work.


Creating Directories II

Now type in this command: mkdir level2/level3. You should see an error message. Reading and understanding error messages is an important skill to learn!

mkdir: cannot create directory ‘level2/level3’: No such file or directory

The problem here is that we want to create level3, but the shell is expecting level2 to exist, and it doesn’t. So it assumes that we’ve probably made a mistake, gives us an error message, and stops without creating anything. This is the default behavior.


Changing Options

Let’s change the default behavior by adding an option, or flag.

mkdir -p level2/level3

The -p is option tells the shell to create a parent directory if it doesn’t exist already. In this case, we asked the shell to create level2 in addition to creating level3.

We can use another command tree to see a nice output of directories and subdirectories.


Moving Files

Now let’s demonstrate the other other use of mv: this time our second argument will be a destination. Our destination will be the name of a directory.

mv test3 level2/level3 What do you expect that to do?

We could use cd level2/level3 then ls -l to check what happened, but we can also peek into a directory without entering it.

ls -l level2/level3


total 0
-rw-rw-r-- 1 student student 0 Sep  9 21:24 test3

Please note that our current location hasn’t changed.


Copying Directories

.
├── level1
├── level2
│   └── level3
│       └── test3
└── test1

3 directories, 2 files

We can see that this wasn’t exactly what we wanted. level2 should probably a subdirectory of level1. So we probably want to perform a command to copy level2 into level1. We can try using the cp command to do this. The first argument will be the target, the second argument will be the destination.

Try this:

cp level2 level1

What happened?

Try this instead:

cp -r level2 level1

.
├── level1
│   └── level2
│       └── level3
│           └── test3
├── level2
│   └── level3
│       └── test3
└── test1

5 directories, 3 files

The -r means recursive. It means to repeat an action for all things contained in the target (level2). necessary for directories and things.


Moving Directories

Let’s use mv level2 level2a to rename that second level2 directory in our current location. You should now have a tree that looks like this:

.
├── level1
│   └── level2
│       └── level3
│           └── test3
├── level2a
│   └── level3
│       └── test3
└── test1

5 directories, 3 files

Use cd level1 to set level1 as your current location.

Now let’s try pulling instead of pushing. The idea here is that your target is somewhere other than your current location, and your destination is the current destination. This works fine.

Try this command: mv ../level2a . (Again, use TAB to make less work!)

.
├── level1
│   ├── level2
│   │   └── level3
│   │       └── test3
│   └── level2a
│       └── level3
│           └── test3
└── test1

5 directories, 3 files

Notice that the mv command doesn’t need the -r option! This is just a weird inconsistency of these core utilities that you’ll have to remember.

. = current directory. We are pointing at a target that’s far away, and pointing at the current location as destination.

mv level2a/level3/test3 . What did this do?

Remove Files/Directories

Finally, a lesson in removing files and directories. The command here is rm. rm test3 This should work!

cd level2a

Now try rmdir level3 This will remove an empty directory.

Type: tree ~ ~ is a shortcut that means home for the current user. Our current user is student. So ~ = /home/student.

Now try this: cd without any argument will take us home.


Summary for Filepaths

Terminology

absolute
print the entire file path. This is very specific, but a lot to type. It will work regardless of where you are in the filesystem. (example: /home/student/level1/level2/level3)
relative
print the path relative to something else. Allows us to use shortcuts like: .., .. It’s easier for humans to use, but can be unpredictable since the result will change based on where you are. (example: ../level2a)
commands
We always interacting with the shell by entering a command. Example: ls
options/flags
changes a command’s default behaviour. These will always be listed in a command’s man page. Example: the “-l” in ls -l.
arguments
specifies (usually) a location, destination, a file, or something else. Example: the “test” in mkdir test.

Commands

Shortcuts Used in Relative Filepaths


The Linux Filesystem

We started by talking about the Windows filesystem, and comparing it to the Linux filesystem. We talked about C:\ being root in Windows, but not really how it works in Linux. Let’s try that now. Enter cd .. until your command prompt no longer changes when you enter the command. You can go no further. Take a look at your command prompt:

student@eric-eriga:/$

Now use pwd to see where you are.

/

/ is root. You are at the top of the filesystem.

Use `ls -l to take a look at the contents. They will look very different from what you are used to.

total 139
drwxr-xr-x   2 root root 12288 Sep  5 17:06 bin
drwxr-xr-x   5 root root  3072 Sep  6 08:24 boot
drwxrwxr-x   2 root root  4096 Mar  5  2016 cdrom
drwxr-xr-x  22 root root  5440 Sep  9 21:15 dev
drwxr-xr-x 181 root root 12288 Sep  7 14:32 etc
drwxr-xr-x   4 root root  4096 Sep  7 14:23 home
drwxr-xr-x  26 root root  4096 May  1 23:28 lib
drwxr-xr-x   2 root root 12288 Jun  4 11:55 lib32
drwxr-xr-x   2 root root  4096 May  1 23:21 lib64
drwxr-xr-x   3 root root  4096 Mar  7  2016 media
drwxr-xr-x   2 root root  4096 Apr 22  2016 mnt
drwxr-xr-x   8 root root  4096 Jun 18 09:48 opt
dr-xr-xr-x 294 root root     0 Sep  4 10:25 proc
drwxr-xr-x  34 root root  1240 Sep  9 21:15 run
drwxr-xr-x   2 root root 12288 Aug 20 11:00 sbin
dr-xr-xr-x  13 root root     0 Sep  7 15:51 sys
drwxrwxrwt  25 root root 24576 Sep  9 21:51 tmp
drwxr-xr-x  12 root root  4096 Nov 10  2016 usr
drwxr-xr-x  14 root root  4096 Apr 22  2016 var

In Linux, Everything is a File!

Understanding the Information in ls -l

Take a look at our previous example:

drwxr-xr-x   2 root root 12288 Sep  5 17:06 bin
Permissions Links Owner Group Size Time Last Modified Name
drwxr-xr-x 2 root root 12288 Sep 5 17:06 bin

The first letter in ‘Permissions’ tells us that this is a directory. There are several options:

We’ll talk more about permissions and links soon. Note that the owner and group are ‘root’. Isn’t / root?

ls -lh

drwxr-xr-x   2 root root  12K Sep  5 17:06 bin/

Now it’s easier for us to see that bin has a size of 12 kilobytes.