UNIX - Files and directory - 1

Hi everyone! Welcome back to the second part of UNIX, if you haven't read the previous UNIX blog please checkout this link for whole UNIX series. So, now that we are done with the introduction to the UNIX environment, we are now ready to dive deeper into files and directories of the UNIX. So, we discussed that every thing in UNIX is arranged into files and files are arranged into directories, further directories are arranges into file systems. Let's now look into this topic in details and for better understanding please run the commands as you follow along. Directory Now a question comes into mind, what is a directory? Is it a collection of file or is it collection of more directories or both? The answer is both. Directory is an organised list of reference to objects that include files and sub-directories. The reference is known as the inode. It stores the attributes and disk block location of the data for the file or directory. So, now we can call a directory as a list of inode values. The list includes an entry file for itself, node of the parent and list of files and sub-directories. Moreover, these nodes are data structure that describe file and directory in a UNIX like file system. Each inode data structure consists of a filename and a file number. The filename is used to identify and access the file/directory where as the file number specifies where the metadata of that object is stored. Commands related to directory pwd : To check the present working directory [rdk@server ~]$ pwd /home/rdk cd : To change directory [rdk@server dir1]$ cd sub_dir1 [rdk@server sub_dir1]$ pwd /home/rdk/dir1/sub_dir1 To navigate back to parent directory we can use cd .. and to go to a particular directory we can use cd absolute-path where absolute-path is the path from the root directory. We can use cd to return to home directory of the current user. ls : To display files and directories of present working directory. [rdk@server /]$ ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var ls : To display files and directories inside a sub-directory. rdk@server /]$ ls usr arm64-linux-gnu bin config games include lib lib64 libexec local powerpc-linux-gnu s390-linux-gnu sbin share src tmp x86-linux-gnu ls : To display the details of the directory located at [rdk@server /]$ ls /home/rdk Desktop dir1 dir2 Documents Downloads Music Pictures Public Templates Videos ls -a : To display all files in directory including hidden files. [rdk@server ~]$ ls -a dir1 . .. file1.txt file2.txt sub_dir1 sub_dir2 ls -i : To display inode number and name. [rdk@server ~]$ ls -i dir1 35449929 file1.txt 35449930 file2.txt 17668684 sub_dir1 35449928 sub_dir2 ls -l : For long listing of files and directories. [rdk@server ~]$ ls -l dir1 total 0 -rw-rw-r-- 1 rdk rdk 0 Mar 14 16:09 file1.txt -rw-rw-r-- 1 rdk rdk 0 Mar 14 16:09 file2.txt drwxrwxr-x 2 rdk rdk 48 Mar 14 16:09 sub_dir1 drwxrwxr-x 2 rdk rdk 6 Mar 14 16:09 sub_dir2 ls -R : To display the content in directory and its sub directories. [rdk@server ~]$ ls -R dir1 dir1: file1.txt file2.txt sub_dir1 sub_dir2 dir1/sub_dir1: sub_file1.txt sub_file2.txt dir1/sub_dir2: ls -ld : To show detailed information about the directory without showing its content. [rdk@server ~]$ ls -ld dir1 drwxrwxr-x 4 rdk rdk 72 Mar 14 16:09 dir1 tree : To display the directory files and sub_directory content in a tree structure. [rdk@server ~]$ tree . ├── Desktop ├── dir1 │   ├── file1.txt │   ├── file2.txt │   ├── sub_dir1 │   │   ├── sub_file1.txt │   │   └── sub_file2.txt │   └── sub_dir2 ├── dir2 ├── Documents ├── Downloads ├── Music ├── Pictures ├── Public ├── Templates └── Videos 12 directories, 4 files ls -F : To display type of file in a directory. [rdk@server dir1]$ ls -F file1.txt file2.txt sub_dir1/ sub_dir2/ Moreover we can use file command to see file type. [rdk@server dir1]$ file file1.txt file1.txt: ASCII text [rdk@server dir1]$ file sub_dir1 sub_dir1: directory Creating files and directories Creating files : The touch command is used to create one or multiple files at a time. We can use .filename to create a hidden file. [rdk@server sub_dir1]$ touch file1 [rdk@server sub_dir1]$ touch file2 file3 #create multiple files [rdk@server sub_dir1]$ touch .file4 #create hidden file [rdk@server sub_dir1]$ ls -a . .. file1 file2 file3 .file4 sub_file1.txt sub_file2.txt If the file already exists in the directory, the touch command updated the last modified time to current date and time. Apart from touch we can use > to create file name, but this can create a single file at a time. [rdk@server sub_dir1]$ > file1 [rdk@server sub_dir1]$ ls -a . .. file1 sub_file1.txt sub_file2.txt The difference between touch and > comes when

