Linux Shell Scripting with Bash(Bash Commands)


Getting Help

The Bash shell comes with a built-in help command to describe the various built-in Bash commands.The -s switch displays a summary for the command you specify.

$ help -s printf
printf: printf format [arguments]

Help only describes Bash commands.To get help on Linux commands, you need to use the man (manual) command.

 $ man date
32 Chapter 3 Files, Users, and Shell Customization

Linux divides its manual pages into a number of logical volumes. man displays any matching entries from any volume.Volume 1 contains the commands you can execute from the shell.To restrict your search to shell commands, use man 1.

 $ man 1 date

If there are pages in more than one manual volume, only the page from the first volume that matches your search is displayed.To find matching pages across all manual volumes, use the -a (all) switch.
The -k switch searches the Linux manual for a particular keyword and lists all man pages referring to that keyword.

 $ man 1 -k alias

The command help type gives you different information than man 1 type.The help type command tells you about Bash’s built-in type command, whereas the man 1 type command tells you about the Linux type command. If you are not sure whether a command is a Bash command, always try the help command before using the man command.

Fixing the Display

There will be times when a Bash session becomes unusable. Certain character sequences can lock your display, hide what you type, or change the characters being shown into strange symbols.This can happen, for example, when you’re trying to display a binary file.

The reset command attempts to restore a Bash session to a safe, sane state. If reset fails, you might also need to use stty sane to restore the session to a normal state. The clear command clears the display and returns the cursor to the upper-left corner.
There are several Linux commands for removing, copying, and moving files. mkdir (make directory) creates a new directory. Use mkdir to organize your files.

$ mkdir prototypes
$ ls -l
total 4
drwxr-xr-x 2 ken users 4096 Jan 24 12:50 prototypes

There are two switches for mkdir:
n –mode=m (-m)—Sets the permission mode (as in chmod)
n –parents (-p)—Makes all necessary directories even if they don’t currently exist

$ mkdir --parents --mode=550 read_only/backup/january
$ ls -l
Working with Files 33
total 4
drwxr-xr-x 2 ken users 4096 Jan 24 12:50 backup
drwxr-xr-x 3 ken users 4096 Jan 24 12:51 read_only
$ ls -l read_only/backup
total 4
dr-xr-x--- 2 ken users 4096 Jan 24 12:51 january

The values for mode are discussed with the chmod command in Chapter 15,“Shell Security.” However, when –parents is used, the –mode affects only the final directory in the list.

rmdir (remove directory) deletes a directory.The directory must be empty before it can be removed.There are two switches: n –ignore-fail-on-non-empty—Doesn’t report an error when it can’t delete a directory with files still in it
n –parents (-p)—Removes all parent directories as well as the subdirectory

 $ rmdir read_only
rmdir: read_only: Directory not empty
$ rmdir --parents read_only/backup/january/

The rm (remove) command permanently deletes files. If the file is a symbolic or hard link, it removes the link but leaves the file intact.

$ rm old_notes.txt
$ ls old_notes.txt
ls: old_notes.txt: No such file or directory

There are several switches for rm:
n –directory (-d)—Removes a directory
n –force (-f)—Never prompts the user and ignores missing files
n –interactive (-i)—Always prompts the user
n –recursive (-r or -R)—Removes contents of all subdirectories

Using the –recursive and –force switches simultaneously removes all the specified files, including all sub directories, without warning. Make sure you are deleting the correct files.

Some Linux distributions have the –interactive switch on by default so that rm requires that you confirm when you want to delete a particular file.

$ rm --interactive old_notes.txt
rm: remove ‘old_notes.txt’? y

Normally rm won’t delete a directory, but the –recursive switch deletes any directories encountered.

The cp (copy) command copies files from any location to another. If the final file listed is a directory, copy copies the other files into that directory.

There are many switches for copy—the complete list is in the reference section at the
end of this chapter. Some common switches are as follows:

n –force (-f)—Never prompts the user; always overwrites
n –interactive (-i)—Always prompts user
n –link (-l)—Creates a hard link instead of copying
n –parents (-P)—Appends the source path to destination directory
n –recursive (-R)—Copies any subdirectories
n –symbolic-link (-s)—Creates a symbolic link instead of copying
n –update (-u)—Overwrites old files or copies missing files

$ cp notes.txt old_notes.txt # copying
$ mkdir backup
$ cp old_notes.txt backup
$ ls backup

Like rm, some Linux distributions have –interactive on by default, warning when a file will be overwritten.

$ cp --interactive project_notes.txt old_notes
cp: overwrite ‘old_notes/project_notes.txt’? n

The mv (move) command moves and renames files.This is the same as making a copy of the file and deleting the original. Move also effectively renames a file by moving it to a new name in the same directory.

$ mv notes.txt project_notes.txt # renaming

The most common mv switches are similar to cp:
n –backup (-b)—Makes a backup of any existing file before overwriting by adding
a ~ to the name
n –force (-f)—Never prompts the user; always overwrites
n –interactive (-i)—Always prompts the user before overwriting
n –update (-u)—Overwrites old files or copies missing files
There is no –recursive switch.When move moves a directory, it moves the directory
and all its contents automatically.

The namei (name inode) command lists all the components in a path, including any symbolic links.

$ namei files
f: files
l files -> /home/geroge/text/scripts
d /
Working with People 35
d home
d geroge
d text
d scripts

In this case, the file named files is a symbolic link. Each of the files in the link path is a directory, marked with a d. Other file designations include l for symbolic link, s for socket, b for block device, c for character device, – for regular file, and ? for an error accessing a file in the path. Complete file permissions, such as seen with ls -l, can be shown with the -m (mode)

Working with People
There are several commands for checking to see who is on the computer and what they are doing.

The finger command shows who is on the computer and provides additional information, including how long their session has been idle, and their contact information.

$ finger
Login  Name          TTY  Idle    When     Bldg.     Phone
grm   Geroge Malgler  *p6   4:19 Thu 14:06 8th Floor   ext 9703
xel  Xian Lim       *con  6:07 Fri 08:47 Technical   ext 9876
vis  Violet Smith   *p7   10   Fri 17:32

Some versions of Linux no longer include finger because of security concerns over finger’s .plan files. Read the finger manual page for more information.
There are several other commands with similar functions.The users command shows a list of login names.

$ users
grm xel vis

The who command shows who is on the computer, which connection they are using, and when they signed on.

$ who
grm ttyp6 Mar 29 14:12
xel console Apr 6 09:57
vis ttyp7 Apr 6 13:32

The w command provides even more information, including system statistics and what the users are currently running.

$ w
3:18pm up 9 days, 20:33, 3 users, load average: 0.64, 0.66, 0.64
User tty login@ idle JCPU PCPU what
grm ttyp6 2:12pm 4:28 8:01 8:01 csh
xel console 9:57am 5:10 sh
vis ttyp7 1:32pm 19 bash

One thought on “Linux Shell Scripting with Bash(Bash Commands)

  1. BTF review says:

    Hello everybody, here every person is sharing these experience, thus it’s fastidious to read this weblog, and I used to go to see this weblog every day.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s