what are hard and symbolic links on Linux, how to create them, and what is the difference between the two ?

1- Hard and symbolic links on Linux :

      Before explaining and define the two links we should define what is a link on Linux. A link in Linux is a pointer to a file or a directory. Creating links is a kind of shortcuts to access a file. Links allow more than one file name to refer to the same file. There are two types of links : Symbolic and Hard links .

Symbolic links :

      A symbolic link, also termed a soft link, is a special kind of file that points to another file, much like a shortcut in Windows or a Macintosh alias. A symbolic link does not contain the data in the target file. It simply points to another entry somewhere in the file system. 

How we create a symbolic link :

 ln -s source_file myfile

img_58656ab05449e

Hard links :

A hard link is essentially a label or name assigned to a file. Conventionally, we think of a file as consisting of a set of information that has a single name. However, it is possible to create a number of different names that all refer to the same contents. Commands executed upon any of these different names will then operate upon the same file contents.

How to create a hard link :

 ln source_file myfile

hardlink-56747ee25f9b586a9e497536

Differences between symbolic link and hard link

Let’s summarize our findings. The list bellow summarizes some differences between symbolic and hard link:

  1. Hard link cannot be created for directories (folders). Hard link can only be created for a file.
  2. Symbolic link can link to a directory (folder).
  3. Removing the original file that your hard link points to does not remove the hard link itself; the hard link still provides the content of the underlying file.
  4. If you remove the hard link or the symbolic link itself, the original file will stay intact.
  5. Removing the original file does not remove the attached symbolic link but without the original file, the symbolic link is useless (the same concept like Windows shortcut).

 

What happens when you type ‘ls *.c’ and hit enter in your shell?

As a software engineer student, I should know most of commands and explain it to the others who are curious about the software. In this article I will explain what will happen when we type ls *.c ? . Before that I need to define what is The Shell and terminal .

1-What Is The Shell?

When we speak of the command line, we are really referring to the shell. The shell is a program that takes keyboard commands and passes them to the operating system to carry out. Almost all Linux distributions supply a shell program from the GNU Project called bash. The name “bash” is an acronym for “Bourne Again SHell”, a reference to the fact bash is an enhanced replacement for sh, the original Unix shell program written by Steve Bourne.

2-What’s a terminal?

      It’s a program called terminal emulator. A terminal is a text-based interface that allows you to type and execute commands to your operating system.

3-What happen when we type ls :

    The “ls” command is a command-line utility for listing the contents of a directory or directories via standard input. It writes results to standard output. To show the contents of a directory pass the directory name to the ls command. This will list the contents of the directory in alphabetical order..

sort-ls-output-by-modified-date-and-time

4- What happen when we type ls *.c :

1. ls command lists files and directories.

2. ls command can be followed by options and arguments to supplement the functionality of the command.

3. * — is a wildcard character that can be used as a substitute for an array of characters in a search.

4. * — can be used with strings or characters/symbols to refine or modify parameters of a search.

5. ls *(string) will result in finding any and all files with the string pattern at the ending of the filename. Vice-versa if we do it ls (string)* it will search for the string pattern at the beginning of the filename.

6. ls *.c will try to list all files with the .c extension, if no files are found the command line output prompt will be something like this “ls: cannot access *.c: No such file or directory”