Below are the basic UNIX commands that we use, while working in UNIX environment. UNIX commands are case sensitive.
Listing of Files
“ls” command
Usage: ls [<options>] [<input_path>]
ls <input-path-of-file-or-directory>
- Lists all the file names in a given directory if input path is a directory.
- Displays the file name if the given input path is a file.
Eg.
1 2 3 4 5 6 7 8 |
[admin@myworkstation clearurdoubt]$ pwd /user/workspace/clearurdoubt [admin@myworkstation clearurdoubt]$ ls subdir_1 testfile_1.txt testfile_2.txt [admin@myworkstation clearurdoubt]$ ls /user/workspace/clearurdoubt subdir_1 testfile_1.txt testfile_2.txt |
ls –l <input-path-of-file-or-directory>
- Lists all the file names with all the details like permissions, owner info, last modified date etc., in a given directory in ascending order of the names if input path is a directory.
- Displays the file name with all the details like permissions, owner info, last modified date etc., if the given input path is a file.
Eg.
1 2 3 4 5 6 7 8 9 10 11 |
[admin@myworkstation clearurdoubt]$ ls -l total 12 drwxrwxr-x 2 admin admin 4096 Jul 22 01:44 subdir_1 -rw-rw-r-- 1 admin admin 62 Jul 22 01:43 testfile_1.txt -rw-rw-r-- 1 admin admin 78 Jul 22 01:43 testfile_2.txt [admin@myworkstation clearurdoubt]$ ls -l /user/workspace/clearurdoubt total 12 drwxrwxr-x 2 admin admin 4096 Jul 22 01:44 subdir_1 -rw-rw-r-- 1 admin admin 62 Jul 22 01:43 testfile_1.txt -rw-rw-r-- 1 admin admin 78 Jul 22 01:43 testfile_2.txt |
ls –ltr <input-path-of-file-or-directory>
- Same as ls –l but the files are listed in descending order of their modified dates. Latest updated files are listed first.
Eg.
1 2 3 4 5 6 7 8 9 10 11 |
[admin@myworkstation clearurdoubt]$ ls -ltr total 12 -rw-rw-r-- 1 admin admin 62 Jul 22 01:43 testfile_1.txt -rw-rw-r-- 1 admin admin 78 Jul 22 01:43 testfile_2.txt drwxrwxr-x 2 admin admin 4096 Jul 22 01:44 subdir_1 [admin@myworkstation clearurdoubt]$ ls -ltr total 12 -rw-rw-r-- 1 admin admin 62 Jul 22 01:43 testfile_1.txt -rw-rw-r-- 1 admin admin 78 Jul 22 01:43 testfile_2.txt drwxrwxr-x 2 admin admin 4096 Jul 22 01:44 subdir_1 |
Directory Creation
“mkdir” command
Usage: mkdir [<options>] <new_dir_path>
mkdir <new_directory_path>
- Creates a new directory as specified in the command. If the parent directory is not present, this command gives an error message saying “no such file/directory.”
Eg.
1 2 3 4 5 6 7 8 9 10 11 12 |
[admin@myworkstation clearurdoubt]$ mkdir subdir_2 [admin@myworkstation clearurdoubt]$ [admin@myworkstation clearurdoubt]$ ls -l total 16 drwxrwxr-x 2 admin admin 4096 Jul 22 01:44 subdir_1 drwxrwxr-x 2 admin admin 4096 Jul 22 01:59 subdir_2 -rw-rw-r-- 1 admin admin 62 Jul 22 01:43 testfile_1.txt -rw-rw-r-- 1 admin admin 78 Jul 22 01:43 testfile_2.txt [admin@myworkstation clearurdoubt]$ mkdir subdir_3/inner_dir mkdir: cannot create directory `subdir_3/inner_dir': No such file or directory |
mkdir –p <new_directory_path>
- Creates a new directory along with the parent directory as specified in the command.
Eg.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[admin@myworkstation clearurdoubt]$ mkdir -p subdir_3/inner_dir [admin@myworkstation clearurdoubt]$ [admin@myworkstation clearurdoubt]$ ls -l total 20 drwxrwxr-x 2 admin admin 4096 Jul 22 01:44 subdir_1 drwxrwxr-x 2 admin admin 4096 Jul 22 01:59 subdir_2 drwxrwxr-x 3 admin admin 4096 Jul 22 02:00 subdir_3 -rw-rw-r-- 1 admin admin 62 Jul 22 01:43 testfile_1.txt -rw-rw-r-- 1 admin admin 78 Jul 22 01:43 testfile_2.txt [admin@myworkstation clearurdoubt]$ ls -l subdir_3/ total 4 drwxrwxr-x 2 admin admin 4096 Jul 22 02:00 inner_dir |
File Creation
“vi” command
Usage: vi [<new_file_path>/]<file_name>
vi <file_name>
- A new file is created with the file name as specified in the present working directory. vi editor is opened to write the contents of the contents of the file.
vi <new_file_path>/<file_name>
- A new file is created with the file name as specified in the path specified in the command i.e., <new_file_path>.
Eg.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[admin@myworkstation clearurdoubt]$ vi newfile_1.txt [admin@myworkstation clearurdoubt]$ pwd /user/workspace/clearurdoubt [admin@myworkstation clearurdoubt]$ vi /user/workspace/clearurdoubt/newfile_2.txt [admin@myworkstation clearurdoubt]$ [admin@myworkstation clearurdoubt]$ ls -l total 28 -rw-rw-r-- 1 admin admin 20 Jul 22 02:09 newfile_1.txt -rw-rw-r-- 1 admin admin 65 Jul 22 02:11 newfile_2.txt drwxrwxr-x 2 admin admin 4096 Jul 22 01:44 subdir_1 drwxrwxr-x 2 admin admin 4096 Jul 22 01:59 subdir_2 drwxrwxr-x 3 admin admin 4096 Jul 22 02:00 subdir_3 -rw-rw-r-- 1 admin admin 62 Jul 22 01:43 testfile_1.txt -rw-rw-r-- 1 admin admin 78 Jul 22 01:43 testfile_2.txt |
File Content Reading
“cat” command
Usage: cat [<new_file_path>/]<file_name>
cat <file_name>
- Displays the contents of the file specified in the command. If the file doesn’t exist in the present working directory, an error is displayed.
cat < file_path>/<file_name>
- Displays the contents of the file specified in the command. If the file doesn’t exist in the specified directory, an error is displayed.
Eg.
1 2 3 4 5 6 7 8 9 |
[admin@myworkstation clearurdoubt]$ cat newfile_1.txt This is a new file. [admin@myworkstation clearurdoubt]$ [admin@myworkstation clearurdoubt]$ cat /user/workspace/clearurdoubt/newfile_2.txt This is another test file. Created to explain the "vi" command. [admin@myworkstation clearurdoubt]$ |
Note: A file can be created by redirecting the output of “cat” command to it as below.
- cat <file_path>/<file_name> > <new_file_path>/<new_file_name>
- cat <file_path>/<file_name> >> <new_file_path>/<new_file_name>
In case of
Point# 1, file contents are overwritten with the output.
Point# 2, the output is appended to the existing file contents.
Eg.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[admin@myworkstation clearurdoubt]$ cat newfile_1.txt > cat_test.txt [admin@myworkstation clearurdoubt]$ [admin@myworkstation clearurdoubt]$ cat cat_test.txt This is a new file. [admin@myworkstation clearurdoubt]$ [admin@myworkstation clearurdoubt]$ cat newfile_2.txt >> cat_test.txt [admin@myworkstation clearurdoubt]$ [admin@myworkstation clearurdoubt]$ cat cat_test.txt This is a new file. This is another test file. Created to explain the "vi" command. [admin@myworkstation clearurdoubt]$ |
Providing Permissions
“chmod” command
Usage: chmod <options> <file_name/directory>
chmod 777 [<file_path>/]<file_name>
- The permissions of the file/directory are set to 777(i.e., rwxrwxrwx)
Eg.
1 2 3 4 5 6 7 |
[admin@myworkstation clearurdoubt]$ ls -l testfile_1.txt -rw-rw-r-- 1 admin admin 62 Jul 22 01:43 testfile_1.txt [admin@myworkstation clearurdoubt]$ chmod 777 testfile_1.txt [admin@myworkstation clearurdoubt]$ ls -l testfile_1.txt -rwxrwxrwx 1 admin admin 62 Jul 22 01:43 testfile_1.txt |
Copying Files
“cp” command
Usage: cp <source_path>/<source_file> <dest_path>/<dest_file>
- Copies the source file from source path to destination path.
Eg.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
[admin@myworkstation clearurdoubt]$ ls -l /user/workspace/clearurdoubt/subdir_1 total 0 [admin@myworkstation clearurdoubt]$ cp /user/workspace/clearurdoubt/newfile_1.txt /user/workspace/clearurdoubt/newfile_2.txt /user/workspace/clearurdoubt/subdir_1 [admin@myworkstation clearurdoubt]$ ls -l /user/workspace/clearurdoubt/subdir_1 total 8 -rw-rw-r-- 1 admin admin 20 Jul 22 05:03 newfile_1.txt -rw-rw-r-- 1 admin admin 65 Jul 22 05:03 newfile_2.txt [admin@myworkstation clearurdoubt]$ ls -l /user/workspace/clearurdoubt/ total 36 -rw-rw-r-- 1 admin admin 65 Jul 22 02:25 cat__append_test.txt -rw-rw-r-- 1 admin admin 85 Jul 22 02:25 cat_test.txt -rw-rw-r-- 1 admin admin 20 Jul 22 02:09 newfile_1.txt -rw-rw-r-- 1 admin admin 65 Jul 22 02:11 newfile_2.txt drwxrwxr-x 2 admin admin 4096 Jul 22 05:03 subdir_1 drwxrwxr-x 2 admin admin 4096 Jul 22 01:59 subdir_2 drwxrwxr-x 3 admin admin 4096 Jul 22 02:00 subdir_3 -rwxrwxrwx 1 admin admin 62 Jul 22 01:43 testfile_1.txt -rw-rw-r-- 1 admin admin 78 Jul 22 01:43 testfile_2.txt |
Moving/Renaming Files
“mv” command
Usage: mv <source_path>/<source_file> <dest_path>/<dest_file>
- Moves the source file from source path to destination path.
- Note: Renaming of file can be done using below command:
Eg.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
[admin@myworkstation clearurdoubt]$ ls -l subdir_2 total 0 [admin@myworkstation clearurdoubt]$ mv /user/workspace/clearurdoubt/newfile_1.txt /user/workspace/clearurdoubt/newfile_2.txt /user/workspace/clearurdoubt/subdir_2 [admin@myworkstation clearurdoubt]$ [admin@myworkstation clearurdoubt]$ ls -l subdir_2 total 8 -rw-rw-r-- 1 admin admin 20 Jul 22 02:09 newfile_1.txt -rw-rw-r-- 1 admin admin 65 Jul 22 02:11 newfile_2.txt [admin@myworkstation clearurdoubt]$ ls -l /user/workspace/clearurdoubt/ total 28 -rw-rw-r-- 1 admin admin 65 Jul 22 02:25 cat__append_test.txt -rw-rw-r-- 1 admin admin 85 Jul 22 02:25 cat_test.txt drwxrwxr-x 2 admin admin 4096 Jul 22 05:03 subdir_1 drwxrwxr-x 2 admin admin 4096 Jul 22 05:04 subdir_2 drwxrwxr-x 3 admin admin 4096 Jul 22 02:00 subdir_3 -rwxrwxrwx 1 admin admin 62 Jul 22 01:43 testfile_1.txt -rw-rw-r-- 1 admin admin 78 Jul 22 01:43 testfile_2.txt |
Printing text
“echo” command
Usage: echo [-e] <any text/variable>
- This command displays the given text or variable value to the stdout. If used with “–e” option, the formatting of the text is retained.
Eg.
1 2 3 4 5 6 7 8 9 10 11 |
[admin@myworkstation clearurdoubt]$ echo "Hello, there! Happy learning. Yours ClearUrDoubt Team." Hello, there! Happy learning. Yours ClearUrDoubt Team. [admin@myworkstation clearurdoubt]$ echo "Hello, there! \nHappy learning. \nYours ClearUrDoubt Team." Hello, there! \nHappy learning. \nYours ClearUrDoubt Team. [admin@myworkstation clearurdoubt]$ echo -e "Hello, there! \nHappy learning. \nYours ClearUrDoubt Team." Hello, there! Happy learning. Yours ClearUrDoubt Team. [admin@myworkstation clearurdoubt]$ |
Additional Commands
“dos2unix” command
Usage: dos2unix [<file_path>/]<file_name>
dos2unix <file_name>
- It is highly suggested to convert the file format from WINDOWS to UNIX whenever a file is edited in WINDOWS and deployed in UNIX environment.
Eg.
1 2 3 4 5 6 7 8 |
[admin@myworkstation clearurdoubt]$ ls -l testfile_2.txt -rw-rw-r-- 1 admin admin 78 Jul 22 01:43 testfile_2.txt [admin@myworkstation clearurdoubt]$ dos2unix testfile_2.txt dos2unix: converting file testfile_2.txt to UNIX format ... [admin@myworkstation clearurdoubt]$ ls -l testfile_2.txt -rw-rw-r-- 1 admin admin 78 Jul 22 05:21 testfile_2.txt |
“date” command
- Displays the current date and time
Eg.
1 2 |
[admin@myworkstation clearurdoubt]$ date Sun Jul 22 05:23:49 CST 2018 |
“pwd” command
- Displays the current working directory.
Eg.
1 2 |
[admin@myworkstation clearurdoubt]$ pwd /user/workspace/clearurdoubt/ |
“clear” command
- Clears the screen.
Eg.
1 |
[admin@myworkstation clearurdoubt]$ clear |