Basic Linux Navigation

Nathan Backman
19 Jan 202212:29

Summary

TLDRIn this tutorial, Dr. Backman introduces basic terminal navigation within a Unix-like OS. He explains the significance of the command prompt, home directory, and the tilde symbol. The video covers essential commands such as 'ls' for listing, 'mkdir' for creating directories, 'cd' for changing directories, and 'nano' for file editing. Dr. Backman demonstrates file manipulation techniques like copying, moving, and deleting files, as well as creating and running a simple Python script. He also teaches how to use relative paths and the importance of recursive commands for directory operations, concluding with a directory deletion example.

Takeaways

  • 💻 The terminal is a powerful tool for navigating, creating, and manipulating files and directories in Unix-like operating systems.
  • 📁 The 'ls' command lists the contents of the current directory, which is crucial for understanding your current location in the file system.
  • 🏠 The 'Home' directory is the default starting point in a terminal and serves as the central location for a user's files.
  • 👤 The tilde symbol (~) is a shorthand for the home directory, indicating the user's base directory in the file system.
  • 📂 The 'mkdir' command is used to create new directories, allowing for organized file management.
  • 🔄 The 'cd' command changes the current working directory, which is essential for navigating through the file system.
  • ✏️ The 'nano' text editor is a basic tool for creating and editing files within the terminal.
  • 🔗 The 'cp' command copies files, allowing for the creation of duplicates or backups.
  • 🔄 The 'mv' command moves or renames files, providing flexibility in file organization.
  • 🗑️ The 'rm' command deletes files, used for removing unnecessary or temporary files from the system.
  • 🔧 The 'rm -r' command is used to delete directories and their contents, providing a way to clear out entire folder structures.

Q & A

  • What is the purpose of the tilde symbol (~) in Unix-like operating systems?

    -The tilde symbol (~) is shorthand for the home directory of the current user in Unix-like operating systems.

  • What is the command to list the contents of the current directory in the terminal?

    -The command 'ls' is used to list the contents of the current directory in the terminal.

  • How do you create a new directory in the terminal?

    -To create a new directory, you use the 'mkdir' command followed by the name of the directory you want to create.

  • What does the 'cd' command do in a Unix-like terminal?

    -The 'cd' command is used to change the current working directory to the one specified after the command.

  • How can you create a file in the terminal?

    -You can create a file by using a text editor like 'nano', followed by the filename you want to create.

  • What keyboard shortcut is used to exit the nano text editor?

    -The keyboard shortcut to exit the nano text editor is Control-X.

  • How do you run a Python script from the terminal?

    -You can run a Python script by typing 'python3' followed by the name of the Python file.

  • What command is used to delete a file in the terminal?

    -The 'rm' command is used to delete files in the terminal.

  • How can you copy a file to create a new version with a different name?

    -You can copy a file by using the 'cp' command, followed by the source file name and the new destination file name.

  • What is the purpose of the '-r' flag when using the 'cp' command to copy directories?

    -The '-r' flag stands for 'recursive' and is used with the 'cp' command to copy directories along with their contents.

  • How do you move a file to a different directory?

    -To move a file, you use the 'mv' command followed by the file's current path and the new destination path.

  • What is the command to delete a directory and its contents?

    -The 'rm -r' command is used to delete a directory and its contents recursively.

  • How can you quickly navigate back to your home directory in the terminal?

    -You can quickly navigate back to your home directory by simply typing 'cd' and hitting enter.

Outlines

00:00

💻 Introduction to Terminal Navigation

Dr. Backman introduces basic navigation in a Unix-like terminal, focusing on file and directory manipulation. The tutorial begins with understanding the terminal prompt, which shows the username, computer name, and current directory. The importance of knowing the current directory is emphasized, as it serves as the basis for all terminal operations. The home directory is highlighted as the default starting point for new terminals, and the tilde symbol (~) is explained as shorthand for the home directory. The video demonstrates creating a new directory using 'mkdir' and lists its contents with 'ls'. A visualization is provided to show the impact of commands on the file system.

05:02

📂 Manipulating Directories and Files

The video continues with changing directories using 'cd' and creating sub-directories for a 'cmsc181' course. The 'ls' command is used to list directory contents, and 'nano' is introduced as a terminal-based text editor for creating and editing files. The process of saving and exiting 'nano' is demonstrated, and the video shows how to run a Python program using the 'python3' command. File operations such as deleting with 'rm', copying with 'cp', and navigating back to the home directory using '..' are covered. A shortcut to return to the home directory with just 'cd' is also mentioned. The video ends with creating a copy of a directory structure for a new course, 'cmsc182', and changing file types from Python to C++.

