Linux Terminal Tutorial - Performing File Operations
Introduction
This section will demonstrate how to perform file operations in the Linux terminal. These operations include creating, deleting, copying, renaming and moving files and directories.
Create directories with mkdir
Navigate to the root of the the Tutorial directory and create a new directory named analysis:
$ mkdir analysis_nvt
Perform a ls to confirm that the directory was successfully created.
Now create a sub-directory top under the newly created analysis_nvt:
$ mkdir analysis_nvt/top
Create sub-directories using the -p option
Create similar directory structures for analysis_npt and analysis_md by using the -p option:
$ mkdir -p analysis_npt/top
$ mkdir -p analysis_md/top
Create empty files using touch
Create empty log files in each of the analysis_ directories using touch:
$ touch analysis_nvt/nvt_analysis.log
$ touch analysis_npt/npt_analysis.log
$ touch analysis_md/md_analysis.log
Copy files using cp
Copy the .top file from nvt_expr_1 to nvt_expr_1/top/ using cp:
$ cp nvt_expr_1/topol.top analysis_nvt/top/
Now repeat for the other directories:
$ cp npt_expr_1/topol.top analysis_npt/top/
$ cp md_expr_1/topol.top analysis_md/top/
Copy multiple files simultaneously using cp
To copy multiple files, specify more than one file, with the destination being the final entry.
First create the directory structure_files in the Tutorial directory
$ mkdir structure_files
Next, copy the .gro file from each main directory to structure_files simultaneously:
$ cp nvt_expr_1/nvt_expr_1.gro \
npt_expr_1/npt_expr_1.gro \
md_expr_1/md_expr_1.gro \
structure_files
Note that the above is equivalent to:
$ cp nvt_expr_1/nvt_expr_1.gro npt_expr_1/npt_expr_1.gro md_expr_1/md_expr_1.gro structure_files
The \ character can be used to type multi-line commands in the terminal and is especially useful for programs that require many options. By adding \ , the terminal will move to a new line and the end of the input is indicated by pressing enter after the final line of input.
Copy multiple files simultaneously using simple regular expressions
It is possible to copy multiple files using simple regular expressions. For example, if a group of files share a prefix (nvt) or suffix (.itp) you can copy all those files at once using cp.
Copy all the .itp files in the nvt_expr_1 to analysis_nvt/top/ directory using cp and a regular expression:
$ cp nvt_expr_1/*.itp analysis_nvt/top/
In the example above the asterisk (*) signifies any number of characters followed by .itp. Thus all the file names ending in the .itp extension would be selected and thus copied.
Now repeat this process for the other main directories.
Copy a directory
To copy a directory and its contents, use the option -r. Make a copy of the newly created structure_files:
$ cp -r structure_files structure_files_backup
Move a directory using mv
To move a file or directory with mv is similar to how you used cp.
Create a directory backups and move the directory structure_files_backup to the newly created directory:
$ mkdir backups
$ mv structure_files_backup backups
Renaming files and directories using mv
Files and directories in Linux are renamed using the mv command. While you may use rename, note that it is simply an alias for the mv command.
To demonstrate this, cd into the directory structure_files_backup
$ cd backups/structure_files_backup
Rename the file md_expr_1.gro to md_expr_1.gro.original:
$ mv md_expr_1.gro md_expr_1.gro.original
cd back into the backups directory:
$ cd ..
Now rename the directory structure_files_backup to to_be_deleted:
$ mv structure_files_backup to_be_deleted
Delete files using rm
Delete the md_expr1.gro.original file in the to_be_deleted directory:
$ rm to_be_deleted/md_expr_1.gro.original
Delete directories using rm with the -d and -r options
Empty directories can be deleted by adding the -d option:
$ mkdir empty_dir
$ rm -d empty_dir
However, non-empty directories must be deleted by adding the -r option. This option will recursively delete everything in the directory.
Additionally, when using i, the terminal will prompt the user to confirm deletion of every file.
To demonstrate the above concept, delete the to_be_deleted directory using rm and the -dri combination of options:
$ rm -dri to_be_deleted/