How to Read the First File Under the Current Directory C++

Navigating Files and Directories

Overview

Teaching: xxx min
Exercises: 20 min

Questions

  • How can I perform operations on files exterior of my working directory?

  • What are some navigational shortcuts I can use to make my piece of work more efficient?

Objectives

  • Use a single command to navigate multiple steps in your directory structure, including moving backwards (one level up).

  • Perform operations on files in directories outside your working directory.

  • Work with hidden directories and hidden files.

  • Interconvert between absolute and relative paths.

  • Employ navigational shortcuts to move around your file system.

Moving around the file system

We've learned how to use pwd to notice our current location within our file system. Nosotros've also learned how to use cd to modify locations and ls to list the contents of a directory. Now nosotros're going to learn some additional commands for moving around inside our file system.

Use the commands we've learned and then far to navigate to the shell_data/untrimmed_fastq directory, if you're non already at that place.

            $ cd $ cd shell_data $ cd untrimmed_fastq                      

What if we want to move back up and out of this directory and to our peak level directory? Tin can nosotros type cd shell_data? Endeavor it and run into what happens.

            -bash: cd: shell_data: No such file or directory                      

Your computer looked for a directory or file called shell_data within the directory you were already in. It didn't know you wanted to look at a directory level higher up the 1 y'all were located in.

We have a special command to tell the computer to move us dorsum or up ane directory level.

At present we can utilise pwd to brand sure that we are in the directory nosotros intended to navigate to, and ls to check that the contents of the directory are correct.

            sra_metadata  untrimmed_fastq                      

From this output, nosotros can see that .. did indeed take us dorsum i level in our file organization.

You can concatenation these together similar so:

prints the contents of /home.

Commencement navigate to the shell_data directory. There is a hidden directory within this directory. Explore the options for ls to find out how to see hidden directories. List the contents of the directory and place the name of the text file in that directory.

Hint: subconscious files and folders in Unix kickoff with ., for example .my_hidden_directory

Solution

First use the man command to look at the options for ls.

The -a selection is short for all and says that information technology causes ls to "not ignore entries starting with ." This is the option nosotros desire.

                .  ..  .hidden	sra_metadata  untrimmed_fastq                              

The name of the hidden directory is .hidden. We can navigate to that directory using cd.

And so listing the contents of the directory using ls.

The proper name of the text file is youfoundit.txt.

In most commands the flags tin can be combined together in no detail lodge to obtain the desired results/output.

Examining the contents of other directories

Past default, the ls commands lists the contents of the working directory (i.east. the directory yous are in). You can always observe the directory you are in using the pwd control. However, you can besides give ls the names of other directories to view. Navigate to your domicile directory if you are not already in that location.

And so enter the command:

            sra_metadata  untrimmed_fastq                      

This volition list the contents of the shell_data directory without you needing to navigate at that place.

The cd control works in a like way.

Try entering:

            $ cd $ cd shell_data/untrimmed_fastq                      

This volition accept yous to the untrimmed_fastq directory without having to become through the intermediate directory.

Navigate to your home directory. From in that location, listing the contents of the untrimmed_fastq directory.

Solution

                $ cd $ ls shell_data/untrimmed_fastq/                              
                SRR097977.fastq  SRR098026.fastq                              

Full vs. Relative Paths

The cd control takes an argument which is a directory name. Directories can be specified using either a relative path or a full accented path. The directories on the figurer are arranged into a hierarchy. The full path tells y'all where a directory is in that bureaucracy. Navigate to the home directory, then enter the pwd command.

You volition see:

This is the full proper noun of your dwelling house directory. This tells you that you are in a directory chosen dcuser, which sits within a directory chosen home which sits within the very meridian directory in the hierarchy. The very top of the hierarchy is a directory chosen / which is normally referred to as the root directory. And then, to summarize: dcuser is a directory in abode which is a directory in /. More on root and home in the side by side department.

At present enter the post-obit command:

            $ cd /dwelling house/dcuser/shell_data/.hidden                      

This jumps forward multiple levels to the .hidden directory. Now go back to the home directory.

You can also navigate to the .hidden directory using:

These 2 commands accept the same outcome, they both take us to the .subconscious directory. The first uses the absolute path, giving the full accost from the home directory. The second uses a relative path, giving simply the address from the working directory. A full path always starts with a /. A relative path does not.

A relative path is similar getting directions from someone on the street. They tell you lot to "become right at the cease sign, and then plow left on Main Street". That works great if you're standing in that location together, but non so well if you're trying to tell someone how to get there from another country. A full path is like GPS coordinates. It tells you exactly where something is no matter where y'all are correct now.

You can usually use either a full path or a relative path depending on what is well-nigh convenient. If we are in the home directory, information technology is more convenient to enter the full path. If nosotros are in the working directory, it is more convenient to enter the relative path since it involves less typing.

Over time, it will become easier for you to keep a mental note of the structure of the directories that you are using and how to rapidly navigate amongst them.

Relative path resolution

Using the filesystem diagram below, if pwd displays /Users/thing, what will ls ../backup display?

  1. ../fill-in: No such file or directory
  2. 2012-12-01 2013-01-08 2013-01-27
  3. 2012-12-01/ 2013-01-08/ 2013-01-27/
  4. original pnas_final pnas_sub

File System for Challenge Questions

Solution

  1. No: there is a directory fill-in in /Users.
  2. No: this is the content of Users/thing/fill-in, but with .. we asked for ane level farther upwardly.
  3. No: see previous explanation. Also, we did not specify -F to display / at the end of the directory names.
  4. Yeah: ../backup refers to /Users/backup.

The root directory is the highest level directory in your file system and contains files that are important for your figurer to perform its daily work. While you will be using the root (/) at the beginning of your accented paths, it is important that y'all avoid working with data in these higher-level directories, equally your commands can permanently alter files that the operating organization needs to function. In many cases, trying to run commands in root directories will require special permissions which are not discussed here, so it's all-time to avoid them and work inside your domicile directory. Dealing with the home directory is very common. The tilde character, ~, is a shortcut for your habitation directory. In our case, the root directory is 2 levels above our habitation directory, so cd or cd ~ will take you to /abode/dcuser and cd / will take you to /. Navigate to the shell_data directory:

Then enter the command:

This prints the contents of your home directory, without you needing to type the full path.

The commands cd, and cd ~ are very useful for speedily navigating dorsum to your domicile directory. We volition exist using the ~ graphic symbol in after lessons to specify our home directory.

Central Points

  • The /, ~, and .. characters represent important navigational shortcuts.

  • Hidden files and directories start with . and can be viewed using ls -a.

  • Relative paths specify a location starting from the current location, while absolute paths specify a location from the root of the file organization.

collinstrage1963.blogspot.com

Source: https://datacarpentry.org/shell-genomics/02-the-filesystem/index.html

0 Response to "How to Read the First File Under the Current Directory C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel