Bash 101

Bash commands

Once you are logged in to a Bash terminal, you can type commands to perform a number of tasks. A command is usually a single word, such as cd or ls, and it can include options that changes its functions, such as ls -l or tar -xfv.

Options often occur after dashes: ls -l runs the ls command (list all files in the current directory) with the options to output a long list of files (-l, outputs one file per line).

Most commands also include the option --help or -h. This options is useful to learn how to use a command, as well as to get more information about it.

Next, we will go over some basic commands.

Listing items in a local directory

ls: Lists a directory, including file info and size in human-readable format.

username@host:~$ ls -lh

Change directory

cd: Changes the current directory of the shell to /data/files.

username@host:~$ cd /data/files

Changes to the directory one level above.

username@host:~$ cd ..

Changes to the user's home directory.

username@host:$ cd ~

Copying a file

cp: Copy a file from to another directory.

username@host:~$ cp original_file.txt /data/files/new_file.txt

To copy a directory, include option -r:

username@host:~$ cp -r original_folder /data/files/new_folder

Moving a file

mv: Moves a file to a destination.

username@host:~$ mv original_file.txt /data/files/new_file.txt

You do not need option -r to move directories:


Deleting a file

rm: Deletes a file.

username@Host:~$ rm file.txt

To delete a directory, include option -r:

username@host:~$ rm -r my_folder

pwd outputs the absolute address of the current working directory.

pwd
/home/<user>/<my_folder>/