Skip to main content

Chapter 2 : Linking/archieve/add-remove-users/disk space/size of file

Notes by vishalk17 ( t.me/vishalk17 ) Devops 

 Chapter 2 : Linking/archieve/add-remove-users/disk space/size of file

=====================================================================


Linking:

 

Soft Links vs Hard Links

The ln command can be used to create two different kinds of links:

  • Soft links (for files / directories)
  • Hard links (only for files)

Soft (Symbolic) Links (for files /directories)

A soft link, sometimes called a symbolic link or symlink, points to the location or path of the original file. It works like a hyperlink on the internet.

Here are a few important aspects of a soft link:

  • If the original file is moved or deleted, the symbolic link won’t work.
  • A soft link can refer to a file on a different file system.
  • Soft links are often used to quickly access a frequently-used file without typing the whole location.
  • A soft link is something like a shortcut in Windows.

How to Use the ln Command 

By default, the ln command creates hard links. To create a symbolic link, use the -s (--symbolic) option.

To use the ln command, open a terminal window and enter the command with the following format:

ln [-sf] [source] [destination]

  • By default, the ln command creates a hard link.
  • Use the -s option to create a soft (symbolic) link.
  • The -f option will force the command to overwrite a file that already exists.
  • Source is the file or directory being linked to.
  • Destination is the location to save the link – if this is left blank, the symlink is stored in the current working directory.

For example, create a symbolic link with:

ln -s test_file.txt link_file.txt

This creates a symbolic link (link_file.txt) that points to the test_file.txt.

o verify whether the symlink has been created, use the ls command:

ls -l link_file.txt


Create a Symbolic Link to Linux Directory

A symbolic link can refer to a directory. To create a symbolic link to a directory in Linux:

ln -s /mnt/external_drive/stock_photos ~/stock_photos

This example creates a symbolic link named stock_photos in the home (~/) directory. The link refers to the stock_photos directory on an external_drive.


Force Overwrite Symbolic Links

You might receive an error message as displayed in the image below:




The error message means that there’s already a file in the destination named link_file.txt. Use the 
-f option to force the system to overwrite the destination link:

ln -sf test_file.txt link_file.txt
 



Note: Using the -f option will permanently delete the existing file.

 

Hard Links ( only for files to link)

When a file is stored on a hard drive, several things happen:

  • The data is physically written to the disk.
  • A reference file, called inode, is created to point to the location of the data.
  • A filename is created to refer to the inode data.

A hard link works by creating another filename that refers to the inode data of the original file. In practice, this is similar to creating a copy of the file.

Here are a few important aspects of hard links:

  • If the original file is deleted, the file data can still be accessed through other hard links.
  • If the original file is moved, hard links still work.
  • It just like backup of original file.

To use the ln command, open a terminal window and enter the command with the following format:

ln [-f] [source] [destination]

  • By default, the ln command creates a hard link.
  • The -f option will force the command to overwrite a file that already exists.
  • Source is the file being linked to.
  • Destination is the location to save the link – if this is left blank, the symlink is stored in the current working directory.


 

Deleting or Removing Links

If the original file is moved, deleted, or becomes unavailable (such as a server going offline), the link will be unusable. To remove a symbolic link, use either the rm (remove) or unlink command:

rm link_file.txt

unlink link_file.txt


How to Get the Size of a Directory

du command (short for "disk usage") to get the size

  • -s : Summarize  du will only display the total size of the specified directories.
  • -h : Human readable. du will print sizes in human readable format (e.g., 1K, 150M, 2G).


Without -s option du will not only display the size of the specified directory, but also the size of the subdirectories inside of that directory separately.

 


 

Check Disk Space in Linux

 

The df command (short for disk free), is used to display information related to file systems about total space and available space. by using '-h' (prints the results in human-readable format (e.g., 1K 2M 3G)).

 

To see the information of the file systems in which currently selected files saved.


 

 To displays the space available on all currently mounted file systems.


 

Archive

Using tar (Tape Archive) command:

a group of files collected together as one. The term suggests a ball of tar (tarball),

archive without gzip compression :

 

tar -cvf output_file_name.tar file1 file2 dir1 dir2

 

 

archive + compresion using gzip :

 

tar -czvf output_file_name.tar file1 file2 dir1 dir2

 

 



