marinetics blog


Bioinformatics Solutions in Ecology and Evolution

11 Feb 2015

Navigating to files and directories

Tagged: Commandline






Related Posts

Got some sequencing data? Many powerful tools to analyse them are based on the command line and this is part of a series of short but essential posts that help you getting started. I assume that you are working on a UNIX-based operating system (‘Mac’ or ‘Linux’ computer).

Navigating through your directories is a big hurdle if you are new to the command line and used to ‘click’ you way in and out of folders. To understand how to move in and out of folders and to look at the content of folders is thus an essential step to analyse your data on the command line.

Moving in and out of directories with cd

cd stands for ‘change directory’. with this command you can navigate in and out of your directories. To see what your present working directory is, simply type pwd (for ‘present working directory’) and hit enter

pwd

The response in my case is:

/home/alj/

When you want to ‘move’ to a different directory, the TAB key comes in handy as it auto-completes the possible sub-directories you can ‘move’ to. For example, when you type cd and hit the TAB key twice, you get an overview of all possible sub-directories. For example,

cd 
.adobe/
Adobe/
.cabal/
.cache/
.compiz/
.config/
--More--

Hit ENTER to see more sub-directories in your shell or ‘n’ to leave the list of sub-directories.

If you know that your target sub-directory starts with the letters ‘Do’, you can type these after the cd command and then hit TAB twice (once is enough if there is only one sub-directory that starts with the letters ‘Do’):

cd Do
Documents/ Downloads/

I, for example, have two directories starting with ‘Do’, Documents and Downloads. So, TAB completion helps when moving into sub-directories, but how to get out of them again? With

cd ..

you move one level up in your hierarchical directory structure. If you want to go to your home directory at once, use

cd ~

What is saved in the current directory?

Once you navigated with cd to your target directory and you want to look at the files and sub-directories that are located in it, you can use the command ls and hit enter. The tool ls comes with many options that refine the way that the results are shown; you get an overview of these options with:

ls --help

The combination of options that I use most frequently are

ls -lhcrta

The option

  • -l provides additional information to the file or folder name
    • file permissions
    • user and group owners of the file
    • size
    • date of modification
  • -h prints the file sizes in human readable format, like 100K instead of 102400
  • -c sort by last modification of file status information
  • -r reverse order while sorting (so that the newest files are the last that are printed)
  • -t sort by modification time, newest first
  • -a prints also the hidden files (starting with a dot ‘.’).

Have a look at the directory structure

The command line tool tree prints the hierarchical structure of your files and directories (recursing into all sub-directories) to the screen.

To discriminate files from folders via colors, use the -C option

tree -C

To show only directories, use the -d option

tree -d

As an example, to look at the directory structure and the files for the posts (that I have written for this blog so far), I can use the command

tree -sh

The option

  • -s provides the file and directory sizes
  • -h prints the sizes in a human readable format

The output of this command is

.
└── [4.0K]  02
    ├── [4.0K]  08
    │ └── [7.9K]  ssh.html
    └── [4.0K]  09
        └── [9.1K]  FileTransfer.html