# Working with files and directories Here we are going to look at some more ways to learn about and manipulate file and directories at the command line. If you'd like to follow along, but need to pull up the proper working environment again, revisit [here](shell-getting-started-01.html#how-to-access-the-shell-for-now) and then come back π ---
To be sure we are starting in the same place, let's run: ```bash cd ~/shell_intro ```--- ## Working with files We will often want to get a look at a file to see how it's structured. We've already used a very common tool for peeking at files, the **`head`** command. There is also **`tail`**, which prints the last 10 lines of a file: ```bash head example.txt tail example.txt ``` This is especially helpful if a file is particularly large, as **`head`** will just print the first ten lines and stop. **This means it will be just as instantaneous whether the file is 10KB or 10GB.** Another standard useful program for viewing the contents of a file is **`less`**. This opens a searchable, read-only program that allows you to scroll through the document: ```bash less example.txt ``` To exit the **`less`** program, press the "**q**" key. The **`wc`** (**w**ord **c**ount) command is useful for counting how many lines, words, and characters there are in a file: ```bash wc example.txt ```
The most common command-line tools like these and many others we'll see are mostly only useful for operating on what are known as **plain-text files** β also referred to as "flat files". ### BONUS ROUND: What's a plain-text file? A general definition of a plain-text file is a text file that doesn't contain any special formatting characters or information, and that can be properly viewed and edited with any standard text editor. Common types of plain-text files are those ending with extensions like ".txt", ".tsv" (**t**ab-**s**eparated **v**alues), or ".csv" (**c**omma **s**eparated **v**alues). Some examples of common file types that are *not* plain-text files would be ".docx", ".pdf", or ".xlsx". This is because those file formats contain special types of compression and formatting information that are only interpretable by programs specifically designed to work with them. > **A note on file extensions**PRACTICE! How can we get only the number of lines in a file from the `wc` command?SolutionWe could find this out by looking at `man wc`, running `wc --help`, or by visiting our good friend Google πwc -l example.txt
Printing out how many lines are in a file like this is super-useful for things like quickly seeing how many rows are in a large table, or how many sequences are in a file.
The commands **`cp`** and **`mv`** (**c**o**p**y and **m**o**v**e) have the same basic structure. They both require two positional arguments β the first is the file you want to act on, and the second is where you want it to go (which can include the name you want to give it). To see how this works, let's make a copy of "example.txt": ```bash ls cp example.txt example_copy.txt ls ``` By just giving the second argument a name and nothing else (meaning no path in front of the name), we are implicitly saying we want it copied to where we currently are. To make a copy and put it somewhere else, like in our subdirectory "data", we could change the second positional argument using a **relative path** ("relative" because it starts from where we currently are): ```bash ls data/ cp example.txt data/example_copy.txt ls data/ ``` To copy it to that subdirectory but keep the same name, we could type the whole name out, but we can also just provide the directory but leave off the file name: ```bash cp example.txt data/ ls data/ ``` If we wanted to copy something *from somewhere else to our current working directory* and keep the same name, we can use another special character, a period (**`.`**), which specifies the current working directory: ```bash ls cp experiment/notes.txt . ls ``` The **`mv`** command is used to move files. Let's move the "example_copy.txt" file into the "experiment" subdirectory: ```bash ls ls experiment/ mv example_copy.txt experiment/ ls ls experiment/ ``` The **`mv`** command is also used to *rename* files. This may seem strange at first, but remember that the path (address) of a file actually includes its name too (otherwise everything in the same directory would have the same path). ```bash ls mv notes.txt notes_old.txt ls ``` To delete files there is the **`rm`** command (**r**e**m**ove). This requires at least one argument specifying the file we want to delete. But again, caution is warranted. There will be no confirmation or retrieval from a waste bin afterwards. ```bash ls rm notes_old.txt ls ``` ### A terminal text editor It is often very useful to be able to generate new plain-text files quickly at the command line, or make some changes to an existing one. One way to do this is using a text editor that operates at the command line. Here we're going to look at one program that does this called **`nano`**. When we run the command **`nano`** it will open a text editor in our terminal window. If we give it a file name as a positional argument, it will open that file if it exists, or it will create it if it doesn't. Here we'll make a new file: ```bash nano sample_names.txt ``` When we press return, our environment changes to this:WARNING! Using commands that do things like create, copy, and move files at the command line will overwrite files if they are in the same directory and have the same name. And using commands that delete things will do so permanently. Use caution while getting used to things β and then forever after π