Best Way to Edit sudoers File in Linux

In Linux, the sudoers file present in the /etc directory will determine the permissions and other settings  while the sudo command is used. If you edit the file directly using terminal or using an editor, if there is any error in your code, the whole sudo command will break. So it is not the best way to edit the sudoers file.

The best way to edit the sudoers file is to use the following command :

$ sudo visudo

This will open the sudoers file in vi editor and you will be able to edit the file here. Once you try to save, it will check for errors and will let you know the same with options to save, not save or re-edit the file.

So it is the safer option while trying to edit sudoers file.

How to Create Directory Structure using Terminal

In this article, we are going to see how to create a directory structure using terminal.

The “mkdir” command  is used to create a folder or a directory using terminal. Now for creating a folder within another folder (multilevel directory structure), if  you use the “mkdir” command like the following command, you will get one error message like this “mkdir: dir1/dir2: No such file or directory”.

$ mkdir dir1/dir2/dir3

Now if you want the terminal to create the structure itself when you try to create a directory, you will have to use the command like this :

$ mkdir -p dir1/dir2/dir3

This will first create the directory, “dir1”, then inside dir1, it will create the directory “dir2” and finally inside dir2, it will create the directory “dir3”.

Single Line Conditional Statement for Terminal

In terminal, it is possible for you to write single line conditional statements, where you can execute a single line of statement if a condition satisfies or otherwise.

Here is the command which you can use.

$ [ -d yourfolder ] && echo “Directory Exists.” || echo “Directory does not exists.”

This command will first check whether there is a file named “yourfolder” in the current location and based on the result, it will decide which statement to execute.

Here if the folder is there, then it will display the line “Directory Exists.” in the terminal and if the folder is not there, then it will display the line “Directory does not exists.”.

How to Create a Directory if Not Already Created using Terminal

In this article, we are going to see how to check whether a directory is that or not in terminal and create the directory if not already there.

For this, then you can use the following command :

$ [ -d yourdirectory ] || mkdir yourdirectory

This is similar to an if condition. First it will check whether there is a directory name “yourdirectory” in the current path and if the directory is there, then it will do nothing. But if the directory is not there, it will create the directory.

How to Delete a Folder using Terminal

In this article, we are going to see how to delete a folder using terminal.

If you want to delete an empty folder, then you can use the following command :

$ rmdir emptyfoldertodelete

This will delete the folder if it is empty. But if the folder is not empty, then it will display one error message like this.

rmdir: emptyfoldertodelete: Directory not empty

If you want to delete the folder which is not empty, then you can use the following command :

$ rm -r foldertodelete

This will delete the folder even if the folder is not empty.

How to Delete the Contents of a Folder using Terminal

In this article, we are going to see how to delete the contents of a folder using terminal without deleting the folder itself.

For this, you can execute the following command :

$ rm -r “yourfoldernamewithpath/“*

This command will remove all the contents of the folder that you specify without deleting the folder. Please ensure that no space is there between the file name and *