X

Rate Us :



Share with Friends :

Menu

Terminal :

To run any application with the help of command requires terminal interface.

We can access it by different ways :

  • Right click on Linux desktop then click on Terminal




  • Go to Application -> Favorites/Accessories -> Terminal




  • Press Alt + F2 to get a window then type "gnome-terminal" on the window and press Enter




Properties of Terminal :


[root@localhost~]#

  • root - Login User Name




  • @ - Indicates local access [for DNS it would replaced by dot(.)]




  • localhost - System Name




  • ~ - User Home Directory




  • # - Super User Privilege [Dollar sign($) for Local User Privilege]




Some basic commands :


[root@localhost~]#whoami This command is used to check which user is login. Output : root

[root@localhost~]#pwd This command is used to print the name of current working directory. [pwd - present working directory] Output : /root

[root@localhost~]#ls This command is used to see the list of files and directories. Output : black/white color [files] blue color [directories] [root@localhost~]#ls -l 'l' refer to long list including attributes Output : starting with - [files] starting with d [directories] [root@localhost~]#ls -a 'a' refer to all files and directories including hidden files and directories. [In Linux, if any file or directory have a prefix as dot(.) then it refer to hidden file or directory] Output : black/white color [files] blue color [directories] prefix dot(.) [hidden files or directories] [root@localhost~]#ls -l <file_name> This command is used to check specific file property. [root@localhost~]#ls -ld <file_name> This command is also used to check specific file property. [root@localhost~]#ls -d <directory_name> This command is used to display specific directory name. [root@localhost~]#ls -ld <directory_name> This command is used to check specific directory property. [root@localhost~]#ls -ld D* This command is used to check all directories' property which starts with letter 'D'. [root@localhost~]#ls -ld *D This command is used to check all directories' property which ends with letter 'D'. [root@localhost~]#ls -ld *D* This command is used to check all directories' property which includes letter 'D'. [root@localhost~]#ls -R 'R' refer to recursive, to see the tree structure.

[root@localhost~]#clear This command is used to clear the screen. [We can use Ctrl + l for the same]

[root@localhost~]#date This command is used to check system date and time.

[root@localhost~]#cal This command is used to display the calendar of current month.

[root@localhost~]#cal <year> This command is used to display the calendar of the specified year (like #cal 2016).

[root@localhost~]#cal <month> <year> This command is used to display the calendar of the specified month of the specified year (like #cal 02 2016).

[root@localhost~]#init 0 This command is used to shut down the computer.

[root@localhost~]#poweroff This command is also used to shut down the computer.

[root@localhost~]#poweroff -f This command is used to forcefully shut down the computer within three seconds without caring about how many applications are running.

[root@localhost~]#init 1 This command is used for single user mode for troubleshooting.

[root@localhost~]#init 2 This command is used for multiple user login with CLI mode without network support.

[root@localhost~]#init 3 This command is used for multiple user login with CLI mode with network support.

[root@localhost~]#init 5 This command is used for multiple user login with GUI mode with network support.

[root@localhost~]#init 6 This command is used to reboot the computer.

[root@localhost~]#reboot This command is also used to reboot the computer.

[root@localhost~]#reboot -f This command is used to forcefully reboot the computer within three seconds without caring about how many applications are running.

[root@localhost~]#fdisk -l This command is used to check the list of partition with the size.

[root@localhost~]#parted -l This command is also used to check the list of partition with the size. [It provide more user-friendly information]

[root@localhost~]#free -m This command is used to check the memory size. [m => MegaByte(MB)]

[root@localhost~]#cat /proc/cpuinfo This command is used to check the CPU information.

[root@localhost~]#lscpu This command is also used to check the CPU information.

[root@localhost~]#arch This command is used to check the OS architecture.

[root@localhost~]#alias c = clear This command is used to create shortcut for any command. [To check -> type c instead of clear to clear the screen]

[root@localhost~]#unalias c This command is used to remove the shortcut created for any command.

[root@localhost~]#history This command is used to check all executed commands [By default 1000 commands are stored in history database].

[root@localhost~]#history -c This command is used to clear the history.


File Creation :

Files can be created by using any of the three methods :

  • cat command




  • vi editor




  • touch command




Creating a file using cat command :-

