Skip to main content

Command Palette

Search for a command to run...

Linux Basic Chet Sheet 01 (Day 1)

Published
8 min read
Linux Basic Chet Sheet 01 (Day 1)

A Beginner’s Guide to Basics of the Ubuntu Terminal:

Welcome to the exciting world of Ubuntu Linux! Whether you're a tech enthusiast or just curious about exploring a new operating system, Ubuntu offers a powerful and versatile platform that can open up endless possibilities. 🚀

📝 In this beginner's guide, we'll walk you through some basic commands that will help you navigate and perform essential tasks in Ubuntu like a pro! Let's dive in! 💻

🔍 Getting Started:


Update and Upgrade:

Before diving into commands, it's essential to update your system.

sudo apt update
sudo apt upgrade


File and Directory Operations:📁

pwd (Print Working Directory)

  • The pwd command displays the path of the current directory (folder) you're in. When you first open the Terminal, you're in your home directory by default. Type pwd and press enter to see its path.
ubuntu@ip-172-31-167-241:~$ pwd
/home/ubuntu ----------------> Present Working Directory
ubuntu@ip-172-31-167-241:~$

cd (Change Directory)

  • To navigate to a different directory, use the cd command followed by the path of the desired directory. For example, to change to the Desktop directory, you would type:
ubuntu@ip-172-31-167-241:/home$ cd / -----------> (/ Directory)
ubuntu@ip-172-31-167-241:/$ ls

ls (List)

  • The ls command lists all files and directories in the current directory. To see the contents of your Desktop, navigate there using cd as described above, then type: ls
ubuntu@ip-172-31-167-241:/$ ls -----------> (To view all files in the current directory)
'"'   bin   boot   dev   etc   home   lib   lib32   lib64   libx32   lost+found   media   mnt   opt   proc   root   run   sbin   snap   srv   sys   tmp   usr   var
ubuntu@ip-172-31-167-241:/$

mkdir (Make a new directory using)

  • creates a new directory with the specified name you should use "mkdir"
ubuntu@ip-172-31-167-241:~$ mkdir new_folder2 ----> (Created new_folder2)
ubuntu@ip-172-31-167-241:~$ ls
file1.txt  file2.txt  new_folder  new_folder2 -----> (Folder created successfully)
  • Create multiple directories at once by using "mkdir"
ubuntu@ip-172-31-167-241:~$ mkdir dev qa sandbox production -----> (Created muliple directories at once)
ubuntu@ip-172-31-167-241:~$ ls
dev  file1.txt  file2.txt  new_folder  new_folder2  production  qa  sandbox (Susccessfully created dev/qa/sandbox/production directories)
  • Creates a range of directories with sequential numbers from 1 to 10 by using "mkdir"
ubuntu@ip-172-31-167-241:~$ mkdir logfolder{1..10}
ubuntu@ip-172-31-167-241:~$ ls
logfolder1  logfolder10  logfolder2  logfolder3  logfolder4  logfolder5  logfolder6

mv (Move)

  • The mv command is used to move or rename files and directories. To move the same file 'file1.txt' from the Documents directory back to the Desktop, you would use "mv"
ubuntu@ip-172-31-167-241:~/new_folder$ touch file3.txt ------> (Created new file in folder)
ubuntu@ip-172-31-167-241:~/new_folder$ ls
detail  file1.txt  file3.txt
ubuntu@ip-172-31-167-241:~/new_folder$ mv file
file1.txt  file3.txt  
ubuntu@ip-172-31-167-241:~/new_folder$ mv file3.txt /home/ubuntu/ ----> (Moving file3.txt in ubuntu folder)
ubuntu@ip-172-31-167-241:~/new_folder$ cd /home/ubuntu/
ubuntu@ip-172-31-167-241:~$ ls
file1.txt  file2.txt  file3.txt  new_folder ------> (Moved command succeffully executed)
ubuntu@ip-172-31-167-241:~$
  • To rename ‘file1.txt’ to ‘file_renamed.txt’, you would use "mv"
ubuntu@ip-172-31-167-241:~$ ls
file1.txt  file2.txt  file3.txt  new_folder
ubuntu@ip-172-31-167-241:~$ mv  file3.txt file_hulk.txt ------> (Renaming file3.txt into file_hulk.txt)
ubuntu@ip-172-31-167-241:~$ ls
file1.txt  file2.txt  file_hulk.txt  new_folder  ------> (Successfully Renamed)

cp (Copy)

  • cp src dest: copy a file/ directory to another location

  • The cp command is used to copy files or directories. For instance, to copy a file named 'file1.txt' from your Desktop to the Documents directory, you would use "cp"

ubuntu@ip-172-31-167-241:~$ cp file1.txt /home/ubuntu/new_folder/  ----> (Copy command execution)
ubuntu@ip-172-31-167-241:~$ ls
file1.txt  file2.txt  new_folder
ubuntu@ip-172-31-167-241:~$ cd new_folder/
ubuntu@ip-172-31-167-241:~/new_folder$ ls
detail  file1.txt  -------> (File Copied here successfully)
ubuntu@ip-172-31-167-241:~/new_folder$

