
The shell is a fundamental component of the Linux operating system, providing users with a flexible and powerful interface for interacting with the system and performing a wide range of tasks efficiently. The shell interprets user input, executes commands, and manages the flow of data between programs and system resources. It provides a powerful environment for performing various tasks, such as navigating the file system, managing files and directories, running programs, and automating tasks through scripting. There are several different shells available in Linux, with Bash (Bourne Again Shell) being the most common and widely used. Other popular shells include Zsh (Z Shell), Fish (Friendly Interactive Shell), and Ksh (Korn Shell).
Navigating to directories
pwd
This command displays the current working directory.
Example:
[vagrant@centos9 ~]$ pwd
/home/vagrant
[vagrant@centos9 ~]$ cd /tmp
[vagrant@centos9 tmp]$ pwd
/tmp
[vagrant@centos9 tmp]$
ls
Lists the contents of a directory.
Options:
-l: Long format, displays detailed information.-a: Shows hidden files (those starting with a dot).-h: Human-readable file sizes.
Example:
Long format, displays detailed information:
[vagrant@centos9 ~]$ ls -l
total 8
drwxr-xr-x. 2 vagrant vagrant 6 Jan 28 16:34 Desktop
drwxr-xr-x. 2 vagrant vagrant 6 Jan 28 16:34 Documents
drwxr-xr-x. 2 vagrant vagrant 6 Jan 28 16:34 Downloads
drwxr-xr-x. 7 vagrant vagrant 4096 Jan 28 12:41 ds-hello-be
drwxr-xr-x. 3 vagrant vagrant 25 Jan 28 16:56 helm
drwxr-xr-x. 2 vagrant vagrant 26 Jan 22 16:48 kind
drwxr-xr-x. 2 vagrant vagrant 6 Jan 28 16:34 Music
drwxr-xr-x. 2 vagrant vagrant 6 Jan 28 16:34 Pictures
drwxr-xr-x. 2 vagrant vagrant 6 Jan 28 16:34 Public
drwxr-xr-x. 2 vagrant vagrant 6 Jan 28 16:34 Templates
-rw-r--r--. 1 root root 27 Feb 1 18:07 token
drwxr-xr-x. 2 vagrant vagrant 6 Jan 28 16:34 Videos
[vagrant@centos9 ~]$
Shows hidden files (those starting with a dot):
[vagrant@centos9 ~]$ ls -la
total 76
drwx------. 20 vagrant vagrant 4096 Apr 27 05:54 .
-rw-------. 1 vagrant vagrant 3336 Feb 1 17:36 .bash_history
-rw-r--r--. 1 vagrant vagrant 18 Aug 9 2021 .bash_logout
-rw-r--r--. 1 vagrant vagrant 141 Aug 9 2021 .bash_profile
-rw-r--r--. 1 vagrant vagrant 492 Aug 9 2021 .bashrc
drwxr-xr-x. 10 vagrant vagrant 123 Jan 28 12:41 .gradle
drwxr-xr-x. 3 vagrant vagrant 25 Jan 28 16:56 helm
drwxr-xr-x. 2 vagrant vagrant 26 Jan 22 16:48 kind
drwxr-x---. 3 vagrant vagrant 19 Jan 28 12:59 .kube
-rw-------. 1 vagrant vagrant 53 Apr 27 05:53 .Xauthority
<<<<<<<<<< Output turncated >>>>>>>>
[vagrant@centos9 ~]$
Human-readable file sizes:
[vagrant@centos9 ~]$ ls -lh
total 8.0K
drwxr-xr-x. 2 vagrant vagrant 6 Jan 28 16:34 Desktop
drwxr-xr-x. 2 vagrant vagrant 6 Jan 28 16:34 Documents
drwxr-xr-x. 2 vagrant vagrant 6 Jan 28 16:34 Downloads
drwxr-xr-x. 7 vagrant vagrant 4.0K Jan 28 12:41 ds-hello-be
drwxr-xr-x. 3 vagrant vagrant 25 Jan 28 16:56 helm
drwxr-xr-x. 2 vagrant vagrant 26 Jan 22 16:48 kind
drwxr-xr-x. 2 vagrant vagrant 6 Jan 28 16:34 Music
drwxr-xr-x. 2 vagrant vagrant 6 Jan 28 16:34 Pictures
drwxr-xr-x. 2 vagrant vagrant 6 Jan 28 16:34 Public
drwxr-xr-x. 2 vagrant vagrant 6 Jan 28 16:34 Templates
-rw-r--r--. 1 root root 27 Feb 1 18:07 token
drwxr-xr-x. 2 vagrant vagrant 6 Jan 28 16:34 Videos
[vagrant@centos9 ~]$
cd
Moves you into the specified directory.
$ cd directory_name
Using relative and absolute paths:
Relative: cd Documents
Absolute: cd /home/user/Documents
Special directories:
~: Your home directory.
..: The parent directory.
Absolute path navigation:
[vagrant@centos9 ~]$ cd /tmp
[vagrant@centos9 tmp]$ pwd
/tmp
Relatvie path navigation:
[vagrant@centos9 tmp]$ cd ../
[vagrant@centos9 /]$
Navigate to your home directory
[vagrant@centos9 /]$ cd ~
[vagrant@centos9 ~]$ pwd
/home/vagrant
[vagrant@centos9 ~]$
Navigate to parent directory.
[vagrant@centos9 ~]$ pwd
/home/vagrant
[vagrant@centos9 ~]$ cd ..
[vagrant@centos9 home]$ pwd
/home
[vagrant@centos9 home]$
Managing Files and Directories
mkdir
Creates a new directory.
$ mkdir directory_namermdir
Removes an empty directory.
$ rmdir directory_nametouch
Creates an empty file or updates the access and modification times of an existing file.
$ touch filenamecp
Copies files or directories.
$ cp source_file destinationmv
Moves files or directories.
$ mv source destinationrm
Deletes files or directories.
$ rm file_name
Options:
-r: Recursive, deletes directories and their contents.
Viewing File Contents:
cat
Displays the contents of a file.
$ cat filenameless
Displays file contents one page at a time, allowing navigation.
$ less filename
head and tail
Display the beginning or end of a file.
$ head filename
$ tail filenamegrep
Searches for patterns within files.
$ grep pattern filename
Tab Completion
Type the first few characters of a directory or file name and press Tab to automatically complete it.