Breaking News

ESSENTIAL COMMANDS IN SHELL SCRIPT

ESSENTIAL COMMANDS IN SHELL SCRIPT



LS COMMAND:-
ls command is one of the most frequently used command in Linux. I believe ls command is the first command you may use when you get into the command prompt of Linux Box.
1. List Files using ls with no option
ls with no option list files and directories in bare format where we won’t be able to view details like file types, size, modified date and time, permission and links etc.
# ls
0001.pcap        Desktop    Downloads         index.html   install.log.syslog  Pictures  Templates
anaconda-ks.cfg  Documents  fbcmd_update.php  install.log  Music               Public    Videos
2 List Files With option –l
Here, ls -l (-l is character not one) shows file or directory, size, modified date and time, file or folder name and owner of file and its permission.
# ls -l
total 176
-rw-r--r--. 1 root root   683 Aug 19 09:59 0001.pcap
-rw-------. 1 root root  1586 Jul 31 02:17 anaconda-ks.cfg
drwxr-xr-x. 2 root root  4096 Jul 31 02:48 Desktop
drwxr-xr-x. 2 root root  4096 Jul 31 02:48 Documents
drwxr-xr-x. 4 root root  4096 Aug 16 02:55 Downloads
-rw-r--r--. 1 root root 21262 Aug 12 12:42 fbcmd_update.php
3. Sort Files by File Size
With combination of -lS displays file size in order, will display big in size first.
# ls -lS
total 176
-rw-r--r--. 1 root root 48867 Jul 31 02:17 install.log
drwxr-xr-x. 2 root root  4096 Jul 31 02:48 Desktop
4. Display Inode number of File or Directory
We can see some number printed before file / directory name. With -ioptions list file / directory with inode number.
# ls -i
20112 0001.pcap        23610 Documents         23793 index.html          23611 Music     23597 Templates
23564 anaconda-ks.cfg  23595 Downloads            22 install.log         23612 Pictures  23613 Videos
23594 Desktop          23585 fbcmd_update.php     35 install.log.syslog  23601 Public
CD COMMAND:-
The cd command, also known as chdir (change directory), is a command-line OS shell command used to change the current working directory in operating systems such as UnixDOSOS/2AmigaOS (where if a bare path is given, cd is implied), Windows, and Linux. It is also available for use in shell scripts and batch files. The system call that affects the command in most operating systems is chdir that is defined by POSIX
EXAMPLE OF CD COMMAND:-
user@wikipedia:~$ ls
workreports games encyclopedia text.txt
user@wikipedia:~$ cd games
user@wikipedia:~/games$

