Bash commands

From Juneday education
(Redirected from Shell tool reference card)
Jump to: navigation, search

Contents

Exercises

Shell Tool exercises

Shell Tool exercises (extra) - some challenging extra exercises. Mostly for the fun of it.

bc

Description

bc - A command line calculator found most Unix systems. The calculator can be used either interactively or non interactively..

On this page we only give you a short introduction with a few examples. We encourage you to read the manual (man bc).


"..bc is typically used as either a mathematical scripting language or as an interactive mathematical shell." bc

Example output

interactively

$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
3 + 3
6
quit


non interactively

$ echo "3+3" | bc
6 

cd

Description

Command (not a program), built in to the shell, that changes the current directory.

The cd command, also known as chdir (change directory), is a command-line OS shell command used to change the current working directory in operating systems such as Unix, DOS, OS/2, AmigaOS (where if a bare path is given, cd is implied), Windows, and Linux.

- Wikipedia on cd

Example usage

Assuming you have a directory called tmp in your present working directory you can enter that directory (tmp) like this

$ cd tmp

If you want to enter the directory /usr/bin you enter:

$ cd /usr/bin

If you want to go to your HOME directory you can do any of the following:

$ cd

or

$ cd ${HOME}

or

$ cd ~

cat

Description

cat - concatenate files and print on the standard output.

On this page we only give you a short introduction with a few examples. We encourage you to read the manual (man cat).


"cat is a standard Unix utility that reads files sequentially, writing them to standard output. The name is derived from its function to concatenate files. " cat

Example output

Output a file's content

$ cat file.txt
Hi there folks

This is a simple text file

.... and here it ends

Read from stdin

$ cat
By typing this text and pressing enter cat will read this line
By typing this text and pressing enter cat will read this line
and output it....
and output it....
Pressing control-d makes bash close the stream (stdin)
Pressing control-d makes bash close the stream (stdin)

Read from stdin and redirect to file

$ cat > file.txt
Hi there

This is just some text 
 
/John Doe

.. don't forget to press control-d

cp


cp - copy files and directories..

On this page we only give you a short introduction with a few examples. We encourage you to read the manual (man cp).

Description

cp is a UNIX command for copying files and directories. The command has three principal modes of operation, expressed by the types of arguments presented to the program for copying a file to another file, one or more files to a directory, or for copying entire directories to another directory.
Read about cp on wikipedia

Example usage

To create a copy of the file MyMain.java and call the copy MyMain.backup

$ cp MyMain.java MyMain.backup

To copy the file MyMain.java (located in the current directory) to the directory backup

$ cp MyMain.java backup

To copy all the Java files (located in the current directory) to the directory backup

$ cp *.java backup

curl

Description

transfer a URL

cURL is a computer software project providing a library and command-line tool for transferring data using various protocols. The cURL project produces two products, libcurl and cURL. It was first released in 1997. The name originally stood for "see URL".
- Wikipedia on curl

Example usage

$ curl -LJO -k https://github.com/progund/tig015-weekly/raw/master/beverages-framework/resources/sortiment.xml


For more examples, check out Download files

date


date - print or set the system date and time.

On this page we only give you a short introduction with a few examples. We encourage you to read the manual (man date).


Description

"In computer science and computer programming, system time represents a computer system's notion of the passing of time. In this sense, time also includes the passing of days on the calendar." System time

"In Unix-like operating systems, date is the command which will print or set the current time." Date command

Example output

 Mon May 23 06:56:45 UTC 2016
 Mon May 23 09:02:10 CEST 2016

Example usage

bash on GNU/Linux and cygwin

Convert seconds since the epoch (1970-01-01 UTC) to a date

             $ date --date='@2147483647'

Show the time on the west coast of the US (use tzselect(1) to find TZ)

             $ TZ='America/Los_Angeles' date

Show the local time for 9AM next Friday on the west coast of the US

             $ date --date='TZ="America/Los_Angeles" 09:00 next Fri'

