File and Directory Management system Linux Commands:
pwd
The "pwd" command stands for "print working directory". When run this command, it displays the absolute path of current working directory.
$ pwd
/home/hari
touch
The "touch" command is used to create new empty file or update the timestamps of existing file without altering their contents.
$ touch temp1.txt
$
ls -l
The "ls -l" command is used to list the contents of a directory in a detailed, long format. It provides a comprehensive view of the files and directories within a specific directory, including various metadata and permissions information.
$ ls -l
-rw-rw-r-- 1 hari hari 0 May 25 11:53 temp1.txt
ls -la
The "ls -la" command lists the contents of a directory in a long format, including hidden files and directories. The -l option provides detailed information about each entry, and the -a option includes hidden files and directories in the output.
$ ls -la
total 20
drwx------ 2 hari hari 4096 May 25 11:53 .
drwxr-xr-x 3 root root 4096 May 24 14:15 ..
-rw-r--r-- 1 hari hari 18 May 5 2022 .bash_logout
-rw-r--r-- 1 hari hari 141 May 5 2022 .bash_profile
-rw-r--r-- 1 hari hari 492 May 5 2022 .bashrc
-rw-rw-r-- 1 hari hari 0 May 25 11:53 temp1.txt
vi
The "vi" command is used to open an existing file or create a new file if file does not exist. The file will open in below modes.
Command mode: We cannot directly edit the text in command mode. In this mode, we can issue commands to navigate, search operations.
Insert mode: To enter insert mode and start editing the text, press the " i " key while in command mode. We can then type and modify the content of the file. If we are in insert mode, press the " Esc " key to return to command mode.
Exiting vi: To exit vi, if you are in command mode, type " :q " and press Enter. If you have unsaved changes, vi will not exit. To forcefully exit without saving changes, use " :q! " .
$ vi temp2.txt
cat
The "cat" command is used to display the contents of file or files on the Linux terminal, but it can also be used to concatenate multiple files together and display the data.
Display the content of a file:
$ cat temp2.txt
this is temp file to test.
this is the content available in temp file.
Display multiple files:
$ cat temp1.txt temp2.txt
this is temp file 1.
the data you are seeing ths is temp file 1 data sample.
this is temp file to test.
this is the content available in temp file.
Concatenate multiple files:
$ cat temp1.txt temp2.txt > combined.txt
$
$ cat combined.txt
this is temp file 1.
the data you are seeing ths is temp file 1 data sample.
this is temp file to test.
this is the content available in temp file.
head
The "head" command is used to display the beginning lines of a file or result from other commands. By default, head displays the first 10 lines of a file, but you can specify a different number of lines to be displayed.
$ head temp3.txt
This is the content of line 1
This is the content of line 2
This is the content of line 3
This is the content of line 4
This is the content of line 5
This is the content of line 6
This is the content of line 7
This is the content of line 8
This is the content of line 9
This is the content of line 10
To display first 15 lines.
$ head -15 temp3.txt
This is the content of line 1
This is the content of line 2
This is the content of line 3
This is the content of line 4
This is the content of line 5
This is the content of line 6
This is the content of line 7
This is the content of line 8
This is the content of line 9
This is the content of line 10
This is the content of line 11
This is the content of line 12
This is the content of line 13
This is the content of line 14
This is the content of line 15
tail
The "tail" command is used to display the last lines of a file or the end of the output from other commands. By default, tail displays the last 10 lines of a file, but we can specify a different number of lines to be displayed.
$ tail temp3.txt
This is the content of line 16
This is the content of line 17
This is the content of line 18
This is the content of line 19
This is the content of line 20
This is the content of line 21
This is the content of line 22
This is the content of line 23
This is the content of line 24
This is the content of line 25
To display last 12 lines.
$ tail -12 temp3.txt
This is the content of line 14
This is the content of line 15
This is the content of line 16
This is the content of line 17
This is the content of line 18
This is the content of line 19
This is the content of line 20
This is the content of line 21
This is the content of line 22
This is the content of line 23
This is the content of line 24
This is the content of line 25
less
The "less" command allows you to view the contents of a file and navigate through it. It is designed to handle large files more efficiently compared to other text viewers like cat. It will display the first page content.
$ less temp3.txt
$
mkdir
The "mkdir" command is used to create directories (folders). It allows you to create one or more directories at a specified location.
Create a single directory:
$ mkdir directory1
$
$ ls -l
total 20
-rw-rw-r-- 1 hari hari 148 May 25 13:55 combined.txt
drwxrwxr-x 2 hari hari 4096 May 25 19:50 directory1
-rw-rw-r-- 1 hari hari 77 May 25 13:31 temp1.txt
-rw-rw-r-- 1 hari hari 71 May 25 13:25 temp2.txt
-rw-rw-r-- 1 hari hari 766 May 25 14:23 temp3.txt
Create multiple directories:
$ mkdir directory2 directory3 directory4
$
$ ls -l
total 32
-rw-rw-r-- 1 hari hari 148 May 25 13:55 combined.txt
drwxrwxr-x 2 hari hari 4096 May 25 19:50 directory1
drwxrwxr-x 2 hari hari 4096 May 25 19:57 directory2
drwxrwxr-x 2 hari hari 4096 May 25 19:57 directory3
drwxrwxr-x 2 hari hari 4096 May 25 19:57 directory4
-rw-rw-r-- 1 hari hari 77 May 25 13:31 temp1.txt
-rw-rw-r-- 1 hari hari 71 May 25 13:25 temp2.txt
-rw-rw-r-- 1 hari hari 766 May 25 14:23 temp3.txt
Create a single directory:
$ mkdir directory1
$
$ ls -l
total 20
-rw-rw-r-- 1 hari hari 148 May 25 13:55 combined.txt
drwxrwxr-x 2 hari hari 4096 May 25 19:50 directory1
-rw-rw-r-- 1 hari hari 77 May 25 13:31 temp1.txt
-rw-rw-r-- 1 hari hari 71 May 25 13:25 temp2.txt
-rw-rw-r-- 1 hari hari 766 May 25 14:23 temp3.txt
cd
The "cd" command is used to change the current working directory or move to different directory.
Change to a specific directory:
$ pwd
/home/hari
$
$ cd directory1
$
$ pwd
/home/hari/directory1
Move up one directory:
$ pwd
/home/hari/directory1
$
$ cd ..
$
$ pwd
/home/hari
Change to the home directory:
$ pwd
/home/hari
$ cd directory2
$
$ mkdir subdirectory1
$
$ cd subdirectory1
$
$ pwd
/home/hari/directory2/subdirectory1
$
$ cd
$
$ pwd
/home/hari
Move to the previous directory:
$ pwd
/home/hari
$
$ cd -
/home/hari/directory2/subdirectory1
$
$ pwd
/home/hari/directory2/subdirectory1
cp
The "cp" command is used to copy files and directories. It allows you to duplicate files and directories, either within the same location or to a different location.
Copy a file:
$ cd
$
$ pwd
/home/hari
$
$ ls
combined.txt directory1 directory2 directory3 directory4 temp1.txt temp2.txt temp3.txt
$
$ cp temp3.txt temp4.txt
$
$ ls
combined.txt directory1 directory2 directory3 directory4 temp1.txt temp2.txt temp3.txt temp4.txt
$
Copy multiple files to a directory:
$ pwd
/home/hari
$
$ cp temp3.txt temp4.txt directory4/
$
$ cd directory4
$
$ ls
temp3.txt temp4.txt
$
$ pwd
/home/hari/directory4
Copy a directory and its contents:
$ pwd
/home/hari
$ ls
combined.txt directory1 directory2 directory3 directory4 temp1.txt temp2.txt temp3.txt temp4.txt
$
$ cp -r directory4 directory3/
$
$ cd directory3
$
$ pwd
/home/hari/directory3
$ ls
directory4
$
$ cd directory4
$
$ pwd
/home/hari/directory3/directory4
$ ls
temp3.txt temp4.txt
rm
The "rm" command is used to remove or delete files and directories. It allows to delete individual files or entire directories and their contents.
Remove a file:
$ pwd
/home/hari/directory3/directory4
$
$ ls
temp3.txt temp4.txt
$
$ rm temp4.txt
$
$ ls
temp3.txt
Remove multiple files:
$ ls
temp3.txt
$
$ cp temp3.txt temp4.txt
$
$ cp temp3.txt temp5.txt
$
$ cp temp3.txt temp6.txt
$
$ ls
temp3.txt temp4.txt temp5.txt temp6.txt
$
$ rm temp5.txt temp6.txt
$
$ ls
temp3.txt temp4.txt
Remove a directory and its contents:
$ pwd
/home/hari/directory3
$
$ ls
directory4
$
$ rm -r directory4
$
$ ls
mv
The "mv" command is used to move or rename files and directories. It allows to move files and directories from one location to another, as well as rename them.
Rename a file:
$ pwd
/home/hari/directory3
$
$ ls
$
$ cd ..
$
$ ls
combined.txt directory1 directory2 directory3 directory4 temp1.txt temp2.txt temp3.txt temp4.txt
$
$ mv temp4.txt temp4_New.txt
$
$ ls
combined.txt directory1 directory2 directory3 directory4 temp1.txt temp2.txt temp3.txt temp4_New.txt
$
Move a file to a new location:
$ ls
combined.txt directory1 directory2 directory3 directory4 temp1.txt temp2.txt temp3.txt temp4_New.txt
$
$ mv temp4_New.txt directory4/
$
$ ls
combined.txt directory1 directory2 directory3 directory4 temp1.txt temp2.txt temp3.txt
$
$ cd directory4
$
$ pwd
/home/hari/directory4
$
$ ls
temp3.txt temp4_New.txt temp4.txt
$
Move multiple files to a directory:
$ pwd
/home/hari/directory4
$
$ ls
temp3.txt temp4_New.txt temp4.txt
$
$ cd ..
$
$ pwd
/home/hari
$
$ ls
combined.txt directory1 directory2 directory3 directory4 temp1.txt temp2.txt temp3.txt
$
$ mv temp1.txt temp2.txt temp3.txt directory4/
$
$ cd directory4
$
$ pwd
/home/hari/directory4
$
$ ls
temp1.txt temp2.txt temp3.txt temp4_New.txt temp4.txt
$
Move and rename a file simultaneously:
$ pwd
/home/hari/directory4
$
$ ls
temp1.txt temp2.txt temp3.txt temp4_New.txt temp4.txt
$
$ mv temp3.txt /home/hari/temp3_new.txt
$
$ ls
temp1.txt temp2.txt temp4_New.txt temp4.txt
$
$ cd ..
$
$ ls
combined.txt directory1 directory2 directory3 directory4 temp3_new.txt
$
$ pwd
/home/hari
$
grep
The "grep" is primarily used for searching and filtering text files or outputs based on specified patterns or regular expressions. Some common options include -i for case-insensitive matching, and -v for inverting the match to display lines that do not match the pattern.
Syntax:
$ grep [options] [string pattern] [file]
Searching for a specific word in a file:
$ grep Application demo.log
2023-05-22 10:15:21 [INFO] Application started.
2023-05-22 10:15:35 [INFO] Application shutdown initiated.
2023-05-22 10:15:37 [INFO] Application shutdown completed.
2023-05-22 10:16:04 [INFO] Application exited.
2023-05-22 10:18:15 [INFO] Application exited.
2023-05-22 10:22:50 [INFO] Application exited.
$
$ grep application demo.log
2023-05-22 10:16:02 [DEBUG] Exiting application.
2023-05-22 10:18:10 [DEBUG] Exiting application.
2023-05-22 10:22:45 [DEBUG] Exiting application.
Searching for a specific word with case-insensitive in a file:
$ grep -i application demo.log
2023-05-22 10:15:21 [INFO] Application started.
2023-05-22 10:15:35 [INFO] Application shutdown initiated.
2023-05-22 10:15:37 [INFO] Application shutdown completed.
2023-05-22 10:16:02 [DEBUG] Exiting application.
2023-05-22 10:16:04 [INFO] Application exited.
2023-05-22 10:18:10 [DEBUG] Exiting application.
2023-05-22 10:18:15 [INFO] Application exited.
2023-05-22 10:22:45 [DEBUG] Exiting application.
2023-05-22 10:22:50 [INFO] Application exited.
Searching for a word in multiple files:
$ grep ERROR demo.log application.log
demo.log:2023-05-22 10:15:27 [ERROR] Failed to process request: Invalid input format.
application.log:2023-05-27 09:12:40 ERROR: Database connection failed. Check the database configuration.
application.log:2023-05-27 09:13:20 ERROR: Failed to send notification email. SMTP server not responding.
application.log:2023-05-27 09:14:20 ERROR: Failed to process payment. Invalid credit card details.
application.log:2023-05-27 09:15:05 ERROR: Unexpected server error occurred. Please contact support.
$
$ grep Application demo.log application.log
demo.log:2023-05-22 10:15:21 [INFO] Application started.
demo.log:2023-05-22 10:15:35 [INFO] Application shutdown initiated.
demo.log:2023-05-22 10:15:37 [INFO] Application shutdown completed.
demo.log:2023-05-22 10:16:04 [INFO] Application exited.
demo.log:2023-05-22 10:18:15 [INFO] Application exited.
demo.log:2023-05-22 10:22:50 [INFO] Application exited.
application.log:2023-05-27 09:12:30 INFO: Application started.
application.log:2023-05-27 09:12:42 INFO: Application shutdown.
application.log:2023-05-27 09:13:22 INFO: Application running in production mode.
application.log:2023-05-27 09:14:25 INFO: Application version 2.1.0 deployed.
Searching for a word on result of another search result:
$ grep Application demo.log application.log | grep started
demo.log:2023-05-22 10:15:21 [INFO] Application started.
application.log:2023-05-27 09:12:30 INFO: Application started.
Searching for a file from the list of files and directories:
$ ls -l | grep temp3_new.txt
-rw-rw-r-- 1 hari hari 766 May 25 14:23 temp3_new.txt
chmod
The "chmod" is used to change the permissions of files and directories. The term "chmod" stands for "change mode".
Syntax:
$ chmod [mode] [file]
$
$ chmod [mode] [directory]
Information on file permissions:
r : The read permission (has a value of 4).
w : The write permission (has a value of 2).
x : The execute permission (has a value of 1).
- : permission denied (has a value of 0).
drwxrwxr-x 2 hari hari 4096 May 28 06:54 directory4
|[---][---][---] - [---] [---]
| | | | | | |
| | | | | | +----------> Group
| | | | | +---------------> Owner
| | | | +-------------------> Alternate Access Method
| | | +-----------------------> Others Permissions
| | +----------------------------> Group Permissions
| +--------------------------------> Owner Permissions
+-----------------------------------> File Type
The first column shown is the permissions assigned to the file or directory
The second column is the number of files or folders contained
The third and fourth columns show the user and group who have permissions for those files, respectively
The fifth and sixth show the size and modification date
The final column shows the name of the file
7 - Full rights ( Read , write and Execute ) ( 4 + 2 + 1 = 7 )
6 - Read and write ( 4 + 2 = 6 )
5 - Read and execute ( 4 + 1 = 5 )
4 - Read-only
3 - Execute and write ( 1 + 2 = 3 )
2 - Write only
1 - Execute only
0 - No rights
Giving full permissions to a file ( rwx to user, group, others ):
$ ls -lrt | grep application.log
-rw-r--r-- 1 hari hari 1623 May 28 09:34 application.log
$
$ chmod 777 application.log
$
$ ls -lrt | grep application.log
-rwxrwxrwx 1 hari hari 1623 May 28 09:34 application.log
Giving Execute only permissions to a file to group and no permissions to user and others:
$ ls -lrt | grep application.log
-rwxrwxrwx 1 hari hari 1623 May 28 09:34 application.log
$
$ chmod 010 application.log
$
$ ls -lrt | grep application.log
------x--- 1 hari hari 1623 May 28 09:34 application.log
Changing permissions of a directory:
$ ls -lrt | grep directory3
drwxrwxr-x 2 hari hari 4096 May 25 23:26 directory3
$
$ chmod 574 directory3
$
$ ls -lrt | grep directory3
dr-xrwxr-- 2 hari hari 4096 May 25 23:26 directory3