Using bash commands linux

The Ultimate Linux Command Line Guide — Full Bash Tutorial

The Ultimate Linux Command Line Guide - Full Bash Tutorial

Welcome to our ultimate guide to the Linux Command Line. This tutorial will show you some of the key Linux command line technologies and introduce you to the Bash scripting language.

What is Bash?

Bash (short for Bourne Again SHell) is a Unix shell, and a command language interpreter. A shell is simply a macro processor that executes commands. It’s the most widely used shell packaged by default for most Linux distributions, and a successor for the Korn shell (ksh) and the C shell (csh).

Many things that can be done Linux operating system can be done via command line. Some examples are…

  • Editing files
  • Adjusting the volume of the operating system
  • Fetching web pages from the internet
  • Automating work you do every day

You can read more about bash here, via the GNU Documentation, and via the tldp guide.

Using bash on the command line (Linux, OS X)

You can start using bash on most Linux and OS X operating systems by opening up a terminal. Let’s consider a simple hello world example. Open up your terminal, and write the following line (everything after the $ sign):

zach@marigold:~$ echo "Hello world!" Hello world!

As you can see, we used the echo command to print the string “Hello world!” to the terminal.

Writing a bash script

You can also put all of your bash commands into a .sh file, and run them from the command line. Say you had a bash script with the following contents:

It’s worth noting that first line of the script starts with #! . It is a special directive which Unix treats differently.

Why did we use #!/bin/bash at the beginning of the script file?

That is because it is a convention to let the interactive shell know what kind of interpreter to run for the program that follows. The first line tells Unix that the file is to be executed by /bin/bash. This is the standard location of the Bourne shell on just about every Unix system. Adding #!/bin/bash as the first line of your script, tells the OS to invoke the specified shell to execute the commands that follow in the script. #! is often referred to as a “hash-bang”, “she-bang” or “sha-bang”. Though it is only executed if you run your script as an executable. For example, when you type ./scriptname.extension , it will look at the top line to find out the interpreter, whereas, running the script as bash scriptname.sh , first line is ignored.

Читайте также:  Автозагрузка линукс как поставить

Then you could run the script like so: For make file executable you should call this command under sudo chmod +x “filename”.

zach@marigold:~$ ./myBashScript.sh Hello world!

The script only has two lines. The first indicates what interpreter to use to run the file (in this case, bash). The second line is the command we want to use, echo, followed by what we want to print which is “Hello World”.

Sometimes the script won’t be executed, and the above command will return an error. It is due to the permissions set on the file. To avoid that use:

zach@marigold:~$ chmod u+x myBashScript.sh

And then execute the script.

Linux Command Line: Bash Cat

Cat is one of the most frequently used commands in Unix operating systems.

Cat is used to read a file sequentially and print it to the standard output. The name is derived from its function to concatenate files.

Usage

  • -b , numer non-blank output lines
  • -n , number all output lines
  • -s , squeeze multiple adjacent blank lines
  • -v , display nonprinting characters, except for tabs and the end of line character

Example

Print in terminal the content of file.txt:

Concatenate the content of the two files and display the result in terminal:

Linux Command Line: Bash cd

Change Directory to the path specified, for example cd projects .

There are a few really helpful arguments to aid this:

  • . refers to the current directory, such as ./projects
  • .. can be used to move up one folder, use cd .. , and can be combined to move up multiple levels ../../my_folder
  • / is the root of your system to reach core folders, such as system , users , etc.
  • ~ is the home directory, usually the path /users/username . Move back to folders referenced relative to this path by including it at the start of your path, for example ~/projects .

Linux Command Line: Bash head

Head is used to print the first ten lines (by default) or any other amount specified of a file or files. Cat is used to read a file sequentially and print it to the standard output.
ie prints out the entire contents of the entire file. — that is not always necessary, perhaps you just want to check the contents of a file to see if it is the correct one, or check that it is indeed not empty. The head command allows you to view the first N lines of a file.

