Tuesday, April 2, 2019

UNIX file and directory creation and deletion

The creation of files and directories in UNIX are pretty straight forward. UNIX provides us with four command line utilities we can use to create any file or directory on the system. The commands ‘touch’, ‘rm’, ‘mkdir’ and ‘rmdir’.

Creating Files on a UNIX system

In order to create a file on our system we can issue the touch command. The format of the command is touch [options] files. To create a empty file we could type the following:

sam@asus:~/unix% touch file_1
sam@asus:~/unix% ls -l
-rw-rw-r-- 1 sam sam     0 Mar 31 09:06 file_1
sam@asus:~/unix% 

The file was created with the default file permissions set by the ‘umask’ command set in the configuration file ~/.bashrc.

Change a files Access, Modify and Change times with touch command

The touch command also gives us the option to modify file attributes such as file access and modification times. In order to view file attributes for a particular file we can use the ‘stat’ command.

sam@asus:~/unix% stat file2.txt
  File: 'file2.txt'
  Size: 164        Blocks: 8          IO Block: 4096   regular file
Device: b301h/45825d Inode: 788056      Links: 1
Access: (0775/-rwxrwxr-x)  Uid: ( 1000/     sam)   Gid: ( 1000/     sam)
Access: 2019-03-28 18:30:07.613561814 -0600
Modify: 2019-03-28 18:30:07.631561380 -0600
Change: 2019-04-01 13:14:12.001132357 -0600
 Birth: -
sam@asus:~/unix% 

We see three fields indicated in the output of the stat command, Access, Modify and Change. The Access time of a file means the last time the file was read. The Modify time of a file means the last time the files content was changed. While the Change time of the file means the last time the files was updated when some of the files attributes have changed.

Modifying the Access and Modify times of a file

The ‘touch -t’ command allows us to change both the Access and Modify times of a file. In order to modify these values you must first specify a date in the format of [year][month][day][time]. The date will be using is 2016/04/20 @ 9:00 o'clock.

sam@asus:~/unix% touch -t 201604200900 file2.txt
sam@asus:~/unix% stat file2.txt
  File: 'file2.txt'
  Size: 164        Blocks: 8          IO Block: 4096   regular file
Device: b301h/45825d Inode: 788056      Links: 1
Access: (0775/-rwxrwxr-x)  Uid: ( 1000/     sam)   Gid: ( 1000/     sam)
Access: 2016-04-20 09:00:00.000000000 -0600
Modify: 2016-04-20 09:00:00.000000000 -0600
Change: 2019-04-02 10:17:06.020620970 -0600
 Birth: -
sam@asus:~/unix% 

As you can see both the access and modify times are altered to the new value we set with the touch command.

Modifying the Access time of a file

Sometimes its handy just to modify the access time of a file. The access time of a file shows when the last time the file was accessed or read by a user. You combined the ‘-a’ option with the ‘-t’ option to set a new access time for the file.

sam@asus:~/unix% touch -at 201204221000 file2.txt
sam@asus:~/unix% stat file2.txt
  File: 'file2.txt'
  Size: 164        Blocks: 8          IO Block: 4096   regular file
Device: b301h/45825d Inode: 788056      Links: 1
Access: (0775/-rwxrwxr-x)  Uid: ( 1000/     sam)   Gid: ( 1000/     sam)
Access: 2012-04-22 10:00:00.000000000 -0600
Modify: 2016-04-20 09:00:00.000000000 -0600
Change: 2019-04-02 10:18:20.830778672 -0600
 Birth: -
sam@asus:~/unix% 

The access time changed from 2016-04-16 to 2012-04-22 giving our file its new access time.

Changing the Modify time of a file

The Modify time of a file tells us the last time the content of the file was changed. Again just as with the ‘-a’ option, we pair ‘-m’ option with the ‘-t’ option to set our new file modify time.

sam@asus:~/unix% touch -mt 201007041000 file2.txt
sam@asus:~/unix% stat file2.txt
  File: 'file2.txt'
  Size: 164        Blocks: 8          IO Block: 4096   regular file
Device: b301h/45825d Inode: 788056      Links: 1
Access: (0775/-rwxrwxr-x)  Uid: ( 1000/     sam)   Gid: ( 1000/     sam)
Access: 2012-04-22 10:00:00.000000000 -0600
Modify: 2010-07-04 10:00:00.000000000 -0600
Change: 2019-04-02 10:19:13.222474179 -0600
 Birth: -
sam@asus:~/unix% 

We changed the Modify time of the file to 2016-05-04 from 2010-07-04.

Removing files on a UNIX system.

To remove files from out system we can issue the ‘rm’ command. One important prerequisite is that, you the user must have ‘execute’ permissions in they directory where the file resides. For more information on permissions you can read UNIX file and directory permissions.

sam@asus:~/unix% rm file_1 
sam@asus:~/unix% ls file_1
ls: cannot access 'dir2/': No such file or directory
sam@asus:~/unix%

Removing directories and sub-directories

The ‘rm -r’ command tells the system to remove files recursively. What that means is to apply the ‘rm’ command to all files, directories and sub-directories.

sam@asus:~/unix% ls -l dir2/
total 4
-rw-rw-r-- 1 sam sam    0 Apr  2 11:55 file1.txt
-rw-rw-r-- 1 sam sam    0 Apr  2 11:55 file2.txt
-rw-rw-r-- 1 sam sam    0 Apr  2 11:55 file3.txt
drwxrwxr-x 2 sam sam 4096 Apr  2 11:49 subdir1
sam@asus:~/unix% rm -r dir2/
sam@asus:~/unix% ls -l dir2/
ls: cannot access 'dir2/': No such file or directory
sam@asus:~/unix% 

Creating Directories on a UNIX system

In order to create directories we need to use the ‘mkdir’ command. This command allows for the user to create directories again with the default permissions set by the umask command in our ./bashrc file located in our home directory.

sam@asus:~/unix% mkdir test1
sam@asus:~/unix% ls -l
drwxrwxr-x 2 sam sam  4096 Mar 31 09:42 test1
sam@asus:~/unix% 

The ‘mkdir -m’ command also give you the option to set the directory permissions for the newly created directory. The default permissions on a new directory are ‘777’ and are set by the umask value for that user.

sam@asus:~/unix% mkdir -m 775 dir1/
sam@asus:~/unix% ls -l
drwxrwxr-x 2 sam sam  4096 Apr  2 12:02 dir1
sam@asus:~/unix% stat --format=%a dir1/
775
sam@asus:~/unix% 

Removing Directories on a UNIX system

Removing directories on are system are just as simple. The ‘rmdir’ command removes a directory or directories from the system we specify. One caveat with this command is that in order to be successful in remove the directories you specify, they must all be empty and contain no file or sub-directories.

sam@asus:~/unix% rmdir test1
sam@asus:~/unix% ls -l test1/
ls: cannot access 'dir2/': No such file or directory
sam@asus:~/unix%

No comments:

Post a Comment

Exploiting Weak WEBDAV Configurations

The server we are going to audit has the following fingerprint. 80/tcp open http Apache httpd 2.2.8 ((Ubuntu) DAV/2) Next we need t...