[root@localhost~]#cat > file_name This command is used to create a file. Example :


[root@localhost~]#cat > file1 After pressing enter, we can write on the file in the space provided on the terminal. After writing on the file, we should press Enter to go to next line and press Ctrl + d to save and exit the file. [We can also exit the file without saving by pressing Ctrl + c]
[root@localhost~]#cat file_name This command is used to see the content of the file. [root@localhost~]#cat -n file_name This command is used to see the content of the file with line number. [root@localhost~]#cat >> file_name This command is used to add new data to the existing file. Example :
[root@localhost~]#cat >> file1 After pressing enter, we can write on the existing file in the space provided on the terminal. After writing on the existing file, we should press Enter to go to next line and press Ctrl + d to save and exit the file. [We can also exit the file without saving by pressing Ctrl + c]

Drawbacks of cat command :


  • We can only add new data to the existing file but we cannot modify the content of the existing file.

  • We can only create one file at a time.

Creating a file using vi editor :-

Editors are used for adding, modifying or deleting text.

The different editors used are :

  • Windows : Notepad etc.




  • DOS : Edit etc.




  • Linux : CLI based - ex, ed, vi, Vim etc. and GUI based - Emacs, Gedit, NEdit, nano, Pico etc.




Modes of vi editor :


  • Command mode




  • Insert mode [to insert and modify the content of the file]




  • Ex: mode




[root@localhost~]#vi file_name This command is used to create a file if not exist. If the file is already existing then it allow to modify the content of the file. Example :


[root@localhost~]#vi file1 After pressing Enter, we will get separate space to write on the file. By default we would be in command mode. To write or modify the contents of the file we would have to go to insert mode by pressing i/I/o/O/a/A (any one of them). We can press Esc to go back to command mode. From command mode we can go to ex: mode by pressing Shift + : and then type 'wq' to save and exit/quit the file or just type 'q' to exit/quit the file without saving it.

How to switch to different modes :


Command mode to insert mode - press i/I/o/O/a/A (any one of them) Command mode to ex: mode - press Shift + : Insert mode to command mode - press Esc Ex: mode to command mode - press Esc There is no any way to directly switch from ex: mode to insert mode and vice-versa. But we can switch to them via command mode.

Operators for command mode :


  • dd - to delete a line
  • ndd - to delete 'n' lines
  • yy - to copy a line
  • nyy - to copy 'n' lines
  • p - put [to paste the deleted or the copied text]
  • np - 'n' put [to paste the deleted or the copied text multiple times]
  • u - undo [to undo the task]
  • Ctrl + r - redo [to redo the task]
  • G - to move the cursor to the last line of the file
  • ngg - to move the cursor to the 'n' line of the file
  • /<word_to_find> - to find the specified word [press n for next repetition of the specified word] [like /linux - to find the word 'linux' in the file]

