Skip to content

Instantly share code, notes, and snippets.

@akuya-ekorot
Created June 8, 2023 17:45
Show Gist options
  • Save akuya-ekorot/1764d9577f9f245f3b7c1372a25c7b6d to your computer and use it in GitHub Desktop.
Save akuya-ekorot/1764d9577f9f245f3b7c1372a25c7b6d to your computer and use it in GitHub Desktop.
This is a script for my video on shell navigation

Shell Navigation

Keyword and Title Brainstorming

Keywords

Linux Command Line, Linux commands, Linux terminal, Terminal commands, Bash Commands

Draft Titles

Navigate the Linux System Like A Pro.

Navigate the Linux System Like A Pro: ls and cd Commands.

Objectives

  • See what files and directories I have
  • Move around the computer
  • Specify the location of a file or a directory
  • Relative and absolute paths

Intro

This is the first video in the series Navigate the Linux System Like A Pro.

At the end of this video, you'll learn

  • how to see what files and directories you have in your linux system
  • how to move around the computer by changing your working directory
  • how to specify the location of a file or a directory
  • and the difference between relative and absolute paths.

Let's jump straight in.

Main

  • Before you go anywhere, figure out where you are first. That sounds like something a monk would say.
    • and that brings us to our first command.
    • when you're in the terminal, you can check where you are using the pwd command.
    • (which stands for 'print working directory') and it does exactly what the name says. It prints the path to your current working directory.
    • Knowing where we are before running commands is important because most commands read and write files in the current working directory.
    • Here's an example of running pwd in your home directory.
seg@fault:~$ pwd
/home/akuya
seg@fault:
  • Before going any further, let's try and understand how the whole file system is organized.
  • On Ubuntu, the folder structure looks like this.
  • The root directory holds everything. It is represented with a /
  • Every other directory is contained inside it.
  • From our example above, we can see we are in the directory akuya which is inside the home directory which is inside the / directory.
  • Now that you know where you are in the file system, how can you see the contents of the folder you're in?
  • This is where the command ls comes in.
  • Head over to your terminal and type the command
seg@fault:~$ ls
Applications  Documents  Desktop  Downloads  main.c  script.sh
seg@fault:~$
  • The output depends on what you actually have in you home folder.
  • The ls command prints the names of directories and files in the current working directory.
  • This command, as many others, also take in options to modify the output of the command.
  • Options are usually prefixed with a - or --.
  • An example of an option to pass to the ls command is the -F option.
  • This option classifies the output by adding a marker to a file or directory to indicate what they are.
    • a trailing / shows that it is a directory
    • @ indicates a link
    • * indicates an executable
  • Here's how you run the ls command with the -F option
seg@fault:~$ ls -F
Applications/  Documents/  Desktop/  Downloads/  main.c  script.sh*
  • Apart from the -F option, the ls command has many more options.
  • To figure out how to use a command and all the options it accepts, you can run the ls command with --help option.
seg@fualt:~$ ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if neither -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options, too.
  -a, --all                  do not ignore entries starting with .
  -A, --almost-all           do not list implied . and ..
      --author               with -l, print the author of each file
  -b, --escape               print C-style escapes for nongraphic characters
      --block-size=SIZE      scale sizes by SIZE before printing them; e.g.,
                               '--block-size=M' prints sizes in units of
                               1,048,576 bytes; see SIZE format below
  -B, --ignore-backups       do not list implied entries ending with ~
  -c                         with -lt: sort by, and show, ctime (time of last
                               modification of file status information);
                               with -l: show ctime and sort by name;
                               otherwise: sort by ctime, newest first
  -C                         list entries by columns
      --color[=WHEN]         colorize the output; WHEN can be 'always' (default
                               if omitted), 'auto', or 'never'; more info below
  -d, --directory            list directories themselves, not their contents
  -D, --dired                generate output designed for Emacs' dired mode
  -f                         do not sort, enable -aU, disable -ls --color
  -F, --classify             append indicator (one of */=>@|) to entries