10:04

🔄 File Renaming and Directory Deletion

The final part of the video script covers renaming files using the 'mv' command, which is demonstrated by changing 'hello.py' to 'hello.cpp'. The process of updating multiple Python files to C++ files is shown, followed by navigating back to the home directory using relative paths. The script concludes with a lesson on deleting directories using 'rm -r', which is applied to remove the 'cmsc182' directory. The video wraps up by summarizing the basic terminal commands learned, such as 'mkdir', 'ls', 'cp', 'mv', 'rm', and 'nano', and encourages viewers to apply these skills in their own file system exploration.

Mindmap

Keywords

💡Terminal

A terminal in the context of the video refers to a text-based interface for accessing the Unix-like operating system's shell. It allows users to interact with the system by typing commands. The video emphasizes the terminal's role in file and directory management, showcasing how to navigate and manipulate files and directories using terminal commands, which is central to the theme of basic navigation in Unix-like systems.

💡Prompt

The prompt is the text displayed by the terminal that indicates it is ready to receive user input. It often includes information such as the username, hostname, and the current working directory. In the video, the prompt is used to demonstrate the user's current location within the file system, which is crucial for understanding the context of commands being executed.

💡Current Directory

The current directory is the folder that the terminal is focused on when executing commands. All operations in the terminal are relative to this directory. The video explains the importance of being aware of the current directory, as it determines the location where files and directories are created or manipulated, similar to how file explorers in other operating systems work.

💡Home Directory

The home directory is a personal directory unique to each user account on a Unix-like system. It serves as the default location for a user's files and is analogous to 'My Documents' in Windows or the user's folder in macOS. The video uses the home directory as the starting point for navigating the file system and creating new directories and files.

💡Tilde Symbol (~)

The tilde symbol (~) is a shorthand used in Unix-like systems to represent the user's home directory. In the video, the tilde symbol is introduced as a quick way to refer to the home directory in commands, simplifying the process of navigating to or from the user's personal space within the file system.

💡ls

The 'ls' command stands for 'list' and is used to display the contents of the current directory. The video demonstrates how 'ls' can be used to view files and directories, which is fundamental for file management tasks such as creating, moving, and deleting files.

💡mkdir

The 'mkdir' command is used to create a new directory. In the video, 'mkdir' is used to create a directory named 'cmsc181', illustrating how to organize files into folders, which is a common task in file management and an essential skill for maintaining an organized file system.

💡cd

The 'cd' command stands for 'change directory' and is used to navigate to a different directory. The video shows how 'cd' can be used to move into a newly created directory, making it the current working directory, which is necessary for performing operations within that directory.

💡nano

Nano is a terminal-based text editor mentioned in the video for creating and editing files. The video demonstrates using 'nano' to create a 'todo.txt' file, highlighting the process of entering text, saving, and exiting the editor, which is a fundamental task for managing text-based data in Unix-like systems.

💡rm

The 'rm' command is used to remove files and directories. In the video, 'rm' is used to delete the 'todo.txt' file, demonstrating how to clean up unnecessary files. This command is a critical part of file management, allowing users to delete files that are no longer needed.

💡cp

The 'cp' command is used to copy files and directories. The video explains how to use 'cp' with the '-r' flag to recursively copy directories, as demonstrated when creating a 'cmsc182' directory by copying 'cmsc181'. This command is essential for duplicating files and directories without manually recreating their contents.

💡mv

The 'mv' command is used to move or rename files and directories. In the video, 'mv' is used to move a 'todo.txt' file to a different directory and to rename Python files to C++ files by changing their extensions. This command is crucial for organizing and managing files within the file system.

💡Relative Path

A relative path is a file path that is specified in relation to the current directory. The video discusses the use of relative paths to specify the location of files and directories, which is an important concept for navigating the file system without needing absolute paths that include the entire directory structure from the root.

Highlights

Introduction to basic navigation in the terminal of a Unix-like operating system.

Explanation of the terminal prompt and its components: username, computer name, and current directory.

Importance of being aware of the current directory in terminal operations.

Description of the 'Home' directory and its significance in Unix-like systems.

Introduction to the tilde symbol (~) as shorthand for the home directory.

Demonstration of listing directory contents with the 'ls' command.

Process of creating a new directory using the 'mkdir' command.

Changing directories with the 'cd' command to set a new current working directory.

Creating sub-directories within the current working directory.

Using the 'nano' text editor to create and edit files in the terminal.

Saving and exiting the 'nano' text editor with keyboard shortcuts.

