Let's Learn Bash! Tutorial 002

in #technology7 years ago (edited)

Let's Learn Bash! 002.png

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.

Addition:
Looking to copy a file? Check out the comment by @alebeta about the cp command.

learn-bash-and-linux-terminal.png

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.

Addition:
Thanks to @not-a-bird for adding that 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. Read the comment here.

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.

learn-bash-scripting.png

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.

Thanks for reading!

If you have any questions please ask and I will do my best to get you the answer. If you have input that may make something in this post more clear please share!

All images came from royalty and attribution free sources unless specified
Sort:  

Since this is geared at people just learning bash, it's worth noting that when you rm a file, it's gone. No undos, no rewinds, it's gone. So, if you want to be a little more cautious with the rm you can use interactive mode.

For example, rm -i file*.txt will prompt you before actually removing any of the matched files.

Great addition! I'll pop a link to this in the content <3

Hello such an great article,

It would be great if you can add the cp command to copy files/directories, since we are talking about mv(move) it would be a nice addition.

From the cp command I find really useful copying files keeping ownership and permissions intact:

cp -rp /home/my_home /media/backup/my_home

in this case we have the parameter -r for recursive copy(it make possible to copy a directory and -p to preserve the same permission and ownership of the file/directory.

Also we can run the simple version:

cp /directory1/file.py /directory2/

Great idea! Let me add an edit now.

Bro i started with a series of post for Ethical Hacking and Security, if you can have a look and give me some feed back I will highly appreciate it.

All the best

thats a great technology post..thanks for sharing..best of luck..

Thanks! I find it is helping me learn what I don't understand as I go through the basics. So it's a win-win!

thanks i will wait for your next post

many many thanks for the tutorial

Awesome job including braces! Too many times i see guides that just cover basic globing (*,?).

Also to note braces can do ranges:
{0..5}
0,1,2,3,4,5

And ranges with intervals:
{0..10..2}
0,2,4,6,8,10

Both of the previous also work with letter ranges to.
{A..E}
A,B,C,D,E

{A..J..3}
A,D,G,J

Coin Marketplace

STEEM 0.17
TRX 0.15
JST 0.028
BTC 62264.03
ETH 2431.11
USDT 1.00
SBD 2.50