...        ...        ...
  • The other way to learn more about a command is by using the man command, followed by the command you want to know about. For example man ls.

  • The man command, which stands for manual, turns your terminal to a manual page of the command.

  • You can scroll the manual page using the up and down arrow or using the vim commands k and j for up and down respectively.

  • To quit out of the manual page, press the q key.

  • Here are some interesting options for the ls command.

    • This command lists all content of the current working directory, including the hidden files starting with a ..
    seg@fault:~$ ls -a
    • This command lists all content of the current working directory just like the previous one, but in long format.
    seg@fault:~$ ls -al
    • This command lists contents of the current working directory and sorts them by time, newest first.
    seg@fault:~$ ls -t
    • Here's a little homework. What options would you pass along with the ls command to list contents of the current working directory in long formart reverse-ordered by the time they were modiefied?
    • Write your answer in the comment section below!
  • If you've been following along, you might find your terminal looking a bit too cluttered for you, with all your previous commands and output still there.

  • You can clean up your terminal with the clear command. Now you have a clean terminal.

  • The clear command also has a nice option. Running the clear command with the -X option clears your terminal but you now have the option of scrolling up to see all the previous commands you ran.

  • I love adding that option. Best of both worlds.

  • So far, we've been looking at listing contents of the current working directory, but you can also list the content of other directories.

  • You can do this by passing the path of the directory you're targeting after the options in the ls command.

  • Here's an example of displaying the content of the Downloads directory which is inside the current working directory.

seg@fault:~$ ls -a Downloads/
  • The path Downloads/ that we passed in the previous command is a relative path.
  • A relative path is a path that considers the current working directory.
  • There are several interesting ways of navigating using the relative path.
  • Before we get into it, let me first introduce the cd command.
  • This command stands for change directory. And from the name, we get to know what it actually does, it takes us to another directory.
  • The cd command takes in a path as it's arguement.
  • If we wanted to go into the Downloads/ directory, we would use the following command.
seg@fault:~$ cd Downloads/
seg@fault:~/Downloads$
  • Another way to go into a folder contained in the current working directory would be by adding the relative path ./
  • Here's an example
seg@fault:~$ cd ./Downloads/
seg@fault:~/Downloads$
  • The ./ is a relative path that represents the current working directory.
  • If we wanted to go back to the parent directory we could run the following command
seg@fault:~/Downloads$ cd ..
seg@fault:~$
  • The .. is a relative path that represents a folder up, or the parent directory.
  • You can chain the .. to go two folders up. Here's how you would go to the home folder from the movies folder contained in the downloads folder.
seg@fault:~/Downloads/Movies$ cd ../..
seg@fault:~$
  • Since the home folder was two directories up in the previous example, another way to go to the home directory is to use the relative path symbol for home which is the ~ tilde symbol.
  • Here's how to do it.
seg@fault:~/Downloads/Movies$ cd ~
seg@fault:~$
  • Or we can just simply type cd which always takes us back home.
seg@fault:~/Downloads/Movies$ cd
seg@fault:~$
  • I wish there was a way to use this in real life, I wouldn't have been lost that much as a kid.

  • Instead of using relative paths, we can also use absolute paths.

  • Absolute paths are the full path of the file or directory from the root /

  • We actually saw an absolute path before when we ran the pwd command.

  • Let's run it again and see the absolute path of the current working directory

seg@fault:~$ pwd
/home/akuya
seg@fault:~$
  • If we wanted to go to the download folder from home, we could pass the absolute path like so
seg@fault:~$cd /home/akuya/Downloads
seg@fault:~/Downloads
  • Another useful symbol to pass to cd is the - symbol.
  • This symbol takes us the previous directory we were in.
  • Think of it as the back button in a browser.
  • As an example, let's say we were in the Movies folder. let's navigate there using this command. I'll pass in the absolute path for this example.
seg@fault:~$ cd /home/akuya/Downloads/Movies
seg@fault:~/Downloads/Movies$
  • Then, let's assume we need to access the Documents folder contained in our home directory.
  • I'll navigate there using the relative path.
seg@fault:~/Downloads/Movies$ cd ../../Documents
seg@fault:~/Documents$
  • If we wanted to go back to our movies folder now, instead of passing the relative or absolute paths, we could pass the - and that would take us to the folder we were at last.
seg@fault:~/Documents$ cd -
seg@fault:~/Downloads/Movies$
  • If we wrote the same command again, it would take us back to the Documents folder.

Recap

  • We have looked pwd command which prints the current working directory.
  • We then looked at the ls command which prints the contents of a directory.
  • We looked at various options that the ls command takes.
  • We also learnt about the cd command. Which changes the directory we are in the to the specified one.
  • We then looked at relative and absolute paths and how to use them to navigate the file system.

Outro

  • Thank you so much for watching.
  • Please like this video and subscribe for more content like this.
  • If you're a C programmer, or just getting started with C, check this other video I made where I went into detail explaining the Compilation process of C programs using GCC.
  • Thank you, see you on the next one.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment