loader

Filename

As we mentioned before, In Linux, everything is a file. So every file must have a filename. A Filename in Linux is a path to the file that contains the directory name and basename. The directory name is the directory path to the file’s parent directory. The basename is the exact name of the file or directory. So here we have the filename :

Filename= directory name + basename

Two commands split the base name and directory name,, as the example code below shows:

Syntax:

basename path

dirname path  

				
					[root@Lpic1-CentOs9 ~]#ls -lah /home/sara
-rw-r--r--  1 sara sara  160 May 18 11:27  67
drwxr-xr-x  2 sara sara 4.0K Dec 19  2022  Videos
****************so we have a file and a directory********************
[root@Lpic1-CentOs9 ~]#basename /home/sara/67
67
[root@Lpic1-CentOs9 ~]#basename /home/sara/Videos/
Videos
*******************************************************************
[root@Lpic1-CentOs9 ~]#dirname /home/sara/67
/home/sara
[root@Lpic1-CentOs9 ~]#dirname /home/sara/Videos/
/home/sara
				

cd Command

Cd is an abbreviation of changing directory, So users change the current working directory using this command.

Syntax

cd  [file name]

Note: this Path can be an absolute or relative path that we covered here.

Examples

cd without any paths, change the working directory to the user’s home directory.

cd ~: as we explained before, the Tilda “~” represents the user home directory; therefore, this command acts as the cd without a path.

cd ..:  The double dots mean the parent directory, so this command leads the user to the parent directory.

				
					[root@Lpic1-CentOs9 /home/sara]# whoami
root
[root@Lpic1-CentOs9 /home/sara]# pwd
/home/sara
[root@Lpic1-CentOs9 /home/sara]# cd 
[root@Lpic1-CentOs9 ~]# pwd 
/root
[root@Lpic1-CentOs9 /home/sara]# cd ~
[root@Lpic1-CentOs9 /]# cd ..
[root@Lpic1-CentOs9 /]# pwd 
/

				

Practice 1

Suppose that this FHS exists in a Linux server. And the current working directory is “/home.”

  1. Display the current working directory
  2. Display the content of the /dave directory using absolute and relative paths.
  3. Change the working directory to the “/my posts” directory using absolute and relative Paths.
				
					***********************Answers***************************
**********************Practice 1*************************
[root@Lpic1-CentOs9 /home]# pwd 
/home
**********************Practice 2*************************
#By Absolute path 
[root@Lpic1-CentOs9 /home]# ls -lah /home/dave
total 36K
drwxr-xr-x 6 dave dave 4.0K May 21 10:58  .
drwxr-xr-x 6 root root 4.0K May 18 13:42  ..
-rw-r--r-- 1 dave dave  220 Aug  5  2021  .bash_logout
-rw-r--r-- 1 dave dave 3.5K Aug  5  2021  .bashrc
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
drwxr-xr-x 2 root root 4.0K May 21 10:58 'my posts'
-rw-r--r-- 1 dave dave  807 Aug  5  2021  .profile
-rw-r--r-- 1 root root    0 May 18 13:51  testfile1
-rw-r--r-- 1 root root    0 May 18 13:51  testfile2
#By relative path 
[root@Lpic1-CentOs9 /home]# ls -lah dave/
total 36K
drwxr-xr-x 6 dave dave 4.0K May 21 10:58  .
drwxr-xr-x 6 root root 4.0K May 18 13:42  ..
-rw-r--r-- 1 dave dave  220 Aug  5  2021  .bash_logout
-rw-r--r-- 1 dave dave 3.5K Aug  5  2021  .bashrc
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
drwxr-xr-x 2 root root 4.0K May 21 10:58 'my posts'
-rw-r--r-- 1 dave dave  807 Aug  5  2021  .profile
-rw-r--r-- 1 root root    0 May 18 13:51  testfile1
-rw-r--r-- 1 root root    0 May 18 13:51  testfile2
**********************Practice 3*************************
#By Absolute path 
[root@Lpic1-CentOs9 /home]# cd /home/dave/my posts/
[root@Lpic1-CentOs9 /home/dave/my posts]# pwd
/home/dave/my posts
# another way By Absolute path
[root@Lpic1-CentOs9 /home]#cd /home/dave/"my posts"
[root@Lpic1-CentOs9 /home/dave/my posts]# pwd 
/home/dave/my posts
#By relative path 
[root@Lpic1-CentOs9 /home]# cd dave/my posts/
[root@Lpic1-CentOs9 /home/dave/my posts]# pwd
/home/dave/my posts
# another way By  relative  path
[root@Lpic1-CentOs9 /home]# cd dave/"my posts"
[root@Lpic1-CentOs9 /home/dave/my posts]# pwd
/home/dave/my posts
				

