Bashコマンド:Bash ls、Bash head、Bash mv、およびBashcatを例で説明

Bash ls

ls は、Unixライクなオペレーティングシステムで、フォルダやファイル名などのディレクトリの内容を一覧表示するコマンドです。


cat [options] [file_names]


  • -a、非表示でで始まるものを含む、すべてのファイルとフォルダ .
  • -l、すべてのファイルを長い形式で一覧表示します
  • -G、カラー出力を有効にする


のファイルを一覧表示 freeCodeCamp/guide/


api-server docker-compose.yml public Dockerfile.tests client docs sample.env search-indexing config lerna.json server curriculum node_modules tools docker-compose-shared.yml package.json utils docker-compose.tests.yml package-lock.json







head [options] [file_name(s)]


  • -n N、ファイルの最初のN行を出力します
  • -q、ファイルヘッダーを印刷しません
  • -v、常にファイルヘッダーを出力します

head file.txt


head -n 7 file.txt


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




mv source target mv source ... directory



  • -f ユーザーに確認せずに、それらを強制的に移動してファイルを上書きします。
  • -i ファイルを上書きする前に確認を求める。


cat Unixオペレーティングシステムで最も頻繁に使用されるコマンドの1つです。



cat [options] [file_names]


  • -b、空白以外の出力行の数
  • -n、すべての出力行に番号を付けます
  • -s、隣接する複数の空白行を絞ります
  • -v、タブと行末文字を除く、印刷されない文字を表示します


cat file.txt


cat file1.txt file2.txt



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 in the GUI of a Linux operating system can be done via the 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):

[email protected]:~$ 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 have a bash script with the following contents:

#!/bin/bash echo "Hello world!"

This 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, here, "Hello world!"

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 the operating system that the file should be executed by the program at /bin/bash, the standard location of the Bourne shell on almost every Unix or Unix-like system. By adding #!/bin/bash at the beginning of the script, it tells the OS to use the shell at that specific path to execute all the following commands in the script.

#! goes by many names such as "hash-bang", "she-bang", "sha-bang", or "crunch-bang". Note that this first line is only considered if the script is an executable.

For example, if is executable, the command ./ will cause the OS will look at the first line figure out which interpreter to use. In this case it would be #!/bin/bash.

On the other hand, if you run bash, then the first line is ignored since the OS already knows to use bash.

To make executable, simply run sudo chmod +x Then run the following command to execute the script:

[email protected]:~$ ./ 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:

[email protected]:~$ chmod u+x

And then execute the script.