As we know that Operating System is an important component of the computer. Linux operating system is an open source and community developed for mainframes, servers. Linux is one of the widely accepted operating systems across the world. 

Here in this article we are going to discuss briefly about top 50 Linux commands, and their working in real time environment. To enhance your skills on Linux, visit our Linux Tutorials.

Now, Let's get into the commands section.

File Commands in Linux Operating System

File command:

This command is used to determine the file type, and you can easily read and understand it. This command categorizes each argument after testing.  

It has three sets to test the argument.

Filesystem Test: Based on the stat system call results it verifies empty files and sorts few files, this prints the type of the file. 

Magic Test: It checks the format of data in a particular file and prints the category.

Language Test: It searches the string which appears in the blocks of a file. 

Syntax: 

file [option] [filename]

Example: 

file email.py
file name.jpeg
file invoice.pdf
file exam.ods
file videosong.mp4

Output:

Option: this displays the file types. 

-b, [–brief]: This gives a brief mode of the file. 

Syntax:

file -b filename

Example: 

file -b email.py

Output:

root@tryit-next:~ file xyz.py
xyz.py : ASCII text

Following are the few commands to check the file type without filename

1. * option: Shows all files, file types

Syntax: file *

Displays all files types in the system.

2. directory name/* option: Shows all files file type in the current directory

Syntax: file directoryname/*

3. -I option: this displays the mime type files.

Syntax: file -i filename

4. -z option: displays compressed files

Syntax: file -z flash.tar.gz

5. -s option: Displays special files

Syntax: file -s filename

 

df command: this command displays the space of hard disk and how much disk was free. 

Syntax: df 

Following image will show you the output of df command. 

To print the same output in GB/MB/KB, use -h option in the command.

Syntax: df -h

du command: It displays the size of the file and to know that how much disk space using by the files in the directories. 

Syntax: #du

Output: It shows how much space used by files in the current directory.

du/boot: this displays the file size in another directory also.

Syntax: #du/boot

Output: Print the file size of the current directory and other directory.

du-h/boot: It displays the size of the file in GB/MB/Kb.

Syntax: #du -h/ boot

Output: 

The diff Command: 

This diff command is used to check the differences between two files.

Syntax: 

diff file1  file2

Let us consider one example and check how the command works

Create a dummy file and copy the same data to dummy file and change the data in the original file now check the command. See the changes in the output.

find command

This command helps to search the necessary data in the specified location.

Syntax:

#find [where to search] -name [what to search]

Example: 

#find /etc -named hostname

 

 

Cat command:

This command used to create and view single or multiple files, and concatenate the files. It will consider one file output as another file input and print in the terminal or file. 

General Syntax: 

cat [option] [file]..

Multiple files content view: In this multiple files data will be displayed in the terminal.

Let’s us consider one example 

1. cat file file1

Output: 

# cat file file1

Hello mindmajix
Hai world,

2. Using cat command create a file:

Syntax:

# cat >org

 

Output:

# cat >org

Hai Mindmajix, Hello world

Subscribe to our youtube channel to get new updates..!

3. Display the line numbers in the file

To display the line numbers in the file using -n in the command.

Syntax:

cat -n filename

 

Let us consider one example

#cat -n mindmajix.text

Output:

#cat -n mindmajix.txt
1 Have a wonderful life
2 Happy Birthday!

 

4. View multiple files

To display multiple files at once we can use the command as below

Syntax:

# cat filename; 
cat filename1; 
cat filename2

Let us check with one example

# cat org; cat file; cat file1

Output:

# cat org; cat file; cat file1
Hai Mindmajix,
Hello mindmajix
Hai world,

 

Copy command

Copy command represent with cp, it helps to copy the data into other files and directories. In cp command at least pass 2 arguments.

Syntax: 

cp [OPTION] Source Destination
cp [OPTION] Source Directory
cp [OPTION] Source-1 Source-2 Source-3 Source-n Directory

 

Two file names: If the command contains 2 file names, then the data will be copied from the first file to the second file. If the second file does not exist it will create a new file and then data will be copied to the second file. 

Syntax: cp Source_file Dest_file

Example: 

$ ls
file.txt
$ cp file.txt file1.txt
$ ls
file.txt  file1.txt

 

One or more arguments: If the command has one or more arguments and the destination is directory then these source files will copy as it is to the directory.

Syntax:

cp Src_file1 Src_file2 Src_file3 Dest_directory

Let us consider one example

$ ls
file.txt file1.txt  new
Initially new is empty
$ ls ne
$ cp file.txt file1.txt new
$ ls new
file.txt file1.txt

 

3. Directories:

In this command,we need to consider two directories, means the data is copied from source directory to destination directory.

Syntax:

cp -R Src_directory Dest_directory

Pwd Command:

This command print the current working directory. 

Syntax: pwd [option]

Option: 

-L: It prints logical links
Syntax: pwd -L
-P: It prints physical links
syntax: pwd -P
-version: Display the current version of the system.
Syntax: pwd -version  

 

ls Command : 

This command is used to display the directory content. 

Syntax: # ls [option] [file]

Display list of files and directories without using options

Syntax: # ls 

Output: 

# ls

 

File.py  file1.text  music  download  mindmajix.html  docs <br><br>

2. Option -l

Syntax:

#ls -l

 

Output: 

total 167
-rw-r--r--. 1 root root   683 Jan 19 08:59 0002.py
-rw-------. 1 root root  1568 Jun 31 03:17 anaconda.cfg
drwxr-xr-rw.2 root root  0496 Jul 26 06:48 Dsktop
drwxr-xr-x. 2 root root  4096 Jul 31 07:48 Docs
drwxr-xr-x. 3 root root  4096 Aug 16 02:55 Downlds
-rw-r--r--. 1 root root 21262 Jan 12 12:42 fbcmd.php
-rw-r--r--. 1 root root 46071 Jul 25 09:58 index1.html
-rw-r--rw-. 1 root root 46887 Jul 31 02:26 install.log
-rw-rw-r--. 1 root root 14139 Jan 31 05:13 install.syslog
drwxr-wr-x. 2 root root  4096 Aug 31 02:48 Music
drwxr-xr-x. 2 root root  4906 Jul 24 06:48 Pictures
drwxr-wr-x. 2 root root  4906 Jul 31 03:48 Public
drwxr-xr-x. 2 root root  4906 Aug 28 02:48 Templts
drwxr-wr-x. 2 root root  9046 Jul 31 05:48 Videos

3. # ls -r

This command is used to display the list in reverse order.

Syntax:

# ls -r 

Output: 

# ls -r 
docs  mindmajix.html download music file1.txt  file.txt 

 

chmod command:

This command is used to change the mode. 

It can be used in two ways

Octal value & position

Using symbolic values and removing the file permission

Octal value & position: To set the permissions for groups, owners and others with the octal value. 

4- read permission

2- write permission

        1- execute permission

Syntax:

chmod [option] MODE file

    2. Using symbolic values and removing the file permission:

It represents as

u- user
g- group
o- others all
r- read
w -write
x - execute

 

Syntax:

chmod [option] Mode1, Mode2,...file 

 

Options:

-c, --changes the mode
-f, --silent, --quiet
-v, --verbose
--no- preserve-root
--preserve-root
--reference=RFILE
-R, --recursive

 

chown command:

This command is used to change the ownership

Syntax:

# chown [OPTION]... [OWNER][:[GROUP]] FILE... 

 

Example:

chown owner_name file_name

 

Chsh command: It changes the user login shell. It specifies the user name in the login command. Superuser changes the ownership to any account, but the user can change only for his account.

Syntax:

$chsh [options] [LOGIN]
 -l It prints the list of variables in the system
chsh  change the login shell 
 -s path_to_ksh -- It changes the current login shell from Bash to Ksh
-s --- It changes the user shell

 

Move command: 

This command is used to move the files and directories

Syntax:

# mv [options] source dest 

Example: 

# ls
file1.txt  file2.txt  file3.txt  file4.txt
# mv file1.txt geek.txt
# ls
file2.txt  file3.txt  file4.txt  geek.txt

 

Options: 

-i (interactive):

This command confirms with the user before moving the file or to overwrite the existing file. If the user is press y, then it moves or overwrites the file.

 

Syntax:

mv -i  srce  dest 

 

Example: 

# ls
file1.txt  file2.txt  file3.txt  geek.txt
# cat geek.txt

Welcome to mindmajix

# cat file2.txt

Hello world

Welcome to Mindmajix

 

2. -f(force):

This command overwrites the data to destination and deletes the source file forcefully. 

Syntax:

mv -f src dest

Example: 

 

# ls
file1.txt  file2.txt  file3.txt  geek.txt
# cat file2.txt

Welcome to Mindmajix

# ls -l b.txt
-r--r--r--+ 1 User User 14 Jan  9 15:37 file2.txt
# mv geek.txt file2.txt
mv: replace 'file2.txt', overriding mode 0444 (r--r--r--)? n
# ls
file1.txt  file2.txt  file3.txt  geek.txt
# mv -f geek.txt file2.txt
# ls
file1.txt  file2.txt  file3.txt
# cat file2.txt

Hello World

3. -n (no-clobber):

It prevents the overwriting for the existing files. 

Syntax:

mv -n source file dest file

Example: 

# ls
file1.txt  file2.txt  file3.txt  geek.txt
# cat file2.txt

Welcome Mindmajix

# mv -n geek.txt file2.txt
# ls
file1.txt  file2.txt  file3.txt  geek.txt
$ cat file2.txt
Welcome Mindmajix

 

4. -b (backup):

Using this command it is easier to take backup for the existing overwritten file using the mv command. It represents the (~) 

Syntax:

mv -b source file dest file

 

Example: 

# ls
file1.txt  file2.txt  file3.txt  geek.txt
$ mv -b geek.txt file2.txt
$ ls
file1.txt  file2.txt~  file3.txt  file4.txt

 

5. -version: This command is used to display the current version of the system.

Syntax:

mv --version

 

Example:

# mv --version
mv (GNU coreutils) 8.26
Packaged by Cygwin (8.26-2)
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Parker, David MacKenzie, and Jim Meyering.

 

Remove command: This command is used to remove the file from the current directory.

Syntax:

rm [option].. file name..

Example:

#ls 
file1.txt  file2.txt  file3.txt 
#rm file1.txt
#ls
file2.txt  file3.txt

 

-i (interactive deletion):

This command confirms with the user whether to delete the file from the current directory or not. Once the user confirms deletion, it deletes the file from the directory.

Syntax:

rm -i file name

 

Example: 

# rm -i file1.tx
rm: remove regular empty file 'file1.txt'? y
# ls
file2.txt

-f(force deletion):

This command forcefully removes the file from the current directory.

Syntax:

rm -f filename

Example: 

# ls -l
total 0
-r--r--r--+ 1 User User 0 Jan  2 22:56 file1.txt
# rm file1.txt
rm: remove write-protected regular empty file 'file1.txt'? n
# ls
file1.txt
# rm -f file1.txt

 

-r(recursion deletion):

It deletes all files, subdirectories and directory. Actually, rm couldn’t delete the directory, but when you use this option it will delete the directory also. 

Syntax:

rm -r filename/directory name

--version:

This command displays the current version of the system.

Syntax: 

rm --version

Example: 

# rm --version
rm (GNU coreutils) 8.26
Packaged by Cygwin (8.26-2)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Paul Rubin, David MacKenzie, Richard M. Stallman,
and Jim Meyering.

 

mkdir:

This command is used to create a directory 

Syntax:

mkdir [options...] [directories ...]

Example:

# mkdir mynew_dir

 

Option: 

-v[verbose]:

Whenever it creates the directory it displays the message.

Syntax:

mkdir -v  directory name

Example: 

#mkdir -v mindmajix tekslate 
mkdir: created directory 'mindmajix'
mkdir: created directory 'tekslate'
#ls 

mindmajix tekslate

2. -p :

This command is used to enable the parent directory.

Syntax:

mkdir -p [directories]

Let the check with an example

# mkdir -p -v mindmajix online trainings
mkdir: created directory 'mindmajix'
mkdir: created directory 'online’ trainings'
mkdir: created directory ‘mindmajix’ ‘online’ 'trainings’

 

3. -m(mode):

This command sets the file permissions to the directories. 

Syntax:

mkdir -m a=wrx [directory names]

Example: 

# mkdir -m a=rw Suneel

rmdir(remove directory):

This command is used to delete the directory. 

Syntax: 

rmdir [-p] [-v | -verbose] [-ignore-fail-on-non-empty] directories ...