bash on MacOS

The command:

          date "+DATE: %Y-%m-%d%nTIME: %H:%M:%S"

will display:

          DATE: 1987-11-21
          TIME: 13:36:16

In the Europe/London timezone, the command:

          date -v1m -v+1y

will display:

          Sun Jan  4 04:15:24 GMT 1998

where it is currently Mon Aug 4 04:15:24 BST 1997.

The command:

          date -v1d -v3m -v0y -v-1d

will display the last day of February in the year 2000:

          Tue Feb 29 03:18:00 GMT 2000

So will do the command:

          date -v30d -v3m -v0y -v-1m

because there is no such date as the 30th of February.

head

Description

head - output the first part of files.

On this page we only give you a short introduction with a few examples. We encourage you to read the manual (man head).


"head is a program on Unix and Unix-like systems used to display the beginning of a text file or piped data." head

Example output

Output part of a file's content

$ head -1 one-file.txt
Hi there

Compare this to a printout of the entire content of the file:

$ cat one-file.txt
Hi there

This is just some text 
 
/John Doe

ls


ls - list directory contents.

On this page we only give you a short introduction with a few examples. We encourage you to read the manual (man ls).

Description

In computing, ls is a command to list files in Unix and Unix-like operating systems. ls is specified by POSIX and the Single UNIX Specification. When invoked without any arguments, ls lists the files in the current working directory.

ls

Example usage

$ ls
shell-tool-template  tmp
$ ls -1
shell-tool-template
tmp
$ ls -l
total 8
-rw-rw-r-- 1 rikard rikard  157 maj 23 12:12 shell-tool-template
drwxrwxr-x 2 rikard rikard 4096 maj 23 12:13 tmp
$ ls -F
shell-tool-template  tmp/

mkdir


mkdir - make directories.

On this page we only give you a short introduction with a few examples. We encourage you to read the manual (man mkdir).

Description

The mkdir (make directory) command in the Unix, DOS, OS/2, and Microsoft Windows operating systems and in the PHP scripting language is used to make a new directory. In DOS, OS/2 and Windows, the command is often abbreviated to md.
Read about mkdir on wikipedia

Example usage

$ mkdir programming
$ ls
programming
$ ls -l programming/
total 0

mv


mv - move (rename) files.

On this page we only give you a short introduction with a few examples. We encourage you to read the manual (man mv).

Description

mv (short for move) is a Unix command that moves one or more files or directories from one place to another.
Read about mv on wikipedia

Example usage

To change the name ("moving the file's name") of a file from oldfilename to newfilename

$ mv oldfilename newfilename

To move the file my-file.txt to a directory some-dir

$ mv my-file.txt some-dir

netcat

Netcat

pwd


pwd - print name of current/working directory.

On this page we only give you a short introduction with a few examples. We encourage you to read the manual (man pwd).

Description

"In Unix-like and some other operating systems, the pwd command (print working directory) writes the full pathname of the current working directory to the standard output." pwd

Example output

/home/dennis
/media/usbdisk/backup/schnittke

redirecting

Description

bash - redirect input/output.

On this page we only give you a short introduction with a few examples. We encourage you to read the manual (man bash).

"In computing, redirection is a form of interprocess communication, and is a function common to most command-line interpreters, including the various Unix shells that can redirect standard streams to user-specified locations" Redirection_(computing)

>

The > takes the output from a command and redirects it to a file. If the file already exists, it's content is overwritten. If the file does not exist, it is created.

Example use: Redirect output of command to a file

Let's say you want to store the current date in a file. To print the date you typically use the command date. Simply printing the date is easily done:

$ date
Fri Sep 22 08:00:33 CEST 2017

And to redirect this command's output to a file, date.txt is done using > date.txt you simply type:

$ ls date.txt
ls: cannot access 'date.txt': No such file or directory
$ date > date.txt
$ cat date.txt 
Fri Sep 22 08:09:32 CEST 2017

