Interacting with Files and Directories

Interacting

Here is a list of common commands used for interacting with files and directories:

mkdir command

mkdirmake a new directory of the given name, as permissions allow.

$ mkdir Newdir
mv command

mvmove files, directories, or both to a new location.

$ mv file1 Newdir

This can also be used to rename files:

$ mv file1 file2

Use wildcards like * to move all files of a specific type to a new location:

$ mv *.c ../CodeDir

You can always verify that the file was moved with ls.

cp command

cpcopy files, directories, or both to a new location.

$ cp file1 ~/

You can give the copy a different name than the original in the same command:

$ cp file1 file1_copy

To copy a directory, use the -r option (recursively). In this case, both the source and the destination are directories, and must already exist.

$ cp -r Test1 ~/testresults
rm command

rmremoves files or directories permanently from the system.

Warning: Linux does not typically have a "trash bin" or equivalent as on other OS

When you issue rm to remove a file, it is difficult to impossible to recover removed files without resorting to backup restore.

$ rm file1

With the -r or -R option, it will also remove/delete entire directories recursively and permanently.

$ rm -r Junk
Caution: Avoid using wildcards like *.

For instance, the example below will remove all of the files and subdirectories within your current directory, so use with caution.

$ rm -r *

To remove an empty directory, use rmdir

touch command

touch – changes a file's modification timestamp without editing the contents of the file. It is also useful for creating an empty file when the filename given does not exist.

Try these commands to get more familiar with Linux files and directories.

 
©   Cornell University  |  Center for Advanced Computing  |  Copyright Statement  |  Inclusivity Statement