J. R. Swab

Learn Bash: Tutorial 002

Categories: [Technology]
Tags: [learn], [open-source], [linux], [bash]

Welcome back to the bash tutorial series. If you have not read the first in the series and you have never used bash please check that out before continuing. There are basic commands and terms we cover there that may make this section easier to follow.

Also, if you are reading this and you know bash please help by filling in any gaps in thought via the comment section. The goal is to keep these about one thousand words long and there may be parts I brush over. So feel free to expand anything you think will help a new bash user.

More Command Basics

Moving Files

We covered a few commands in the last post but there are a few more that we should get under our belt. The first is mv. This command is used to move a file from one location to another. Or to change the name of a file as it stands on your system.

To move a file we type mv [file] [path_to/new_location/file]. We should make sure we are in the directory of the file to move or we will have to add the entire path to the first argument. That is all there is to moving a file. To check if the file has left our working directory type ls as we learned previously.

Renaming a file is easy. To do so, we type mv [file] [new_file_name]. Again, we need to make sure we are in the directory of the file that needs renamed. Else we will need to type the entire path to the file in both arguments. That can be a big hassle if the path is long or complicated.

Deleting Content

Let's cover how to remove files. To do this, we use the rm command. To remove a file all we need to do is type rm [file]. However, this will not work on directories without adding the proper option. To remove a directory, we need to type rm -r [directory]. This tells the computer we want the remove command to run recursively. Thus deleting all the contents of the directory and the directory itself. rm will completely remove a file without prompt. Nor will you be able to find it in a trash bin of sorts. Once it is gone, it is gone forever.

Basic Pro Tips

If we need to write out the path of a file because we are in a different directory and wish to stay there, we can use some these basics in ways that will help us be more efficient. One such tip is to view the contents of a directory while in another. This is done by running ls [path_to_directory]. We learned in the last post that ls will show us the contents of our working directory. By adding the path to another directory we can see those contents instead.

Another tip to help speed things up is to limit the amount you need to type. One way to do so is by not typing the start of the path for any directory that branches from our home folder. Instead of typing something like ls /home/jrswab/music/ we can use ~/ to replace /home/jrswab/. The resulting command will look like this, ls ~/music. We can also use the tab key to auto-fill the names of files and directories.

Bash Level Two

Dealing With Spaces

Now that we are manipulating files in the command line we must point out that the names can be tricky. On a UNIX like system such as Linux, we can add spaces to the file names. This reads nice but can be a major pain when working in the command line. There are two ways to work with files when we encounter spaces.

The first is to use quotations. We can use either single or double quotes in the operations. If we wish to rename a file that contains spaces the command will look as follows, mv "file with spaces.txt" filewithoutspaces.txt or mv 'file with spaces.txt' filewithoutspaces.txt`. This is great in most use cases but will not work in all scenarios.

The second option is to use the backslash. This is a much less intuitive approach when starting out but we need to know how to use it if the quotations do not work. Let's use the same command as above to show the difference. The command becomes mv file\ with\ spaces.txt filewithoutspaces.txt. This is known as escaping the character but we will dive more into that in a future post.

Filename Expansion

This will be our best friend the more we need to manipulate several files via the command line. What this does is allows us to tell the computer we want to run the same action on all files that match whatever is not covered by a star. Let's show an example and it will make more sense.

Say we want to delete every file in our working directory that ends with .png. We could run rm filename.png several times but that will take too long. We could also run rm filename.png anotherfilename.png but if we have more than a handful, we will type too much. Enter *!

All we need to do is append * right before .png. It looks like rm *.png and tells the computer to delete any file that ends in .png. The filename expansion has many uses to make our life easier in both the command line and in any bash scripts we write. Want to delete everything that starts with file and ends with .png? Its as easy as rm file*.png.

Brace Expansion

Brace expansion is like filename expansion but allows us to be more precise. This is helpful if we want to remove several files with similar names but not them all. To do so, we use braces { } in the remove command such as rm file{1,3,5}.txt. This will delete file1.txt, file3.txt, and file5.txt but no others.

Both filename and brace expansion are not limited to the remove command. We can use these with all of the commands we talked about over these first two posts. We can us ls with filename expansion to see all files in a directory that end in.txt or another extension. This is helpful when working with a large amount of files.

Ways to support the blog.

If you are an email kind of nerd you can sign up for mine here. You can donate to this site from my Liberapay account if you so choose. If you want a more passive way to support this site, use this link when shopping on Amazon; it kicks some of Amazon's profit to me at no extra cost to you.