Ubuntu Tutorial

Directory (Folder) : directory is used to holds files and subdirectories(child directories). files are used to store data or data structure. it is important to have directories into filesystem because directory makes easy to perform operations like store,access,update files,..

creating new directory : following are different ways to create directory.

  1.   go to directory where you want to create new directory then right click in black space > select "create folder" > enter directory name > select "create". see following example.
  2.   go to directory where you want to create new directory then use shortcut ctrl + shift + n > enter directory name > select "create". see following example.
  3. image
    create directory using file manager
  4.   mkdir command is used to create new directory. it will create directory on terminal path or location.
    syntax : mkdir option name-or-name-with-path
image
create directory using terminal

by default mkdir command does not print any message on success. if command get any error to create directory then it will print error message on terminal.

but if you want to print message on success or error use -v (print execution progress of command) option with mkdir command

Example : mkdir -v newdir

output : mkdir: created directory 'newdir'

if we try to create directory on location where system need permission. then error can be look like following. to avoid this error use sudo command with mkdir (Example : sudo mkdir name )
mkdir: cannot create directory ‘myfolder’: Permission denied

following table shows example for using mkdir command.

Syntax Example Description
mkdir name name ... mkdir folder1 folder2 this will create multiple directories separated by space.
example : folder1 folder2 gets created.
Note : space works as separator for directory name.
mkdir "name with space" mkdir "my folder" this will create directory specified in double quotes.
example : it will create one directory as my folder.
Note : directory name can have space in double quotes.
mkdir {list of directories} mkdir {"my folder2",myfolder3,myfolder4} this will create directories that are listed in curly brackets
example : 'my folder2' , myfolder3 and myfolder4 directories gets created
Note : space is not allowed in curly brackets except
for double quoted directory name.
mkdir path/directory-name mkdir folder1/folder2/folder3 this will create directory if path exist
example : folder3 will get created if folder1/folder2 path exist
mkdir -p path/directory-name mkdir -p folder1/folder2/folder3 this will create directory even if path does not exist,
path will be created automatically
example : folder3 will created. if path (folder1/folder2) does not exist it will automatically gets created
Note : -p is used to create path automatically

try following commands for practice. also try to run command without -pv and see what new changes happens into system.

  1. mkdir -pv newdir1
  2. mkdir -pv newdir2 newdir3 newdir4
  3. mkdir -pv "new dir" newdir5 newdir6
  4. mkdir -pv {"ne folder",jhon,tom,"foo joo",bob}
  5. mkdir -pv newdir1/subdir1/dsubdir2
  6. mkdir -pv newdir1//subdir2/{"john folder",john,tom,"foo joo",bob}

Listing Directory Content : ls command will list all directory content(files and subdirectories) except hidden content. we will learn more about ls command later. In following Example 3      colored text shows directory name and white color text shows file name.

image
listing directory content using terminal

following commands will print only directories(except hidden directory).

  • echo */
  • ls -d */
  • ls -l | grep "^d"

ls -R command will print all files, subdirectories with its all subdirectories and files . it print complete directory content.

Hidden Directory or Files: hidden directory or file is same as normal directory or file but it does not display by default when listing directory content. it is not security mechanism because the access is not restricted. hidden directory or file start with dot character (example .test). directory that start with dot character will be treated as hidden directory.

Listing Hidden Directory : following are ways to display hidden directory

  1. Using File Explorer : as shown in following Example 4 open desire directory > click on 3 line icon button > Select Hidden Files now File Explorer will show all hidden files and subdirectories.

    image
    listing hidden directory content using file explorer

  2. Using Terminal : execute ls -a command on terminal Now you will see list of all directory content including hidden files and subdirectories (name that start with dot character). at the beginning of output we get one dot (.) and double dot (. .) each time because each directory store 2 paths one is the current directory path (single dot) and second one is parent directory path (double dots). see Example 5.

    image
    listing hidden directory content using terminal

    echo .*/ command will print only hidden subdirectories

Changing or Opening Directory : changing directory is same as opening directory. cd command is used for moving from one directory to another. cd (Change Directory). most of the time options of cd command does not get used. when we execute cd command successfully, it changes the terminal path.

syntax : cd directory-name-or-name-with-path

see following example.

  • we list "bootstrap-4.3.1" directory content by executing ls command.
  • "js" is subdirectory of "bootstrap-4.3.1".
  • then we change directory to "js" by executing cd js (ie. change directory to js).
  • terminal path get change to js directory path as "ubuntu@ubuntu-inspiron-3542:~/Downloads/bootstrap-4.3.1/js".
  • then we list the content of "js" directory by executing ls .
  • image
    changing directory

    following are the some examples of cd command.

    Command Description
    cd .. change directory to parent directory. double dot(..) is parent directory path
    cd my\ document change directory to my document. \space used to represent space in directory name
    cd "my document" change directory to my document. double quote allow to use space in directory name
    cd js/"my test"/subdir change directory to subdir directory. here we passing path of directory to cd command