Command line is an interface or terminal for typing commands directly to a computer’s operating system. It can be used for viewing, handling and manipulating files that reside on your computer and a lot of other stuff too.
In this post, we will not get into too many details about the Command Line as this is just the first post related to it, therefore we will start with some basic commands to understand how things work.
Basic commands and terminology:
Here we have listed some basic commands that can prove to be very useful if you work on Linux or like to use the command prompt in Windows OS.
$ - Shell Prompt
It appears when the terminal is ready to accept commands. The absence of this symbol in the terminal indicates that some process is already running.
NOTE: In Command Line we refer to folders as directories. The parent of all directories and files in the file system is called root directory.
echo “Hello”
The echo command accepts the string “Hello” as standard input and prints the string back to the console screen as standard output.
pwd (Linux) / cd (Windows) - Print Working Directory
This command is used to output the name of the directory you are currently in, called the present working directory.
NOTE: When a file, directory or program is passed into a command it is called as an argument.
cd - Change Directory
This command is used to navigate directly to some other directory, we use cd with the directory’s path as argument.
cd ~
This command is used to navigate directly to the root directory.
cd ..
This command is used to move up one directory. Example it will navigate up from a directory files/imp/ to files/
mkdir - Make a directory
This command is used to create a new directory. To create a new directory we use mkdir command with the name of the directory as an argument.
For example:
mkdir myFolder
touch
This command is used to create a new file inside the present working directory. To create a new file we use touch with the filename as an argument.
For Example:
touch new.txt
ls (Linux) / dir (Windows)
This command is used to list down all the files and folders present inside the directory you are in.
ls –a
We know that ls command lists all the files and directories in the present working directory. Well adding ‘-a’ modifies the behavior of the ls command to also list the files and directories starting with a dot(.)
NOTE: Files starting with a dot are hidden and don’t appear when using ls command alone.
‘-a’ here is called an option.
Options modify the behavior of command:
-a: lists all the contents, including hidden files and directories.
-l: list all the content of a directory in long format.
-t: order files and directories by the time they were last modified.
For Example:
ls –alt
Yes, ‘-a’, ‘-l’, ‘ –t’ can be used together.
cp (Linux) / copy (Windows)
This command is used to copy file or directory. To copy a file or directory we use cp command and pass two arguments to it. The first argument contains the name of the source file and the second argument contains the name of the destination directory.
cp *
The use of ‘*’ with cp command selects all the content of the present working directory.
Special characters like ‘*’ are called wildcards.
cp m*.txt
The use of ‘*’ here selects all the files in the working directory starting with ‘m’ and ending with ‘.txt’.
mv (Linux) / move (Windows)
This command is used to move files from one place to other. To move a file we use mv command and pass two arguments to it. The first argument contains the name of the source file and the second argument contains the name of the destination directory.
NOTE: [to rename a file] we use mv command and pass two arguments to it. The first argument contains the old name and the second argument contains the new name.
rm (Linux) / rmdir (Windows)
This command is used to remove a file or directory. To remove file or directory we use rm command and pass the name of that file as an argument to it.
rm –r
Here ‘-r’ is an option that modifies the behavior of rm command. The ‘-r’ stands for recursive and it is used to delete a directory and all of its child directories.
Conclusion
If you spend the time to learn these commands you will be surprised by the operations that you can execute using that little black rectangle with the flashing white cursor. If you want to be an efficient developer you must have a good bonding with the command line.