Basic UNIX Commands

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.

 

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.

 

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.

 

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.

 

mkdir –p <new_directory_path>

  • Creates a new directory along with the parent directory as specified in the command.

Eg.

 

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. 

 

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.

 

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.

 

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.

 

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.

 

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.

 

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.

 

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.

 

date” command

  • Displays the current date and time

Eg.

 

pwd” command

  • Displays the current working directory.

Eg.

 

clear” command

  • Clears the screen.

Eg.

 

Leave a Reply

Your email address will not be published.