Working With Files and Directories
Overview
Teaching: 15 min
Exercises: 0 minQuestions
How can I create, copy, and delete files and directories?
How can I edit files?
Objectives
Renaming Files
Suppose that you created a
.txtfile in your current directory to contain a list of the statistical tests you will need to do to analyze your data, and named it:statstics.txtAfter creating and saving this file you realize you misspelled the filename! You want to correct the mistake, which of the following commands could you use to do so?
cp statstics.txt statistics.txtmv statstics.txt statistics.txtmv statstics.txt .cp statstics.txt .Solution
- No. While this would create a file with the correct name, the incorrectly named file still exists in the directory and would need to be deleted.
- Yes, this would work to rename the file.
- No, the period(.) indicates where to move the file, but does not provide a new file name; identical file names cannot be created.
- No, the period(.) indicates where to copy the file, but does not provide a new file name; identical file names cannot be created.
Moving and Copying
What is the output of the closing
lscommand in the sequence shown below?$ pwd/Users/jamie/data$ lsproteins.dat$ mkdir recombine $ mv proteins.dat recombine $ cp recombine/proteins.dat ../proteins-saved.dat $ ls
proteins-saved.dat recombinerecombineproteins.dat recombineproteins-saved.datSolution
We start in the
/Users/jamie/datadirectory, and create a new folder calledrecombine. The second line moves (mv) the fileproteins.datto the new folder (recombine). The third line makes a copy of the file we just moved. The tricky part here is where the file was copied to. Recall that..means “go up a level”, so the copied file is now in/Users/jamie. Notice that..is interpreted with respect to the current working directory, not with respect to the location of the file being copied. So, the only thing that will show using ls (in/Users/jamie/data) is the recombine folder.
- No, see explanation above.
proteins-saved.datis located at/Users/jamie- Yes
- No, see explanation above.
proteins.datis located at/Users/jamie/data/recombine- No, see explanation above.
proteins-saved.datis located at/Users/jamie
Organizing Directories and Files
Jamie is working on a project and she sees that her files aren’t very well organized:
$ ls -Fanalyzed/ fructose.dat raw/ sucrose.datThe
fructose.datandsucrose.datfiles contain output from her data analysis. What command(s) covered in this lesson does she need to run so that the commands below will produce the output shown?$ ls -Fanalyzed/ raw/$ ls analyzedfructose.dat sucrose.datSolution
mv *.dat analyzedJamie needs to move her files
fructose.datandsucrose.datto theanalyzeddirectory. The shell will expand *.dat to match all .dat files in the current directory. Themvcommand then moves the list of .dat files to the “analyzed” directory.
Copy with Multiple Filenames
For this exercise, you can test the commands in the
data-shell/data directory.In the example below, what does
cpdo when given several filenames and a directory name?$ mkdir backup $ cp amino-acids.txt animals.txt backup/In the example below, what does
cpdo when given three or more file names?$ ls -Famino-acids.txt animals.txt backup/ elements/ morse.txt pdb/ planets.txt salmon.txt sunspot.txt$ cp amino-acids.txt animals.txt morse.txtSolution
If given more than one file name followed by a directory name (i.e. the destination directory must be the last argument),
cpcopies the files to the named directory.If given three file names,
cpthrows an error because it is expecting a directory name as the last argument.cp: target ‘morse.txt’ is not a directory
Listing Recursively and By Time
The command
ls -Rlists the contents of directories recursively, i.e., lists their sub-directories, sub-sub-directories, and so on in alphabetical order at each level. The commandls -tlists things by time of last change, with most recently changed files or directories first. In what order doesls -R -tdisplay things?Solution
The command
ls -R -tdisplays the directories recursively in chronological order at each level, and the files in each directory are displayed chronologically.
Creating Files a Different Way
We have seen how to create text files using the
nanoeditor. Now, try the following command in your home directory:$ cd # go to your home directory $ touch my_file.txt
What did the touch command do? When you look at your home directory using the GUI file explorer, does the file show up?
Use
ls -lto inspect the files. How large ismy_file.txt?When might you want to create a file this way?
Solution
The touch command generates a new file called ‘my_file.txt’ in your home directory. If you are in your home directory, you can observe this newly generated file by typing ‘ls’ at the command line prompt. ‘my_file.txt’ can also be viewed in your GUI file explorer.
When you inspect the file with ‘ls -l’, note that the size of ‘my_file.txt’ is 0kb. In other words, it contains no data. If you open ‘my_file.txt’ using your text editor it is blank.
Some programs do not generate output files themselves, but instead require that empty files have already been generated. When the program is run, it searches for an existing file to populate with its output. The touch command allows you to efficiently generate a blank text file to be used by such programs.
Moving to the Current Folder
After running the following commands, Jamie realizes that she put the files
sucrose.datandmaltose.datinto the wrong folder:$ ls -F raw/ analyzed/ $ ls -F analyzed fructose.dat glucose.dat maltose.dat sucrose.dat $ cd raw/Fill in the blanks to move these files to the current folder (i.e., the one she is currently in):
$ mv ___/sucrose.dat ___/maltose.dat ___Solution
$ mv ../analyzed/sucrose.dat ../analyzed/maltose.dat .Recall that
..refers to the parent directory (i.e. one above the current directory) and that.refers to the current directory.
Using
rmSafelyWhat happens when we type
rm -i thesis/quotations.txt? Why would we want this protection when usingrm?Solution
$ rm: remove regular file 'thesis/quotations.txt'?The -i option will prompt before every removal. The Unix shell doesn’t have a trash bin, so all the files removed will disappear forever. By using the -i flag, we have the chance to check that we are deleting only the files that we want to remove.
Copy a folder structure sans files
You’re starting a new experiment, and would like to duplicate the file structure from your previous experiment without the data files so you can add new data.
Assume that the file structure is in a folder called ‘2016-05-18-data’, which contains folders named ‘raw’ and ‘processed’ that contain data files. The goal is to copy the file structure of the
2016-05-18-datafolder into a folder called2016-05-20-dataand remove the data files from the directory you just created.Which of the following set of commands would achieve this objective? What would the other commands do?
$ cp -r 2016-05-18-data/ 2016-05-20-data/ $ rm 2016-05-20-data/raw/* $ rm 2016-05-20-data/processed/*$ rm 2016-05-20-data/raw/* $ rm 2016-05-20-data/processed/* $ cp -r 2016-05-18-data/ 2016-5-20-data/$ cp -r 2016-05-18-data/ 2016-05-20-data/ $ rm -r -i 2016-05-20-data/Solution
The first set of commands achieves this objective. First we have a recursive copy of a data folder. Then two
rmcommands which remove all files in the specified directories. The shell expands the ‘*’ wild card to match all files and subdirectories.The second set of commands have the wrong order: attempting to delete files which haven’t yet been copied, followed by the recursive copy command which would copy them.
The third set of commands would achieve the objective, but in a time-consuming way: the first command copies the directory recursively, but the second command deletes interactively, prompting for confirmation for each file and directory.
Key Points