Note: the first command is there to show you that the file does not exist, then the redirect and finally we output the content of the file.

>>

Functions much like the previous > but instead of overwriting a file, if it already exists, it appends the output.

Example use: Append output

Let's say you want to store the current date in a file but keep the previous content in the file. To print the date you typically use the command date. Simply printing the date is easily done:

$ date
Fri Sep 22 08:00:33 CEST 2017

And to redirect and append this command's output to a file, date.txt you use >> date.txt like this:

$ ls date.txt 
date.txt
$ wc -l date.txt 
1 date.txt
$ date >> date.txt
$ wc -l date.txt 
2 date.txt

Note: the first command is there to show you that the file already exists, the second command outputs the number of lines in the file, then the redirect/append and finally we check the number of lines in the file which is now 1 line more.

<

The normal setup for a program is to read input from stdin and stdin being "connected" to the keyboard. You can switch stdin to be a file instead, think of it like your program, unknowingly, reading user input from a user typing the content of a file.

Example use: Reading input from file

Assume we want to calculate all the expressions in a file, calculations.txt, with the following content:

$ cat calculations.txt 
1+2
234+456

We could ask a person to start bc and type in the lines one by one, but imagine the time it takes if the file is 10000 lines long. If you still think 10000 lines long file is ok to manually type the content of we ask you to think of a file with 1 million lines in it. If we have such a big file it is easier to fool the program into thinking that a user types line by line. This tricking is done using < like this:

$ bc < calculations.txt 
3
690

Note: the program is not fooled at all. The program can actually check what it reads input from. But for most programs simply reading from stdin this trick works fine.

Example use: Read file and redirect output

We can combine the > and <. Imagine you want to calculate the experessions stored in the file calculations.txt and redirect the output to a file results.txt then you simply type:

$ bc < calculations.txt > results.txt
$ cat results.txt 
3
690

|

The normal setup for a program is to read input from stdin and stdin being "connected" to the keyboard. You can switch stdin to be a stream instead, think of it like your program use input from a user typing the content coming from a file.

Example use: Read stream and redirect output

Let's assume we want to count the java source code files recursively (in all folders and their subfolders etc below). To find the files we use the find command. This command outputs each file on a single line so we can use the command wc to count the lines. We could potentially do the counting like this:

$ find . -name "*.java" > files.txt
$ wc -l files.txt 
14 files.txt

But using a temporary file like this is cumbersome and pointless, however it is easy ti understand given what we've just learned. But let's use a pipe instead:

$ find . -name "*.java" | wc -l
14


Example use: Read stream and redirect output

This means that we can now, for an unknown reason, do the calculations in the < example above like this instead:

$ cat calculations.txt | bc > results.txt
$ cat results.txt 
3
690


Further reading

Read more about redirection: Bash:Bash-Redirection

Read more about standard streams: Bash:Bash-Standard_streams

rev


rev - reverse lines characterwise.

On this page we only give you a short introduction with a few examples. We encourage you to read the manual (man rev).

Description

rev - reverse lines characterwise

Example output

Output part of a file's content

$ rev one-file.txt
ereht iH

 txet emos tsuj si sihT
 
eoD nhoJ/

Compare this to a printout of the entire content of the file:

$ cat one-file.txt
Hi there

This is just some text 
 
/John Doe

rm


rmdir - remove empty directories.

On this page we only give you a short introduction with a few examples. We encourage you to read the manual (man rmdir).

Description

rmdir (or rd) is a command which will remove an empty directory on a Unix (e.g. OS X), Unix-like (e.g. FreeBSD, Linux), DOS, OS/2 or Microsoft Windows operating system.
Read about rmdir on wikipedia

Example usage

$ rmdir programming

rmdir


rmdir - remove empty directories.

On this page we only give you a short introduction with a few examples. We encourage you to read the manual (man rmdir).

Description

rmdir (or rd) is a command which will remove an empty directory on a Unix (e.g. OS X), Unix-like (e.g. FreeBSD, Linux), DOS, OS/2 or Microsoft Windows operating system.
Read about rmdir on wikipedia