Operators for ex: mode :


  • :q - to exit/quit the file without saving it
  • :q! - to exit/quit the file forcefully without saving it
  • :w - to write on the file
  • :wq - to save and exit/quit the file
  • :wq! - to save and exit/quit the file forcefully
  • :se nu - to set the line number to the content of the file
  • :se nonu - to remove the line number of the content of the file
  • :<number> - to move the cursor to the 'n' line of the file [like :10 - to move the cursor to the line number 10 of the file]
  • :<starting_line_number>,$s/<old_text>/<new_text> - to replace old text from new text starting from specified line number [like :1,$s/OS/Linux - to replace 'OS' with 'Linux' starting from line number 1]

  • [root@localhost~]#du -su <file_name or dir_name> This command is used to see the size of a file or directory. Example :


    [root@localhost~]#du -su /root/Desktop/file1 It will show the size of file1.

    [root@localhost~]#cat -n <file_name> | less This command is used to see the content of a file. [We can scroll up and down. Use 'q' to exit] Example :


    [root@localhost~]#cat -n /root/Desktop/file1 | less It will show the content of file1.

    [root@localhost~]#cat -n <file_name> | more This command is used to see the content of a file. [We can scroll down only. Use 'g' to exit] Example :


    [root@localhost~]#cat -n /root/Desktop/file1 | more It will show the content of file1.

    [root@localhost~]#cat -n <file_name> | head This command is used to see top 10 lines of a file. Example :


    [root@localhost~]#cat -n /root/Desktop/file1 | head It will show top 10 lines of file1.
    [root@localhost~]#cat -n /root/Desktop/file1 | head -2 It will show top 2 lines of file1.

    [root@localhost~]#cat -n <file_name> | tail This command is used to see bottom 10 lines of a file. Example :


    [root@localhost~]#cat -n /root/Desktop/file1 | tail It will show bottom 10 lines of file1.
    [root@localhost~]#cat -n /root/Desktop/file1 | tail -2 It will show bottom 2 lines of file1.


    Creating a file using touch command :-

    It is used to create multiple files at a time. But we cannot store data onto it. [To store data onto it go for cat command or vi editor]

    [root@localhost~]#touch file_name This command is used to create a file. Example :


    [root@localhost~]#touch file1 It will create a file in the present working directory. [To see the file - #ls]
    [root@localhost~]#touch file_name{<starting_number(usually 1)>..<total_number_of_files>} This command is used to create multiple files at a time. Example :
    [root@localhost~]#touch file{1..10} It will create the 10 files in the present working directory. [To see the files - #ls]

    Directory Creation :


    [root@localhost~]#mkdir dir_name This command is used to create a directory. Example :


    [root@localhost~]#mkdir dir1 It will create a directory in the present working directory. [To see the directory - #ls]
    [root@localhost~]#mkdir dir_name{<starting_number(usually 1)>..<total_number_of_files>} This command is used to create multiple directories at a time. Example :
    [root@localhost~]#mkdir dir{1..10} It will create the 10 directories in the present working directory. [To see the directories - #ls]
    [root@localhost~]#mkdir .dir_name This command is used to create a hidden directory. Example :
    [root@localhost~]#mkdir .dir1 It will create a hidden directory in the present working directory. [To see the hidden directory - #ls -a]
    [root@localhost~]#mkdir -p <first_dir> / <second_dir> / <third_dir>... This command is used to create nested directories. Example :
    [root@localhost~]#mkdir dir1/dir2/dir3 It will create nested directories in the present working directory. [To see all nested directories - #ls -R or #tree]

    cd command :


    [root@localhost~]#cd dir_name This command is used to change the directory. Example :


    [root@localhost~]#cd /home It will switch to home directory.
    [root@localhost~]#cd .. This command is used to switch one level back. [root@localhost~]#cd ../.. This command is used to switch two level back. [root@localhost~]#cd This command is used to switch home user directory.

    Copying directories or files :


    [root@localhost~]#cp <option> <source> <destination> This command is used to copy file or directory. Options : -r -> Recursive [To copy the directory along with its contents] -v -> Verbose [To check the background process] -p -> Copy with permission Example : [root@localhost~]#cp -v /opt/file1 /root/Desktop It will copy file1 to Desktop. [root@localhost~]#cp -v /opt/file{1..10} /root/Desktop It will copy all 10 files to Desktop. [root@localhost~]#cp -rv /opt/dir* /root/Desktop It will copy all the content of directories starting with 'dir' to Desktop.

    Moving and renaming directories or files :


    [root@localhost~]#mv <source(can be multiple)> <destination(only one)> This command is used to move file or directory to different location. [root@localhost~]#mv <old_name> <new_name> This command is used to rename file or directory. Example : [root@localhost~]#mv /opt/file1 /root/Desktop It will cut and paste file1 to Desktop. [root@localhost~]#mv /root/Desktop/file1 /root/Desktop/file2 It will rename file1 to file2. [root@localhost~]#mv /root/Desktop/file2 /opt/file3 It will cut and paste the file2 to opt directory and also rename it as file3.

    Removing or deleting directories or files :


    [root@localhost~]#rmdir <dir_name> This command is used to delete an empty directory. [root@localhost~]#rm <option> <file_name or directory_name> This command is used to remove or delete a file or directory. Option : -r -> recursive [directory along with its content] -f -> forcefully [deletes multiple file without any confirmation] Example : [root@localhost~]#rm /root/Desktop/file1 It will delete file1 from Desktop. [root@localhost~]#rmdir /root/Desktop/dir It will delete empty directory 'dir' from desktop. [root@localhost~]#rm -rf /root/Desktop/* It will delete all files and directories from desktop.