loader

We talked about directory content list commands in the previous post and introduced the “ls” command. We will continue the last lesson here, But before that, we have to get to know special characters and their usages.

Special characters

In Linux, a group of characters have special meaning and act in two different ways in bash. First, when administrators use them in the bash, they act like commands and instructions and tell the bash to do some specific functions. Second, sometimes administrators need to use the character with their actual meaning, so they have to do this differently.

What is Quoting?

Sometimes administrators must use these characters without their special meanings. So they have to use quotes to prevent the bash from interpreting the meaning. Putting special characters in a single quote and using backslashes are quoting ways. Here are some of the special characters and their meanings.

Character

Meaning

~

The home directory of the current user

Quoting or command substitution

$

Variable value

Next character quoting

#

Comment

|

                 Redirecting one command output as  the following command input

Example

If The administrator wants to have an output like this 

100 > 67 * 73 is not a valid inequality

Using echo as an example code below shows nothing in output. so we have to use single quotes or backslashes like this:

echo ‘100 > 67 * 73 is not a valid inequality’

 or 

echo ‘100 > 67 * 73’ is not a valid inequality

or 

echo 100 > 67 * 73 is not a valid inequality

Note: Use backslash just before the special character without any speces. Therefore, bash skips that character’s special meaning and considers it a regular character.

				
					**************************Showing nothing in the output***************************
[sara@Lpic1-CentOs9 ~]$ echo 100 > 67 * 73 is not a valid inequality
[sara@Lpic1-CentOs9 ~]$
**************************using single quotes*************************************
[sara@Lpic1-CentOs9 ~]$ echo '100 > 67 * 73 is not a valid inequality'
100 > 67 * 73 is not a valid inequality
[sara@Lpic1-CentOs9 ~]$ echo '100 > 67 * 73' is not a valid inequality
100 > 67 * 73 is not a valid inequality
**************************using backslashes***************************************
[sara@Lpic1-CentOs9 ~]$ echo 100 > 67 * 73 is not a valid inequality
100 > 67 * 73 is not a valid inequality

				
			

dir Command

The “dir” command is similar to the “ls” command in listing the content of directories, but they are different in the output format. “dir” command lists the content in columns so, sorts the content vertically. “dir” command displays the special characters using backslashes and unlike the “ls” command doesn’t display outputs in colors.

dir Syntax

dir [OPTION] [FILE]

Examples

dir without any options: displays only files and directories names without showing any differences.
dir –F: Differes the files and directories by adding a slash at the end of the directory name
dir –a: shows all files and folders, including dotfiles
di –A: if the administrator wants to have all directories and files name except the “.” the current working directory and the “..” the parent working directory.
dir –l: displays additional information about the content
dir –h: with the “–l” switch, it displays the information in a human-readable format.

				
					[root@Lpic1-CentOs9 ~]# dir /home/dave/
Desktop  Documents  Downloads  testfile1  testfile2
*****************************************************************
[root@Lpic1-CentOs9 ~]# dir -F /home/dave
Desktop/  Documents/  Downloads/  testfile1  testfile2
*****************************************************************
[root@Lpic1-CentOs9 ~]# dir -a /home/dave
.  ..  .bash_logout  .bashrc  Desktop  Documents  Downloads  .profile  testfile1  testfile2
*****************************************************************
[root@Lpic1-CentOs9 ~]# dir -A /home/dave
.bash_logout  .bashrc  Desktop  Documents  Downloads  .profile  testfile1 
testfile2
*****************************************************************
[root@Lpic1-CentOs9 ~]# dir -l /home/dave
total 12
drwxr-xr-x 2 root root 4096 May 18 13:50 Desktop
drwxr-xr-x 2 root root 4096 May 18 13:50 Documents
drwxr-xr-x 2 root root 4096 May 18 13:50 Downloads
-rw-r--r-- 1 root root    0 May 18 13:51 testfile1
-rw-r--r-- 1 root root    0 May 18 13:51 testfile2
*****************************************************************
[root@Lpic1-CentOs9 ~]# dir -lh /home/dave
total 12K
drwxr-xr-x 2 root root 4.0K May 18 13:50 Desktop
drwxr-xr-x 2 root root 4.0K May 18 13:50 Documents
drwxr-xr-x 2 root root 4.0K May 18 13:50 Downloads
-rw-r--r-- 1 root root    0 May 18 13:51 testfile1
-rw-r--r-- 1 root root    0 May 18 13:51 testfile2
				
			