Example usage

$ rmdir programming

seq


Description

seq - print a sequence of numbers.

On this page we only give you a short introduction with a few examples. We encourage you to read the manual (man seq).

Example output

Print the first 5 numbers

$ seq 1 5
1
2
3
4
5

Loop five times

Let's use seq to execute date five times and sleep 1 second in between

$ for nr in $(seq 1 5); do date; sleep 1 ; done
Tue Jan 24 10:48:47 CET 2017
Tue Jan 24 10:48:48 CET 2017
Tue Jan 24 10:48:49 CET 2017
Tue Jan 24 10:48:50 CET 2017
Tue Jan 24 10:48:51 CET 2017

Calculate the product of the 5 first numbers

$ PROD=1; for nr in $(seq 1 5); do PROD=$(( $PROD * $nr )); done; echo "Product: $PROD"

sort

Description

sort - sort lines of text files.

On this page we only give you a short introduction with a few examples. We encourage you to read the manual (man sort).

Example output

Sort file list

Let's create some files to sort first.

$ touch file-{a..c}{0..3}
$ ls
file-a0  file-a2  file-b0  file-b2  file-c0  file-c2
file-a1  file-a3  file-b1  file-b3  file-c1  file-c3

And now list them in alphabetical order using sort:

$ ls | sort
file-a0
file-a1
file-a2
file-a3
file-b0
file-b1
file-b2
file-b3
file-c0
file-c1
file-c2
file-c3

tac

Description

tac - concatenate and print files in reverse.

On this page we only give you a short introduction with a few examples. We encourage you to read the manual (man tac).


Example output

Output a file's content

$ tac file.txt
.... and here it ends

This is a simple text file

Hi there folks

Read from stdin

$ tac
Hi this is the first line
and this is the second
.. and I press control-d to close the file
.. and I press control-d to close the file
and this is the second
Hi this is the first line

Read from stdin and redirect to file

$ tac > file.txt 
Hi there

This is just some text 
 
/John Doe

.. don't forget to press control-d. If we output the file file.txt using cat we get:

$ cat file.txt 
/John Doe
 
This is just some text 

Hi there

.... and, of course, as a fun exercise we get the following if we use tac to output the content of the file.

$ tac file.txt 
Hi there

This is just some text 
 
/John Doe

tail

Description

tail - output the last part of files.

On this page we only give you a short introduction with a few examples. We encourage you to read the manual (man tail).


"tail is a program on Unix and Unix-like systems used to display the tail end of a text file or piped data. " tail

Example output

Output part of a file's content

$ tail -1 one-file.txt
/John Doe

Compare this to a printout of the entire content of the file:

$ cat one-file.txt
Hi there

This is just some text 
 
/John Doe

telnet

Description

Telnet is a protocol used on the Internet or local area networks to provide a bidirectional interactive text-oriented communication facility using a virtual terminal connection. User data is interspersed in-band with Telnet control information in an 8-bit byte oriented data connection over the Transmission Control Protocol (TCP). - Telnet(wikipedia)

wget

Description

A non-interactive network downloader.

GNU Wget (or just Wget, formerly Geturl, also written as its package name, wget) is a computer program that retrieves content from web servers. It is part of the GNU Project. Its name derives from World Wide Web and get. It supports downloading via HTTP, HTTPS, and FTP.
- Wikipedia on wget

Example usage

$ wget --no-check-certificate --content-disposition https://github.com/progund/tig015-weekly/raw/master/beverages-framework/resources/sortiment.xml


For more examples, check out Download files

which

Description

which - shows the full path of (shell) commands..

On this page we only give you a short introduction with a few examples. We encourage you to read the manual (man which).


"which is a Unix command used to identify the location of executables." which

Example output

Output an executable's location

$ which mkdir
/usr/bin/mkdir
$ which pwd
/usr/bin/pwd