0x00. Shell, navigation

File System Organization

Like Windows, the files on a Linux system are arranged in what is called a hierarchical directory structure. This means that they are organized in a tree-like pattern of directories (called folders in other systems), which may contain files and subdirectories. 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.
The basic three commands include:

  1. pwd (print working directory)

  2. cd (change directory)

  3. ls (list files and directories).

pwd

The directory we are standing in is called the working directory. To see the name of the working directory, we use the pwd command.

[me@linuxbox me]$ pwd
/home/me
Enter fullscreen mode
Exit fullscreen mode

When we first log on to our Linux system, the working directory is set to our home directory.

cd

To change the working directory (where we are standing in the maze) we use the cd command. To do this, we 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.

me@linuxbox me]$ cd /usr/bin
me@linuxbox bin]$ pwd
/usr/bin
Enter fullscreen mode
Exit fullscreen mode

If we type cd followed by nothing, cd will change the working directory to our home directory.

me@linuxbox me]$ cd
me@linuxbox bin]$ pwd
/home/me
Enter fullscreen mode
Exit fullscreen mode

A related shortcut is to type cd ~user_name. In this case, cd will change the working directory to the home directory of the specified user.

me@linuxbox me]$ cd ~me
me@linuxbox bin]$ pwd
/home/me
Enter fullscreen mode
Exit fullscreen mode

Typing cd - or cd .. changes the working directory to the previous one.

me@linuxbox me]$ cd /usr/bin
me@linuxbox bin]$ pwd
/usr/bin
me@linuxbox me]$ cd ..
me@linuxbox bin]$ pwd
/usr
Enter fullscreen mode
Exit fullscreen mode

ls

It is used to list the files in the current working directory.

[me@linuxbox me]$ ls
Desktop Download Pictures Music Templates Documents examples.desktop Public Videos
Enter fullscreen mode
Exit fullscreen mode

File names that begin with a period character are hidden. This only means that ls will not list them unless we say ls -a.

[me@linuxbox me]$ ls -la
.git/ .ssh/ .ipython/ Desktop Download Pictures Music Templates
Enter fullscreen mode
Exit fullscreen mode

ls -lList the files in the working directory in long format

[me@linuxbox me]$ ls -l
drwxr-xr-x 1 me 197121 0 Oct 17 2023 OneDrive/
drwxr-xr-x 1 me 197121 0 Jan 17 2023 Pictures/
drwxr-xr-x 1 me 197121 0 Mar 3 2023 Saved Games/
drwxr-xr-x 1 me 197121 0 Apr 27 2023 Searches/
Enter fullscreen mode
Exit fullscreen mode

Displaying file contents

There are several commands to display the content of a file in Linux.

  • Using cat command
$ cat filename
Enter fullscreen mode
Exit fullscreen mode
  • Using head and tail commands

The head command displays the first 10 lines of a file, while the tail command displays the last 10 lines of a file.

$ head filename # displays the first 10 lines of a file
$ tail filename # displays the last 10 lines of a file
Enter fullscreen mode
Exit fullscreen mode

You can modify the number of lines displayed by using the -n option, for example:

$ head -n 5 filename # displays the first 5 lines of a file
$ tail -n 5 filename # displays the last 5 lines of a file
Enter fullscreen mode
Exit fullscreen mode
  • Using less command

The less command allows you to view a file one page at a time. It allows you navigate through the file using the arrow keys or page up/down keys.

$ less filename
Enter fullscreen mode
Exit fullscreen mode
  • Using awk command

This command uses awk to print each line of the file.

$ awk '1' filename
Enter fullscreen mode
Exit fullscreen mode

Creating files and directories

Create a file:

  • Using the touch command:
$ touch filename
Enter fullscreen mode
Exit fullscreen mode

This will create a new empty file with the specified name.

  • Using a text editor:
$ nano filename # using the nano editor.
$ vi filename # using vim editor.
$ code filename # using vscode editor.
Enter fullscreen mode
Exit fullscreen mode

This will open a text editor where you can create and edit the file. Once you’re done, save and exit the editor.

  • Using the echo command:
$ echo "Hello World!" > filename
Enter fullscreen mode
Exit fullscreen mode

This will create a new file with the specified name and add the text “Hello World!” to it.
Create a directory:

  • Using the mkdir command:
$ mkdir directoryname
Enter fullscreen mode
Exit fullscreen mode

This will create a new directory with the specified name.

Removing a file or directory

Removing a file:
To remove a file, use the rm command followed by the name of the file you want to remove:

$ rm filename
Enter fullscreen mode
Exit fullscreen mode

If the file is write-protected, rm will ask you to confirm the deletion. To remove the file without prompting, use the -f option:

$ rm -f filename
Enter fullscreen mode
Exit fullscreen mode

Removing a directory:
To remove an empty directory, use the rmdir command followed by the name of the directory:

$ rmdir directoryname
Enter fullscreen mode
Exit fullscreen mode

If the directory is not empty, you will get an error message. To remove a non-empty directory and all its contents, use the rm command with the -r option:

$ rm -r directoryname
Enter fullscreen mode
Exit fullscreen mode

Moving or Copying a file or directory

Moving a file or directory:
To move a file or directory, use the mv command followed by the source file or directory and the destination:

$ mv source destination
Enter fullscreen mode
Exit fullscreen mode

Renaming a file or directory:
To rename a file or directory, use the mv command with the source file or directory and the new name:

$ mv oldname newname
Enter fullscreen mode
Exit fullscreen mode

Copying a file or directory:
To copy a file or directory, use the cp command followed by the source file or directory and the destination:

$ cp source destination
Enter fullscreen mode
Exit fullscreen mode

To copy a directory and all its contents, use the -r option with the cp command:

$ cp -r source destination
Enter fullscreen mode
Exit fullscreen mode

This will copy the entire directory source and all its contents to destination.
Using rsync command:
The rsync command is a powerful tool for copying and synchronizing files and directories. It can be used to copy files and directories while preserving permissions, timestamps, and other attributes:

$ rsync -avz sourceDir destinationDir
Enter fullscreen mode
Exit fullscreen mode

This will copy the entire directory sourceDir and all its contents to the specified destination, preserving permissions, timestamps, and other attributes.

Thanks for your time! Please leave a comment and any suggestions are welcome. Follow me to get updates.


Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top