Note: To write a filename with white space in basename, consider the white space a special character. Therefore to quote it, use the backslash or single or double quotations.

stat Command

To explain other basic Linux commands, we describe the stat command briefly. The stat command displays some efficient information about the file that the user sends as an argument. This information includes:

Access time: the last time that a user opens a file and reads it or accesses it. For example, by using “cat,” “less”, or “more” commands.

Modification time: the time a user changes the file’s content, for example, editing a file by “vi” text editor.

Change time: the last time a user changes the inode of the file by changing the ownership or permission or file name or the last time that the user changes the file’s content.

				
					[root@Lpic1-CentOs9 /home]# stat /home/dave/my posts/Lpic1.txt
  File: /home/dave/my posts/Lpic1.txt
  Size: 5               Blocks: 8          IO Block: 4096   regular file
Device: fe00h/65024d    Inode: 261643      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2022-05-21 16:10:10.746343190 +0430
Modify: 2022-05-21 16:09:48.921607289 +0430
Change: 2022-05-21 16:09:48.921607289 +0430
 Birth: 2022-05-21 10:58:53.292350120 +0430

				

touch command

As we mentioned in the last section, every file has a timestamp that users can see it using the “stat” or “ls” command. The “touch” command modifies this timestamp as the primary function. Generally, users use this utility to create an empty file. Note that touch only creates a file if it does not already exist.

Syntax

touch [option] file name

touch” with no options creates an empty file if that file does not already exist in the path. If the file already exists, this command modifies the timestamp
-a: this option changes the access time to the current time.
-m: changing modification time is possible with this option.
-t: modifies the timestamp in a specific format.

Note: Generally, the file timestamp follows this format
[[CC] YY] MMDDhhmm[.SS]
Options in brackets are optional.

				
					[root@Lpic1-CentOs9 ~]# touch /home/dave/my posts/Lpic2.txt
[root@Lpic1-CentOs9 ~]# ls -lah  /home/dave/my posts
total 12K
drwxr-xr-x 2 root root 4.0K May 22 09:58 .
drwxr-xr-x 6 dave dave 4.0K May 22 08:50 ..
-rw-r--r-- 1 root root    5 May 22 08:38 Lpic1.txt
-rw-r--r-- 1 root root    0 May 22 09:58 Lpic2.txt
******************************************************************
[root@Lpic1-CentOs9 ~]# ls -lah  /home/dave/my posts/Lpic1.txt
-rw-r--r-- 1 root root 5 May 22 08:38 '/home/dave/my posts/Lpic1.txt'
[root@Lpic1-CentOs9 ~]#
[root@Lpic1-CentOs9 ~]# touch /home/dave/my posts/Lpic1.txt
[root@Lpic1-CentOs9 ~]# ls -lah  /home/dave/my posts/Lpic1.txt
-rw-r--r-- 1 root root 5 May 22 10:00 '/home/dave/my posts/Lpic1.txt'
******************************************************************
[root@Lpic1-CentOs9 ~]# stat /home/dave/my posts/Lpic1.txt
  File: /home/dave/my posts/Lpic1.txt
  Size: 5               Blocks: 8          IO Block: 4096   regular file
Device: fe00h/65024d    Inode: 261643      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2022-05-22 10:00:08.689116164 +0430
[root@Lpic1-CentOs9 ~]# touch -a /home/dave/my posts/Lpic1.txt
[root@Lpic1-CentOs9 ~]#stat /home/dave/my posts/Lpic1.txt
  File: /home/dave/my posts/Lpic1.txt
  Size: 5               Blocks: 8          IO Block: 4096   regular file
