Basic File Management

In terms of moving, deleting, and creating files, there are a bunch of basic commands you should know. But first, here is are some shortcuts you should remember:

denotes the current directory. We will use this below, say if we want to copy a file from one directory to our current one, we can use this instead of typing out the entire filepath to where we are

is a wildcard. This can be used in many ways, as long as you are using it in terms of files and directories. For example, to target all files your working directory, you can use * instead of typing out a filepath and using a separate command for each one. If you want to target all text files, you could use *.txt, or conversely, if you want to target all files with the name TEST, you could use TEST.*


rm

This command is used to “remove” files. Be careful with this one, this does not send files to your trash. This command will permanently remove your files. If you want to remove a file, all you need to type is “rm filepath”

If you wanted to remove all files your working directory, like we were talking about above, you can use the command “rm *”

To remove a directory (not just the files within it, but the folder itself as well), we can use the flag -r

Using the command “rm -r filepath” and the directory removed would be the directory at the end of the file path. For example, if we use “rm -r /Users/username/documents” the folder “documents” would be removed.


rmdir

This can be used to remove empty directories. If you list a filepath, say “rmdir /Users/username/documents” the documents folder would be removed if it was empty.

Using the flag -p removes parent directories. If we used the command “rmdir -p /Users/username/documents” the directories username and Users would also be removed if they too are empty.


mv

This command moves files from one location to another, in the form of “rm firstfilepath secondfilepath” where the first file path is the location of the file you want to move, and the second file path is the location you want to move the file to. You can also move whole directories, so instead of using a particular file, you can also use the name of a folder.

For example, say you want to move a file called file.txt that is in your working directory to the”documents” folder at /Users/username/documents. The command we then want to use is “mv file.txt /Users/username/documents”

A useful flag we can use here is -v

v can be used with many different commands, and it stands for “verbosity.” You can use -v, -vv, or -vvv. These stand for a low level of verbosity, medium level of verbosity, and high level of verbosity (respectively). Using this flag for this command will display information about when files are being moved. This is particularly useful if you are moving an entire directory.


cp

This command copies a file from one location, and pastes it in another. The syntax here is very similar to mv, you use “cp firstfilepath secondfilepath”

Again, you can copy files or whole directories. A flag that’s useful here is -r

Using “cp -r /documents/pictures /documents/music” would copy over pictures, every subdirectory within pictures, and every file within those subdirectories over to music.

Another flag that’s useful is -p

Using “cp -p file filepath” would preserve the time of last modification, time of last access, owner, and file permissions of file.

You can copy a file to another file (instead of a directory), which essentially copies over the contents of the first file into the second file. If the second file does not exist, it will be created.

Here, we’d like to do an example of using .

Say we have a file in the directory above us, and we want to copy and paste that in our working directory. We can use “cp ../file .”


mkdir

This command is extremely straightforward. Essentially, all you are doing is creating a new folder.

You can, however, create multiple folders at one. Using the flag -p you can create a system of folders. For example, say we have a folder User. We want to create a folder Music inside a folder Documents which should be inside User. Documents and Music both do not exist. If we use “mkdir -p /User/Documents/Music” both Documents and Music will be created and nested properly.