Introduction to basic Linux commands

Get started with Linux using these basic commands

·

7 min read

What happens when you start a computer?

When the computer is turned on, it first performs the power-on-self-test, also known as POST. If the POST is successful and no issues are found, the bootstrap loader will load the operating system for the computer into memory.

OS

It is a software that is responsible for managing and operating a computing device.

Things any software needs to be considered as an OS

  1. Kernel: Core of any OS; facilitates interaction between a software and hardware.
  2. File System: Method or DS (data structure) that the OS uses to store and retrieve data in the memory
  3. User Interface (could be GUI or CLI)
  4. Should be able to manipulate data based on commands

Now, we will see definitions of some basic terms so that it will build your foundation.

  • What is a system?

    • The definition of a system is a set of rules, an arrangement of things, or a group of related things that work toward a common goal.
    • A computer system primarily comprises a central processing unit (CPU), memory, input/output devices and storage devices.
  • What is an interface?

    • The place at which independent and often unrelated systems meet and act on or communicate with each other.
  • Prompt

    • A sign on a computer screen that shows that the computer has finished what it was doing and is ready for more instructions.

Why use Linux?

  1. Open source
  2. Supports almost all programming language
  3. Terminal is superior to CMD
  4. Bash scripting
  5. SSH

Terminal (or Console)

  • It is basically a text input output environment.
  • It’s job is to launch the shell

Shell

  • The shell is the Linux command line interpreter. It provides an interface between the user and the kernel and executes programs called commands. For example, if a user enters ls then the shell executes the ls command.
  • The shell can also execute other programs such as applications, scripts, and user programs.
  • Most of the shells use Bash as their programming language.
  • So, whatever we type in terminal is basically Bash scripting.

Shell format

sanskriti@sanskriti-Inspiron:~$

Some key points

  • Everything in any OS is just a file. All of these files have a path and they are organised in a hierarchical order.
  • Meaning of some symbols:
    • /’ → Root directory
    • .’ → means that it is your current directory.
    • ..’ → means parent directory of your current directory
    • ~’ → home directory
    • -’ → previous directory
    • Don't get confused between ‘-’ and ‘..’ because your previous directory may be your parent directory but that is not always the case.
  • flags add more functionality to what you are doing.
  • # → Comment; just add ‘#’ before any text that you want to comment.
  • You may also create new files or directory using the relative path. You need not present in the directory where you want new file or directory.
  • The root directory contains all other directories, sub-directories, and files on the system. It is the top-most level of the Linux file system hierarchy.
  • The home directory can be said as a personal working space for all the users except root. There is a separate directory for every user.

History command

  • Your machine saves the history of your commands into a file.
  • To view history:

    • Press Ctrl + r.
    • Start typing what you remember of your command. You will see the latest command from your historythat matches your search term.

      • Screenshot from 2022-05-30 14-45-11.png
    • Press Ctrl + ragain to navigate through earlier entries in your history that match your search term.

      • Screenshot from 2022-05-30 14-45-22.png

      • Screenshot from 2022-05-30 14-45-28.png

      • Screenshot from 2022-05-30 14-45-40.png

  • Do you work on different projects that use different commands? Or do you want a convenient way to filter your searches by category (rather than search term)?