Creating a Python 'Hello World' program and running it using the Python interpreter.

Deleting a file using the 'rm' command.

Copying a file with the 'cp' command and creating a new version of a file.

Using the '..' shorthand to navigate up to the parent directory.

Copying an entire directory structure recursively with the 'cp -r' command.

Creating a 'todo' file to note changes required for transitioning from Python to C++ files.

Moving a file to a specific directory using the 'mv' command.

Renaming files by moving them to the same directory with a new name.

Deleting a directory and its contents using the 'rm -r' command.

Summary of basic terminal commands covered: 'mkdir', 'ls', 'cp', 'mv', 'rm', and 'nano'.

Transcripts

play00:00

Hey, Dr. Backman here. Let's talk about basic navigation in the terminal  

play00:04

of a Unix-like operating system. We'll be creating  and manipulating files and directories and  

play00:08

learning to copy, move, and delete them as well. Okay, let's go.  

play00:16

When starting a terminal, you'll see text  displayed that we call a prompt. This prompt  

play00:20

largely indicates that it's just waiting for your  input, but it also can display some interesting  

play00:24

things as well. In this case, it's displaying  3 things: 1) the username that I'm logged in as  

play00:29

that's backman; 2) the name of the computer that  I'm connected to in this case it's called kay;  

play00:34

and 3) it indicates the current directory  that the terminal is working from.  

play00:38

Being aware of your current directory is actually  really important because everything that you do  

play00:42

in a terminal happens with respect to the current  directory that you're in. It's similar to being in  

play00:46

Windows Explorer on Windows or the Finder in macOS  where you're working through a hierarchical set of  

play00:52

directories and you kind of have to remember  where you are in the midst of all of that.  

play00:55

When opening a new terminal, it's most common  for your current, working directory to be your  

play00:59

"Home" directory. Now, there's a single home  directory for each user account in the system  

play01:04

and it's basically where you store all of  your files and your home base of operation.  

play01:08

These home directories exist in Windows and  macOS as well but in Unix-like systems it's a  

play01:13

bit more central and apparent to the user. Now, there's one piece of shorthand that's  

play01:17

actually really important to know and that's  called the tilde symbol and that's the little  

play01:21

squiggly line there if you haven't heard the  name of it before. That's shorthand for the home  

play01:24

directory. So, whenever you see that tilde symbol,  know that it's referring to your home directory.  

play01:29

When we look back to look the terminal  that I'm logged into right now,  

play01:32

you can see that I'm logged in as user backman,  on computer kay, and I'm in my home directory.  

play01:37

Now that we know who and where are,  it's time to start playing around!  

play01:40

The first thing we should do is just  start looking around to see what other  

play01:42

files or directories are here. We can do  that by typing in "ls" which stands for  

play01:48

list and we can list the contents of the  current directory. So, I'll hit enter and  

play01:52

Nothing. There's not a whole lot going on here.  So, let's get started by making a directory.  

play01:57

To make a new directory you type "mkdir" which  stands for make directory and then you provide  

play02:03

a name for the directory that you intend to  create. I'm going to go with a theme of computer  

play02:06

science courses and name this directory after our  Computer Science I course, "cmsc181". Hit enter  

play02:13

and it should be there. Let's check by typing "ls"  to list the contents of the current directory.  

play02:19

There it is! We've got a directory. On the left side of the screen,  

play02:22

I've created a visualization that shows  you the impacts of the commands that we're  

play02:26

running so as we create and manipulate files and  directories, this visualization will be updated  

play02:30

so that you can see sort of what's happening with  respect to the commands that we're running.  

play02:33

Now, let's try to move into this  new directory so that we can  

play02:36

make it our current working directory. To change directories, you type in "cd"  

play02:41

and then specify the name of the directory that  you want to move in to. In this case, "cmsc181"  

play02:47

and then we'll hit enter. And there you go. Now notice that the prompt has changed! It  

play02:52

shows that the directory we're currently in is  "~/cmsc181". Now that's home slash cmsc181. Let's  

play03:00

make a few homework directories for our "cmsc181"  course. So, we'll type in "mkdir" again and  

play03:05

specify the name of the directories. We'll call  it "hw_1" for homework 1 and then "mkdir hw_2"  

play03:13

for homework 2. Now with two sub-directories  in our working directory, if we type "ls" we  

play03:18

should be able to see the results. And there we  go, homework 1, homework 2, both in our current,  

play03:23

working directory which is "cmsc181". Now I want to create a file, but I want  

play03:27

to do it in my homework 1 directory. So, I  want to move into that homework 1 directory  

