Working with files in Bash

I remember that the first thing I got stuck with when I started to code was how to use the command line. I was just so used to GUIs that seeing the prompt on the terminal was shocking. The aim of this blog post is explain some basics of the command line on Bash for working with files (directories and regular files). It’s not an extensive list of commands but some of the commands I find more useful.

The motivation for writing this post came as an attempt to contribute to the John Hopkins Data Science Specialization. Recently, while I was doing the Data Scientist’s Toolbox course, they opened a GitHub repository for gathering resources people find useful. I’ve learnt a lot by the posts, and courses that people wrote and shared on internet. I hope this could be one of my first contributions to pay back something to the community.

Enough introduction, let’s start.

Working with the Linux file system

The first thing you should know to be confortable while using a terminal is understanding how linux machines are structured. We could summarize telling that linux has a tree directory structure. There’s a root directory and inside it, there are directory
s that can contain other directories as well and so on. To understand this better let’s learn our first bash command.

pwd: For discovering the current directory

Open a terminal and type:

$ pwd

Note: the dollar sign ‘$’ just indicates that I’m in the terminal. You don’t have to type it.

pwd” stands for Print Working Directory. In plain English this means that it tells you what directory you are in at the moment. After executing it, you will get something like this.

pwd

Accordingly to that, I’m in the directory “Rumil/” which is inside the directory “Users/” which is inside the main directory “/”. The tree structure is like the following (the directory
s of my path are in bold).

  • /
    • Applications
    • etc
    • Systems
    • Users
      • Rumil
      • Guest
      • Shared
    • var
    • dev

The directory shown when you first open a terminal is called your home directory. As I’m working on a Mac, mine is /Users/Your_Username. However, if you’re using other unix variant it could be different.

ls: Show the directory content

We know already where we are, but we don’t know which files and directories we have in our current directory. This will be retrieved by the next command.

$ ls

By typing ‘ls‘ on our terminal we’ll get a list of all the visible files and directories.

ls command

My home directory

A better way to use this command is with the arguments “l” and “a”:

$ ls -la

Captura de pantalla 2015-06-20 a la(s) 16.40.19

The argument -l gives us extra information from each file such as file mode, owner name, group name and so on. The -a argument shows the files that starts with a dot “.” which otherwise will be hidden. (For more information check the ls manual)

cd: Changing your current directory

cd‘ command that stands for Change Directory lets us to move through the directories tree (that means changing our working directory). This command works typing:

$ cd <path>

Where we have to change <path> with the directory we want to change to. For example, knowing that I’m at my home directory if I want to move to my Downloads directory (which is in my home directory) I’d type:

$ cd Downloads

Quite easy, isn’t it? So let’s add some difficulty talking about the two kinds of path.

  • An absolute path is the full extension of a directory from the main directory ‘/‘. Do you remember the path we got by typing ‘pwd‘? That was the absolute path of that directory
    . The good thing about the absolute paths is that they let you to refer to a directory wherever directory
    you were. So, by typing cd /Users/Rumil, You always will move to that directory.
  • However, when I introduced ‘cd‘ I just typed ‘Downloads’ whereas I should have typed /Users/Rumil/Downloads shouldn’t I? This is known as relative path. Since I’m already in /Users/Rumil/ I don’t have to type that again, I can type just the directory
    inside my current working directory and the computer will complete the path by itself.

So if I’m at /Users/Rumil I can go to Downloads typing either:

  • $ cd /Users/Rumil/Downloads (Absolute path)
  • $ cd Downloads (Relative path)

However if we were, for instance, at /Users/Rumil/Documents and we wanted to move to /Users/Rumil/Downloads we couldn’t just type cd Downloads since Downloads isn’t at our working directory. We should use an absolute path or refer to the parent directory /Users/Rumil. Thus, instead of typing the absolute path We can type

        • $ cd /Users/Rumil/Downloads (Absolute path)
        • $ cd ../Downloads (Relative path)

‘..’ is a reference to the parent directory of our working directory (/Users/Rumil in the example).And we could refer back as many as we wanted .. (the parent directory), ../.. (the parent directory of the parent directory) and so on.

Note: The reference (..) works for any command, so $ ls .. in our terminal will display the content of the parent directory of our working directory

Some extra hints:

  1. $ cd - gets you back to your previous working directory
  2. The “~” symbol is a direct reference to your home directory. So you can type $ cd ~ and go directly instead of typing any path
  3. Typing just cd gets you to your home directory as well