Device: fe00h/65024d    Inode: 261643      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2022-05-22 10:03:00.210915351 +0430
******************************************************************
[root@Lpic1-CentOs9 ~]# stat /home/dave/my posts/Lpic1.txt
Modify: 2022-05-22 10:00:08.689116164 +0430
[root@Lpic1-CentOs9 ~]# touch -m /home/dave/my posts/Lpic1.txt
[root@Lpic1-CentOs9 ~]# stat /home/dave/my posts/Lpic1.txt
Modify: 2022-05-22 10:04:54.582140225 +0430
******************************************************************
[root@Lpic1-CentOs9 ~]# ls -lah /home/dave/my posts/Lpic2.txt
-rw-r--r-- 1 root root 0 May 22 09:58 '/home/dave/my posts/Lpic2.txt'
[root@Lpic1-CentOs9 ~]# touch -t 198707200630 /home/dave/my posts/Lpic2.txt
[root@Lpic1-CentOs9 ~]#  ls -lah /home/dave/my posts/Lpic2.txt
-rw-r--r-- 1 root root 0 Jul 20  1987 '/home/dave/my posts/Lpic2.txt'
				

Practice 2

Consider the practice one’s FHS Image
1) Create files prac1 to prac5 in the “my posts” directory
2) In the “dave” directory, create files test-j to test-r
3) Modify access time of prac5 to 15 July 1987
4) Change the modification time of test-k to your birthday

				
					**************************ANSWERS*******************************
*************************Practice1******************************
[root@Lpic1-CentOs9 ~]# touch /home/dave/my posts/prac{1..5}
[root@Lpic1-CentOs9 ~]# ls -lah  /home/dave/my posts
total 8.0K
drwxr-xr-x 2 root root 4.0K May 22 10:40 .
drwxr-xr-x 6 dave dave 4.0K May 22 08:50 ..
-rw-r--r-- 1 root root    0 May 22 10:40 prac1
-rw-r--r-- 1 root root    0 May 22 10:40 prac2
-rw-r--r-- 1 root root    0 May 22 10:40 prac3
-rw-r--r-- 1 root root    0 May 22 10:40 prac4
-rw-r--r-- 1 root root    0 May 22 10:40 prac5
*************************Practice2******************************
[root@Lpic1-CentOs9 ~]#touch /home/dave/test-{j..r}
[root@Lpic1-CentOs9 ~]# ls -lah /home/dave/
-rw-r--r-- 1 root root    0 May 22 10:42  test-j
-rw-r--r-- 1 root root    0 May 22 10:42  test-k
-rw-r--r-- 1 root root    0 May 22 10:42  test-l
-rw-r--r-- 1 root root    0 May 22 10:42  test-m
-rw-r--r-- 1 root root    0 May 22 10:42  test-n
-rw-r--r-- 1 root root    0 May 22 10:42  test-o
-rw-r--r-- 1 root root    0 May 22 10:42  test-p
-rw-r--r-- 1 root root    0 May 22 10:42  test-q
-rw-r--r-- 1 root root    0 May 22 10:42  test-r
*************************Practice3******************************
[root@Lpic1-CentOs9 ~]# stat /home/dave/my posts/prac5
  File: /home/dave/my posts/prac5
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fe00h/65024d    Inode: 261647      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2022-05-22 10:40:05.475872684 +0430
[root@Lpic1-CentOs9 ~]# touch -at 198707150000 /home/dave/my posts/prac5
[root@Lpic1-CentOs9 ~]# stat /home/dave/my posts/prac5
File: /home/dave/my posts/prac5
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fe00h/65024d    Inode: 261647      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 1987-07-15 00:00:00.000000000 +0330
*************************Practice4******************************
[root@Lpic1-CentOs9 ~]# stat /home/dave/test-k
Modify: 2022-05-22 10:42:03.962399484 +0430
[root@Lpic1-CentOs9 ~]# touch -mt  198611110000  /home/dave/test-k
[root@Lpic1-CentOs9 ~]# stat /home/dave/test-k
Modify: 1986-11-11 00:00:00.000000000 +0330

				

Leave a Reply

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