Bash:Bash - Files and Folders

From Juneday education
(Redirected from Bash - Files and Folders)
Jump to: navigation, search

Work in progress

This chapter is a work in progress. Remove this section when the chapter is production-ready.

Introduction

This is a short warm-up chapter, introducing the concept of files and directories in bash. It will show you the basics of the file system, and how to create directories and files, as well as how to navigate the file system.

Videos

Here are the lecture slides and the videos for this chapter

Exercises

Listing files

  • List the files in your current directory

Expand using link to the right to see a hint.

$ ls
  • List the files in the current directory in long format. Which files are directories?

Expand using link to the right to see a hint.

$ ls -l

The directories are the files whose permissions start with a d, for instance a permission list like this: drwxr-xr-x.

  • List the files in the directory one level up from the current directory

Expand using link to the right to see a hint.

$ ls ..

The symbols .. represents "the parent directory".

  • List the files in the /etc/ directory

Expand using link to the right to see a hint.

$ ls /etc

The leading character / represents the root directory at the top of the directory tree in your computer.

Making a directories

  • Make a directory in your home directory called bash-exercises

Expand using link to the right to see a hint.

$ cd
$ mkdir bash-exercises
  • Make a whole tree bash-exercises/files-and-folders/text-files

Expand using link to the right to see a hint.

$ cd
$ mkdir -p bash-exercises/files-and-folders/text-files

The -p is a "flag" or "option" which allows for creating a tree at once.

If you like typing, you can do it in steps:

$ cd
$ mkdir bash-exercises/files-and-folders
$ mkdir bash-exercises/files-and-folders/text-files

Moving around

  • Enter the directory bash-exercises/files-and-folders/text-files

Expand using link to the right to see a hint.

$ cd bash-exercises/files-and-folders/text-files
  • List the contents of that directory

Expand using link to the right to see a hint.

$ ls
  • List the contents of the current directory relative to your home (~)

Expand using link to the right to see a hint.

$ ls ~/bash-exercises/files-and-folders/text-files
  • Move to the directory above current

Expand using link to the right to see a hint.

$ cd ..

Creating files

  • Create an empty file (using touch) called empty in text-files from current directory (you are still in ~/bash-exercise/files-and-folders )

Expand using link to the right to see a hint.

$ touch text-files/empty
  • Open the file with an editor, e.g. Atom

Expand using link to the right to see a hint.

$ atom.cmd text-files/empty

In Cygwin, you'll use atom.cmd when opening files from bash. On Mac OS you'll need to create an alias called atom. On GNU/Linux you simply type atom followed by the path to the file.

  • Enter your name in the file on one line and save the file
  • Change the name of the file to name.txt using mv

Expand using link to the right to see a hint.

$ mv text-files/empty text-files/name.txt
  • Type the contents of the file to the standard out in your terminal using cat

Expand using link to the right to see a hint.

$ cat text-files/name.txt

Now, use only the terminal and cat with redirection to create simple text file. You don't need to know what redirection is yet, just experiment by following our instructions.

Type exactly this to the terminal:

$ cat > text-files/email.txt

Note that you now are in interactive mode with the cat command. It is waiting for input. Type your email address, and enter. Type Ctrl-D. You are now back in bash. This is what your session would look like (but with your email instead):

$ cat > text-files/email.txt
donald@ducktown.net
$

The > means that any output from the cat command will go to a file instead of the terminal (as it usually does). Now confirm that you have a new file and that it contains your email address. Use cat again for that, but without the > character (the so called redirection).

Expand using link to the right to see a hint.

$ cat text-files/email.txt
donald@ducktown.net
$

Another way to create a text file without opening an editor is to use the echo command, followed by the > for redirection and a file name again. Type in exactly this command (but use your own phone number instead):

$ echo '001-555-123-456' > text-files/phone.txt
$

Confirm using cat that you have a new text file and that it contains your phone number.

Expand using link to the right to see a hint.

$ cat text-files/phone.txt
001-555-123-456
$

Finally, use the tree command to see the complete directory structure. You may need to install tree at this point. Use a search engine or consult with a class mate or a teacher for how to install tree on your platform.

After installing (or if you already had installed it), simply type tree in your terminal and view the directory tree.

If you felt that the redirection with the > thing was esoteric and weird, don't worry. We have a whole chapter dedicated to redirection and one chapter for the standard streams. Why don't you check them out, when you have the time?

Links

« PreviousBook TOCNext »