play03:31

which we can do by typing "cd hw_1". And  now, by hitting enter, you'll see that  

play03:36

the prompt has updated and it shows our new  current location our new working directory.  

play03:41

To create this new file, I'm going to use a  terminal-based text editor and it's called nano.  

play03:46

To use it, we type "nano" and then we name the  file that we'd like to edit or modify or create.  

play03:51

In this case, we'll create  a file called "todo.txt".  

play03:56

Alright, this looks different. Since we're in the  nano text editor, we're free to type any text we'd  

play04:02

like to store into this file. Since we're in a  homework 1 folder for a computer science course,  

play04:06

let's leave a note to uh, create a hello world  program in Python, the Python programming  

play04:14

language. Okay, that should do it. At the bottom of the screen, in Nano,  

play04:18

you'll see something of a menu that describes  certain keyboard shortcuts that allows Nano  

play04:22

to take specific actions. For instance, in  the bottom-left, we see this upper-carrot-x  

play04:26

which stands for exit. That means control-x,  the keyboard sequence, control with x at the  

play04:32

same time. So, let's try that option to exit  nano, control-x. At this point I'm being asked  

play04:38

if I want to save the file before exiting and I  want to type "y" for yes. Now it's asking me to  

play04:43

confirm the name of the file I intend to write  and I'm fine with "todo.txt" so I'll hit enter.  

play04:49

And there we go, we just saved and  quit. Now, type "ls" and hit enter.  

play04:54

And now you see our file here displayed in white,  actually, which is different than the other things  

play04:57

which were displayed in blue. Those other things  displayed in blue were directories and then this  

play05:02

file here is in white. It's just a nice little  differentiation that allows you to keep track  

play05:06

of the files and directories that exist. Alright, so, let's actually create a Hello  

play05:10

World file now. So, we'll do that with "nano"  and we'll say "hello.py" for a Python file.  

play05:16

Typing print("Hello World")' ought to do the  trick. I'll save and quit with control-x. Yes,  

play05:23

to say that I want to save this file. Enter, to  confirm the name of the file. And there we go!  

play05:30

And to confirm that this file exists, we'll  type in "ls" again. And now we see "hello.py"  

play05:34

and "todo.txt" both existing within our current  directory. Just for fun, let's try running our  

play05:40

Python program by invoking the Python interpreter.  We can do that by typing "python3" and then  

play05:45

naming the file that we created "hello.py".  Hit enter, and we have our Hello World.  

play05:50

Well, the "todo" file isn't needed any longer so  we can get rid of that. You can delete a file by  

play05:55

typing in "rm" and then typing in the name of the  file you intend to delete. For us that will be  

play06:01

"todo.txt". We'll press enter and then "ls" to  see that it's gone. And no more "todo" file.  

play06:07

Suppose that we want to make a copy of our Hello  World file. Something like a Hello World 2.0.  

play06:12

To make a copy of a file, you type "cp". You  specify the file that you want to copy "hello.py"  

play06:19

and then you type the destination location of the  file you want to produce. In this case, let's call  

play06:24

if "hello_2.py". We'll hit enter, type "ls", and  now you can see we have two hello world files.  

play06:32

Alright, I think we're done with our Computer  Science 1 course now. Let's go back towards  

play06:36

our home directory. But how do we go back? We  haven't talked about that yet. We definitely  

play06:41

need to type "cd" to change directories but  there's a little piece of shorthand, that's  

play06:45

really useful for going back a directory, up a  directory towards the parent. And that's, "..". So  

play06:51

we can type in "cd .." and then hit enter. Now notice that our prompt has changed and  

play06:56

the current working directory has updated  "cmsc181". We've gone back, we've gone up,  

play07:01

one directory. Let's do it once more and get back  to the home directory by typing "cd .." yet again.  

play07:08

And now we can see that we're in just tilde, the  home directory. Here's another little tip. If you  

play07:13

just type in "cd" and immediately hit enter, then  it'll take you directory to your home directory  

play07:18

no matter what your current working directory  is. That's actually a really handy shortcut.  

play07:23

We'll, now that we've got this "cmsc181" directory  structure for our Computer Science 1 course,  

play07:28

let's just replicate it and use it as the  foundation for our Computer Science 2 course  

play07:31

"cmsc182". We can do this pretty easily with the  copy command "cp" to copy "cmsc181" to "cmsc182"  

play07:40

but, there is one little bit of a difference  that's required for copying directories.  

play07:44

And that's that immediately after typing "cp"  you need to add a "-r" and this specifies that  

play07:49

we want to recursively copy the contents of  the original directory into that new location.  

