CLI fundamental commands — a sailing journey

Diamond Dai
7 min readMay 31, 2024

--

Linux is the most popular operating system, especially for developers. Usually, we access Linux without Graphical User Interface (GUI) to do document management such as opening directories, editing files, etc. To interact with a Linux system, we would need to use Command Line Interface (CLI) such as Shell and know the commands.

Command Line Interface CLI (left) vs Graphical User Interface GUI (right)

In this article, I will revisit some basic commands of Shell and have a comparison among different platforms such as Windows CMD, PowerShell, Mac terminal, and Bash (almost the same as Linux CLI).

First of all, how do we open the CLI on your computer? For Linux, CLI is the default user interface as it is primarily a text-based operating system. I will use Bash as a demonstration in the upcoming discussion.

Download link of Bash: https://git-scm.com/downloads

For the Apple Macbook, you can search in “Launchpad” by typing “Terminal” to enter CLI.

We can enter “cmd” or “PowerShell” in the search bar and open it for Windows.

I will give the summary of commands here for your quick reference. You can know more details in the following discussion.

10 basic commands of CLI file management for different platforms

The 1st command is “pwd”. This command helps to check where we are currently located, in other words, locate in which directory.

Both Powershell and Mac terminal support this command, while cmd does not recognize this command, although you can see the current location in cmd windows already.

The 2nd command is “ls”. This will print out all the sub-directories (Bash indicates sub-directories as blue color with slash / ) and files inside the current directory. Again, cmd does not recognize this command, we need to use “dir” as equivalent.

Both Bash, and Powershell support “dir” command, but not in the Mac terminal. For Mac, we need to use “ls” for navigation.

It seems we are on a ship in the ocean. Now we have a navigator to tell us where we are and which ports we can go to.

The 3rd command “cd [directory_name]” helps us to move forth and back. Take mine as an example, I can move to the sub-directory BrainStorm, Images, Paragraph, or Script. So let’s move to Paragraph, we type “cd Paragraph” and press Enter. You will see the current location changes and move into the “Paragraph” directory. Let’s explore what is included in this directory, by typing “ls” and pressing Enter.

There is a text file paragraph1.txt inside.

Now if we need to move away from this directory and go back to the parent directory, we can type “cd ..” and press Enter. The two dots stand for the parent directory.

One more thing we can pay attention to is we can go forth and back two or more layers of directories. Say if there is a sub-directory “Creative Department” inside the directory in “Brainstorm”, we can enter cd “BrainStorm/Creative Department” to go directly (Note that if there is space in the directory name, we should use “ ” to highlight the directory name). On the contrary, the command “cd ../..” can help us to return the parent directory of the parent directory.

Awesome! Now you can sail the ship freely like a helmsman!

The 4th command is “mkdir [newDirectoryName]”. This creates a new directory in the current location. For example, we can create a new directory called “Writing” in the current directory “CLI_Demo” by entering “mkdir Writing”, and then check by ls command. You will see an additional directory “Writing” appear in the current location.

Our construction workers built up a new port. It’s good!

Let’s go inside the newly created directory Writing by cd command and build up something. The 5th command is to build a new file command “touch [fileName]”. Here we just create a file called “writing1.txt”, so the command should be “touch writing1.txt”. You will see the testing.txt file appear in the current directory by the ls command.

The “touch” command is for Bash and Mac terminals only. For PowerShell, we should use “New-Item [fileName]”. For cmd, we should use “cd>[fileName]”.

Excellent! We have manufacturers and merchants to create goods in the port now.

The 6th command is “mv [fileName] [Destination]”. This helps move an existing file from one directory to another. Let’s go back to our original directory and move the existing file “Document1.txt” to the newly created directory “Writing”, by entering “mv Document1.txt Writing”. And we will see the file now appears in its new location.

This command works except for Windows cmd. It requires us to use “move [fileName] [Destination]” instead.

It’s wonderful. We have crews to move the goods and ship to another port.

The 7th command is “cp [fileName] [Destination]”. This helps copy files to the target directory. Given that we have a file “writing1.txt” inside the directory “Writing”, we need to copy it from the “Writing” directory to the “Paragraph” directory. We can enter the command “cp Writing/writing1.txt Paragraph” to handle the task.

You can also change the name of the copied file. For example, a copy file with changing name as “writing1_copy.txt” can run the following command: “cp Writing/writing1.txt Paragraph/writing1_copy.txt”.

Again, CLI other than Bash can use this command except Windows cmd requires us to follow the full name command, using “copy [fileName] [Destination]” instead.

We can take a rest and have a nice tea as we have a quartermaster to help us with shipment arrangements.

The 8th command is “cp -r [directoryName] [Destination]”. This is similar to the 7th command but even more useful. This helps us to copy all things inside the source directory to the new location. The example is that we try to copy the whole Paragraph directory to the same location with the name “Paragraph_New”. We can enter the command “cd -r Paragraph Paragraph_New”.

The “-r” stands for recursive which means the command can be performed in all subdirectories.

Windows cmd requires to use of another type of command “xcopy [Directory] [Destination] /s /i” instead.

The 9th command is “rm [fileName]” or “rm -r [directoryName]”. This is a dangerous command as it will remove the target file or directory and all the things inside it. You even cannot recover it if the command is being run.

For example, if we need to remove the “Credit.txt” file in our current location, we enter the command “rm Credit.txt” and check, the file was deleted. If we need to remove the “Paragraph_New” directory, we enter the command “rm -r Paragraph_New” and the whole folder will disappear.

Windows cmd requires to use of another type of command “del [fileName/directoryName]” instead.

Be careful! The gunner will destroy all targets after receiving your command.

The 10th command is “clear”. This helps to clear all the current command history. You can use this command whenever the screen is full of text and you feel annoyed. The equivalent command of Windows cmd is “cls”.

Overall, these 10 basic commands will help you to process simple file management inside Linux effectively. Below I recap the summary of 10 commands again.

I hope you enjoy this sailing journey and hope you have a good day! Looking forward to having next sailing with you.

--

--

Diamond Dai
Diamond Dai

No responses yet