Skip to main content

Section 2.4 Advanced Commands

If you seek a more advanced glance into the potential of the command line, you have come to the right place. Each of the following commands are not necessary to proceed through this book but may ease your workflow in the future. As you get comfortable with the terminal, consider adopting some of these commands.

Using sudo.

The sudo command allows you to run programs with the security privileges of another user (by default, as the superuser). It prompts you for your personal password and confirms your request to execute a command by checking a file, called sudoers, which the system administrator configures. Using the sudoers file, system administrators can give certain users or groups access to some or all commands without those users having to know the root password. It also logs all commands and arguments so there is a record of who used it for what, and when.
This is a little too much for what is necessary here but has good information. Essentially, sudo exists to allow you to run commands as an admin who, in some cases, might have more permissions to run certain commands. The benefit of sudo is that your password is required.
So, if there is ever a command that involves risky business (such as permanently deleting files or folders) or if you ever want to purposefully add a layer of safety, use the sudo command. The command is actually very simple: add the word sudo to the beginning of any terminal command and you will be required to enter your computer’s password before the command executes.

Using mv.

The mv command has many uses, all revolving around moving and renaming files. There are a few use cases as described below. The title of each case is the situation you may find yourself in followed by a shell of the command, an example command, and a more detailed description of the command. This content is inspired by the University of Alberta 30 .
You already have a file and you want to rename it
mv <existing-file-name> <new-file-name>
mv birds.txt cats.txt
Takes prexisting file birds.txt and renames it to cats.txt
You already have a file and you want to move it AND rename the file
mv <existing-file-name>
	<destination-folder-name>/<new-file-name>
mv birds.txt animals/cats.txt
Takes prexisting file birds.txt, renames it to cats.txt and moves it to the animals/ folder. (Also removes the original birds.txt)
You already have a file and you want to move it AND keep the same file name
mv <existing-file-name> <destination-folder-name>
mv birds.txt animals/
Takes prexisting file birds.txt and moves it to the animals/ folder.
You already have a folder and you want to rename it OR you want to move files and folders in an existing folder to a different folder
mv <folder-to-move> <destination-folder-name>
mv drums/ instruments/
Takes prexisting folder drums/ and moves it into the folder instruments/. If instruments/ didn’t exists, drums/ would have been renamed to instruments/
You already have multiple files and you want to move them to another folder
mv <existing-file-name1> <existing-file-name2> ...
	<destination-folder-name>
mv dogs.txt cats.txt animals/mammals/
Takes prexisting files dogs.txt and cats.txt and moves them to the animals/mammals/ folder.
You already have a file and you want to copy it to another folder
cp <existing-file-name> <destination-folder-name>
cp dogs.txt animals/mammals/
(Notice the different command, cp) Takes prexisting file dogs.txt and copies it to the animals/mammals/ folder. Keeps the original dogs.txt.
You can also use pattern-matching commands (wildcards) such as the asterisk (*) and period (.) but these are beyond the scope of this book.

Using which.

You may never need to use which but it might be useful in the future. which is used for finding the location of certain executables on your computer. The syntax follows which <program-to-find> and outputs the file path of that program.
To test this out, try which git, which bash, or which fish.