DEV Community

AJITH D R
AJITH D R

Posted on

Introduction to Basic Linux Commands

In this post, I will be sharing some basic information about Linux commands, which are essential tool to interact with the Linux operating system.

As you may know, Linux is an open-source operating system based on Unix. One of the primary ways to interact with Linux is through the terminal, which is a command-line interface.

You can perform various tasks in the terminal using commands. Here I will be explaining 5 commands:

  1. mkdir
  2. cd
  3. mv
  4. cp
  5. man

mkdir(make directory)

mkdir is a command in Unix-based systems used to create a new directory.
Examples of using the mkdir command:

  • To create a new directory called newdir in the current directory:
mkdir newdir
Enter fullscreen mode Exit fullscreen mode
  • To create a new directory called folder inside the desktop directory:
mkdir ~/Desktop/folder
Enter fullscreen mode Exit fullscreen mode
  • If you want to create a directory along with its parent directories in your home directory in one go:
mkdir -p newfolder/myFolder/private
Enter fullscreen mode Exit fullscreen mode

This will create the private directory, and any necessary parent directories. The -p option allows us to create a directory hierarchy,creating any necessary parent directories along the way.


cd (change directory)

The cd is used to navigate the file system and change the current working directory.
Examples of using cd command:

  • If you want to go to your home directory:
cd ~
Enter fullscreen mode Exit fullscreen mode

or you can just type cd and it will take you to your home directory.

cd
Enter fullscreen mode Exit fullscreen mode
  • If you are in your Desktop directory and you want to go to the parent directory of your Desktop directory:
cd ..
Enter fullscreen mode Exit fullscreen mode
  • The below command will take you to the specified directory.
cd <directory_name>
Enter fullscreen mode Exit fullscreen mode
  • If you want to go to the root directory of the file system:
cd /
Enter fullscreen mode Exit fullscreen mode
  • If you are in the directory /home/user/documents and you run the command cd /usr/bin, you can use the cd - command to go back to the previous directory, which is /home/user/documents.
cd -
Enter fullscreen mode Exit fullscreen mode

Note that using cd - will only take you to the most recently visited directory. If you have navigated through multiple directories and want to return to an earlier directory, you will need to use cd in combination with the path to that directory.

  • There are other flags to use with cd command
    • cd . : It does nothing but stay in your current working directory.
    • cd ~ : It will take you to your home directory.

mv (move or rename)

  • The mv command is used to move or rename files or directories in your system.

Examples of using mv command:

  • Moving a file:
mv file.txt /path/to/destination/directory
Enter fullscreen mode Exit fullscreen mode

This moves the file file.txt from its current location to the directory at /path/to/destination/directory.

  • Renaming a file:
mv file.txt file_new.txt
Enter fullscreen mode Exit fullscreen mode

This renames the file file.txt to file_new.txt in the same directory.

  • Moving multiple files:
mv file1.txt file2.txt /path/to/destination/directory
Enter fullscreen mode Exit fullscreen mode

This moves the files file1.txt and file2.txt from their current location to the directory at /path/to/destination/directory.

  • Moving a directory:
mv directory_name /path/to/destination/directory
Enter fullscreen mode Exit fullscreen mode

This moves the directory directory_name and its contents to the directory at /path/to/destination/directory.


cp(copy)

  • The cp command is used to copy files and directories. Here are a few examples to illustrate how to use the cp command along with some flags:

  • Basic copy:

cp file.txt /path/to/destination/directory
Enter fullscreen mode Exit fullscreen mode

This copies the file file.txt from its current location to the directory at /path/to/destination/directory

  • Copy directory recursively:
cp -R directory_name /path/to/destination/directory
Enter fullscreen mode Exit fullscreen mode

This copies the directory directory_name and its contents recursively to the directory at /path/to/destination/directory

  • Overwrite existing files:
cp -f file.txt /path/to/destination/directory
Enter fullscreen mode Exit fullscreen mode

This copies the file file.txt to the directory at /path/to/destination/directory and overwrites existing files in the destination directory if any.


man(manual)

  • It would be difficult to remember all the commands and its flags and options, especially if you are new to linux.
  • If you ever want to know all the information related a particular command, you can use man command.

  • The man command in Linux is short for manual and is used to display the manual pages of the specified command. * The manual pages provide a detailed description of the command, including its syntax, options, and usage examples.

  • All you have to do is type man and which command's details you want to know.

man ls
Enter fullscreen mode Exit fullscreen mode

That is it for this post. I covered how to create a folder/directory, change to a directory, rename or move files/directories, copy files into directory and lastly if you ever stuck on a command make use of man command.

Top comments (0)