This is what this command is doing:

  • -c: Create an archive
  • -z: Use gzip to compress the archive
  • -v: Enable verbose mode to show the progress of the creation process
  • -f: Lets you specify the name of the archive

 

Extract tar archive

The tar command will auto-detect compression type and will extract the archive. The same command can be used to extract tar archives compressed with other algorithms such as .tar.bz2 .

unarchive ( *tar.* ):

 

tar -xvf input_file_name.tar

 

or

 

tar -xvf input_file_name.tar.gz

 



 

By default, tar will extract the archive contents in the current working directory . Use the --directory (-C) to extract archive files in a specific directory:


 

gzip :

gzip (GNU zip) used for file compression and decompression

GNU/Linux is a Unix-like operating system made up of different OS components and services that create the Linux OS. 

On Linux, gzip is unable to compress a folder, it used to compress a single file only.

For example : To compress a folder, you should use tar  (to archive folder)+ gzip (to compressed tar archive) 

gzip compression:

 

gzip output_file_name.tar ( It will compressed tar archive)

 


 

 

gzip decompression:

 

gzip input_file_name.tar.gz ( It will decompressed tar.gz )

 


 

How to Add and Delete Users on linux os

 

While running as the root user gives you complete control over a system and its users, it is also dangerous and possibly destructive. For common system administration tasks, it’s a better idea to add an unprivileged user and carry out those tasks without root privileges. 

For tasks that require administrator privileges, there is a tool installed on Ubuntu systems called sudo. Briefly, sudo allows you to run a command as another user, including users with administrative privileges

Adding a User

If you are signed in as the root user, you can create a new user at any time by running the following:

sudo su

 

adduser vishalk17      ( vishalk17 is a newuser to add )


putting your user in the sudo group, you can use the visudo command, which opens a configuration file called /etc/sudoers  or just vim /etc/sudoers

visudo   or  vim /etc/sudoers

 

root    ALL=(ALL:ALL) ALL

Below this line, add the following  line. Be sure to change vishalk17 to the name of the user profile that you would like to grant sudo privileges.

root    ALL=(ALL:ALL) ALL

vishalk17    ALL=(ALL:ALL) ALL


Add a new line like this for each user that should be given full sudo privileges. When you’re finished, save and close the file.

 

Testing sudo privilege using vishalk17 as a user :


 

Deleting a User

In the event that you no longer need a user, it’s best to delete the old account.

You can delete the user itself, without deleting any of their files, by running the following command as root:


If you previously configured sudo privileges for the user you deleted, you may want to remove the relevant line again:

visudo   or  vim /etc/sudoers


Here I have to delete vishalk17 user line. Because I have deleted user vishalk17.

This will prevent a new user created with the same name from being accidentally given sudo privileges.



Comments

Popular posts from this blog

Chapter 1 : Basics of Vi editor

 Notes by vishalk17 ( t.me/vishalk17 ) Devops  =====================================================================   1.    Starting vi Open a file with vi. Type:  vi myfile.txt 2.    vi Modes Command Mode o    Command mode is the mode you are in when you start (default mode) o    Command mode is the mode in which commands are given to move around in the file, to make changes, and to leave the file o    Commands are case sensitive: j not the same as J Insert (or Text) Mode o    The mode in which text is created. o    There is more than one way to get into insert mode but only one way to leave: return to command mode by pressing  <Esc> 3.    Setting Basic Options in vi Displaying Line Numbers     From Command Mode        :set nu     Display line numbers        :set nonu   Hide line numbers       4.    Basic commands To repeat and undo: Commands Action u Undo the last command . Repeat the l
 Notes by vishalk17 ( t.me/vishalk17 ) Devops    Chapter 3 : Linking/archieve/add-remove-users/disk space/size of file ===================================================================== groupadd command in Linux with examples           Groups  in Linux refer to the user groups. In Linux, there can be many users of a single system. In a scenario where there are many users, there might be some privileges that some users have and some don’t, and it becomes difficult to manage all the permissions at the individual user level. So using groups, we can group together a number of users, and set privileges and permissions for the entire group. groupadd command is used to create a new user group.      We can use either usermod or gpasswd command to add user to group in Linux operating system. And both commands are very easy to use.   Syntax groupadd [option] group_name            Every new group created is registered in the file “/etc/group“. To verify that the group has b