tree Command

The last content list command is the tree command. This command recursively Lists directories content in a specific format like a tree. It is similar to “ls –R” but with a different format.

tree Syntax 

tree [-acdfghilnpqrstuvxACDFJQNSUX] [-L level [-R]] [-H  baseHREF]

        [-T title] [-o filename] [-P pattern] [-I pattern] [–gitignore]

        [–matchdirs] [–metafirst] [–ignore-case] [–nolinks] [–inodes]

        [–device] [–sort[=]] [–dirsfirst] [–filesfirst]

        [–filelimit #] [–si] [–du] [–prune] [–charset X]

        [–timefmt[=]format] [–fromfile] [–noreport] [–version] [–help]

        [–] [directory …]

Examples

tree with no options displays all directories and files and folders and sub directories
tree –L: give a level to this option, and the tree digs until that level, not more
tree –a: prints all files, even hidden files
tree –f: shows the full path prefix for each file
tree –s: displays the size of each file
tree –p: this option displays the permission of the file

				
					[root@Lpic1-CentOs9 ~]# tree /home/sara
/home/sara
├── 100
├── 67
├── CyberoamLinuxClient
│   ├── crclient
│   └── README
├── Desktop
│   └── Apache NetBeans-12.6.desktop
├── docs
│   └── plan.txt
├── Documents
│   └── Apachenetbeans
├── Downloads
├── img
│   └── pic1.jpg
├── Music
├── *newday
├── Pictures
├── Public
├── Templates
├── thinclient_drives
└── Videos
13 directories, 8 files
******************************************
[root@Lpic1-CentOs9 ~]# tree -L 1 /home/sara
/home/sara
├── 100
├── 67
├── CyberoamLinuxClient
├── Desktop
├── docs
├── Documents
├── Downloads
├── img
├── Music
├── *newday
├── Pictures
├── Public
├── Templates
├── thinclient_drives
└── Videos
12 directories, 3 files
***************************************
 [root@Lpic1-CentOs9 ~]# tree -a -L 1/home/sara
.
├── 5
├── .android
├── Apachenetbeans
├── apache-tomcat-10.0.20.tar.gz
├── .bash_history
├── .bashrc
├── .cache
├── .config
├── CyberClient.conf
├── Desktop
├── Documents
├── Downloads
************************************
 [root@Lpic1-CentOs9 ~]# tree -L 1 -s /home/sara
[       4096]  /home/sara
├── [        150]  100
├── [        160]  67
├── [       4096]  CyberoamLinuxClient
├── [       4096]  Desktop
├── [       4096]  docs
├── [       4096]  Documents
├── [       4096]  Downloads
├── [       4096]  img
├── [       4096]  Music
├── [          0]  *newday
├── [       4096]  Pictures
├── [       4096]  Public
├── [       4096]  Templates
├── [       4096]  thinclient_drives
└── [       4096]  Videos
12 directories, 3 files
**********************************
 [root@Lpic1-CentOs9 ~]# tree -L 1 -p /home/sara
[drwxr-xr-x]  /home/sara
├── [-rw-r--r--]  100
├── [-rw-r--r--]  67
├── [drwxr-xr-x]  CyberoamLinuxClient
├── [drwxr-xr-x]  Desktop
├── [drwxr-xr-x]  docs
├── [drwxr-xr-x]  Documents
├── [drwxr-xr-x]  Downloads
├── [drwxr-xr-x]  img
├── [drwxr-xr-x]  Music
├── [-rw-r--r--]  *newday
├── [drwxr-xr-x]  Pictures
├── [drwxr-xr-x]  Public
├── [drwxr-xr-x]  Templates
├── [drwxr-xr-t]  thinclient_drives
└── [drwxr-xr-x]  Videos
12 directories, 3 files

				
			

Note : the tree command is not of of built in commands in Linux. And administrators must install it.

Leave a Reply

Your email address will not be published. Required fields are marked *