Power of CLI commands .

Yatik PatidarYatik Patidar
7 min read

what is CLI ?

CLI stands for Command Line Interface. It's a text-based interface used to interact with computer systems and programs . They are powerful for tasks such as file manipulation, system configuration, network management, and more, and are commonly used in operating systems like Unix/Linux, macOS, and Windows.

Basic CLI Commands :

  1. ls : List out all files and directories .
  • some option you can use with ls command .

  •   ls -a 
      # To display all files including hidden files.
    
  •   ls -l
      # To display Long Listing of files.
    
  •   ls -t
      # Sorts files and directories by modification time, with the newest files first.
    
  •   ls -r
      #It will display all files and directories in reverse of aplhabetical order.
    
  •   ls -rt
      # To display all files based on reverse of last modified date and time. Old files are at top and recent files are at bottom.
      # r : reverse order of sorted files and directoires .
      # t : sort files and directories by modification time .
    
  •   ls -latr
      # To display Long listing of all the Files in our System including Hidden Files, sorted by Modification Date (Oldest First)
      # l : Long listing
      # a : all hidden files and directories .
      # t : sort files and directories by modification time .
      # r : reverse order of sorted files and directoires .
    
  1. mkdir : We can create directories by using mkdir command.
  •   mkdir dir1
      # To create a directory
    
  •   mkdir dir1 dir2 dir3
      # To create multiple directories
    
  •   mkdir dir1/dir2/dir3
      #To create dir3. But make sure dir1 and in that dir2 should be available already.
    
  •   mkdir -p dir1/dir2/dir3
      # -p means path of directories.
      # All directories in the specified path will be created.
      # First dir1 will be created and in that dir2 will be created and within that dir3 will be created.
    
  1. rmdir : We can remove directories by using rmdir command.
  •   rmdir dir1
      # To remove empty directory dir1
    
  •   rmdir dir1 dir2 dir3
      # To remove multiple empty directories
    

    Note : rmdir command will work only for empty directories . If the directory is not empty then to remove that directory we should use rm command.

  •   rm -r dir1
      # if dir1 is non-empty directories
    
  •   rm -i dir1
      # While removing files and directories, if we want confirmation then we have to use -i option.
    
  •   rm -f dir1
      # While removing files and directories, if we don't want any error messages, then we should use -f option. It is opposite to -i option
    
  •   rm -v dir1
      # If we want to know the sequence of removals on the screen we should go for -v option.
    
  1. cp : copy files and directories from one location to another.
  •   # cp source_file destination_file
      cp file1 file2
      # Total content fo file1 will be copied to file2.
      # Total content fo file1 will be copied to file2.
      # If file2 is not already available, then this command will create that file.If file2 is not already available, then this command will create that file.
    
  •   cp -r dir1 dir2
      # Whenever we are copying one directory to another directory, compulsory we should use -r option.
    
  •   cp file1 file2 file3 output
      # file1 ,file2 and file3 will be copied to output directory .
    
  1. mv : Both moving and renaming activities can be performed by using single command .
  •   mv oldname newname
      # commands used for renaming files and directories .
    
  •   mv a.txt b.txt c.txt output
      # a.txt,b.txt and c.txt will be moved to output directory.
    
  •   mv dir1/* dir2
      # All files of dir1 will be moved to dir2. After executing this command dir1 will become empty.
    
  •   mv dir1 dir2
      # If dir2 is already available then dir1 will be moved to dir2. If dir1 is not already available then dir1 will be renamed to dir2.
    
  1. cat : To display the contents of files .
  •   cat filename
      # To display the contents of a single file .
    
  •   cat file1 file2 file3
      # To display the contents of multiple files sequentially.
    
  •   cat file1 file2 file3 > newfile
      # To concatenate the contents of multiple files and display them .
    
  •   cat file1 >> file2
      # To append the contents of one file to another .
    
  1. tac : It is the reverse of cat. It will display file content in reverse order of lines. i.e first line will become last line and last line will become first line. This is vertical reversal.
  •   tac filename
    
  1. wc : We can use wc command to count number of lines, words and characters present in the given file.

     wc a.txt
     # output 4 26 166 a.txt
     # 4 Number of Lines
     # 26 Number of words
     # 166 Number of characters (File size in bytes)
    
  1. sort : We can sort data of the file by using sort command.

     sort filename
     # Here sorting is based on alphabetical order.
    
     sort -r filename
     # It sort based on reverse of alphabetical order
    
    • Note :-

      1. If the file contains alphanumeric data, then first numbers will be considered and then alphabet symbols.

      2. If the file contains only numbers, then the sorting is not based on numeric value and it is just based on digits.

  2. uniq : We can use uniq command to display unique content in the file.

  • But to use uniq command, compulsory the file should be sorted, otherwise it won't work properly.

  • With uniq command we can use multiple options:

  • -d : To display only duplicate lines .

  • -c : To display number of occurrences of each line .

  • -i : Ignore case while comparing .

  • -u : To display only unique lines i.e the lines which are not duplicated.

  •   sort a.txt | uniq -d # duplicate
      sort a.txt | uniq -c # count of each occurence
    
  1. Regular Expressions and Wildcard Characters :
  • If we want to represent a group of strings according to a particular pattern, then we should go for regular expressions.
  • [abc] : Either a or b or c

  • [!abc] :Any character except a,b and c

  • [a-z] : Any lower case alphabet

  • [A-Z] : Any upper case alphabet

  • [a-zA-Z] : Any alphabet

  • [0-9] : Any digit from 0 to 9

  • [a-zA-Z0-9] : Any alphanumeric character

  • [!a-zA-Z0-9] : Except alpha numeric character (i.e special symbol)

  •   ls [abc]*
      # To list out all files where file name starts with a or b or c .
    
  1. find : We can use find command to find files and directories present in our system

    find /path/to/search -name "*.txt"
    # This command searches for all files with the .txt extension in the /path/to/search directory and its subdirectories.
    
  2. chmod : We can use chmod command to change file or directory permissions.

  •   chmod u+x,g+w,o-r demo.txt
      # For user add execute permission,for group add write permission,for others remove read permission
    

    Note:

  • 4 : Read Permission

  • 2 : Write Permission

  • 1 : Execute Permission

  1. ps : To display information about currently running processes.
  •   ps aux
      # List all processes with detailed information
    
  •   ps -ef
      # To list all processes running on the system in a detailed format
    
  1. kill : used to terminate processes by sending signals to them .

    kill pid
    # which forcefully terminates the process
    
  2. ifconfig : used to configure network interfaces and display information about network interfaces currently configured on the system

  •   ifconfig -a
      # Display information about all network interfaces:
    
0
Subscribe to my newsletter

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

Written by

Yatik Patidar
Yatik Patidar