rm (Remove)

  • The rm command is used to delete files. To remove 'file_renamed.txt', you would use "rm"
ubuntu@ip-172-31-167-241:~$ rm file_hulk.txt -----> (Removing file_hulk.txt) Command
ubuntu@ip-172-31-167-241:~$ ls
file1.txt  file2.txt  new_folder   -----> (file_hulk.txt not visible now as we deleted)
ubuntu@ip-172-31-167-241:~$

rmdir/ rm-d (Remove Empty Directory)

  • The command is used to remove empty directories "rmdir" and "rm -d"
ubuntu@ip-172-31-167-241:~$ ls
DC  DevOps  Marvel  new_folder 
ubuntu@ip-172-31-167-241:~$ rmdir DC ----> (Removing DC Directory)
ubuntu@ip-172-31-167-241:~$ ls
DevOps  Marvel  new_folder ------> (Removed Successfully)
ubuntu@ip-172-31-167-241:~$ rmdir Marvel ------> (Removing Marvel Directory)
ubuntu@ip-172-31-167-241:~$ ls
DevOps  new_folder ------> (Removed Successfully)
ubuntu@ip-172-31-167-241:~$ rmdir DevOps ------->  (Removing DevOps Directory)
rmdir: failed to remove 'DevOps': Directory not empty -------> (Not able to delete as DevOps directory not an empty)
ubuntu@ip-172-31-167-241:~$

rm -r (Remove Non-Empty Directory)

  • The command is used to remove nonempty directories "rm -r"
ubuntu@ip-172-31-167-241:~$ ls
DevOps  new_folder
ubuntu@ip-172-31-167-241:~$ rm -r DevOps ----> (Deleting DevOps directory now with rm -r Command) 
ubuntu@ip-172-31-167-241:~$ ls
new_folder  ----> (Successfully deleted)
ubuntu@ip-172-31-167-241:~$

Text Editing:📝

touch (Create File)

  • The touch command creates a new file. For example, to create a file named 'file1.txt' on your Desktop, you would type: touch file1.txt
ubuntu@ip-172-31-167-241:~$ touch file1.txt -----> (Created File1)
ubuntu@ip-172-31-167-241:~$ ls
file1.txt  new_folder
ubuntu@ip-172-31-167-241:~$ touch file2.txt -----> (Created File2)
ubuntu@ip-172-31-167-241:~$ ls  --------> (To list files which we created)
file1.txt  file2.txt  new_folder

nano (text editor)

  • A simple text editor for beginners
ubuntu@ip-172-31-167-241:~$ nano file2

cat (Used to Display the content of the file)

  • Used to display the content of the file.
ubuntu@ip-172-31-167-241:~$ nano file2
ubuntu@ip-172-31-167-241:~$ cat file2 ---> (To show content from file) 
Hi This is Hemant Welcome in Linux. ------> (Content Successfully Shows us)
ubuntu@ip-172-31-167-241:~$
  • To view multiple files

    • If we have two files, file1 and file2.
ubuntu@ip-172-31-167-241:~$ cat file2 file1.txt 
Hi This is Hemant Welcome in Linux.
Hi All
This is Hemant !!
ubuntu@ip-172-31-167-241:~$
  • To view the contents of a file preceding with line numbers.
ubuntu@ip-172-31-167-241:~$ cat -n file1.txt
     1  Hi All
     2  This is Hemant !!
  • Create a file and add content concatenate file1, and file2 content in a new file - cat file1 file2 > newfile
ubuntu@ip-172-31-167-241:~$ ls
file1.txt  file2.txt
ubuntu@ip-172-31-167-241:~$ cat file1.txt
This is Hemant.
Welcome to Linux File1 !!
ubuntu@ip-172-31-167-241:~$ cat file2.txt
This is Hemant !!
Welcome to Linux File2. !!
ubuntu@ip-172-31-167-241:~$ cat file1 file2 > file3.txt
cat: file1: No such file or directory
cat: file2: No such file or directory
ubuntu@ip-172-31-167-241:~$ cat file1.txt file2.txt > file3.txt
ubuntu@ip-172-31-167-241:~$ ls
file1.txt  file2.txt  file3.txt
ubuntu@ip-172-31-167-241:~$ cat file3.txt
This is Hemant.
Welcome to Linux File1 !!
This is Hemant !!
Welcome to Linux File2. !!
ubuntu@ip-172-31-167-241:~$
  • Display the content with a $ sign at the end of each
ubuntu@ip-172-31-167-241:~$ cat -E file1.txt 
This is Hemant.$
Welcome to Linux File1 !!$

Vi/Vim: A more powerful text editor

  • Press i to enter insert mode, and make changes.

  • Press Esc to exit insert mode.

  • Type:wq and press Enter to save and exit.

