Introduction to basic Linux commands
Get started with Linux using these basic commands
Photo by Gabriel Heinzer on Unsplash
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
- Kernel: Core of any OS; facilitates interaction between a software and hardware.
- File System: Method or DS (data structure) that the OS uses to store and retrieve data in the memory
- User Interface (could be GUI or CLI)
- 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?
- Open source
- Supports almost all programming language
- Terminal is superior to CMD
- Bash scripting
- 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 thels
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
history
that matches your search term.Press
Ctrl + r
again to navigate through earlier entries in yourhistory
that match your search term.
- Press
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)?
- You can do that with comments.
When running commands in your terminal, you can add comments with
#
For further reading on comments refer to this link: https://codeburst.io/use-reverse-i-search-to-quickly-navigate-through-your-history-917f4d7ffd37
Linux commands studied so far
- pwd - Print working (current) directory; shows the path where you are from the root directory
sanskriti@sanskriti-Inspiron:~/Desktop$ pwd
- 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
- 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 incd FolderRelativePath
→ navigate to the folder using the path relative to the current directorycd /
→ navigte to root directorycd ..
→ navigate to parent directory of your current directorycd ~
→ navigate to home directorycd -
→ navigate to previous directory
- clear - shifts the text upwards, out of the viewable area (pressing
Ctrl+L
also does this). - touch - To create a file without any content
sanskriti@sanskriti-Inspiron:~/Desktop$ touch file1.txt file2.txt
- 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
- file - To determine the file type. Type may be 'directory', 'ascii' ,'pdf document', 'JPEG image data' etc.
sanskriti@sanskriti-Inspiron:~/Desktop$ file names.txt
- 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
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
history - We've already discussed that in detail.
- Ctrl + c - Gives your prompt back; means it halts the ongoing command and let's you enter new command.
- 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
- 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
- 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
- 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.
- 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
- use
- To find files: Let's say you have to find file named 'names.txt'.
- man - To display the user manual of any command.
sanskriti@sanskriti-Inspiron:~/Desktop$ man ls
- whatis - To get a one-line manual page descriptions.
sanskriti@sanskriti-Inspiron:~/Desktop$ whatis ls