Command Line Interface (CLI)

P PRAKASHP PRAKASH
4 min read

Why CLI ?

Before knowing what is CLI, one should know why CLI is used and it's significance. To get our work done we need to communicate with system, for example to create ten files in a folder we right click and create a new folder which is one way of doing, but not the efficient way. What is there is a command that can create number of files in the desired folder? Yes! that's where CLI comes into picture.

CLI is an interface where user inputs human readable commands which are processed by shell and shell sends the information to kernal in system readable formate where kernal processes the data and connects with hardware to complete the work.

Most useful commands a developer should know.

  • man

    man command displays the manual for purticular command that you desired to know about.

    Syntax : man [command]

    example : man man --> gives the manual for man command

  • cd

    Changes the present working directory to the specified directory.

    Syntax : cd [destination directory path]

    examples : cd . --> changes to current directory (no change)

    cd .. --> changes to parent directory

    cd ~ --> changes to home direcotry

    cd - --> changes to previously working directory

    cd ~/Desktop --> changes to desktop

  • mkdir

    Creates a new directory

    Syntax mkdir [directory name]

    example : mkdir newDirectory --> creates new directory in the current folder

    mkdir ~/Desktop/newDirectory/newestDirectory --> creates newestDirectory if newDirectory exists.

    mkdir -p ~/Desktop/newDirectory/newestDirectory --> It creates the whole path including newDirectory if not existed

  • mv

    mv command is used for both renaming a file or folder and moving content of a file or a folder.

    Syntax : mv oldName newName --> changes the name of file or folder to newName

    mv currentPath TargetPath --> moves the file or folder in current location to target location

  • cp

    cp command is used to copy content of one file into another file, also used to copy multiple files into target directory.

    Syntax : cp file1 file2 --> copies content of file1 into fil2

    cp file1 file2 file3 targetDirectory -->copied all files file{1..3} into targetDirectory

  • ls

    ls command lists all the files and directories in the current directory

    syntax : ls -[option]

    example ls --> lists all files and folders in current directory

    ls -l --> gives the long list of files and folders

    ls -a --> gives all hidden (.files)

  • pwd

    displays the Present Working Directory

    Syntax : pwd

  • rm

    rm command is used to remove the file or folder.

    Syntax : rm filePath --> removes the file in filePath

  • chmod and chown

    chmod

    chomd is used to modify the permission of file or directory.

    Syntax : chmod [permission] [file/folder] --> changes the permission of file/folder

    Illustrations : chmod u+rw,g-w,o+x [file/folder]

    u --> user; g-->group,o-->other

    r-->read(4); w-->write(2); x--> execute(1)

    the above command gives the user read and write permissions and removes write permission to group and allows others to execute. The above command can also be written like this in binary format chmod 651 [file/folder]

chown

chown is used to change the owner of the file or directory.

Syntax : chown NewOwner filePath -->changes the owen of the file in filePath to NewOwner

  • touch

    touch command is used to create file and aslo update the last modified time of file to the present time.

    Syntax : touch NewFile --> creates the file

    touch ExistingFile --> Updates the last modified time.

  • cat

    cat command is used to modify the file content, also used to display the content of the file.

    Syntax : cat file.txt --> displays the content of file.txt

    cat > file.txt --> rewrites the content of filr.txt

    cat >> file.txt --> appends new content to the end of file.txt

  • tail and head

tail

Displays the last 10 lines of content

Syntax : tail file --> displays the last 10 lines of content

tail -15 file --> displays the last 15 lines of content

head

Displays the first 10 lines of the content

Syntax : head file --> displays the first 10 lines of content

head -9 file --> displays the first 9 lines of content

  • pipe

    pipe is a special command used to pass the output of one command to input to another command. Represented by command '|'

    Syntax : command1 | command2 --> output of command1 is given to command2

    example : ls -l | head -5 --> displays the first 5 lines of long list of current folder

  • grep

    grep command is used to search a pattern or text

    Syntax : grep [Pattern] file --> searches for patter in file

    example : grep Hello file.txt --> returns every line of file.txt having Hello

  • find

    find command is used to find the files by name of the file (-name) by type (-f for file and -d for directories) aswell as by size (-size +-n{K/M})

    Syntax : find [option] file

    example find -name file.txt -size -200K --> searches for file having name file.txt and having size less than 200kb.

  • nano

    nano command is used to edit the file content in nano editor

    Syntax : nano fileName --> opens a file editor called nano enabling to edit the content of the file.

0
Subscribe to my newsletter

Read articles from P PRAKASH directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

P PRAKASH
P PRAKASH