How to Convert doc File to txt File using Mac Terminal

The Mac terminal provides a powerful “textutil” command, which can be used to convert .doc files to .txt files directly from the terminal without opening the .doc file.

Here is the command which can be used for the same :

$ textutil -convert txt yourdocfile.doc

Here, the doc file “yourdocfile.doc” will be converted to txt file and a new file “yourdocfile.txt” will be created in the folder with the contents of the doc file.

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 *

Moving a File from One Folder to Another Using Mac Terminal

In this article. you will see how to move a file from one folder to another without using Mac terminal.

For this, I have with me, two folders : “junk” and “junk1”. Now lets list the contents of those two folders before we start.

:junk admin$ ls -l

total 16

-rw-r–r–  1 admin  staff  32 Nov 27 09:20 file1.txt

-rw-r–r–  1 admin  staff  50 Nov 27 09:20 file2.txt

 

:junk1 admin$ ls -l

Here, you can see that the folder “junk” contains two files : file1.txt and file2.txt. And the folder “junk1” is empty with no files.

Lets say you want to move file1.txt from the folder “junk” to “junk1” in my “Desktop”.

First ‘cd’ into your source folder. In this case, its the “junk” folder. Now, here is how you can do the copy :

:junk admin$ mv file1.txt ../junk1

As you can see, we are using the ‘mv’ command. The ‘../’ pattern will point to the parent path of the current working directory.

Now you can list the contents of the folder “junk”.

:junk admin$ ls -l

total 8

-rw-r–r–  1 admin  staff  50 Nov 27 09:20 file2.txt

Here you can see that the file, “file1.txt” is not there in the “junk” folder now.

Next you can ‘cd’ into your “junk1” folder and list the contents of the same. You will see something like this :

:junk1 admin$ ls -l

total 8

-rw-r–r–  1 admin  staff  32 Nov 27 09:20 file1.txt

From this, you can see that the file is moved to your destination folder.

Copying Files from One Folder to Another without Using Path in Mac Terminal

In this article. you will see how to copy a file from one folder to another without using path in Mac terminal.

For this, I have with me, two folders : “junk” and “junk1”. Now lets list the contents of those two folders before we start.

:junk admin$ ls -l

total 16

-rw-r–r–  1 admin  staff  32 Nov 27 09:20 file1.txt

-rw-r–r–  1 admin  staff  50 Nov 27 09:20 file2.txt

:junk1 admin$ ls -l

Here, you can see that the folder “junk” contains two files : file1.txt and file2.txt. And the folder “junk1” is empty with no files.

Lets say you want to copy file1.txt from the folder “junk” to “junk1” in my “Desktop”.

First ‘cd’ into your source folder. In this case, its the “junk” folder. Now, here is how you can do the copy :

junk admin$ cp file1.txt ../junk1

As you can see, we are using the ‘cp’ command and not giving the full path of the destination folder. Rather, we are using the ‘../’ pattern to point to the parent path of the current working directory.

Next you can ‘cd’ into your “junk1” folder and list the contents of the same. You will see something like this :

:junk1 admin$ ls -l

total 8

-rw-r–r–  1 admin  staff  32 Nov 27 10:24 file1.txt

From this, you can see that the file is copied to your destination folder.

Finding the Difference in Two Files using Mac Terminal

In this article. you will see how to find the difference between the contents of two files using Mac terminal.

For this, I have with me, two text files with names “file1” and “file2”. Now lets see what the contents of those files are. For this, we can use the ‘cat’ command.

$ cat file1.txt

File Content :

file1

content A

$ cat file2.txt

File Content :

file2

content B

New line in file2

Now here is how you can find the differences in the contents of those two files :

$ diff file1.txt file2.txt

3,4c3,5

< file1

< content A

> file2

> content B

> New line in file2

Here, in the result of the ‘diff’ command, you can see that the contents that are different corresponding to both the files are listed. First, it will list the difference in content of the first file we are giving as parameter. For better understanding, lets change the order of file arguments and see :

$ diff file2.txt file1.txt

3,5c3,4

< file2

< content B

< New line in file2

> file1

> content A

 

Here, you from the result, you can see that when we change the order of parameter, the order of differences displayed by the ‘diff’ command also changes.