loading...
Cover image for Command Line (Tutorial)

Command Line (Tutorial)

iamismile profile image Ismile Hossain Updated on ・4 min read

πŸ’» Command Line:

The command line is a tool or interface for interacting with a computer using only text rather than the mouse.

Command-Line is an essential tool for software development. We can execute a wide variety of programs on our computer through this. In this tutorial, we are going to learn the necessary UNIX commands for development.

UNIX command is a type of command that is used in LINUX and macOS.

To run some basic UNIX command in windows you can download Git Command Line Interface from Git SCM.


πŸ”° Getting Started:

✨ Let's start to learn (can use it as a reference guide)πŸƒβ€β™‚οΈπŸƒβ€β™‚οΈπŸƒβ€β™‚οΈ...

βœ” Check the Current Directory ➑ pwd:

On the command line, it's important to know the directory we are currently working on. For that, we can use pwd command.

Checking the Current Directory

It shows that I'm working on my Desktop directory.

βœ” Display List of Files ➑ ls:

To see the list of files and directories in the current directory use ls command in your CLI.

Displaying List of Files

Shows all of my files and directories of my Desktop directory.

  • To show the contents of a directory pass the directory name to the ls command i.e. ls directory_name.
  • Some useful ls command options:-
Option Description
ls -a list all files including hidden file starting with '.'
ls -l list with the long format
ls -la list long format including hidden files

βœ” Create a Directory ➑ mkdir:

We can create a new folder using the mkdir command. To use it type mkdir folder_name.

Creating a Directory

Use ls command to see the directory is created or not.

I create a cli-practice directory in my working directory i.e. Desktop directory.

βœ” Move Between Directories ➑ cd:

It's used to change directory or to move other directories. To use it type cd directory_name.

Moving Between Directories

Can use pwd command to confirm your directory name.

Change my directory to the cli-practice directory. And the rest of the tutorial I'm gonna work within this directory.

βœ” Parent Directory ➑ ..:

We have seen cd command to change directory but if we want to move back or want to move to the parent directory we can use a special symbol .. after cd command, like cd ..

βœ” Create Files ➑ touch:

We can create an empty file by typing touch file_name. It's going to create a new file in the current directory (the directory you are currently in) with your provided name.

Creating Files

I create a hello.txt file in my current working directory. Again you can use ls command to see the file is created or not.

Now open your hello.txt file in your text editor and write Hello Everyone! into your hello.txt file and save it.

βœ” Display the Content of a File ➑ cat:

We can display the content of a file using the cat command. To use it type cat file_name.

Displaying the Content of a File

Shows the content of my hello.txt file.

βœ” Move Files & Directories ➑ mv:

To move a file and directory, we use mv command.

By typing mv file_to_move destination_directory, you can move a file to the specified directory.

By entering mv directory_to_move destination_directory, you can move all the files and directories under that directory.

Before using this command, we are going to create two more directories and another txt file in our cli-practice directory.

mkdir html css
touch bye.txt

Creating Directories and File

Yes, we can use multiple directories & files names one after another to create multiple directories & files in one command.

Moving Files & Directories

Move my bye.txt file into my css directory and then move my css directory into my html directory.

βœ” Rename Files & Directories ➑ mv:

mv command can also be used to rename a file and a directory.

You can rename a file by typing mv old_file_name new_file_name & also rename a directory by typing mv old_directory_name new_directory_name.

Renaming Files & Directories

Rename my hello.txt file to the hi.txt file and html directory to the folder directory.

βœ” Copy Files & Directories ➑ cp:

To do this, we use the cp command.

  • You can copy a file by entering cp file_to_copy new_file_name.

Copying Files

Copy my hi.txt file content into hello.txt file. For confirmation open your hello.txt file in your text editor.

  • You can also copy a directory by adding the -r option, like cp -r directory_to_copy new_directory_name.

The -r option for "recursive" means that it will copy all of the files including the files inside of subfolders.

Copying Directories

Here I copy all of the files from the folder to folder-copy.

βœ” Remove Files & Directories ➑ rm:

To do this, we use the rm command.

  • To remove a file, you can use the command like rm file_to_remove.

Removing Files

Here I remove my hi.txt file.

  • To remove a directory, use the command like rm -r directory_to_remove.

Removing Directories

I remove my folder-copy directory from my cli-practice directory i.e. current working directory.

βœ” Clear Screen ➑ clear:

Clear command is used to clear the terminal screen.

βœ” Home Directory ➑ ~:

The Home directory is represented by ~. The Home directory refers to the base directory for the user. If we want to move to the Home directory we can use cd ~ command. Or we can only use cd command.


πŸ›  Tools I used for this tutorial:-

  1. Fluent Terminal
  2. Git Bash Shell

Thanks for reading and stay tuned.πŸ™‚πŸ‘‹

Posted on by:

Discussion

markdown guide
 

I would like to know more about find and grep.

 
 

I have a repo dedicated for such text processing tools:

GitHub logo learnbyexample / Command-line-text-processing

⚑ From finding text to search and replace, from sorting to beautifying text and more 🎨

Command Line Text Processing

Learn about various commands available for common and exotic text processing needs. Examples have been tested on GNU/Linux - there'd be syntax/feature variations with other distributions, consult their respective man pages for details.

⚠️ 🚧 Work in progress, stay tuned...


Chapters

Rough drafts

Possible additions - seems like these won't be coming any time soon :(

  • Parsers for html, xml, csv, json, etc
  • Renaming files
  • other commands like split, combine, datamash, etc

Contributing

  • Please open an issue for typos/bugs/suggestions/etc
    • Even for pull requests, open an issue for discussion…
 

Nice, but if it gets complicated or involves additional installation, I would probably create a Node.js script.