Featured image

Table of Contents Link to heading

Introduction Link to heading

  • In Linux, everything is stored in files.
  • Files are used to store data such as text, graphics, and programs.
  • Directories (or folders for Windows and MAC OS X users) are a type of file used to store other files.
  • The data that they hold are the names of the files that have been entered into them, along with the inode number (a unique identifier number assigned to each file) for where the data for that file exists on the disk.
  • Directories are used to provide a hierarchical organisation structure.

Directory Structure Link to heading

  • On a Windows system, the top-level of the directory structure is called My Computer.

  • Physical devices, such as hard drives, USB drives, network drives, show up under My Computer and are each assigned a drive letter, such as C: or D:.

  • A visual representation of a Windows directory structure: Windows directorystructure

  • The Linux directory structure, called a filesystem, also has a top-level called the root directory (symbolised by the slash / character).

  • Nonetheless,, there are no drives in Linux; each physical device is accessible under a directory, as opposed to a drive letter.

  • A visual representation of a Linux filesystem: Linux directorystructure

  • To view the contents of the root directory, use the ls command with the / character as the argument: ls/

  • Notice that there are many directories with descriptive names including /boot which contains files to boot the computer.

Home Directory Link to heading

  • On most Linux distributions, there is a directory called home under the root / directory. HomeDirectory
  • Under this /home directory, there is a directory for each user on the system. The directory name is the same as the name of the user, so a user named sysadmin would have a home directory called /home/sysadmin. UserName
  • When a user opens a shell, they should automatically be placed in their home directory.
    • The user has the full control to create and delete additional files and directories in their home directory.
    • Most other directories in a Linux filesystem are protected with file permissions.
  • The home directory has a special symbol used to represent it, the tilde ~ character.
  • It is also possible to refer to another user’s home directory by using the tilde ~ character followed by the name of the user account. For example, ~henry will be the equivalent of /home/henry.

Current Directory Link to heading

The pwd (print working directory) command can be used to determine where the user is currently located within the filesystem.

pwd

Change Directory Link to heading

The cd (change directory) command can be used to navigate the filesystem.

  • To move from the home directory into the .vim directory, use the directory name as an argument to the cd commandk cd
  • When used with no arguments, the cd command will take the user to their home directory.
  • If the user tries to change to a directory that does not exist, the command returns a “No such file or directory” error.

Paths Link to heading

A path is a list of directories separated by the / character. There are two types of paths: absolute and relative.

  • For example, /home/sysadmin is a path to the home directory: paths

Absolute Paths Link to heading

allow the user to specify the exact location of a directory, starting at the root directory and thus always begins with the / character.

  • The path /home/sysadmin is an absolute path; it tells the system to begin at the root / directory > move into the home directory > then into the sysadmin directory.

Relative Paths Link to heading

give directions to a file relative to the current location in the filesystem.

  • The user must currently be in a directory that contains objects in the path.
  • Relative paths start with the name of a directory.

Shortcuts Link to heading

  • The single period . character always represents the current directory.
  • Two period .. characters always represents one directory higher relative to the current directory, sometimes referred to as the parent directory.

Listing Files in a Directory Link to heading

The ls (list) command is used to display the contents of a directory and can provide detailed information about the files. When used with no options or arguments, it lists the files in the current directory.

  • The ls command can also be used to list the contents of any directory in the filesystem by being provided with the path to the directory as an argument. lsrelative

Listing Hidden Files Link to heading

  • A hidden file is any file (or directory) that begins with a dot . character.
  • The ls command omits hidden files by default.
  • To display all files, including hidden files, use the -a option to the ls command. HiddenFiles
  • Most of the hidden files are customisation files, designed to customise how Linux, your shell, or programs work.
    • For example, the .bashrc file in the home directory customises features of the shell, such as creating or modifying variables and aliases.
    • These customisation files are not ones that you work with on a regular basis, and having them displayed makes it more difficult to find other files.

Long Display Listing Link to heading

  • Each file has details associated with it called metadata, this can include information such as the size, ownership, or timestamps.

  • Use the -l option to the ls command to view this information. ls-l

  • In the output above, each line describes metadata about a single file.

    drwxr-xr-x 5 kali kali  4096 Nov 24 22:11 dictionary
    *rw-r--r-- 1 kali kali 45665 Nov 24 22:11 vimrc
    
  • File Type: [drwxr-xr-x 5 kali kali 4096 Nov 24 22:11 dictionary]

    • The first character of each line indicates the type of file. The file types are:
SymbolFile TypeDescription
ddirectoryA file used to store other files
-regular fileIncludes readable files, images files, binary files, and compressed files
lsymbolic linkPoints to another file
ssocketAllows for communication between processes
ppipeAllows for communication between processes
bblockfile Used to communicate with hardware
ccharacter fileUsed to communicate with hardware
  • Permissions: [drwxr-xr-x 5 kali kali 4096 Nov 24 22:11 dictionary]
    • The next nine characters demonstrate the permissions of the file.
    • Permissions indicate how certain users can access a file.
  • Hard Link Count: [-rw-r–r– 1 kali kali 45665 Nov 24 22:11 vimrc]
    • This number indicates how many hard links point to this file.
  • User Owner: [-rw-r–r– 1 kali kali 45665 Nov 24 22:11 vimrc]
    • Every file is owned by a user account.
    • This is important because the owner has the rights to set permissions on a file.
  • Group Owner: [-rw-r–r– 1 kali kali 45665 Nov 24 22:11 vimrc]
    • Indicates which group owns this file, this is important because any member of this group has a set of permissions on the file.
  • File Size: [-rw-r–r– 1 kali kali 45665 Nov 24 22:11 vimrc]
    • Size of files in bytes.
    • To present the file size in a more human-readable size, like 1K 234M 2G etc., add the -h option. The command beomes: ls -lh.
  • Timestamp: [-rw-r–r– 1 kali kali 45665 Nov 24 22:11 vimrc]
    • Indicates when a file’s contents were last modified.
  • File Name: [-rw-r–r– 1 kali kali 45665 Nov 24 22:11 vimrc]
    • The name of the file or directory.

Listing Directories Link to heading

The -d option is used to refer to the current directory, and not the contents within it.

  • To display metadata of of the /home/kali directory, use a combination of -l and -d options. ls-ld

Recursive Listing Link to heading

When you want to display all of the files in a directory as well as all of the files in all subdirectories under a directory.

  • To perform a recursive listing, use the -R option to the ls command. ls-R
  • In the example, the files in the current directory were listed first and the files in subdirectories were listed after.
  • Be careful with this option; for example, running the command on the root directory would list every file on the filesystem, including all files on any attached USB device and DVD in the system. Limit the use of recursive listings to smaller directory structures.

Sort a Listing Link to heading

  • To sort files by size, we can use the -S option.
  • While the -S option works by itself, it is most useful when used with the -l option so the file sizes are visible. ls-lS
  • It may also be useful to use the -h option to display human-readable file sizes. ls-lSh
  • The -t option sorts files based on the time they were modified. ls-tl
  • For more detailed modification time information you can use the --full-time option to display the complete timestamp (including hours, minutes, seconds). It will assume the -l option automatically. ls -t–full-time
  • It is possible to perform a reverse sort by using the -r option. It can be used alone, or combined with either the -S or -t options. ls-lrSh