ubuntu@ip-172-31-167-241:~$ ls
file1.txt  file2.txt  file3.txt
ubuntu@ip-172-31-167-241:~$ vim file4.txt
ubuntu@ip-172-31-167-241:~$ ls
file1.txt  file2.txt  file3.txt  file4.txt
ubuntu@ip-172-31-167-241:~$

ubuntu@ip-172-31-167-241:~$ cat file4.txt 
Hi This is Hemant !!!!!
I am using vim editor for creating file in Linux.
ubuntu@ip-172-31-167-241:~$

System Maintenance:🗑️

  • Reboot: Restart your system.
sudo reboot
  • Shutdown: Turn off your system.
sudo shutdown
  • You can add a time delay: sudo shutdown -h <time in minutes>

  • For immediate shutdown: sudo shutdown -h now


Package Management: 🔧

  • Install Software: Use apt to install packages: sudo apt install "apache2"
sudo apt install apache2

  • Remove Software: Uninstall packages "sudo apt remove apache2"
sudo apt remove apache2

  • Search for Software: Find packages "apt search nginx"
ubuntu@ip-172-31-167-241:~$ apt search nginx
Sorting... Done
Full Text Search... Done
centreon-plugins/jammy 0.0~20220113-1 all
  Collection of Nagios plugins to monitor OS, services and network devices
collectd-core/jammy-updates 5.12.0-10ubuntu0.1 amd64
  statistics collection and monitoring daemon (core system)
elpa-nginx-mode/jammy 1.1.9-2 all
  major mode for editing nginx config files
fcgiwrap/jammy 1.1.0-12 amd64
  simple server to run CGI applications over FastCGI
gitweb/jammy-updates,jammy-security 1:2.34.1-1ubuntu1.9 all
  fast, scalable, distributed revision control system (web interface)
golang-github-gorilla-handlers-dev/jammy 1.4.2-1 all
  collection of useful handlers for Go's net/http package
golang-github-hashicorp-hcl-dev/jammy 1.0.0-1.1 all
  Go implementation of HashiCorp Configuration Language
golang-github-nginxinc-nginx-plus-go-client-dev/jammy 0.9.0-2 all
  client for NGINX Plus API for Go (library)
gunicorn/jammy 20.1.0-2 all
  Event-based HTTP/WSGI server
gunicorn-examples/jammy 20.1.0-2 all
  Event-based HTTP/WSGI server (examples)
kopano-webapp-nginx/jammy 3.5.14+dfsg1.orig-1 all
  WebApp for the Kopano Collaboration Platform - Nginx

User Management: 🔒

  • Root User (Superuser):

    • The root user, also known as the superuser, has unrestricted access to the entire system. Only use the root account for administrative tasks that require privileges. Avoid logging in directly as the root user to prevent accidental system damage.
  • Regular Users:

    • Regular users have limited access to the system and can only modify files they own or have appropriate permissions for. Each regular user has a separate home directory where they can store personal files and configurations.
  • Adding a New User:

    • Superuser (Root) Access: Prefix commands with sudo for administrative privileges.

    • To add a new user, use the adduser or useradd command:

    • Add User: Create a new user with sudo adduser <username>

    • You'll be prompted to set a password and provide optional user information as shown in below code snap.

ubuntu@ip-172-31-167-241:~$ sudo adduser ironman
Adding user `ironman' ...
Adding new group `ironman' (1002) ...
Adding new user `ironman' (1002) with group `ironman' ...
Creating home directory `/home/ironman' ...
Copying files from `/etc/skel' ...
New password: 
Retype new password: 
passwd: password updated successfully
Changing the user information for ironman
Enter the new value, or press ENTER for the default
        Full Name []: IronMan T3
        Room Number []: T3
        Work Phone []: T3
        Home Phone []: Jarvis
        Other []: Jarvis
Is the information correct? [Y/n] Y
ubuntu@ip-172-31-167-241:~$
  • Switching Between Users:

    • Use the su (switch user) command to temporarily switch to another user account:
ubuntu@ip-172-31-167-241:~$ whoami
ubuntu
ubuntu@ip-172-31-167-241:~$ su ironman -----> Login with Ironman User
Password: 
ironman@ip-172-31-167-241:/home/ubuntu$
  • Deleting a User:

    • To remove a user and their home directory, use the deluser command:
ubuntu@ip-172-31-167-241:~$ whoami
ubuntu
ubuntu@ip-172-31-167-241:~$ sudo deluser ironman
Removing user `ironman' ...
Warning: group `ironman' has no more members.
Done.
ubuntu@ip-172-31-167-241:~$ su ironman
su: user ironman does not exist or the user entry does not contain all the required fields

Congratulations! 🎉

You've taken your first steps into the world of Ubuntu Linux. These basic commands will serve as the foundation for your exciting journey into the domain of Linux. 😃

Remember, practice makes perfect, so don't hesitate to experiment and explore more commands as you become more comfortable with the Ubuntu terminal. Happy Linuxing! 🌟🐧