File System and Directory Management in Ubunto using CLI

This Lecture teaches you the following topics:

  • Navigation through Linux file system using CLI
  • Working with directories in Linux using CLI

Introduction

  • Linux organizes its files in a hierarchical directory structure.
  • The first directory in the file system is called the root directory.
  • The root directory contains files and subdirectories, which contain more files and subdirectories and so on and so on.
  • If we map out the files and directories in Linux, it would look like an upside-down tree.
  • At the top is the root directory, which is represented by a single slash (/).
  • Below that is a set of common directories in the Linux system, such as bin, dev, home, lib , and tmp , to name a few.

  • Each of those directories, as well as directories added to the root, can contain subdirectories.

1.   Navigation

The first thing we need to learn is how to navigate the file system on our Linux system. In this section we will introduce the commands used for navigation in Linux system.

    • Print Working Directory

      • The directory we are standing in is called the current working directory.
      • To display the current working directory, we use the pwd (print working directory) command.
      • When we first log in to our system our current working directory is set to our home directory.
      • Suppose, a user is created with name me on machine Ubuntu; we display its current working directory as given below

    • Listing The Contents Of A Directory

 

      • To list the files and directories in the current working directory, we use the ls command. Suppose, a user me is in its home directory; to display the contents of current working directory can be displayed as follows:

      • Besides the current working directory, we can specify the directory to list, like so:

      • Or even specify multiple directories. In this example we will list both the user’s home directory (symbolized by the “~” character) and the /usr directory:

ls command options

The following options can also be used with ls command:

Options Long-options Description
-a – – all List all files, even those with names
-d – – directory Ordinarily, if a directory is specified, ls will list the contents of the directory, not the directory itself. Use this option in conjunction with the -l option to see details

about the directory rather than its contents.

-h – – human-

readable

In long format listings, display file sizes in human readable format rather than in bytes.
-r – – reeverse Display the results in reverse order. Normally,ls

displays its results in ascending alphabetical order.

-S Sort results by file size.
-t Sort by modification time
-l Display results in long format.

 

  • Changing the Current Working Directory

    • To change your working directory, we use the cd command.
    • To do this, type cd followed by the pathname of the desired working directory.
    • A pathname is the route we take along the branches of the tree to get to the directory we want.
    • Pathnames can be specified in one of two different ways; as absolute pathnames or as relative pathnames.
    • An absolute pathname begins with the root directory and follows the tree branch by branch until the path to the desired directory or file is completed.
    • On the other hand a relative pathname starts from the working directory.
    • Suppose, a user me is in its home directory and we want to go into the Desktop directory, then it can be done as follows:

    • The “..” operator is used to go to the parent directory of the current working directory.
    • In continuation of the above example, suppose we are in the Desktop directory and we have to go to the Documents directory. To this task, first we will go the parent directory of Desktop (i.e. me, home directory of the user) that contains the Documents directory then we will go into the Documents directory as given below.

  • Working With Directories

 In this Section, we introduce the most commonly used commands related to Directories.

    • Creating a Directory

      •  In Linux, mkdir command is used to create a directory.
      • We pass the directory name as the argument to the mkdir command.
      • Suppose, the user me is in its home directory and we want to create a new directory named mydir in the Desktop directory.
      • To do this, first we will change the current directory to Desktop and then we will create the new directory. It is shown below:

      • Multiple directories can also be created using single mkdir command as given below:

    • Copying Files and Directories

      •  cp command is used to copy files and directories. The syntax to use cp command is given below:

      • Here, item1 and item2 may be files or directories. Similarly, multiple files can also be copied using single cp command.

cp command options:

The common options that can be used with cp commands are:

Option Long Option Explanation
-a –archive Copy the files and directories and all of their attributes
-i –interactive Before overwriting an existing file, prompt the user for

confirmation

-r –recursive Recursively copy directories and their contents
-u -update When copying files from one directory to another, only copy files that either don’t exist, or are newer

 

  • Moving and Renaming Files and Directories

      • mv command is used to move files and directories.
      • This command can also be used to rename files and folder.
      • To rename files and directories, we just perform the move operation with old name and new name.
      • As a result, the files or directory is created again with a new name. The syntax to use mv command is given below:

 

      • Similarly, multiple files can be moved to a directory as given below

mv command options:

Common options, used with mv command are:

 

Option Long Option Explanation
-i –interactive Before overwriting an existing file, prompt the user for

confirmation

-u -update When moving files from one directory to another, only copy files that either don’t exist, or are newer

 

Removing and Files and Directories

      • To remove or delete a files and directories, rm command is used.
      • Empty directories can also be deleted using rmdir command but rm can be used for both empty and non-empty directories as well as for files. The syntax is given below:

rm command Options:

The common options, used with rm command are:

 

Option Long Option Explanation
-i –interactive Before deleting an existing file, prompt the user for confirmation.
-r –recursive Recursively delete directories.

Activities:

Activity 1:

In this activity, you are required to perform tasks given below:

  1. Display your current
  2. Change to the /etc
  3. Go to the parent directory of the current
  4. Go to the root
  5. List the contents of the root
  6. List a long listing of the root
  7. Stay where you are, and list the contents of /etc.
  8. Stay where you are, and list the contents of /bin and /sbin.
  9. Stay where you are, and list the contents of ~.
  10. List all the files (including hidden files) in your home
  11. List the files in /boot in a human readable

Activity 2:

Perform the following tasks using Linux CLI

  1. Create a directory “mydir1” in Desktop Directory. Inside mydir1 create another directory “mydir2”.
  2. Change your current directory to “mydir2” using absolute path
  3. Now, change you current directory to Documents using relative path
  4. Create mydir3 directory in Documents directory and go into it
  5. Now, change your current directory to mydir2 using relative path

Related Links  to Operating System topics

Operating system Course content

Lab Practice Task

#Operating System complete course #Operating System past paper #Operating System-project #Computer Science all courses  #operating system Problem with source code#University Past Paper #Programming language #Question paper #old paper #Operating System-Functions and History #Generations of Operating System #Functions of an Operating System #Components of Operating System #Types of Operating System #Services of Operating System #Properties of Operating System #Processes in Operating System #Process Scheduling in Operating System #Introduction to Linux Ubunto #Installation with virtual Box #Writing Linux Commands #Navigation in File System and Directory Management in Ubunto using CLI #File Handling and I/O Redirection In Ubunto #File Access Permission in Linux #Text Processing Tools and Basic System Configuration Tools in Linux #Package Management in Linux #How to manage processes in Linux #Compiling and Executing C++ programs in Linux #System Calls #Introduction To Shell Programming

Search within CuiTutorial

Scroll to Top