Bash Introduction – What is a command shell? – Directories



Bash Introduction – What is a command shell? – Directories

0 0


BashIntroduction

A presentation on the essential bash commands.

On Github JoaoFCosta / BashIntroduction

Bash Introduction

A lightweight introduction to bash commands.

What is a command shell?

  • A program that interprets commands
  • Allows a user to execute commands by typing them manually at a terminal, or automatically in programs called shell scripts
  • A shell is not an operating system. It is a way to interface with the operating system and run commands

What is bash?

  • BASH = Bourne Again Shell
  • Written as a free replacement to the standard Bourne Shell (/bin/sh) originally written by Steve Bourne for UNIX systems
  • Since it is Free Software, it has been adopted as the default shell on most Linux systems

Directories

$ pwd

Prints the working directory.

$ ls

List files and folders in the current directory.

Flag Description -a List all files and all folders. -l Long format.

$ cd PATH

Change the working directory.

.. Parent directory. ~ Home directory.

$ mkdir [OPT] DIR

Create a new directory.

Flag Description -p Make parents.

$ rmdir [OPT] DIR

Remove a directory.

Flag Description -p Remove parents.

$ mv FROM TO

Move a folder.

$ CP [OPT] FROM TO

Copy directory to another place.

Flag Description -r Copy directories recursively.

FILES

$ touch FILE

Create an empty file.

$ cp FROM TO

Copy a file.

$ mv FROM TO

Move a file.

$ rm FILE

Remove a file.

$ cat FILE

Concatenate and print a file.

Flag Description -n Number the output lines, starting at 1.

$ less [OPT] FILE

View, navigate and search a file.

Flag Description --help Summary of less commands.

$ head [OPT] FILE

Display first lines of a file.

Flag Description -n N Display first N lines.

$ tail [OPT] FILE

Display the last part of a file.

Flag Description -n N Display last N lines.

$ grep [OPT] PATTERN FILE

Search for a pattern in a file.

Flag Description -c Display the number of matched lines. -i Ignore case sensitive. -l Display the file names. -n Display the line numbers. -w Match whole word.

$ wc [OPT] FILE

Count the number of words, lines, characters and bytes of a file.

Flag Description -l Line count. -c Byte count. -m Character count. -w Word count.

Piping & Redirection

|

"Pipe", redirect the output of one command into another command.

Example: $ ls | more

>

Redirect the output of a command into a new file. If the file already exists, overwrite it.

Example: $ ls > myfiles.txt

>>

Append the output of a command into a file. A new file is created if the file doesn't exist.

Example: $ echo "Mary 555-1234" >> phonenumbers.txt

<

Redirect a file as input to a program.

Example: $ more < phonenumbers.txt

PERMISSIONS

Permissions Table

u g o user group others r w x r w x r w x

u - read and write permissions; g - read permissions; o - read permissions

1 1 0 1 0 0 1 0 0 6 4 4

$ sudo [OPT] [USER] COMMAND

Run commands with the security privileges of another user. Normally the root.

$ chmod [OPT] MODE FILE

Change file modes or Access Control Lists.

Flag Description -r Recursively.

Example: Add read and write permissions to user$ sudo chmod u+rw index.html or $ sudo chmod 644 index.html

Special Characters

\

Escape character. If you want to reference a special character, you must "escape" it first.

Example: $ touch /tmp/filename\*

/

Directory separator, used to separate a string of directory names.

Example: $ cd /usr/src/linux

.

Current directory. Can also "hide" files when it is the first character in a filename.

Example: $ cat ./file.txt

..

Parent directory of the current working directory.

Example: $ ls ..

~

User's home directory.

Example: $ ls ~/Documents

*

Represents 0 or more characters in a filename, or by itself, all files in a directory.

Example: pic*2015.jpg can represent 'picJan2015.jpg', 'picFeb2015.jpg', etc.

?

Represents a single character in a filename.

Example: hello?.txt can represent 'hello1.txt', 'helloz.txt' but not 'helloabc.txt'.

[ ]

Can be used to represent a range of values, e.g. [0-9], [A-Z], etc.

Example: hello[0-2].txt represents the files 'hello0.txt', 'hello1.txt' and 'hello2.txt'.

;

Command separator. Allows you to execute multiple commands on a single line.

Example: $ cd /var/log ; less messages

&&

Command separator as ';', but only runs the second command if the first one finished without errors.

Example: $ cd /var/logs &&

&

Execute a command in the background, and immediately get your shell back.

Example: $ which ruby > /tmp/locations.txt &

PROCESSES

CTRL SIGNALS

MAN PAGES

Bash Introduction A lightweight introduction to bash commands.