Skip to content
LogeshVel edited this page Sep 3, 2022 · 36 revisions

Shell Scripting

What is shell script?

A file that contains a series of commands.

Anything you can do on the command line can be automated by writing a shell script.

In Linux everything is a File. Everything is a file. 📁

type

We can use type command to check whether the command or is what type shell builtin or ...

example:

type ps

SHELL

To check which shell type currently using, we could use the SHELL variable

image

command

command Execute a simple command or display information about commands.

Its just used to execute the command and also by using the -v flag we could see the some information about that program.

image

os-release

image

man bash

To see all available shell variables and much more.

man bash

Here are some of the shell variables

image

image

printenv

printenv - print all or part of environment

image

date

date - print or set the system date and time

To print the date in our required format date +YOUR_FORMAT

See the available formats in the man page

image

/usr/local/bin

What's the suitable location to place the script that is shared with some users.

/usr/local/bin

Linux Foundation FHS

image

find

find - search for files in a directory hierarchy recursively

find <from_path> -type <FILE_TYPE> -name <NAME_OF_FILE>

-type File is of type c:

          b      block (buffered) special

          c      character (unbuffered) special

          d      directory

          p      named pipe (FIFO)

          f      regular file

          l      symbolic  link; this is never true if the -L option or the -follow option is in effect, unless the symbolic link is broken.  If
                 you want to search for symbolic links when -L is in effect, use -xtype.

          s      socket

          D      door (Solaris)

          To search for more than one type at once, you can supply the combined list of type letters separated by a comma `,' (GNU extension).

-name

name of the file or dir, supports the wildcard masks.

image

tar

tar - an archiving utility

Usage:

To create tar file from the files or folders

tar -c [-f ARCHIVE] [OPTIONS] [FILE...]

-c is to create. -f is used to mentioned the tar file name, you can specify the additional options like -v for the verbose, finally files name and/or folders name

Example:

image

If you have the folder to archive then instead of files array give the folder name

image

-t is used to list the contents of the tar file

image

-x is used to Extract files from an archive.

image

-z is used to to compress the file. (like gzip)

image

To extract the compressed tar using the tar command

Note: When you are working with the tar compressed file always use the -z options.

image

tar command,

  • -f to specify the archive file name
  • -c to create
  • -t to list the contents of the archive file
  • -x to extract the archive file
  • -z while working with the compressed archive
  • -v is for verbosity

gzip

gzip compress files

Gzip reduces the size of the named files using Lempel-Ziv coding (LZ77). Whenever possible, each file is replaced by one with the extension .gz

gzip filename

image

gunzip

```gunzip`` expand files

image

echo

echo display a line of text

-e enable interpretation of backslash escapes

image

du

du - estimate file space usage

image

-h human readable

image

ln

ln creating links between files

By default, the ln command creates hard links. To create a symbolic link, use the -s (--symbolic) option.

The ln command syntax for creating symbolic links is as follows:

ln -s [OPTIONS] FILE LINK

Source file (My script)

image

Linking the file to form a Global cmd.

image

Output of the ls shows the link that we created.

image

Clone this wiki locally