cd by itself or cd ~ will always put you in your home directory.
cd . will leave you in the same directory you are currently in (i.e. your current directory won't change). This can be useful if your shell's internal code can't deal with the directory you are in being recreated; running cd . will place your shell in the recreated directory.
cd ~username will put you in username's home directory.
cd dir (without a /) will put you in a subdirectory; for example, if you are in /usr, typing cd bin will put you in /usr/bin, while cd /bin puts you in /bin.
cd .. will move you up one directory. So, if you are /usr/bin/tmp, cd .. moves you to /usr/bin, while cd ../.. moves you to /usr (i.e. up two levels). You can use this indirection to access subdirectories too. So, from /usr/bin/tmp, you can use cd ../../local to go to /usr/local.
cd - will switch you to the previous directory. For example, if you are in /usr/bin/tmp, and go to /etc., you can type cd - to go back to /usr/bin/tmp. You can use this to toggle back and forth between two directories
MV COMMAND:-
 mv command.
mv command is used to move files and directories.
mv command syntax
$ mv [optionssource dest
mv command  options:
option
description
mv -f
force move by overwriting destination file without prompt
mv -i
interactive prompt before overwrite
mv -u
update - move when source is newer than destination
mv -v
verbose - print source and destination files
man mv
help manual
EXAMPLE OF MV COMMAND:-
Move main.c def.h files to /home/usr/rapid/ directory:
$ mv main.c def.h /home/usr/rapid/
MAN COMMAND:-
On Linux and other Unix-like operating systemsman is the interface used to view the system's reference manuals.
MAN  COMMAND SYNTAX:-
man [-C file] [-d] [-D] [--warnings[=warnings]] [-R encoding] [-L locale]
    [-m system[,...]] [-M path] [-S list] [-e extension] [-i|-I]
    [--regex|--wildcard] [--names-only] [-a] [-u] [--no-subpages] [-P pager]
    [-r prompt] [-7] [-E encoding] [--no-hyphenation] [--no-justification]
    [-p string] [-t] [-T[device]] [-H[browser]] [-X[dpi]] [-Z]
    [[section] page ...] ...

-h--help
Print a help message and exit.
-V--version
Display version information and exit.
-C file--config-file=file
Use configuration file file rather than the default of ~/.manpath.
-d--debug
Print debugging information.
-D--default
This option, when used, is normally specified as the first option; it resets man's behaviour to its default. Its use is to reset those options that may have been set in $MANOPT. Any options that follow -D will have their usual effect.
--warnings[=warnings]
Enable warnings from the groff text formatter. This may be used to perform sanity checks on the source text of manual pages. The warnings is a comma-separated list of warning names; if it is not supplied, the default is "mac". See the "Warnings"node in the groff info page for a list of available warning names.

MKDIR COMMAND:-
The mkdir command in UNIX allows users to create directories or folders as they are referred to in some operating systems. The mkdir command can create multiple directories at once and also set permissions when creating the directory. The user running the command must have appropriate permissions on the parent directory to create a directory or will receive a permission denied error.
How to create a directory
To create a directory in UNIX or Linux using the mkdir command pass the name of directory to the mkdir command.
mkdir mydirectory
ls
mydirectory
syntax of mkdir
mkdir directory
The rmdir command removes empty directories from your filesystem.
Description
The rmdir command removes each directory specified on the command line, if they are empty. That is, each directory removed must contain no files or directories, or it cannot be removed by rmdir.
Syntax
rmdir [-p] [-v | --verbose] [--ignore-fail-on-non-empty] directory ...

-p
Each directory argument is treated as a pathname of which all components will be removed, if they are empty, starting with the last component. (See rm for fully non-discriminant recursiveremoval.)
-v,
--verbose
Display verbose information for every directory processed.
--ignore-fail-on-non-empty
Do not report a failure which occurs solely because a directory is non-empty. Normally, when rmdir is instructed to remove a non-empty directory, it reports an error. This option suppresses those error messages.
--help
Display a help message, and exit.
--version
Output version information, and exit.

Examples
rmdir mydir
Remove the directory mydir, if it is empty.
rmdir dir1 dir2 dir3
The rm ("remove") command is used to delete files. When used recursively, it may be used to delete directories.
Description
rm removes each file specified on the command line. By default, it does not remove directories; see removing directories below for details
Syntax
rm [-f | --force] {[-i | --interactive[=always]] | [-I | --interactive=once] |
   [--interactive=never]} [--one-file-system] [--no-preserve-root |
   --preserve-root] [-r | -R | --recursive] [-d | --dir] [-v | --verbose]
   FILE...

-f,
--force
Ignore nonexistant files, and never prompt before removing.
-i
Prompt before every removal.
-I
Prompt once before removing more than three files, or when removing recursively. This option is less intrusive than -i, but still gives protection against most mistakes.
--interactive[=WHEN]
Prompt according to WHENneveronce (-I), or always (-i). If WHEN is not specified, then prompt always.
--one-file-system
When removing a hierarchy recursively, skip any directory that is on a file system different from that of the corresponding command line argument
--no-preserve-root
Do not treat "/"(the root directory) in any special way.
--preserve-root
Do not remove "/"(the root directory), which is the default behavior.
-r,
-R,
--recursive
Remove directories and their contents recursively.
-d,
--dir
Remove empty directories. This option permits you to remove a directory without specifying -r/-R/--recursive, provided that the directory is empty. In other words, rm -d is equivalent to using rmdir.
-v,
--verbose
Verbose mode; explain at all times what is being done.
--help
Display a help message, and exit.
--version
Display version information, and exit.

NAME
clear - clear the terminal screen
SYNOPSIS
clear
DESCRIPTION
clear clears your screen if this is possible. It looks in the environment for the terminal type and then in the terminfo database to figure out how to clear the screen. clear ignores any command-line parameters that may be present.
OPTIONS
--help
display help and exit
EXAMPLES
Clear the terminal
$ clear



No comments