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:
1
2
3
4
5
| 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:
1
| file -b filename |
Example:
1
| file -b email.py |
Output:
1
2
| 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:
1
| 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:
1
| #find [where to search] -name [what to search] |
Example:
1
| #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:
1
| 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
1
2
| Hello mindmajix Hai world, |
2. Using cat command create a file:
Syntax:
1
| # cat >org |
Output:
1
| # cat >org |
Hai Mindmajix, Hello world
3. Display the line numbers in the file
To display the line numbers in the file using -n in the command.
Syntax:
1
| cat -n filename |
Let us consider one example
1
| #cat -n mindmajix.text |
Output:
1
| #cat -n mindmajix.txt |
1
2
| 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:
1
2
3
| # cat filename; cat filename1; cat filename2 |
Let us check with one example
1
| # cat org; cat file; cat file1 |
Output:
1
| # cat org; cat file; cat file1 |
1
2
3
| 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:
1
2
3
| 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.
1
| Syntax: cp Source_file Dest_file |
Example:
1
2
3
4
5
| $ 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:
1
| cp Src_file1 Src_file2 Src_file3 Dest_directory |
Let us consider one example
1
2
3
4
5
6
7
| $ 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:
1
| cp -R Src_directory Dest_directory |
Pwd Command:
This command print the current working directory.
Syntax: pwd [option]
Option:
1
2
3
4
5
6
| -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.
1
| Syntax: # ls [option] [file] |
Display list of files and directories without using options
1
| Syntax: # ls |
Output:
1
| # ls |
File.py file1.text music download mindmajix.html docs <br><br>
2. Option -l
Syntax:
1
| #ls -l |
Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| 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:
1
| # ls -r |
Output:
1
| # ls -r |
1
| 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:
1
| chmod [option] MODE file |
2. Using symbolic values and removing the file permission:
It represents as
1
2
3
4
5
6
| u- user g- group o- others all r- read w -write x - execute |
Syntax:
1
| chmod [option] Mode1, Mode2,...file |
Options:
1
2
3
4
5
6
7
| -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:
1
| # chown [OPTION]... [OWNER][:[GROUP]] FILE... |
Example:
1
| 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:
1
2
3
4
5
| $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:
1
| # mv [options] source dest |
Example:
1
2
3
4
5
| # 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:
1
| mv -i srce dest |
Example:
1
2
3
| # ls file1.txt file2.txt file3.txt geek.txt # cat geek.txt |
Welcome to mindmajix
1
| # 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:
1
| mv -f src dest |
Example:
1
2
3
| # ls file1.txt file2.txt file3.txt geek.txt # cat file2.txt |
Welcome to Mindmajix
1
2
3
4
5
6
7
8
9
10
| # 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:
1
| mv -n source file dest file |
Example:
1
2
3
| # ls file1.txt file2.txt file3.txt geek.txt # cat file2.txt |
Welcome Mindmajix
1
2
3
4
5
| # 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:
1
| mv -b source file dest file |
Example:
1
2
3
4
5
| # 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:
1
| mv --version |
Example:
1
2
3
4
5
6
7
| # 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:
1
| rm [option].. file name.. |
Example:
1
2
3
4
5
| #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:
1
| rm -i file name |
Example:
1
2
3
4
| # 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:
1
| rm -f filename |
Example:
1
2
3
4
5
6
7
8
| # 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:
1
| rm -r filename/directory name |
--version:
This command displays the current version of the system.
Syntax:
1
| rm --version |
Example:
1
2
3
4
5
6
7
8
9
| # 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:
1
| mkdir [options...] [directories ...] |
Example:
1
| # mkdir mynew_dir |
Option:
1
| -v[verbose]: |
Whenever it creates the directory it displays the message.
Syntax:
1
| mkdir -v directory name |
Example:
1
2
3
4
| #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:
1
| mkdir -p [directories] |
Let the check with an example
1
2
3
4
| # 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:
1
| mkdir -m a=wrx [directory names] |
Example:
1
| # mkdir -m a=rw Suneel |
rmdir(remove directory):
This command is used to delete the directory.
Syntax:
1
| rmdir [-p] [-v | -verbose] [-ignore-fail-on-non-empty] directories ... |
No comments:
Post a Comment