Mar 24, 2025 - 17:54
 0
UNIX - Files and directory - 1

Hi everyone! Welcome back to the second part of UNIX, if you haven't read the previous UNIX blog please checkout this link for whole UNIX series.

So, now that we are done with the introduction to the UNIX environment, we are now ready to dive deeper into files and directories of the UNIX. So, we discussed that every thing in UNIX is arranged into files and files are arranged into directories, further directories are arranges into file systems. Let's now look into this topic in details and for better understanding please run the commands as you follow along.

Directory

Now a question comes into mind, what is a directory? Is it a collection of file or is it collection of more directories or both? The answer is both. Directory is an organised list of reference to objects that include files and sub-directories. The reference is known as the inode. It stores the attributes and disk block location of the data for the file or directory. So, now we can call a directory as a list of inode values. The list includes an entry file for itself, node of the parent and list of files and sub-directories.
Moreover, these nodes are data structure that describe file and directory in a UNIX like file system. Each inode data structure consists of a filename and a file number. The filename is used to identify and access the file/directory where as the file number specifies where the metadata of that object is stored.

Commands related to directory

  • pwd : To check the present working directory
[rdk@server ~]$ pwd
/home/rdk
  • cd : To change directory
[rdk@server dir1]$ cd sub_dir1
[rdk@server sub_dir1]$ pwd
/home/rdk/dir1/sub_dir1

To navigate back to parent directory we can use cd .. and to go to a particular directory we can use cd absolute-path where absolute-path is the path from the root directory. We can use cd to return to home directory of the current user.

  • ls : To display files and directories of present working directory.
[rdk@server /]$ ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
  • ls : To display files and directories inside a sub-directory.
rdk@server /]$ ls usr
arm64-linux-gnu  bin  config  games  include  lib  lib64  libexec  local  powerpc-linux-gnu  s390-linux-gnu  sbin  share  src  tmp  x86-linux-gnu
  • ls : To display the details of the directory located at
[rdk@server /]$ ls /home/rdk
Desktop  dir1  dir2  Documents  Downloads  Music  Pictures  Public  Templates  Videos
  • ls -a : To display all files in directory including hidden files.
[rdk@server ~]$ ls -a dir1
.  ..  file1.txt  file2.txt  sub_dir1  sub_dir2
  • ls -i : To display inode number and name.
[rdk@server ~]$ ls -i dir1
35449929 file1.txt  35449930 file2.txt  17668684 sub_dir1  35449928 sub_dir2
  • ls -l : For long listing of files and directories.
[rdk@server ~]$ ls -l dir1
total 0
-rw-rw-r-- 1 rdk rdk  0 Mar 14 16:09 file1.txt
-rw-rw-r-- 1 rdk rdk  0 Mar 14 16:09 file2.txt
drwxrwxr-x 2 rdk rdk 48 Mar 14 16:09 sub_dir1
drwxrwxr-x 2 rdk rdk  6 Mar 14 16:09 sub_dir2

Image description

  • ls -R : To display the content in directory and its sub directories.
[rdk@server ~]$ ls -R dir1
dir1:
file1.txt  file2.txt  sub_dir1  sub_dir2

dir1/sub_dir1:
sub_file1.txt  sub_file2.txt

dir1/sub_dir2:
  • ls -ld : To show detailed information about the directory without showing its content.
[rdk@server ~]$ ls -ld dir1
drwxrwxr-x 4 rdk rdk 72 Mar 14 16:09 dir1
  • tree : To display the directory files and sub_directory content in a tree structure.
[rdk@server ~]$ tree
.
├── Desktop
├── dir1
│   ├── file1.txt
│   ├── file2.txt
│   ├── sub_dir1
│   │   ├── sub_file1.txt
│   │   └── sub_file2.txt
│   └── sub_dir2
├── dir2
├── Documents
├── Downloads
├── Music
├── Pictures
├── Public
├── Templates
└── Videos