play07:54

SO, in this case, we can type "cp -r cmsc182"  followed by "cmsc182" our destination folder. And  

play08:02

hit enter. Let's type "ls" to see what we've got.  And there we go, a brand new "cmsc182" directory  

play08:09

standing right next to our "cmsc181" directory. So, one little side distinction I should make is  

play08:13

that our "cmsc181" course is taught in Python  and our "cmsc182" course is taught in C++. So,  

play08:20

I should really go into our "cmsc182" folder and  change all of those files from being Python files  

play08:25

to C++ files. I'm going to create another "todo"  text file to note all of that. SO, "nano todo.txt"  

play08:32

and "Change all of the python files to C++ files".  Alright, control-x, yes I want to save the buffer,  

play08:41

confirm the file name. Okay, but wait, this "todo"  file doesn't really belong in my home directory,  

play08:46

it belongs in the homework 1 folder of my  "cmsc182" directory. So, let's move it there.  

play08:52

To move a file, you type "mv" short for move and  then specify the file you want to move "todo.txt"  

play08:58

in this case. And, we'd like to push it into the  homework 1 folder of "cmsc182". So you'll type in  

play09:04

the destination you'd like it to go to which would  be "cmsc182/hw_1/todo.txt". And then hit enter.  

play09:15

So, notice here that we typed a path to the  destination point which traversed through a  

play09:21

couple of directories. And we did it all relative  to where we currently are the current working  

play09:26

directory. That's actually called a relative  path and we should get familiar with the notion  

play09:30

of using relative paths. And let's now type  the path of the folder we want to get to.  

play09:35

So, we could type "cd cmsc182" and then "cd  hw_1" but let's just do it all in one shot  

play09:41

with a relative path. "cd cmsc182/hw_1". Enter.  We'll see that our path updated in our prompt  

play09:50

which is great. And now we can type "ls" to see  what we've got. And here we've got the copied  

play09:55

Python files as well as the "todo" file. Now, to rename these Python files,  

play10:00

I haven't exactly talked about how to do that  but we have used a command that can be used  

play10:04

to rename files and that's the move command  (mv). Now watch this, I'll move "hello.py"  

play10:11

to a new location and this time I'm going to  call it "hello.cpp" for our C++ file and I'll  

play10:17

hit enter. "ls" and now you can see that we've  updated the filename just by moving it from  

play10:23

the current directory to the same directory but  with a new filename as it's destination point.  

play10:28

Now let's do the same with the next file "mv  hello_2.py" to "hello_2.cpp". Enter, and "ls".  

play10:39

Now both of our Python files have been  updated to "cpp" files. Excellent  

play10:43

I think we're done with our "cmsc182" course now  so let's go back to the home directory. We could  

play10:48

do this just by typing "cd" and hitting enter that  cool shortcut I talked about before, or we could  

play10:52

type "cd .." followed by "cd .." but let's get  used to the notion of relative paths again. We can  

play10:58

chain these things together by typing in "cd .."  which would get us back to the "cmsc182" directory  

play11:04

but then followed by a "/.." yet again to take  us to its parent directory the home directory.  

play11:10

If we hit enter, we'll see that the prompt has  updated to just tilde, just our home directory.  

play11:16

We're about done here but there's one last  thing that I want to show you how to do,  

play11:19

and that's how to delete a directory. So, let's  just delete our Computer Science 2 directory,  

play11:22

"cmsc182". We saw before that you can type in "rm"  to remove a filename deleting the file. To delete  

play11:30

a directory it's very similar but, just like "cp",  there's one little extra piece of information  

play11:35

that you need and that's to specify that we  want to delete the contents of the directory  

play11:39

recursively. So, "rm -r cmsc182" for the  directory we want to delete. We'll hit enter  

play11:46

and now let's type "ls". All we have left is  "cmsc181", the "cmsc182" directory is all gone.  

play11:54

Alright, we've done it. That was a great start and  now you've seen a lot of the basics. We've covered  

play11:58

making directories with "mkdir", we talked about  listing the contents of directories with "ls",  

play12:02

we talked about copying files with "cp", moving  files with "mv", removing files deleting files  

play12:08

with "rm", and we event talked about editing  and creating files with nano. A good start.  

play12:13

Now, go off and change the world! Or, at least  the contents of your home directory. Have fun!

Rate This

5.0 / 5 (0 votes)

相关标签
Unix TutorialTerminal CommandsFile ManagementDirectory NavigationCommand LineHome DirectoryText EditorPython CodingC++ ConversionSystem Administration
您是否需要英文摘要?