mkdir: Creating directories

After knowing how to move through directories we could be interested in creating our owns. The command is mkdir and works as follows

mkdir

mkdir‘ which stands for MaKe DIRrectory works with the path types we’ve seen with cd.

        • $ mkdir my_directory (Creates a directory on our current working directory)
        • $ mkdir ../my_directory (Creates a directory on the parent directory
          of our working directory)
        • $ mkdir /Users/Rumil/Documents/my_directory (Creates a directory inside the Documents directory)

Files management commands

A file is either a directory (directory) or any file whatever its extension is (.txt, .doc …). The following commands will work on both.

cp : Copying files

If you want to copy a file into another location type:

$ cp origin_path destination_path

This command will duplicate your origin_file into the destination directory. If the destination directory doesn’t exists it will create it.

For exemple, if I have a directory in my /Users/Rumil/Downloads called test and I want to copy it in /Users/Rumil/Images I’d type:

$ cp /Users/Rumil/Downloads/test /Users/Rumil/Images

mv : Moving files

Works the same as ‘cp’ but deletes the original file once the new copy is made.

$ mv origin_path destination_path

An additional feature is that it works for renaming a file. For instance if in my working directory I have a file called example.txt I want to rename to example2.txt:

$ mv example.txt example2.txt

rm: Removing files

The command that will let us to remove a directory and all its content is

rm -rf

We haven’t seen the use of arguments before but -rf are 2 arguments for the rm command which allows to rm to delete the file (and it’s content if it’s a directory) automatically.

-r tells to rm that we want to delete the given directory and all of its contents.

-f tells to rm that delete the content without prompting for confirmation.

Note: Noticed that as I’ve used 2 arguments I’ve typed ‘‘ just once and the 2 arguments together

However be careful by using this command because it can’t be undone.

A final tip: Commands documentation

I’ve explained just the basic use of some commands, but there are a lot of arguments and options for each one. Explaining each option would be tedious. However, there’s a way to check what a command does and which are its options by typing

$ man ls

(changing ls for whatever command you want) you will acces to its manual.

Captura de pantalla 2015-03-28 a la(s) 23.18.15

When you are in the manual, press the arrow keys to read it, and ‘q’ to exit.

If you have any suggestion

There are a lot more of commands but I think I’ve covered the most relevant here. However, if you read the post and find some command is missing and should be here, tell me in the comments and I’ll gladly update the post.

Thank you for reading.

Aknowledgments

I want to thank Andrew Esler, from Code Newbie, who took the time to review this post.

And thank you to Gregor Samsa and Brieana Polk-Perez for leaving additional tips for the ls and cd commands which I’ve already included in the post.

  • Stephany

    Hi!, this is my first read text and I’m little concerned about if every in Github and others programs in the specialization, are in linux language?

    • Hi Stephany. No it’s not. You can do the whole specialization from Windows, if this is your question. However, the Git Terminal that you install on your Windows works as if it was a Unix Terminal, so this commands will work for you too.

    • Alla

      In some cases in Windows you can use command “dir” instead of “ls”. The rest of the commands are the same.

  • Gregor Samsa

    A few useful options:
    Use “ls -la” to get a better file listing.
    Use “cd -” to get back to the place you were before.
    Use “~” to reference your own “home” directory. For instance, “cd ~/Desktop”.

    • Thanks for telling me. I’ll edit the post this weekend to add your suggestions

      • Maybe we can add that “cd” alone will lead you back to the “home” directory as well. It can help when you’re trying to get to something quickly.

    • It took me a month but finally I’ve added this in the post. Thank you for your comment!

  • Eric Winiecke

    I have a question and do not know if this is the proper place for it, but here goes. I have a new high res computer. Git Bash appears very very tiny on my screen. When I try to enlarge the window, the font remains tiny and the window only enlarges in length not width. Is there a fix for this somewhere??????

    • The size of your font has to be changed on your terminal settings. Are you using windows?

  • Abhishek Kumar Srivastava

    need analytic tools

  • Pineiden

    Hello
    I wrote this year a handbook (in spanish) about bash an some tools like sed, awk, etc. for work with some data from sensors

    • Great job, it looks very complete and useful. Definitely I’ll check it out. Thanks for sharing.

  • Vishnu krishna prateek

    Thanks a lot for such an organised explanation.

    • Thanks to you for leaving a comment =). I’m glad that you found it useful