12 directories, 4 files
  • ls -F : To display type of file in a directory.
[rdk@server dir1]$ ls -F
file1.txt  file2.txt  sub_dir1/  sub_dir2/

Moreover we can use file command to see file type.

[rdk@server dir1]$ file file1.txt
file1.txt: ASCII text
[rdk@server dir1]$ file sub_dir1
sub_dir1: directory

Creating files and directories

  • Creating files : The touch command is used to create one or multiple files at a time. We can use .filename to create a hidden file.
[rdk@server sub_dir1]$ touch file1
[rdk@server sub_dir1]$ touch file2 file3 #create multiple files
[rdk@server sub_dir1]$ touch .file4 #create hidden file
[rdk@server sub_dir1]$ ls -a
.  ..  file1  file2  file3  .file4  sub_file1.txt  sub_file2.txt

If the file already exists in the directory, the touch command updated the last modified time to current date and time.

Apart from touch we can use > to create file name, but this can create a single file at a time.

[rdk@server sub_dir1]$ > file1
[rdk@server sub_dir1]$ ls -a
.  ..  file1  sub_file1.txt  sub_file2.txt

The difference between touch and > comes when the file already exists.The touch command updated the inode number too but > doesn't and the inode remains the same.

  • Creating directory : To create a directory we use mkdir command.
[rdk@server dir2]$ mkdir sub_dir2
[rdk@server dir2]$ ls
sub_dir2

To create a directory at a designated path we can use -p flag to do so. If the intermediate folders doesn't exists it will create them as well.

Removing files and directories

We can remove files and directory permanently by using rm command. The following table explains the different options that we use with rm command generally in our day to day work.

Option Description
-r This delete the directory recursively. It deletes directory and all its content
-i This is interactive way to remove. The CLI prompts for a yes to no asking if you really want to delete the specified file or directory.
-f This forces the files to be deleted.
-ir To remove the directory recursively and interactively

Copying files and directories

The cp command is used to copy files and directories. The syntax to the command is as follows : cd source destination
For Example, let us perform a simple activity. Hope you have practiced all the commands that I have discussed above, if not please do it because the files created in those commands will be needed here in this activity.

[rdk@server ~]$ ls
Desktop  dir1  dir2  Documents  Downloads  Music  Pictures  Public  Templates  Videos
[rdk@server ~]$ cd dir1
[rdk@server dir1]$ ls
file1.txt  file2.txt  sub_dir1  sub_dir2
[rdk@server dir1]$ cd sub_dir1
[rdk@server sub_dir1]$ ls
file1  sub_file1.txt  sub_file2.txt
[rdk@server sub_dir1]$ cp sub_file1.txt /home/rdk/dir2 #copy 
[rdk@server sub_dir1]$ cd /home/rdk/dir2
[rdk@server dir2]$ ls
sub_dir2  sub_file1.txt

Here we first navigate to the source destination and then copy the file. We can simply do this in one line using absolute or relative paths.

[rdk@server ~]$ cp ./dir1/sub_dir1/sub_file1.txt ./dir2
[rdk@server ~]$ cd dir2
[rdk@server dir2]$ ls
sub_dir2  sub_file1.txt

Some important points to keep in mind with cp command:

  • To copy multiple files to a single destination we can simply modify the command as cp file1 file2 ..... fileN path_to_destination.
  • To copy interactively we can use -i option. For example, cp -i file1 /home/rdk/dir2.
  • To copy directory and all the content inside it we can use -R flag to copy them recursively. For example, cp -R sub_dir1 /home/rdk/dir2.

Moving files and directories

The files and directories can be moved from one location to other using mv command.
The syntax goes as follows : mv source(s) destination.

The syntax of mv is almost same as that of cp command. Moreover the mv command can be used to rename a file as well. For example, if you want to change the file name of file1.txt to file2.txt then you can simply type mv file1.txt file2.txt.

Hope this post has helped you with basics of files and directory. In the next post we will be discussing about creating link files, viewing and editing files (using VIM editor) and searching for files and directory. Till then stay tuned.... Bye!