Читайте также:  Hp 1005 драйвер linux

if more than on file is called then the first ten lines of each file is displayed, unless specific number of lines are specified. Choosing to display the file header is optional using the option below

Usage

  • -n N , prints out the first N lines of the file(s)
  • -q , doesn’t print out the file headers
  • -v , always prints out the file headers

Example

Prints in terminal the first ten lines of file.txt (default)

Prints in terminal the first seven lines of file.txt

head -q -n 5 file1.txt file2.txt

Print in terminal the first 5 lines of file1.txt, followed by the first 5 lines of file2.txt

Linux Command Line: Bash ls

ls is a command on Unix-like operating systems to list contents of a directory, for example folder and file names.

Usage

  • -a , all files and folders, including ones that are hidden and start with a .
  • -l , List in long format
  • -G , enable colorized output.

Example:

List files in freeCodeCamp/guide/

ls ⚬ master CODE_OF_CONDUCT.md bin package.json utils CONTRIBUTING.md gatsby-browser.js plugins yarn.lock LICENSE.md gatsby-config.js src README.md gatsby-node.js static assets gatsby-ssr.js translations

Linux Command Line: Bash man

Man, the abbreviation of manual, is a bash command used to display on-line reference manuals of the given command.

Man displays the reletive man page (short for manual page) of the given command.

Usage

  • -f , print a short description of the given command
  • -a , display, in succession, all of the available intro manual pages contained within the manual

Example

Display the man page of ls:

Linux Command Line: Bash mv

Moves files and folders.

mv source target mv source . directory

The first argument is the file you want to move, and the second is the location to move it to.

  • -f to force move them and overwrite files without checking with the user.
  • -i to prompt confirmation before overwriting files.

That’s all. Go forth and use Linux.

Источник

Bash cheat sheet: Top 25 commands and creating custom commands

Being well-versed in the Bourne Again SHell (Bash Shell) as a developer or Linux user can significantly boost productivity. The Bash Shell is a Swiss Army knife of great features with deceptively simple commands, and it can provide far more flexible options for organizing and streamlining your workflow.

Читайте также:  Linux format disk fat32

The Bash Shell is the most widely used command-line interface (CLI) available. Bash is a Unix shell and command language that is the default login shell for the majority of Linux distributions. It provides users with a set of tools for managing tasks on a Linux system.

Linux is an open-source operating system that is widely used in the tech industry. One of the key advantages of Linux is its command-line interface, which provides users with direct access to the underlying system and allows them to execute commands and scripts using a shell such as Bash. Bash commands perform a wide range of tasks, such as:

  • Creating, moving, renaming, and deleting files and directories
  • Comparing and merging files
  • Editing and manipulating text within files
  • Compressing and decompressing files
  • Scheduling tasks to run automatically
  • Networking and remote access

The advantage of using Bash commands to navigate Linux lies in its flexibility and versatility. Linux is an open-source OS, and provides users with control over the underlying system. This allows the users to customize and configure it to their specific needs.

By allowing users to tailor their environment to their specific needs, Bash and Linux enable a high level of personalization and efficiency. Some examples of possible customizations and configurations include aliases (custom shortcuts for frequently used commands or groups of commands), custom shell scripts to automate repetitive tasks, and options to fine-tune your environment variables to control the behavior of various applications running on your system.

This adaptability extends to Bash commands, which can be customized using bash scripts and command-line utilities. Linux offers a steady environment for executing Bash commands, ensuring that they can be ran consistently. This is especially vital in production environments where seamless automation and repeatability are a must.

In the following sections, we’ll introduce the most popular Bash commands, what they do, and how to extend them with options. Later on in this article, you’ll learn how to create your own custom commands (aliases), allowing you to create shortcuts for a single command or a group of commands.

When it comes down to it, if you don’t know the command line, you’re not using your computer to its full potential.

Check out the rest of this bash cheat sheet to get even more familiar with the command line concepts!

Источник

Оцените статью
Adblock
detector