Linux commands studied so far

  1. pwd - Print working (current) directory; shows the path where you are from the root directory
    • sanskriti@sanskriti-Inspiron:~/Desktop$ pwd
      
  2. ls - Lists all the directory/files within a current working directory.
    • `ls -a’ → prints all files (including hidden ones)
    • `ls -l’→ prints in long listing format
    • sanskriti@sanskriti-Inspiron:~/Desktop$ ls -la
      
  3. cd - This command is used to change directory
    • cd FolderAbsolutePath → navigate to the folder using the absolute path; doesn't matter which directory you're in
    • cd FolderRelativePath → navigate to the folder using the path relative to the current directory
    • cd / → navigte to root directory
    • cd .. → navigate to parent directory of your current directory
    • cd ~ → navigate to home directory
    • cd - → navigate to previous directory
  4. clear - shifts the text upwards, out of the viewable area (pressing Ctrl+L also does this).
  5. touch - To create a file without any content
    • sanskriti@sanskriti-Inspiron:~/Desktop$ touch file1.txt file2.txt
      
  6. mkdir - To create directories
    • sanskriti@sanskriti-Inspiron:~/Desktop$ mkdir Project
      
    • mkdir -p→A flag which enables the command to create parent directories as necessary. If the directories exist, no error is specified.
      • sanskriti@sanskriti-Inspiron:~/Desktop$ mkdir -p Project/Linux/Part1
        
  7. file - To determine the file type. Type may be 'directory', 'ascii' ,'pdf document', 'JPEG image data' etc.
    • sanskriti@sanskriti-Inspiron:~/Desktop$ file names.txt
      
  8. gedit - gedit is a powerful general purpose text editor in Linux. It can edit multiple files at a time.
    • sanskriti@sanskriti-Inspiron:~/Desktop$ gedit names.txt
      
  9. cat - cat(concatenate) command reads data from the file and gives their content as output. It helps us to create, view, concatenate files.

    • sanskriti@sanskriti-Inspiron:~/Desktop$ cat names.txt
      
    • You may also overwrite a file or append contents of one file to another using '> ' and '>>' operator respectively.
      • sanskriti@sanskriti-Inspiron:~/Desktop$ cat names.txt > surname.txt
        
      • sanskriti@sanskriti-Inspiron:~/Desktop$ cat names.txt >> surname.txt
        
  10. history - We've already discussed that in detail.

  11. Ctrl + c - Gives your prompt back; means it halts the ongoing command and let's you enter new command.
  12. cp - To copy files or group of files or directory.
    • sanskriti@sanskriti-Inspiron:~/Desktop$ cp SourceFilePath DestFolderPath
      
    • Use -r flag to copy an entire directory to another directory
    • sanskriti@sanskriti-Inspiron:~/Desktop$ cp SourceFolderPath DestFolderPath
      
  13. mv - To move one or more files or directories from one place to another. No need to use flag -r to move one directory to another.
    • sanskriti@sanskriti-Inspiron:~/Desktop$ mv SourceFolderPath DestFolderPath
      
  14. rm - To remove files or directories
    • sanskriti@sanskriti-Inspiron:~/Desktop$ rm fileName
      
    • rm -rv directoryName → v means verbose, which usually means they output more information than the default.
    • To remove directory use -r flag
      • sanskriti@sanskriti-Inspiron:~/Desktop$ rm -r folderName
        
  15. rmdir - To remove empty directories from the filesystem in Linux.
    • sanskriti@sanskriti-Inspiron:~/Desktop$ rmdir EmptyFolderName
      
    • rmdir can only remove empty directories whereras rm -r can remove non-empty directories.
  16. find - To find files and directories.
    • To find files: Let's say you have to find file named 'names.txt'.
      • find ~ -name names.txt (’~’ → it means start searching from home directory; you may replace it with some other path to limit your search)
      • find ~ -name names.* → If you don’t know the extension of the file use '*'.
      • find ~ -name '*.txt' → will list all the files with extension ‘.txt’.
      • If two or more files having same name exists then, find command outputs paths of all these files.
    • To find directories:
      • use type -d → type suggests the file type. In this case, it is ‘d' which stands for directory.
      • find ~ -type d -name FolderName
        • sanskriti@sanskriti-Inspiron:~/Desktop$ find ~ -type d -name LinuxNotes
          
  17. man - To display the user manual of any command.
    • sanskriti@sanskriti-Inspiron:~/Desktop$ man ls
      
  18. whatis - To get a one-line manual page descriptions.
    • sanskriti@sanskriti-Inspiron:~/Desktop$ whatis ls
      

Linux shortcuts learned so far

Screenshot from 2022-05-31 09-34-26.png